diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index cf0837845..8a624934d 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -93,12 +93,27 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { // init timeouts timeout.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000; - if (memcmp(pcPortName, "tcp:", 4) == 0) { - struct addrinfo *addr = NULL, *rp; - char *addrstr = strdup(pcPortName + 4); + char *prefix = strdup(pcPortName); + if (prefix == NULL) { + PrintAndLogEx(ERR, "error: malloc"); + free(sp); + return INVALID_SERIAL_PORT; + } + str_lower(prefix); + if (memcmp(prefix, "tcp:", 4) == 0) { + free(prefix); + + if (strlen(pcPortName) <= 4) { + free(sp); + return INVALID_SERIAL_PORT; + } + + struct addrinfo *addr = NULL, *rp; + + char *addrstr = strdup(pcPortName + 4); if (addrstr == NULL) { - printf("Error: strdup\n"); + PrintAndLogEx(ERR, "error: malloc"); free(sp); return INVALID_SERIAL_PORT; } @@ -122,7 +137,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { int s = getaddrinfo(addrstr, portstr, &info, &addr); if (s != 0) { - printf("Error: getaddrinfo: %s\n", gai_strerror(s)); + PrintAndLogEx(ERR, "error: getaddrinfo: %s", gai_strerror(s)); freeaddrinfo(addr); free(addrstr); free(sp); @@ -143,7 +158,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { } if (rp == NULL) { /* No address succeeded */ - printf("Error: Could not connect\n"); + PrintAndLogEx(ERR, "error: Could not connect"); freeaddrinfo(addr); free(addrstr); free(sp); @@ -164,16 +179,18 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { return sp; } - if (memcmp(pcPortName, "bt:", 3) == 0) { + if (memcmp(prefix, "bt:", 3) == 0) { + free(prefix); + #ifdef HAVE_BLUEZ if (strlen(pcPortName) != 20) { free(sp); return INVALID_SERIAL_PORT; } - char *addrstr = strndup(pcPortName + 3, 17); + char *addrstr = strndup(pcPortName + 3, 17); if (addrstr == NULL) { - printf("Error: malloc\n"); + PrintAndLogEx(ERR, "error: malloc"); free(sp); return INVALID_SERIAL_PORT; } @@ -214,7 +231,9 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { // Is local socket buffer, not a TCP or any net connection! // so, you can't connect with address like: 127.0.0.1, or any IP // see http://man7.org/linux/man-pages/man7/unix.7.html - if (memcmp(pcPortName, "socket:", 7) == 0) { + if (memcmp(prefix, "socket:", 7) == 0) { + free(prefix); + if (strlen(pcPortName) <= 7) { free(sp); return INVALID_SERIAL_PORT; @@ -255,6 +274,8 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { return sp; } + free(prefix); + sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); if (sp->fd == -1) { uart_close(sp); @@ -311,7 +332,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { speed = 115200; if (!uart_set_speed(sp, speed)) { uart_close(sp); - printf("[!] UART error while setting baudrate\n"); + PrintAndLogEx(ERR, "UART error while setting baudrate"); return INVALID_SERIAL_PORT; } }