Put uart_bind() into uart_common.c

This commit is contained in:
wh201906 2023-12-25 23:50:34 +08:00
parent 5e06656580
commit ecec001fdd
No known key found for this signature in database
7 changed files with 81 additions and 66 deletions

View file

@ -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

View file

@ -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 \

View file

@ -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

View file

@ -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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "comms.h"
#include "ui.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/un.h>
#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);
}

View file

@ -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

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

View file

@ -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