mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-22 14:23:50 -07:00
Add uart_parse_address_port()
This commit is contained in:
parent
ecec001fdd
commit
81780bd164
4 changed files with 183 additions and 351 deletions
|
@ -84,6 +84,11 @@ uint32_t uart_get_timeouts(void);
|
||||||
|
|
||||||
/* Specify the outbound address and port for TCP/UDP connections
|
/* Specify the outbound address and port for TCP/UDP connections
|
||||||
*/
|
*/
|
||||||
bool uart_bind(void *socket, char *bindAddrStr, char *bindPortStr, bool isBindingIPv6);
|
bool uart_bind(void *socket, const char *bindAddrStr, const char *bindPortStr, bool isBindingIPv6);
|
||||||
|
|
||||||
|
/* Parse address and port from string.
|
||||||
|
This could change the addrPortStr
|
||||||
|
*/
|
||||||
|
int uart_parse_address_port(char *addrPortStr, const char **addrStr, const char **portStr, bool *isIPv6);
|
||||||
|
|
||||||
#endif // _UART_H_
|
#endif // _UART_H_
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool uart_bind(void *socket, char *bindAddrStr, char *bindPortStr, bool isBindingIPv6) {
|
bool uart_bind(void *socket, const char *bindAddrStr, const char *bindPortStr, bool isBindingIPv6) {
|
||||||
if (bindAddrStr == NULL && bindPortStr == NULL)
|
if (bindAddrStr == NULL && bindPortStr == NULL)
|
||||||
return true; // no need to bind
|
return true; // no need to bind
|
||||||
|
|
||||||
|
@ -71,4 +71,64 @@ bool uart_bind(void *socket, char *bindAddrStr, char *bindPortStr, bool isBindin
|
||||||
int res = bind(*(int *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr));
|
int res = bind(*(int *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr));
|
||||||
#endif
|
#endif
|
||||||
return (res >= 0);
|
return (res >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart_parse_address_port(char *addrPortStr, const char **addrStr, const char **portStr, bool *isIPv6) {
|
||||||
|
|
||||||
|
if (addrPortStr == NULL || addrStr == NULL || portStr == NULL) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
*addrStr = addrPortStr;
|
||||||
|
*portStr = NULL;
|
||||||
|
|
||||||
|
// find the start of the address
|
||||||
|
char *endBracket = strrchr(addrPortStr, ']');
|
||||||
|
if (addrPortStr[0] == '[') {
|
||||||
|
*addrStr += 1;
|
||||||
|
if (endBracket == NULL) {
|
||||||
|
// [] unmatched
|
||||||
|
return PM3_ESOFT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isIPv6 != NULL) {
|
||||||
|
// Assume v4
|
||||||
|
*isIPv6 = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the port
|
||||||
|
char *lColon = strchr(addrPortStr, ':');
|
||||||
|
char *rColon = strrchr(addrPortStr, ':');
|
||||||
|
if (rColon == NULL) {
|
||||||
|
// no colon
|
||||||
|
// "<ipv4 address>", "[<ipv4 address>]"
|
||||||
|
*portStr = NULL;
|
||||||
|
} else if (lColon == rColon) {
|
||||||
|
// only one colon
|
||||||
|
// "<ipv4 address>:<port>", "[<ipv4 address>]:<port>"
|
||||||
|
*portStr = rColon + 1;
|
||||||
|
} else {
|
||||||
|
// two or more colon, IPv6 address
|
||||||
|
// "[<ipv6 address>]:<port>"
|
||||||
|
// "<ipv6 address>", "[<ipv6 address>]"
|
||||||
|
if (endBracket != NULL && rColon == endBracket + 1) {
|
||||||
|
*portStr = rColon + 1;
|
||||||
|
} else {
|
||||||
|
*portStr = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isIPv6 != NULL) {
|
||||||
|
*isIPv6 = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle the end of the address
|
||||||
|
if (endBracket != NULL) {
|
||||||
|
*endBracket = '\0';
|
||||||
|
} else if (rColon != NULL && lColon == rColon) {
|
||||||
|
*rColon = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return PM3_SUCCESS;
|
||||||
}
|
}
|
|
@ -114,8 +114,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
struct addrinfo *addr = NULL, *rp;
|
struct addrinfo *addr = NULL, *rp;
|
||||||
|
|
||||||
char *addrPortStr = str_dup(pcPortName + 4);
|
char *addrPortStr = str_dup(pcPortName + 4);
|
||||||
char *addrstr = addrPortStr;
|
|
||||||
const char *portstr;
|
|
||||||
if (addrPortStr == NULL) {
|
if (addrPortStr == NULL) {
|
||||||
PrintAndLogEx(ERR, "error: string duplication");
|
PrintAndLogEx(ERR, "error: string duplication");
|
||||||
free(sp);
|
free(sp);
|
||||||
|
@ -126,54 +124,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
char *bindAddrStr = NULL;
|
const char *bindAddrStr = NULL;
|
||||||
char *bindPortStr = NULL;
|
const char *bindPortStr = NULL;
|
||||||
bool isBindingIPv6 = false; // Assume v4
|
bool isBindingIPv6 = false;
|
||||||
|
|
||||||
if (bindAddrPortStr != NULL) {
|
if (bindAddrPortStr != NULL) {
|
||||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||||
bindAddrPortStr += 6;
|
bindAddrPortStr += 6; // strlen(",bind=")
|
||||||
bindAddrStr = bindAddrPortStr;
|
|
||||||
|
|
||||||
// find the start of the bind address
|
int result = uart_parse_address_port(bindAddrPortStr, &bindAddrStr, &bindPortStr, &isBindingIPv6);
|
||||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
if (result != PM3_SUCCESS) {
|
||||||
if (bindAddrPortStr[0] == '[') {
|
if (result == PM3_ESOFT) {
|
||||||
bindAddrStr += 1;
|
|
||||||
if (endBracket == NULL) {
|
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the bind port
|
|
||||||
char *lColon = strchr(bindAddrPortStr, ':');
|
|
||||||
char *rColon = strrchr(bindAddrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// ",bind=<ipv4 address>", ",bind=[<ipv4 address>]"
|
|
||||||
bindPortStr = NULL;
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// ",bind=<ipv4 address>:<port>", ",bind=[<ipv4 address>]:<port>"
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// ",bind=[<ipv6 address>]:<port>"
|
|
||||||
// ",bind=<ipv6 address>", ",bind=[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
bindPortStr = NULL;
|
PrintAndLogEx(ERR, "error: failed to parse address and port in bind option");
|
||||||
}
|
}
|
||||||
isBindingIPv6 = true;
|
free(addrPortStr);
|
||||||
}
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
// handle the end of the bind address
|
|
||||||
if (endBracket != NULL) {
|
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for bind option, it's possible to only specify address or port
|
// for bind option, it's possible to only specify address or port
|
||||||
|
@ -183,51 +151,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
bindPortStr = NULL;
|
bindPortStr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the start of the address
|
const char *addrStr = NULL;
|
||||||
char *endBracket = strrchr(addrPortStr, ']');
|
const char *portStr = NULL;
|
||||||
if (addrPortStr[0] == '[') {
|
bool isIPv6 = false;
|
||||||
addrstr += 1;
|
|
||||||
if (endBracket == NULL) {
|
int result = uart_parse_address_port(addrPortStr, &addrStr, &portStr, &isIPv6);
|
||||||
|
if (result != PM3_SUCCESS) {
|
||||||
|
if (result == PM3_ESOFT) {
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// assume v4
|
|
||||||
g_conn.send_via_ip = PM3_TCPv4;
|
|
||||||
|
|
||||||
// find the port
|
|
||||||
char *lColon = strchr(addrPortStr, ':');
|
|
||||||
char *rColon = strrchr(addrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// "tcp:<ipv4 address>", "tcp:[<ipv4 address>]"
|
|
||||||
portstr = "18888";
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// "tcp:<ipv4 address>:<port>", "tcp:[<ipv4 address>]:<port>"
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// "tcp:[<ipv6 address>]:<port>"
|
|
||||||
// "tcp:<ipv6 address>", "tcp:[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
portstr = "18888";
|
PrintAndLogEx(ERR, "error: failed to parse address and port");
|
||||||
}
|
}
|
||||||
g_conn.send_via_ip = PM3_TCPv6;
|
free(addrPortStr);
|
||||||
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the end of the address
|
g_conn.send_via_ip = isIPv6 ? PM3_TCPv6 : PM3_TCPv4;
|
||||||
if (endBracket != NULL) {
|
portStr = (portStr == NULL) ? "18888" : portStr;
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
struct addrinfo info;
|
struct addrinfo info;
|
||||||
|
|
||||||
|
@ -236,13 +177,13 @@ 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) ||
|
if ((strstr(addrStr, "localhost") != NULL) ||
|
||||||
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
(strstr(addrStr, "127.0.0.1") != NULL) ||
|
||||||
(strstr(addrstr, "::1") != NULL)) {
|
(strstr(addrStr, "::1") != NULL)) {
|
||||||
g_conn.send_via_local_ip = true;
|
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));
|
||||||
freeaddrinfo(addr);
|
freeaddrinfo(addr);
|
||||||
|
@ -306,8 +247,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
struct addrinfo *addr = NULL, *rp;
|
struct addrinfo *addr = NULL, *rp;
|
||||||
|
|
||||||
char *addrPortStr = str_dup(pcPortName + 4);
|
char *addrPortStr = str_dup(pcPortName + 4);
|
||||||
char *addrstr = addrPortStr;
|
|
||||||
const char *portstr;
|
|
||||||
if (addrPortStr == NULL) {
|
if (addrPortStr == NULL) {
|
||||||
PrintAndLogEx(ERR, "error: string duplication");
|
PrintAndLogEx(ERR, "error: string duplication");
|
||||||
free(sp);
|
free(sp);
|
||||||
|
@ -318,54 +257,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
char *bindAddrStr = NULL;
|
const char *bindAddrStr = NULL;
|
||||||
char *bindPortStr = NULL;
|
const char *bindPortStr = NULL;
|
||||||
bool isBindingIPv6 = false; // Assume v4
|
bool isBindingIPv6 = false;
|
||||||
|
|
||||||
if (bindAddrPortStr != NULL) {
|
if (bindAddrPortStr != NULL) {
|
||||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||||
bindAddrPortStr += 6;
|
bindAddrPortStr += 6; // strlen(",bind=")
|
||||||
bindAddrStr = bindAddrPortStr;
|
|
||||||
|
|
||||||
// find the start of the bind address
|
int result = uart_parse_address_port(bindAddrPortStr, &bindAddrStr, &bindPortStr, &isBindingIPv6);
|
||||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
if (result != PM3_SUCCESS) {
|
||||||
if (bindAddrPortStr[0] == '[') {
|
if (result == PM3_ESOFT) {
|
||||||
bindAddrStr += 1;
|
|
||||||
if (endBracket == NULL) {
|
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the bind port
|
|
||||||
char *lColon = strchr(bindAddrPortStr, ':');
|
|
||||||
char *rColon = strrchr(bindAddrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// ",bind=<ipv4 address>", ",bind=[<ipv4 address>]"
|
|
||||||
bindPortStr = NULL;
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// ",bind=<ipv4 address>:<port>", ",bind=[<ipv4 address>]:<port>"
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// ",bind=[<ipv6 address>]:<port>"
|
|
||||||
// ",bind=<ipv6 address>", ",bind=[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
bindPortStr = NULL;
|
PrintAndLogEx(ERR, "error: failed to parse address and port in bind option");
|
||||||
}
|
}
|
||||||
isBindingIPv6 = true;
|
free(addrPortStr);
|
||||||
}
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
// handle the end of the bind address
|
|
||||||
if (endBracket != NULL) {
|
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for bind option, it's possible to only specify address or port
|
// for bind option, it's possible to only specify address or port
|
||||||
|
@ -375,50 +284,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
bindPortStr = NULL;
|
bindPortStr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the start of the address
|
const char *addrStr = NULL;
|
||||||
char *endBracket = strrchr(addrPortStr, ']');
|
const char *portStr = NULL;
|
||||||
if (addrPortStr[0] == '[') {
|
bool isIPv6 = false;
|
||||||
addrstr += 1;
|
|
||||||
if (endBracket == NULL) {
|
int result = uart_parse_address_port(addrPortStr, &addrStr, &portStr, &isIPv6);
|
||||||
|
if (result != PM3_SUCCESS) {
|
||||||
|
if (result == PM3_ESOFT) {
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume v4
|
|
||||||
g_conn.send_via_ip = PM3_UDPv4;
|
|
||||||
|
|
||||||
// find the port
|
|
||||||
char *lColon = strchr(addrPortStr, ':');
|
|
||||||
char *rColon = strrchr(addrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// "udp:<ipv4 address>", "udp:[<ipv4 address>]"
|
|
||||||
portstr = "18888";
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// "udp:<ipv4 address>:<port>", "udp:[<ipv4 address>]:<port>"
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// "udp:[<ipv6 address>]:<port>"
|
|
||||||
// "udp:<ipv6 address>", "udp:[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
portstr = "18888";
|
PrintAndLogEx(ERR, "error: failed to parse address and port");
|
||||||
}
|
}
|
||||||
g_conn.send_via_ip = PM3_UDPv6;
|
free(addrPortStr);
|
||||||
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the end of the address
|
g_conn.send_via_ip = isIPv6 ? PM3_UDPv6 : PM3_UDPv4;
|
||||||
if (endBracket != NULL) {
|
portStr = (portStr == NULL) ? "18888" : portStr;
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
struct addrinfo info;
|
struct addrinfo info;
|
||||||
|
|
||||||
|
@ -427,13 +310,13 @@ 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) ||
|
if ((strstr(addrStr, "localhost") != NULL) ||
|
||||||
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
(strstr(addrStr, "127.0.0.1") != NULL) ||
|
||||||
(strstr(addrstr, "::1") != NULL)) {
|
(strstr(addrStr, "::1") != NULL)) {
|
||||||
g_conn.send_via_local_ip = true;
|
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));
|
||||||
freeaddrinfo(addr);
|
freeaddrinfo(addr);
|
||||||
|
|
|
@ -115,8 +115,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
struct addrinfo *addr = NULL, *rp;
|
struct addrinfo *addr = NULL, *rp;
|
||||||
|
|
||||||
char *addrPortStr = str_dup(pcPortName + 4);
|
char *addrPortStr = str_dup(pcPortName + 4);
|
||||||
char *addrstr = addrPortStr;
|
|
||||||
const char *portstr;
|
|
||||||
if (addrPortStr == NULL) {
|
if (addrPortStr == NULL) {
|
||||||
PrintAndLogEx(ERR, "error: string duplication");
|
PrintAndLogEx(ERR, "error: string duplication");
|
||||||
free(sp);
|
free(sp);
|
||||||
|
@ -127,54 +125,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
char *bindAddrStr = NULL;
|
const char *bindAddrStr = NULL;
|
||||||
char *bindPortStr = NULL;
|
const char *bindPortStr = NULL;
|
||||||
bool isBindingIPv6 = false; // Assume v4
|
bool isBindingIPv6 = false;
|
||||||
|
|
||||||
if (bindAddrPortStr != NULL) {
|
if (bindAddrPortStr != NULL) {
|
||||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||||
bindAddrPortStr += 6;
|
bindAddrPortStr += 6; // strlen(",bind=")
|
||||||
bindAddrStr = bindAddrPortStr;
|
|
||||||
|
|
||||||
// find the start of the bind address
|
int result = uart_parse_address_port(bindAddrPortStr, &bindAddrStr, &bindPortStr, &isBindingIPv6);
|
||||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
if (result != PM3_SUCCESS) {
|
||||||
if (bindAddrPortStr[0] == '[') {
|
if (result == PM3_ESOFT) {
|
||||||
bindAddrStr += 1;
|
|
||||||
if (endBracket == NULL) {
|
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the bind port
|
|
||||||
char *lColon = strchr(bindAddrPortStr, ':');
|
|
||||||
char *rColon = strrchr(bindAddrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// ",bind=<ipv4 address>", ",bind=[<ipv4 address>]"
|
|
||||||
bindPortStr = NULL;
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// ",bind=<ipv4 address>:<port>", ",bind=[<ipv4 address>]:<port>"
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// ",bind=[<ipv6 address>]:<port>"
|
|
||||||
// ",bind=<ipv6 address>", ",bind=[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
bindPortStr = NULL;
|
PrintAndLogEx(ERR, "error: failed to parse address and port in bind option");
|
||||||
}
|
}
|
||||||
isBindingIPv6 = true;
|
free(addrPortStr);
|
||||||
}
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
// handle the end of the bind address
|
|
||||||
if (endBracket != NULL) {
|
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for bind option, it's possible to only specify address or port
|
// for bind option, it's possible to only specify address or port
|
||||||
|
@ -184,50 +152,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
bindPortStr = NULL;
|
bindPortStr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the start of the address
|
const char *addrStr = NULL;
|
||||||
char *endBracket = strrchr(addrPortStr, ']');
|
const char *portStr = NULL;
|
||||||
if (addrPortStr[0] == '[') {
|
bool isIPv6 = false;
|
||||||
addrstr += 1;
|
|
||||||
if (endBracket == NULL) {
|
int result = uart_parse_address_port(addrPortStr, &addrStr, &portStr, &isIPv6);
|
||||||
|
if (result != PM3_SUCCESS) {
|
||||||
|
if (result == PM3_ESOFT) {
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume v4
|
|
||||||
g_conn.send_via_ip = PM3_TCPv4;
|
|
||||||
|
|
||||||
// find the port
|
|
||||||
char *lColon = strchr(addrPortStr, ':');
|
|
||||||
char *rColon = strrchr(addrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// "tcp:<ipv4 address>", "tcp:[<ipv4 address>]"
|
|
||||||
portstr = "18888";
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// "tcp:<ipv4 address>:<port>", "tcp:[<ipv4 address>]:<port>"
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// "tcp:[<ipv6 address>]:<port>"
|
|
||||||
// "tcp:<ipv6 address>", "tcp:[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
portstr = "18888";
|
PrintAndLogEx(ERR, "error: failed to parse address and port");
|
||||||
}
|
}
|
||||||
g_conn.send_via_ip = PM3_TCPv6;
|
free(addrPortStr);
|
||||||
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the end of the address
|
g_conn.send_via_ip = isIPv6 ? PM3_TCPv6 : PM3_TCPv4;
|
||||||
if (endBracket != NULL) {
|
portStr = (portStr == NULL) ? "18888" : portStr;
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
struct addrinfo info;
|
struct addrinfo info;
|
||||||
|
@ -246,13 +188,13 @@ 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) ||
|
if ((strstr(addrStr, "localhost") != NULL) ||
|
||||||
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
(strstr(addrStr, "127.0.0.1") != NULL) ||
|
||||||
(strstr(addrstr, "::1") != NULL)) {
|
(strstr(addrStr, "::1") != NULL)) {
|
||||||
g_conn.send_via_local_ip = true;
|
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));
|
||||||
freeaddrinfo(addr);
|
freeaddrinfo(addr);
|
||||||
|
@ -322,8 +264,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
struct addrinfo *addr = NULL, *rp;
|
struct addrinfo *addr = NULL, *rp;
|
||||||
|
|
||||||
char *addrPortStr = str_dup(pcPortName + 4);
|
char *addrPortStr = str_dup(pcPortName + 4);
|
||||||
char *addrstr = addrPortStr;
|
|
||||||
const char *portstr;
|
|
||||||
if (addrPortStr == NULL) {
|
if (addrPortStr == NULL) {
|
||||||
PrintAndLogEx(ERR, "error: string duplication");
|
PrintAndLogEx(ERR, "error: string duplication");
|
||||||
free(sp);
|
free(sp);
|
||||||
|
@ -334,54 +274,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
|
|
||||||
// find the "bind" option
|
// find the "bind" option
|
||||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||||
char *bindAddrStr = NULL;
|
const char *bindAddrStr = NULL;
|
||||||
char *bindPortStr = NULL;
|
const char *bindPortStr = NULL;
|
||||||
bool isBindingIPv6 = false; // Assume v4
|
bool isBindingIPv6 = false;
|
||||||
|
|
||||||
if (bindAddrPortStr != NULL) {
|
if (bindAddrPortStr != NULL) {
|
||||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||||
bindAddrPortStr += 6;
|
bindAddrPortStr += 6; // strlen(",bind=")
|
||||||
bindAddrStr = bindAddrPortStr;
|
|
||||||
|
|
||||||
// find the start of the bind address
|
int result = uart_parse_address_port(bindAddrPortStr, &bindAddrStr, &bindPortStr, &isBindingIPv6);
|
||||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
if (result != PM3_SUCCESS) {
|
||||||
if (bindAddrPortStr[0] == '[') {
|
if (result == PM3_ESOFT) {
|
||||||
bindAddrStr += 1;
|
|
||||||
if (endBracket == NULL) {
|
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched in bind option");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the bind port
|
|
||||||
char *lColon = strchr(bindAddrPortStr, ':');
|
|
||||||
char *rColon = strrchr(bindAddrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// ",bind=<ipv4 address>", ",bind=[<ipv4 address>]"
|
|
||||||
bindPortStr = NULL;
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// ",bind=<ipv4 address>:<port>", ",bind=[<ipv4 address>]:<port>"
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// ",bind=[<ipv6 address>]:<port>"
|
|
||||||
// ",bind=<ipv6 address>", ",bind=[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
bindPortStr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
bindPortStr = NULL;
|
PrintAndLogEx(ERR, "error: failed to parse address and port in bind option");
|
||||||
}
|
}
|
||||||
isBindingIPv6 = true;
|
free(addrPortStr);
|
||||||
}
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
// handle the end of the bind address
|
|
||||||
if (endBracket != NULL) {
|
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for bind option, it's possible to only specify address or port
|
// for bind option, it's possible to only specify address or port
|
||||||
|
@ -391,50 +301,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||||
bindPortStr = NULL;
|
bindPortStr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the start of the address
|
const char *addrStr = NULL;
|
||||||
char *endBracket = strrchr(addrPortStr, ']');
|
const char *portStr = NULL;
|
||||||
if (addrPortStr[0] == '[') {
|
bool isIPv6 = false;
|
||||||
addrstr += 1;
|
|
||||||
if (endBracket == NULL) {
|
int result = uart_parse_address_port(addrPortStr, &addrStr, &portStr, &isIPv6);
|
||||||
|
if (result != PM3_SUCCESS) {
|
||||||
|
if (result == PM3_ESOFT) {
|
||||||
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
PrintAndLogEx(ERR, "error: wrong address: [] unmatched");
|
||||||
free(addrPortStr);
|
|
||||||
free(sp);
|
|
||||||
return INVALID_SERIAL_PORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume v4
|
|
||||||
g_conn.send_via_ip = PM3_UDPv4;
|
|
||||||
|
|
||||||
// find the port
|
|
||||||
char *lColon = strchr(addrPortStr, ':');
|
|
||||||
char *rColon = strrchr(addrPortStr, ':');
|
|
||||||
if (rColon == NULL) {
|
|
||||||
// no colon
|
|
||||||
// "udp:<ipv4 address>", "udp:[<ipv4 address>]"
|
|
||||||
portstr = "18888";
|
|
||||||
} else if (lColon == rColon) {
|
|
||||||
// only one colon
|
|
||||||
// "udp:<ipv4 address>:<port>", "udp:[<ipv4 address>]:<port>"
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
|
||||||
// two or more colon, IPv6 address
|
|
||||||
// "udp:[<ipv6 address>]:<port>"
|
|
||||||
// "udp:<ipv6 address>", "udp:[<ipv6 address>]"
|
|
||||||
if (endBracket != NULL && rColon == endBracket + 1) {
|
|
||||||
portstr = rColon + 1;
|
|
||||||
} else {
|
} else {
|
||||||
portstr = "18888";
|
PrintAndLogEx(ERR, "error: failed to parse address and port");
|
||||||
}
|
}
|
||||||
g_conn.send_via_ip = PM3_UDPv6;
|
free(addrPortStr);
|
||||||
|
free(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the end of the address
|
g_conn.send_via_ip = isIPv6 ? PM3_UDPv6 : PM3_UDPv4;
|
||||||
if (endBracket != NULL) {
|
portStr = (portStr == NULL) ? "18888" : portStr;
|
||||||
*endBracket = '\0';
|
|
||||||
} else if (rColon != NULL && lColon == rColon) {
|
|
||||||
*rColon = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
struct addrinfo info;
|
struct addrinfo info;
|
||||||
|
@ -453,13 +337,13 @@ 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) ||
|
if ((strstr(addrStr, "localhost") != NULL) ||
|
||||||
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
(strstr(addrStr, "127.0.0.1") != NULL) ||
|
||||||
(strstr(addrstr, "::1") != NULL)) {
|
(strstr(addrStr, "::1") != NULL)) {
|
||||||
g_conn.send_via_local_ip = true;
|
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));
|
||||||
freeaddrinfo(addr);
|
freeaddrinfo(addr);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue