Put uart_bind() into uart_common.c

This commit is contained in:
wh201906 committed 2023-12-26 17:56:14 +08:00
1 parent 5e06656580
commit ecec001fdd
7 files changed
+81 -66

No files matched your search

+1 -33
View File
@@ -314,7 +314,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
free(prefix);
if (strlen(pcPortName) <= 4) {
PrintAndLogEx(ERR, "error: tcp port name length too short");
PrintAndLogEx(ERR, "error: udp port name length too short");
free(sp);
return INVALID_SERIAL_PORT;
}
@@ -780,36 +780,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