Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions include/tinycsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#ifndef TINYCSOCKET_INTERNAL_H_
#define TINYCSOCKET_INTERNAL_H_

static const char* const TCS_VERSION_TXT = "v0.3.58";
static const char* const TCS_VERSION_TXT = "v0.3.59";
static const char* const TCS_LICENSE_TXT =
"Copyright 2018 Markus Lindelöw\n"
"\n"
Expand Down Expand Up @@ -277,13 +277,17 @@ struct TcsAddress
} data;
};

#ifndef TCS_INTERFACE_NAME_SIZE
#define TCS_INTERFACE_NAME_SIZE 64
#endif

/**
* @brief Network Interface Information
*/
struct TcsInterface
{
TcsInterfaceId id;
char name[32];
char name[TCS_INTERFACE_NAME_SIZE];
};

struct TcsInterfaceAddress
Expand Down Expand Up @@ -2710,7 +2714,7 @@ static inline int tds_map_remove(void** keys,
#include <sys/ioctl.h> // Flags for ifaddrs
#include <sys/socket.h> // pretty much everything
#include <sys/types.h> // POSIX.1 compatibility
#include <sys/uio.h> // UIO_MAXIOV
#include <sys/uio.h> // struct iovec
#include <unistd.h> // close()

#if TCS_HAS_GETIFADDRS
Expand Down Expand Up @@ -2783,12 +2787,20 @@ const int TCS_SO_BROADCAST = SO_BROADCAST;
const int TCS_SO_KEEPALIVE = SO_KEEPALIVE;
const int TCS_SO_LINGER = SO_LINGER;
const int TCS_SO_REUSEADDR = SO_REUSEADDR;
#ifdef SO_REUSEPORT
const int TCS_SO_REUSEPORT = SO_REUSEPORT;
#else
const int TCS_SO_REUSEPORT = -1;
#endif
const int TCS_SO_RCVBUF = SO_RCVBUF;
const int TCS_SO_RCVTIMEO = SO_RCVTIMEO;
const int TCS_SO_SNDBUF = SO_SNDBUF;
const int TCS_SO_OOBINLINE = SO_OOBINLINE;
#ifdef SO_PRIORITY
const int TCS_SO_PRIORITY = SO_PRIORITY;
#else
const int TCS_SO_PRIORITY = -1;
#endif

// IP options
const int TCS_SO_IP_NODELAY = TCP_NODELAY;
Expand Down Expand Up @@ -3286,7 +3298,7 @@ TcsResult tcs_sendv(TcsSocket socket_ctx,
if (flags & TCS_MSG_SENDALL)
return TCS_ERROR_NOT_IMPLEMENTED;

if (buffer_count > UIO_MAXIOV)
if (buffer_count > IOV_MAX)
return TCS_ERROR_INVALID_ARGUMENT;

struct iovec stack_iovec[TCS_SENDV_STACK_MAX];
Expand Down Expand Up @@ -3321,7 +3333,7 @@ TcsResult tcs_sendv(TcsSocket socket_ctx,
msg.msg_namelen = 0;
msg.msg_iov = my_iovec;
// msg_iovlen type varies across platforms (int on POSIX, size_t on glibc).
// buffer_count is already validated against UIO_MAXIOV above.
// buffer_count is already validated against IOV_MAX above.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
Expand Down Expand Up @@ -4021,8 +4033,8 @@ TcsResult tcs_interface_list(struct TcsInterface* found_interfaces,
{
for (size_t i = 0; i < interfaces_length && interfaces[i].if_index != 0; ++i)
{
strncpy(found_interfaces[i].name, interfaces[i].if_name, 31);
found_interfaces[i].name[31] = '\0';
strncpy(found_interfaces[i].name, interfaces[i].if_name, TCS_INTERFACE_NAME_SIZE - 1);
found_interfaces[i].name[TCS_INTERFACE_NAME_SIZE - 1] = '\0';
found_interfaces[i].id = interfaces[i].if_index;
if (interfaces_populated != NULL)
*interfaces_populated += 1;
Expand Down Expand Up @@ -4207,8 +4219,8 @@ TcsResult tcs_address_list(unsigned int interface_id_filter,
return errno2retcode(errno);
}

strncpy(interface_addresses[populated].iface.name, iter->ifa_name, 31);
interface_addresses[populated].iface.name[31] = '\0';
strncpy(interface_addresses[populated].iface.name, iter->ifa_name, TCS_INTERFACE_NAME_SIZE - 1);
interface_addresses[populated].iface.name[TCS_INTERFACE_NAME_SIZE - 1] = '\0';
interface_addresses[populated].iface.id = interface_id;
interface_addresses[populated].address = address;
populated++;
Expand Down Expand Up @@ -4330,12 +4342,13 @@ TcsResult tcs_address_socket_family(TcsSocket socket_ctx, TcsAddressFamily* out_
#include "dbg_wrap.h"
#endif

#define WIN32_LEAN_AND_MEAN
// Header only should not need other files
#ifndef TINYDATASTRUCTURES_H_
#include "tinydatastructures.h"
#endif
// before windows.h
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <winsock2.h> // sockets

#include <windows.h>
Expand Down Expand Up @@ -4367,10 +4380,6 @@ typedef struct
#pragma comment(lib, "Iphlpapi.lib")
#endif

#ifdef __cplusplus
using std::min;
#endif

#ifndef ULIST_SOC
#define ULIST_SOC
TDS_ULIST_IMPL(SOCKET, soc)
Expand Down Expand Up @@ -5819,8 +5828,8 @@ TcsResult tcs_interface_list(struct TcsInterface interfaces[], size_t capacity,
if (!is_up)
continue;

memset(interfaces[i].name, '\0', 32);
TcsResult name_sts = adapter_get_friendly_name(iter, interfaces[i].name, 31);
memset(interfaces[i].name, '\0', TCS_INTERFACE_NAME_SIZE);
TcsResult name_sts = adapter_get_friendly_name(iter, interfaces[i].name, TCS_INTERFACE_NAME_SIZE - 1);
if (name_sts != TCS_SUCCESS)
{
free(adapters);
Expand Down Expand Up @@ -6029,8 +6038,9 @@ TcsResult tcs_address_list(unsigned int interface_id_filter,

if (interface_addresses != NULL && populated < capacity)
{
memset(interface_addresses[populated].iface.name, '\0', 32);
TcsResult name_sts = adapter_get_friendly_name(iter, interface_addresses[populated].iface.name, 31);
memset(interface_addresses[populated].iface.name, '\0', TCS_INTERFACE_NAME_SIZE);
TcsResult name_sts = adapter_get_friendly_name(
iter, interface_addresses[populated].iface.name, TCS_INTERFACE_NAME_SIZE - 1);
if (name_sts != TCS_SUCCESS)
{
free(adapters);
Expand Down Expand Up @@ -6635,9 +6645,9 @@ TcsResult tcs_raw_str(TcsSocket* socket_ctx, const char* interface_name, uint16_
if (interface_name == NULL)
return TCS_ERROR_INVALID_ARGUMENT;

struct TcsInterface interfaces[32];
struct TcsInterface interfaces[16]; // TODO: use heap if more
size_t count = 0;
TcsResult res = tcs_interface_list(interfaces, 32, &count);
TcsResult res = tcs_interface_list(interfaces, 16, &count);
if (res != TCS_SUCCESS)
return res;

Expand Down Expand Up @@ -6686,9 +6696,9 @@ TcsResult tcs_packet_str(TcsSocket* socket_ctx, const char* interface_name, uint
if (interface_name == NULL)
return TCS_ERROR_INVALID_ARGUMENT;

struct TcsInterface interfaces[32];
struct TcsInterface interfaces[16];
size_t count = 0;
TcsResult res = tcs_interface_list(interfaces, 32, &count);
TcsResult res = tcs_interface_list(interfaces, 16, &count);
if (res != TCS_SUCCESS)
return res;

Expand Down
8 changes: 4 additions & 4 deletions src/tinycsocket_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ TcsResult tcs_raw_str(TcsSocket* socket_ctx, const char* interface_name, uint16_
if (interface_name == NULL)
return TCS_ERROR_INVALID_ARGUMENT;

struct TcsInterface interfaces[32];
struct TcsInterface interfaces[16]; // TODO: use heap if more
size_t count = 0;
TcsResult res = tcs_interface_list(interfaces, 32, &count);
TcsResult res = tcs_interface_list(interfaces, 16, &count);
if (res != TCS_SUCCESS)
return res;

Expand Down Expand Up @@ -569,9 +569,9 @@ TcsResult tcs_packet_str(TcsSocket* socket_ctx, const char* interface_name, uint
if (interface_name == NULL)
return TCS_ERROR_INVALID_ARGUMENT;

struct TcsInterface interfaces[32];
struct TcsInterface interfaces[16];
size_t count = 0;
TcsResult res = tcs_interface_list(interfaces, 32, &count);
TcsResult res = tcs_interface_list(interfaces, 16, &count);
if (res != TCS_SUCCESS)
return res;

Expand Down
8 changes: 6 additions & 2 deletions src/tinycsocket_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef TINYCSOCKET_INTERNAL_H_
#define TINYCSOCKET_INTERNAL_H_

static const char* const TCS_VERSION_TXT = "v0.3.58";
static const char* const TCS_VERSION_TXT = "v0.3.59";
static const char* const TCS_LICENSE_TXT =
"Copyright 2018 Markus Lindelöw\n"
"\n"
Expand Down Expand Up @@ -271,13 +271,17 @@ struct TcsAddress
} data;
};

#ifndef TCS_INTERFACE_NAME_SIZE
#define TCS_INTERFACE_NAME_SIZE 64
#endif

/**
* @brief Network Interface Information
*/
struct TcsInterface
{
TcsInterfaceId id;
char name[32];
char name[TCS_INTERFACE_NAME_SIZE];
};

struct TcsInterfaceAddress
Expand Down
22 changes: 15 additions & 7 deletions src/tinycsocket_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include <sys/ioctl.h> // Flags for ifaddrs
#include <sys/socket.h> // pretty much everything
#include <sys/types.h> // POSIX.1 compatibility
#include <sys/uio.h> // UIO_MAXIOV
#include <sys/uio.h> // struct iovec
#include <unistd.h> // close()

#if TCS_HAS_GETIFADDRS
Expand Down Expand Up @@ -139,12 +139,20 @@ const int TCS_SO_BROADCAST = SO_BROADCAST;
const int TCS_SO_KEEPALIVE = SO_KEEPALIVE;
const int TCS_SO_LINGER = SO_LINGER;
const int TCS_SO_REUSEADDR = SO_REUSEADDR;
#ifdef SO_REUSEPORT
const int TCS_SO_REUSEPORT = SO_REUSEPORT;
#else
const int TCS_SO_REUSEPORT = -1;
#endif
const int TCS_SO_RCVBUF = SO_RCVBUF;
const int TCS_SO_RCVTIMEO = SO_RCVTIMEO;
const int TCS_SO_SNDBUF = SO_SNDBUF;
const int TCS_SO_OOBINLINE = SO_OOBINLINE;
#ifdef SO_PRIORITY
const int TCS_SO_PRIORITY = SO_PRIORITY;
#else
const int TCS_SO_PRIORITY = -1;
#endif

// IP options
const int TCS_SO_IP_NODELAY = TCP_NODELAY;
Expand Down Expand Up @@ -642,7 +650,7 @@ TcsResult tcs_sendv(TcsSocket socket_ctx,
if (flags & TCS_MSG_SENDALL)
return TCS_ERROR_NOT_IMPLEMENTED;

if (buffer_count > UIO_MAXIOV)
if (buffer_count > IOV_MAX)
return TCS_ERROR_INVALID_ARGUMENT;

struct iovec stack_iovec[TCS_SENDV_STACK_MAX];
Expand Down Expand Up @@ -677,7 +685,7 @@ TcsResult tcs_sendv(TcsSocket socket_ctx,
msg.msg_namelen = 0;
msg.msg_iov = my_iovec;
// msg_iovlen type varies across platforms (int on POSIX, size_t on glibc).
// buffer_count is already validated against UIO_MAXIOV above.
// buffer_count is already validated against IOV_MAX above.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
Expand Down Expand Up @@ -1377,8 +1385,8 @@ TcsResult tcs_interface_list(struct TcsInterface* found_interfaces,
{
for (size_t i = 0; i < interfaces_length && interfaces[i].if_index != 0; ++i)
{
strncpy(found_interfaces[i].name, interfaces[i].if_name, 31);
found_interfaces[i].name[31] = '\0';
strncpy(found_interfaces[i].name, interfaces[i].if_name, TCS_INTERFACE_NAME_SIZE - 1);
found_interfaces[i].name[TCS_INTERFACE_NAME_SIZE - 1] = '\0';
found_interfaces[i].id = interfaces[i].if_index;
if (interfaces_populated != NULL)
*interfaces_populated += 1;
Expand Down Expand Up @@ -1563,8 +1571,8 @@ TcsResult tcs_address_list(unsigned int interface_id_filter,
return errno2retcode(errno);
}

strncpy(interface_addresses[populated].iface.name, iter->ifa_name, 31);
interface_addresses[populated].iface.name[31] = '\0';
strncpy(interface_addresses[populated].iface.name, iter->ifa_name, TCS_INTERFACE_NAME_SIZE - 1);
interface_addresses[populated].iface.name[TCS_INTERFACE_NAME_SIZE - 1] = '\0';
interface_addresses[populated].iface.id = interface_id;
interface_addresses[populated].address = address;
populated++;
Expand Down
16 changes: 7 additions & 9 deletions src/tinycsocket_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
#include "dbg_wrap.h"
#endif

#define WIN32_LEAN_AND_MEAN
// Header only should not need other files
#ifndef TINYDATASTRUCTURES_H_
#include "tinydatastructures.h"
#endif
// before windows.h
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <winsock2.h> // sockets

#include <windows.h>
Expand Down Expand Up @@ -65,10 +66,6 @@ typedef struct
#pragma comment(lib, "Iphlpapi.lib")
#endif

#ifdef __cplusplus
using std::min;
#endif

#ifndef ULIST_SOC
#define ULIST_SOC
TDS_ULIST_IMPL(SOCKET, soc)
Expand Down Expand Up @@ -1517,8 +1514,8 @@ TcsResult tcs_interface_list(struct TcsInterface interfaces[], size_t capacity,
if (!is_up)
continue;

memset(interfaces[i].name, '\0', 32);
TcsResult name_sts = adapter_get_friendly_name(iter, interfaces[i].name, 31);
memset(interfaces[i].name, '\0', TCS_INTERFACE_NAME_SIZE);
TcsResult name_sts = adapter_get_friendly_name(iter, interfaces[i].name, TCS_INTERFACE_NAME_SIZE - 1);
if (name_sts != TCS_SUCCESS)
{
free(adapters);
Expand Down Expand Up @@ -1727,8 +1724,9 @@ TcsResult tcs_address_list(unsigned int interface_id_filter,

if (interface_addresses != NULL && populated < capacity)
{
memset(interface_addresses[populated].iface.name, '\0', 32);
TcsResult name_sts = adapter_get_friendly_name(iter, interface_addresses[populated].iface.name, 31);
memset(interface_addresses[populated].iface.name, '\0', TCS_INTERFACE_NAME_SIZE);
TcsResult name_sts = adapter_get_friendly_name(
iter, interface_addresses[populated].iface.name, TCS_INTERFACE_NAME_SIZE - 1);
if (name_sts != TCS_SUCCESS)
{
free(adapters);
Expand Down
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ include(CTest)
add_executable(tests "tests.cpp" "mock.cpp" "mock.h")
target_compile_options(tests PUBLIC "-DDO_WRAP")
target_link_libraries(tests PRIVATE tinycsocket_wrapped)
# doctest's signal handling needs POSIX feature test macros to expose
# sigaltstack/sigaction/SA_ONSTACK/SIGSTKSZ on Cygwin and glibc with -std=c++11.
# Set them only on test targets to avoid leaking into header-only consumers.
if(NOT MSVC)
target_compile_definitions(tests PRIVATE
_XOPEN_SOURCE=600
_POSIX_C_SOURCE=200112L
_DEFAULT_SOURCE
_ISOC99_SOURCE
)
endif()
set_target_properties(tests PROPERTIES FOLDER tinycsocket/tests)
add_definitions(-DDOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING)
if(MINGW)
Expand Down Expand Up @@ -68,7 +79,6 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL "Android" AND NOT MSVC)
find_package(Threads REQUIRED)
target_link_libraries(tests_header_only PRIVATE Threads::Threads)
endif()
target_compile_options(tests_header_only PUBLIC "-DDO_WRAP")
target_link_libraries(tests_header_only PRIVATE tinycsocket_header)
target_include_directories(
tests_header_only
Expand Down
Loading
Loading