Merge pull request #2218 from wh201906/net_simplify
Clean up the code for network connection
This commit is contained in:
8 files changed
+257
-660
No files matched your search
+52
-334
@@ -104,227 +104,20 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||
}
|
||||
str_lower(prefix);
|
||||
|
||||
if (memcmp(prefix, "tcp:", 4) == 0) {
|
||||
free(prefix);
|
||||
|
||||
if (strlen(pcPortName) <= 4) {
|
||||
PrintAndLogEx(ERR, "error: tcp port name length too short");
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
struct addrinfo *addr = NULL, *rp;
|
||||
|
||||
char *addrPortStr = str_dup(pcPortName + 4);
|
||||
char *addrstr = addrPortStr;
|
||||
const char *portstr;
|
||||
if (addrPortStr == NULL) {
|
||||
PrintAndLogEx(ERR, "error: string duplication");
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
timeout.tv_usec = UART_NET_CLIENT_RX_TIMEOUT_MS * 1000;
|
||||
|
||||
// find the "bind" option
|
||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||
char *bindAddrStr = NULL;
|
||||
char *bindPortStr = NULL;
|
||||
bool isBindingIPv6 = false; // Assume v4
|
||||
if (bindAddrPortStr != NULL) {
|
||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||
bindAddrPortStr += 6;
|
||||
bindAddrStr = bindAddrPortStr;
|
||||
|
||||
// find the start of the bind address
|
||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
||||
if (bindAddrPortStr[0] == '[') {
|
||||
bindAddrStr += 1;
|
||||
if (endBracket == NULL) {
|
||||
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 {
|
||||
bindPortStr = NULL;
|
||||
}
|
||||
isBindingIPv6 = true;
|
||||
}
|
||||
|
||||
// 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
|
||||
if (strlen(bindAddrStr) == 0)
|
||||
bindAddrStr = NULL;
|
||||
if (bindPortStr != NULL && strlen(bindPortStr) == 0)
|
||||
bindPortStr = NULL;
|
||||
}
|
||||
|
||||
// find the start of the address
|
||||
char *endBracket = strrchr(addrPortStr, ']');
|
||||
if (addrPortStr[0] == '[') {
|
||||
addrstr += 1;
|
||||
if (endBracket == NULL) {
|
||||
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 {
|
||||
portstr = "18888";
|
||||
}
|
||||
g_conn.send_via_ip = PM3_TCPv6;
|
||||
}
|
||||
|
||||
// handle the end of the address
|
||||
if (endBracket != NULL) {
|
||||
*endBracket = '\0';
|
||||
} else if (rColon != NULL && lColon == rColon) {
|
||||
*rColon = '\0';
|
||||
}
|
||||
|
||||
WSADATA wsaData;
|
||||
struct addrinfo info;
|
||||
int iResult;
|
||||
|
||||
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
if (iResult != 0) {
|
||||
PrintAndLogEx(ERR, "error: WSAStartup failed with error: %d", iResult);
|
||||
free(addrPortStr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.ai_family = AF_UNSPEC;
|
||||
info.ai_socktype = SOCK_STREAM;
|
||||
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);
|
||||
if (s != 0) {
|
||||
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
||||
freeaddrinfo(addr);
|
||||
free(addrPortStr);
|
||||
free(sp);
|
||||
WSACleanup();
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
SOCKET hSocket = INVALID_SOCKET;
|
||||
for (rp = addr; rp != NULL; rp = rp->ai_next) {
|
||||
hSocket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
|
||||
if (hSocket == INVALID_SOCKET)
|
||||
continue;
|
||||
|
||||
if (!uart_bind(&hSocket, bindAddrStr, bindPortStr, isBindingIPv6)) {
|
||||
PrintAndLogEx(ERR, "error: Could not bind. error: %u", WSAGetLastError());
|
||||
closesocket(hSocket);
|
||||
hSocket = INVALID_SOCKET;
|
||||
freeaddrinfo(addr);
|
||||
free(addrPortStr);
|
||||
free(sp);
|
||||
WSACleanup();
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
if (connect(hSocket, rp->ai_addr, (int)rp->ai_addrlen) != INVALID_SOCKET)
|
||||
break;
|
||||
|
||||
closesocket(hSocket);
|
||||
hSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr);
|
||||
free(addrPortStr);
|
||||
|
||||
if (rp == NULL) { /* No address succeeded */
|
||||
PrintAndLogEx(ERR, "error: Could not connect");
|
||||
WSACleanup();
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
sp->hSocket = hSocket;
|
||||
|
||||
int one = 1;
|
||||
int res = setsockopt(sp->hSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one));
|
||||
if (res != 0) {
|
||||
closesocket(hSocket);
|
||||
WSACleanup();
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
return sp;
|
||||
bool isTCP = false;
|
||||
bool isUDP = false;
|
||||
if (strlen(prefix) > 4) {
|
||||
isTCP = (memcmp(prefix, "tcp:", 4) == 0);
|
||||
isUDP = (memcmp(prefix, "udp:", 4) == 0);
|
||||
}
|
||||
|
||||
if (memcmp(prefix, "udp:", 4) == 0) {
|
||||
free(prefix);
|
||||
if (isTCP || isUDP) {
|
||||
|
||||
if (strlen(pcPortName) <= 4) {
|
||||
PrintAndLogEx(ERR, "error: tcp port name length too short");
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
free(prefix);
|
||||
|
||||
struct addrinfo *addr = NULL, *rp;
|
||||
|
||||
char *addrPortStr = str_dup(pcPortName + 4);
|
||||
char *addrstr = addrPortStr;
|
||||
const char *portstr;
|
||||
if (addrPortStr == NULL) {
|
||||
PrintAndLogEx(ERR, "error: string duplication");
|
||||
free(sp);
|
||||
@@ -335,54 +128,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||
|
||||
// find the "bind" option
|
||||
char *bindAddrPortStr = strstr(addrPortStr, ",bind=");
|
||||
char *bindAddrStr = NULL;
|
||||
char *bindPortStr = NULL;
|
||||
bool isBindingIPv6 = false; // Assume v4
|
||||
const char *bindAddrStr = NULL;
|
||||
const char *bindPortStr = NULL;
|
||||
bool isBindingIPv6 = false;
|
||||
|
||||
if (bindAddrPortStr != NULL) {
|
||||
*bindAddrPortStr = '\0'; // as the end of target address (and port)
|
||||
bindAddrPortStr += 6;
|
||||
bindAddrStr = bindAddrPortStr;
|
||||
bindAddrPortStr += 6; // strlen(",bind=")
|
||||
|
||||
// find the start of the bind address
|
||||
char *endBracket = strrchr(bindAddrPortStr, ']');
|
||||
if (bindAddrPortStr[0] == '[') {
|
||||
bindAddrStr += 1;
|
||||
if (endBracket == NULL) {
|
||||
int result = uart_parse_address_port(bindAddrPortStr, &bindAddrStr, &bindPortStr, &isBindingIPv6);
|
||||
if (result != PM3_SUCCESS) {
|
||||
if (result == PM3_ESOFT) {
|
||||
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 {
|
||||
bindPortStr = NULL;
|
||||
PrintAndLogEx(ERR, "error: failed to parse address and port in bind option");
|
||||
}
|
||||
isBindingIPv6 = true;
|
||||
}
|
||||
|
||||
// handle the end of the bind address
|
||||
if (endBracket != NULL) {
|
||||
*endBracket = '\0';
|
||||
} else if (rColon != NULL && lColon == rColon) {
|
||||
*rColon = '\0';
|
||||
free(addrPortStr);
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// for bind option, it's possible to only specify address or port
|
||||
@@ -392,50 +155,24 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||
bindPortStr = NULL;
|
||||
}
|
||||
|
||||
// find the start of the address
|
||||
char *endBracket = strrchr(addrPortStr, ']');
|
||||
if (addrPortStr[0] == '[') {
|
||||
addrstr += 1;
|
||||
if (endBracket == NULL) {
|
||||
const char *addrStr = NULL;
|
||||
const char *portStr = NULL;
|
||||
bool isIPv6 = false;
|
||||
|
||||
int result = uart_parse_address_port(addrPortStr, &addrStr, &portStr, &isIPv6);
|
||||
if (result != PM3_SUCCESS) {
|
||||
if (result == PM3_ESOFT) {
|
||||
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 {
|
||||
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
|
||||
if (endBracket != NULL) {
|
||||
*endBracket = '\0';
|
||||
} else if (rColon != NULL && lColon == rColon) {
|
||||
*rColon = '\0';
|
||||
}
|
||||
g_conn.send_via_ip = isIPv6 ? (isTCP ? PM3_TCPv6 : PM3_UDPv6) : (isTCP ? PM3_TCPv4 : PM3_UDPv4);
|
||||
portStr = (portStr == NULL) ? "18888" : portStr;
|
||||
|
||||
WSADATA wsaData;
|
||||
struct addrinfo info;
|
||||
@@ -451,16 +188,16 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.ai_family = AF_UNSPEC;
|
||||
info.ai_socktype = SOCK_DGRAM;
|
||||
info.ai_protocol = IPPROTO_UDP;
|
||||
info.ai_socktype = isTCP ? SOCK_STREAM : SOCK_DGRAM;
|
||||
info.ai_protocol = isTCP ? IPPROTO_TCP : IPPROTO_UDP;
|
||||
|
||||
if ((strstr(addrstr, "localhost") != NULL) ||
|
||||
(strstr(addrstr, "127.0.0.1") != NULL) ||
|
||||
(strstr(addrstr, "::1") != NULL)) {
|
||||
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) {
|
||||
PrintAndLogEx(ERR, "error: getaddrinfo: %d: %s", s, gai_strerror(s));
|
||||
freeaddrinfo(addr);
|
||||
@@ -506,7 +243,20 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
|
||||
}
|
||||
|
||||
sp->hSocket = hSocket;
|
||||
sp->udpBuffer = RingBuf_create(MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 30);
|
||||
|
||||
if (isTCP) {
|
||||
int one = 1;
|
||||
int res = setsockopt(sp->hSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one));
|
||||
if (res != 0) {
|
||||
closesocket(hSocket);
|
||||
WSACleanup();
|
||||
free(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
} else if (isUDP) {
|
||||
sp->udpBuffer = RingBuf_create(MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 30);
|
||||
}
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
@@ -795,36 +545,4 @@ int uart_send(const serial_port sp, const uint8_t *p_tx, const uint32_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
bool uart_bind(void *socket, char *bindAddrStr, char *bindPortStr, bool isBindingIPv6) {
|
||||
if (bindAddrStr == NULL && bindPortStr == NULL)
|
||||
return true; // no need to bind
|
||||
|
||||
struct sockaddr_storage bindSockaddr;
|
||||
memset(&bindSockaddr, 0, sizeof(bindSockaddr));
|
||||
int bindPort = 0; // 0: port unspecified
|
||||
if (bindPortStr != NULL)
|
||||
bindPort = atoi(bindPortStr);
|
||||
|
||||
if (!isBindingIPv6) {
|
||||
struct sockaddr_in *bindSockaddr4 = (struct sockaddr_in *)&bindSockaddr;
|
||||
bindSockaddr4->sin_family = AF_INET;
|
||||
bindSockaddr4->sin_port = htons(bindPort);
|
||||
if (bindAddrStr == NULL)
|
||||
bindSockaddr4->sin_addr.s_addr = INADDR_ANY;
|
||||
else
|
||||
bindSockaddr4->sin_addr.s_addr = inet_addr(bindAddrStr);
|
||||
} else {
|
||||
struct sockaddr_in6 *bindSockaddr6 = (struct sockaddr_in6 *)&bindSockaddr;
|
||||
bindSockaddr6->sin6_family = AF_INET6;
|
||||
bindSockaddr6->sin6_port = htons(bindPort);
|
||||
if (bindAddrStr == NULL)
|
||||
bindSockaddr6->sin6_addr = in6addr_any;
|
||||
else
|
||||
inet_pton(AF_INET6, bindAddrStr, &(bindSockaddr6->sin6_addr));
|
||||
}
|
||||
|
||||
int res = bind(*(SOCKET *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr));
|
||||
return (res >= 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user