mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-02-28 18:14:39 +08:00
Clean up the code for timeout
Rename UART_TCP_CLIENT_RX_TIMEOUT_MS to UART_NET_CLIENT_RX_TIMEOUT_MS Add UART_UDP_LOCAL_CLIENT_RX_TIMEOUT_MS for shorter timeout Check if the target address is local in uart_open()
This commit is contained in:
parent
0e203fb60c
commit
de7b6f35bc
5 changed files with 47 additions and 20 deletions
|
@ -666,9 +666,9 @@ int TestProxmark(pm3_device_t *dev) {
|
||||||
g_conn.send_via_fpc_usart = g_pm3_capabilities.via_fpc;
|
g_conn.send_via_fpc_usart = g_pm3_capabilities.via_fpc;
|
||||||
g_conn.uart_speed = g_pm3_capabilities.baudrate;
|
g_conn.uart_speed = g_pm3_capabilities.baudrate;
|
||||||
|
|
||||||
bool is_tcp_conn = (memcmp(g_conn.serial_port_name, "tcp:", 4) == 0);
|
bool is_tcp_conn = (g_conn.send_via_ip == PM3_TCPv4 || g_conn.send_via_ip == PM3_TCPv6);
|
||||||
bool is_bt_conn = (memcmp(g_conn.serial_port_name, "bt:", 3) == 0);
|
bool is_bt_conn = (memcmp(g_conn.serial_port_name, "bt:", 3) == 0);
|
||||||
bool is_udp_conn = (memcmp(g_conn.serial_port_name, "udp:", 4) == 0);
|
bool is_udp_conn = (g_conn.send_via_ip == PM3_UDPv4 || g_conn.send_via_ip == PM3_UDPv6);
|
||||||
|
|
||||||
PrintAndLogEx(INFO, "Communicating with PM3 over %s%s%s%s",
|
PrintAndLogEx(INFO, "Communicating with PM3 over %s%s%s%s",
|
||||||
(g_conn.send_via_fpc_usart) ? _YELLOW_("FPC UART") : _YELLOW_("USB-CDC"),
|
(g_conn.send_via_fpc_usart) ? _YELLOW_("FPC UART") : _YELLOW_("USB-CDC"),
|
||||||
|
@ -680,15 +680,11 @@ int TestProxmark(pm3_device_t *dev) {
|
||||||
PrintAndLogEx(INFO, "PM3 UART serial baudrate: " _YELLOW_("%u") "\n", g_conn.uart_speed);
|
PrintAndLogEx(INFO, "PM3 UART serial baudrate: " _YELLOW_("%u") "\n", g_conn.uart_speed);
|
||||||
} else {
|
} else {
|
||||||
int res;
|
int res;
|
||||||
if (is_tcp_conn || is_udp_conn) {
|
if (g_conn.send_via_local_ip) {
|
||||||
if ((strstr(g_conn.serial_port_name, "localhost") != NULL) ||
|
// (g_conn.send_via_local_ip == true) -> ((is_tcp_conn || is_udp_conn) == true)
|
||||||
(strstr(g_conn.serial_port_name, "127.0.0.1") != NULL) ||
|
res = uart_reconfigure_timeouts(is_tcp_conn ? UART_TCP_LOCAL_CLIENT_RX_TIMEOUT_MS : UART_UDP_LOCAL_CLIENT_RX_TIMEOUT_MS);
|
||||||
(strstr(g_conn.serial_port_name, "[::1]") != NULL) ||
|
} else if (is_tcp_conn || is_udp_conn) {
|
||||||
(strstr(g_conn.serial_port_name, "p:::1") != NULL)) {
|
res = uart_reconfigure_timeouts(UART_NET_CLIENT_RX_TIMEOUT_MS);
|
||||||
res = uart_reconfigure_timeouts(UART_TCP_CLIENT_LOCAL_RX_TIMEOUT_MS);
|
|
||||||
} else {
|
|
||||||
res = uart_reconfigure_timeouts(UART_TCP_CLIENT_RX_TIMEOUT_MS);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
res = uart_reconfigure_timeouts(UART_USB_CLIENT_RX_TIMEOUT_MS);
|
res = uart_reconfigure_timeouts(UART_USB_CLIENT_RX_TIMEOUT_MS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,8 +70,10 @@ typedef struct {
|
||||||
bool send_with_crc_on_fpc;
|
bool send_with_crc_on_fpc;
|
||||||
// "Session" flag, to tell via which interface next msgs are sent: USB or FPC USART
|
// "Session" flag, to tell via which interface next msgs are sent: USB or FPC USART
|
||||||
bool send_via_fpc_usart;
|
bool send_via_fpc_usart;
|
||||||
// to tell if we are using TCP/UDP/TCPv6
|
// to tell if we are using TCP/UDP/TCP(IPv6)/UDP(IPv6)
|
||||||
CommunicationProtocol_t send_via_ip;
|
CommunicationProtocol_t send_via_ip;
|
||||||
|
// to tell if the target address is local address(127.0.0.1/localhost/::1)
|
||||||
|
bool send_via_local_ip;
|
||||||
// To memorise baudrate
|
// To memorise baudrate
|
||||||
uint32_t uart_speed;
|
uint32_t uart_speed;
|
||||||
uint16_t last_command;
|
uint16_t last_command;
|
||||||
|
|
|
@ -91,6 +91,8 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
sp->udpBuffer = NULL;
|
sp->udpBuffer = NULL;
|
||||||
// init timeouts
|
// init timeouts
|
||||||
timeout.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
g_conn.send_via_local_ip = false;
|
||||||
|
g_conn.send_via_ip = PM3_NONE;
|
||||||
|
|
||||||
char *prefix = strdup(pcPortName);
|
char *prefix = strdup(pcPortName);
|
||||||
if (prefix == NULL) {
|
if (prefix == NULL) {
|
||||||
|
@ -119,7 +121,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
|
@ -233,6 +235,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
info.ai_family = PF_UNSPEC;
|
info.ai_family = PF_UNSPEC;
|
||||||
info.ai_socktype = SOCK_STREAM;
|
info.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
|
if ((strstr(addrstr, "localhost") != NULL) ||
|
||||||
|
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
||||||
|
(strstr(addrstr, "::1") != NULL)) {
|
||||||
|
g_conn.send_via_local_ip = true;
|
||||||
|
}
|
||||||
|
|
||||||
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
PrintAndLogEx(ERR, "error: getaddrinfo: %s", gai_strerror(s));
|
PrintAndLogEx(ERR, "error: getaddrinfo: %s", gai_strerror(s));
|
||||||
|
@ -304,7 +312,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
|
@ -417,6 +425,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
info.ai_family = PF_UNSPEC;
|
info.ai_family = PF_UNSPEC;
|
||||||
info.ai_socktype = SOCK_DGRAM;
|
info.ai_socktype = SOCK_DGRAM;
|
||||||
|
|
||||||
|
if ((strstr(addrstr, "localhost") != NULL) ||
|
||||||
|
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
||||||
|
(strstr(addrstr, "::1") != NULL)) {
|
||||||
|
g_conn.send_via_local_ip = true;
|
||||||
|
}
|
||||||
|
|
||||||
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
PrintAndLogEx(ERR, "error: getaddrinfo: %s", gai_strerror(s));
|
PrintAndLogEx(ERR, "error: getaddrinfo: %s", gai_strerror(s));
|
||||||
|
@ -528,7 +542,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// we must use max timeout!
|
// we must use max timeout!
|
||||||
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
|
||||||
size_t servernameLen = (strlen(pcPortName) - 7) + 1;
|
size_t servernameLen = (strlen(pcPortName) - 7) + 1;
|
||||||
char serverNameBuf[servernameLen];
|
char serverNameBuf[servernameLen];
|
||||||
|
|
|
@ -44,7 +44,7 @@ typedef struct {
|
||||||
// this is for TCP connection
|
// this is for TCP connection
|
||||||
struct timeval timeout = {
|
struct timeval timeout = {
|
||||||
.tv_sec = 0, // 0 second
|
.tv_sec = 0, // 0 second
|
||||||
.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000
|
.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t newtimeout_value = 0;
|
uint32_t newtimeout_value = 0;
|
||||||
|
@ -92,6 +92,8 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sp->udpBuffer = NULL;
|
sp->udpBuffer = NULL;
|
||||||
|
g_conn.send_via_local_ip = false;
|
||||||
|
g_conn.send_via_ip = PM3_NONE;
|
||||||
|
|
||||||
char *prefix = strdup(pcPortName);
|
char *prefix = strdup(pcPortName);
|
||||||
if (prefix == NULL) {
|
if (prefix == NULL) {
|
||||||
|
@ -121,7 +123,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
|
@ -244,6 +246,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
info.ai_socktype = SOCK_STREAM;
|
info.ai_socktype = SOCK_STREAM;
|
||||||
info.ai_protocol = IPPROTO_TCP;
|
info.ai_protocol = IPPROTO_TCP;
|
||||||
|
|
||||||
|
if ((strstr(addrstr, "localhost") != NULL) ||
|
||||||
|
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
||||||
|
(strstr(addrstr, "::1") != NULL)) {
|
||||||
|
g_conn.send_via_local_ip = true;
|
||||||
|
}
|
||||||
|
|
||||||
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
||||||
|
@ -322,7 +330,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
|
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
|
@ -445,6 +453,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
info.ai_socktype = SOCK_DGRAM;
|
info.ai_socktype = SOCK_DGRAM;
|
||||||
info.ai_protocol = IPPROTO_UDP;
|
info.ai_protocol = IPPROTO_UDP;
|
||||||
|
|
||||||
|
if ((strstr(addrstr, "localhost") != NULL) ||
|
||||||
|
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
||||||
|
(strstr(addrstr, "::1") != NULL)) {
|
||||||
|
g_conn.send_via_local_ip = true;
|
||||||
|
}
|
||||||
|
|
||||||
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
int s = getaddrinfo(addrstr, portstr, &info, &addr);
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
||||||
|
|
|
@ -831,8 +831,9 @@ typedef struct {
|
||||||
// uart_windows.c & uart_posix.c
|
// uart_windows.c & uart_posix.c
|
||||||
# define UART_FPC_CLIENT_RX_TIMEOUT_MS 200
|
# define UART_FPC_CLIENT_RX_TIMEOUT_MS 200
|
||||||
# define UART_USB_CLIENT_RX_TIMEOUT_MS 20
|
# define UART_USB_CLIENT_RX_TIMEOUT_MS 20
|
||||||
# define UART_TCP_CLIENT_RX_TIMEOUT_MS 500
|
# define UART_NET_CLIENT_RX_TIMEOUT_MS 500
|
||||||
# define UART_TCP_CLIENT_LOCAL_RX_TIMEOUT_MS 40
|
# define UART_TCP_LOCAL_CLIENT_RX_TIMEOUT_MS 40
|
||||||
|
# define UART_UDP_LOCAL_CLIENT_RX_TIMEOUT_MS 20
|
||||||
|
|
||||||
|
|
||||||
// CMD_DEVICE_INFO response packet has flags in arg[0], flag definitions:
|
// CMD_DEVICE_INFO response packet has flags in arg[0], flag definitions:
|
||||||
|
|
Loading…
Reference in a new issue