diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index f14b2d50f..d92c15fbe 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -288,6 +288,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/mifare/gallaghercore.c ${PM3_ROOT}/client/src/uart/ringbuffer.c + ${PM3_ROOT}/client/src/uart/uart_common.c ${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/ui/overlays.ui diff --git a/client/Makefile b/client/Makefile index 6534f27a0..fc0dedb82 100644 --- a/client/Makefile +++ b/client/Makefile @@ -713,6 +713,7 @@ SRCS = mifare/aiddesfire.c \ proxmark3.c \ scandir.c \ uart/ringbuffer.c \ + uart/uart_common.c \ uart/uart_posix.c \ uart/uart_win32.c \ scripting.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 9dae9d64b..15a74d38c 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -289,6 +289,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/mifare/gallaghercore.c ${PM3_ROOT}/client/src/uart/ringbuffer.c + ${PM3_ROOT}/client/src/uart/uart_common.c ${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/ui/overlays.ui diff --git a/client/src/uart/uart_common.c b/client/src/uart/uart_common.c new file mode 100644 index 000000000..af439699a --- /dev/null +++ b/client/src/uart/uart_common.c @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Generic uart / rs232/ serial port library +//----------------------------------------------------------------------------- + +#include "uart.h" + +#include +#include +#include + +#include "comms.h" +#include "ui.h" + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#else +#include +#include +#include +#include +#include +#endif + +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)); + } +#ifdef _WIN32 + int res = bind(*(SOCKET *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr)); +#else + int res = bind(*(int *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr)); +#endif + return (res >= 0); +} \ No newline at end of file diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 588e9ae90..267e0fe43 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -106,6 +106,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"); free(sp); return INVALID_SERIAL_PORT; } @@ -297,6 +298,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { free(prefix); if (strlen(pcPortName) <= 4) { + PrintAndLogEx(ERR, "error: udp port name length too short"); free(sp); return INVALID_SERIAL_PORT; } @@ -996,36 +998,4 @@ uint32_t uart_get_speed(const serial_port sp) { return uiPortSpeed; } -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(*(int *)socket, (struct sockaddr *)&bindSockaddr, sizeof(bindSockaddr)); - return (res >= 0); -} - #endif diff --git a/client/src/uart/uart_win32.c b/client/src/uart/uart_win32.c index 43299ca5a..f91a69290 100644 --- a/client/src/uart/uart_win32.c +++ b/client/src/uart/uart_win32.c @@ -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 diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 33803857d..abee08bb2 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -842,7 +842,7 @@ typedef struct { // all zero's configure: no timeout for read/write used. // took settings from libnfc/buses/uart.c -// uart_windows.c & uart_posix.c +// uart_win32.c & uart_posix.c # define UART_FPC_CLIENT_RX_TIMEOUT_MS 200 # define UART_USB_CLIENT_RX_TIMEOUT_MS 20 # define UART_NET_CLIENT_RX_TIMEOUT_MS 500