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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main(int argc, const char* argv[])
size_t bytes_received = 0;
tcs_receive(client_socket, recv_buffer, sizeof(recv_buffer), TCS_MSG_WAITALL, &bytes_received);

tcs_shutdown(client_socket, TCS_SD_BOTH);
tcs_shutdown(client_socket, TCS_SHUTDOWN_BOTH);
tcs_close(&client_socket);

tcs_lib_free();
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main(void)
recv_buffer[bytes_received] = '\0';
printf("received: %s\n", recv_buffer);

if (tcs_shutdown(client_socket, TCS_SD_BOTH) != TCS_SUCCESS)
if (tcs_shutdown(client_socket, TCS_SHUTDOWN_BOTH) != TCS_SUCCESS)
return show_error("Could not shutdown socket");

if (tcs_close(&client_socket) != TCS_SUCCESS)
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(void)
if (tcs_send(child_socket, (const uint8_t*)msg, sizeof(msg), TCS_MSG_SENDALL, NULL) != TCS_SUCCESS)
return show_error("Could not send reply message");

if (tcs_shutdown(child_socket, TCS_SD_BOTH) != TCS_SUCCESS)
if (tcs_shutdown(child_socket, TCS_SHUTDOWN_BOTH) != TCS_SUCCESS)
return show_error("Could not shutdown socket");

if (tcs_close(&child_socket) != TCS_SUCCESS)
Expand Down
84 changes: 37 additions & 47 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.75";
static const char* const TCS_VERSION_TXT = "v0.3.76";
static const char* const TCS_LICENSE_TXT =
"Copyright 2018 Markus Lindelöw\n"
"\n"
Expand Down Expand Up @@ -77,7 +77,7 @@ static const char* const TCS_LICENSE_TXT =
* - TcsResult tcs_connect_str(TcsSocket socket_ctx, const char* remote_address, uint16_t port);
* - TcsResult tcs_listen(TcsSocket socket_ctx, int backlog);
* - TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct TcsAddress* address);
* - TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction);
* - TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction);
*
* Data Transfer:
* - TcsResult tcs_send(TcsSocket socket_ctx, const uint8_t* buffer, size_t buffer_size, uint32_t flags, size_t* bytes_sent);
Expand Down Expand Up @@ -274,8 +274,9 @@ struct TcsAddress
{
TcsInterfaceId
interface_id; /**< Local interface index, use tcs_interface_list() to find valid interfaces. Native type. */
uint16_t protocol; /**< Host byte order. E.i. TCS_ETH_P_ALL, 0 (block all until bind), ETH_P_TSN etc. */
uint8_t mac[6]; /**< Typical destination mac address or local mac address when joining groups */
uint16_t
protocol; /**< Host byte order. E.g. TCS_PROTOCOL_ETH_ALL, 0 (block all until bind), ETH_P_TSN etc. */
uint8_t mac[6]; /**< Typical destination mac address or local mac address when joining groups */
} packet;
} data;
};
Expand Down Expand Up @@ -358,12 +359,7 @@ typedef uint16_t TcsProtocol;

static const TcsProtocol TCS_PROTOCOL_IP_TCP = 6; /**< TCP, IANA-assigned (RFC 9293). Use with TCS_SOCK_STREAM. */
static const TcsProtocol TCS_PROTOCOL_IP_UDP = 17; /**< UDP, IANA-assigned (RFC 768). Use with TCS_SOCK_DGRAM. */

// Ethernet protocols (host byte order)
static const TcsProtocol TCS_ETH_P_ALL = 0x0003; /**< Receive all protocols. Use with TCS_FAMILY_PACKET for capture. */

// Flags
extern const uint32_t TCS_AI_PASSIVE; /**< Use this flag for pure listening sockets */
static const TcsProtocol TCS_PROTOCOL_ETH_ALL = 3; /**< Receive all protocols. Use with TCS_FAMILY_PACKET. */

// Recv flags
extern const uint32_t TCS_MSG_PEEK;
Expand All @@ -379,10 +375,10 @@ extern const int TCS_BACKLOG_MAX; /**< Max number of queued sockets when listeni
// Socket Direction
typedef enum
{
TCS_SD_RECEIVE, /**< To shutdown incoming packets for socket */
TCS_SD_SEND, /**< To shutdown outgoing packets for socket */
TCS_SD_BOTH, /**< To shutdown both incoming and outgoing packets for socket */
} TcsSocketDirection;
TCS_SHUTDOWN_RECEIVE, /**< To shutdown incoming packets for socket */
TCS_SHUTDOWN_SEND, /**< To shutdown outgoing packets for socket */
TCS_SHUTDOWN_BOTH, /**< To shutdown both incoming and outgoing packets for socket */
} TcsShutdownDirection;

// Option levels
extern const int TCS_SOL_SOCKET; /**< Socket option level for socket options */
Expand All @@ -397,19 +393,19 @@ extern const int TCS_SO_REUSEADDR;
extern const int TCS_SO_REUSEPORT;
extern const int TCS_SO_RCVBUF; /**< Byte size of receiving buffer */
extern const int TCS_SO_RCVTIMEO;
extern const int TCS_SO_SNDBUF; /**< Byte size of receiving buffer */
extern const int TCS_SO_SNDBUF; /**< Byte size of sending buffer */
extern const int TCS_SO_OOBINLINE;
extern const int TCS_SO_PRIORITY;

// IP options
extern const int TCS_SO_IP_NODELAY;
extern const int TCS_SO_IP_MEMBERSHIP_ADD;
extern const int TCS_SO_IP_MEMBERSHIP_DROP;
extern const int TCS_SO_IP_MULTICAST_LOOP;
extern const int TCS_TCP_NODELAY;
extern const int TCS_IP_MEMBERSHIP_ADD;
extern const int TCS_IP_MEMBERSHIP_DROP;
extern const int TCS_IP_MULTICAST_LOOP;

// Packet options
extern const int TCS_SO_PACKET_MEMBERSHIP_ADD;
extern const int TCS_SO_PACKET_MEMBERSHIP_DROP;
extern const int TCS_PACKET_MEMBERSHIP_ADD;
extern const int TCS_PACKET_MEMBERSHIP_DROP;

// Use for timeout to wait until infinity happens
extern const int TCS_WAIT_INF;
Expand Down Expand Up @@ -1114,7 +1110,7 @@ TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct T
* @param direction defines in which direction you want to turn off the communication.
* @return #TCS_SUCCESS if successful, otherwise the error code.
*/
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction);
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction);

/**
* @brief Sends data on a socket, blocking
Expand Down Expand Up @@ -1400,7 +1396,7 @@ TcsResult tcs_opt_set(TcsSocket socket_ctx,
* @code
* uint8_t c;
* size_t a = sizeof(c);
* tcs_opt_get(socket, TCS_SOL_IP, TCS_SO_IP_MULTICAST_LOOP, &c, &a);
* tcs_opt_get(socket, TCS_SOL_IP, TCS_IP_MULTICAST_LOOP, &c, &a);
* @endcode
*
* @param socket_ctx is your in-out socket context.
Expand Down Expand Up @@ -2429,9 +2425,6 @@ const TcsSockType TCS_SOCK_STREAM = {SOCK_STREAM};
const TcsSockType TCS_SOCK_DGRAM = {SOCK_DGRAM};
const TcsSockType TCS_SOCK_RAW = {SOCK_RAW};

// Flags
const uint32_t TCS_AI_PASSIVE = AI_PASSIVE;

// Recv flags
const uint32_t TCS_MSG_PEEK = MSG_PEEK;
const uint32_t TCS_MSG_OOB = MSG_OOB;
Expand Down Expand Up @@ -2469,17 +2462,17 @@ const int TCS_SO_PRIORITY = -1;
#endif

// IP options
const int TCS_SO_IP_NODELAY = TCP_NODELAY;
const int TCS_SO_IP_MEMBERSHIP_ADD = IP_ADD_MEMBERSHIP;
const int TCS_SO_IP_MEMBERSHIP_DROP = IP_DROP_MEMBERSHIP;
const int TCS_SO_IP_MULTICAST_LOOP = IP_MULTICAST_LOOP;
const int TCS_TCP_NODELAY = TCP_NODELAY;
const int TCS_IP_MEMBERSHIP_ADD = IP_ADD_MEMBERSHIP;
const int TCS_IP_MEMBERSHIP_DROP = IP_DROP_MEMBERSHIP;
const int TCS_IP_MULTICAST_LOOP = IP_MULTICAST_LOOP;

#if TCS_HAS_AF_PACKET
const int TCS_SO_PACKET_MEMBERSHIP_ADD = PACKET_ADD_MEMBERSHIP;
const int TCS_SO_PACKET_MEMBERSHIP_DROP = PACKET_DROP_MEMBERSHIP;
const int TCS_PACKET_MEMBERSHIP_ADD = PACKET_ADD_MEMBERSHIP;
const int TCS_PACKET_MEMBERSHIP_DROP = PACKET_DROP_MEMBERSHIP;
#else
const int TCS_SO_PACKET_MEMBERSHIP_ADD = -1;
const int TCS_SO_PACKET_MEMBERSHIP_DROP = -1;
const int TCS_PACKET_MEMBERSHIP_ADD = -1;
const int TCS_PACKET_MEMBERSHIP_DROP = -1;
#endif

// Default flags
Expand Down Expand Up @@ -2795,7 +2788,7 @@ TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct T
}
}

TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction)
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction)
{
const int LUT[] = {SHUT_RD, SHUT_WR, SHUT_RDWR};

Expand Down Expand Up @@ -4301,9 +4294,6 @@ const TcsSockType TCS_SOCK_STREAM = {SOCK_STREAM};
const TcsSockType TCS_SOCK_DGRAM = {SOCK_DGRAM};
const TcsSockType TCS_SOCK_RAW = {SOCK_RAW};

// Flags
const uint32_t TCS_AI_PASSIVE = AI_PASSIVE;

// Recv flags
const uint32_t TCS_MSG_PEEK = MSG_PEEK;
const uint32_t TCS_MSG_OOB = MSG_OOB;
Expand Down Expand Up @@ -4333,10 +4323,10 @@ const int TCS_SO_OOBINLINE = SO_OOBINLINE;
const int TCS_SO_PRIORITY = -1;

// IP options
const int TCS_SO_IP_NODELAY = TCP_NODELAY;
const int TCS_SO_IP_MEMBERSHIP_ADD = IP_ADD_MEMBERSHIP;
const int TCS_SO_IP_MEMBERSHIP_DROP = IP_DROP_MEMBERSHIP;
const int TCS_SO_IP_MULTICAST_LOOP = IP_MULTICAST_LOOP;
const int TCS_TCP_NODELAY = TCP_NODELAY;
const int TCS_IP_MEMBERSHIP_ADD = IP_ADD_MEMBERSHIP;
const int TCS_IP_MEMBERSHIP_DROP = IP_DROP_MEMBERSHIP;
const int TCS_IP_MULTICAST_LOOP = IP_MULTICAST_LOOP;

// ######## Internal Helpers ########

Expand Down Expand Up @@ -4616,7 +4606,7 @@ TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct T
}
}

TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction)
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction)
{
const int LUT[] = {SD_RECEIVE, SD_SEND, SD_BOTH};

Expand Down Expand Up @@ -5490,7 +5480,7 @@ TcsResult tcs_opt_membership_add_to(TcsSocket socket_ctx,
imr.imr_multiaddr.s_addr = htonl(multicast_address->data.ip4.address);
if (local_address != NULL)
imr.imr_interface.s_addr = htonl(local_address->data.ip4.address);
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_SO_IP_MEMBERSHIP_ADD, &imr, sizeof(imr));
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_IP_MEMBERSHIP_ADD, &imr, sizeof(imr));
}
else if (multicast_address->family.native == TCS_FAMILY_IP6.native)
{
Expand Down Expand Up @@ -5547,7 +5537,7 @@ TcsResult tcs_opt_membership_drop_from(TcsSocket socket_ctx,
imr.imr_multiaddr.s_addr = htonl(multicast_address->data.ip4.address);
if (local_address != NULL)
imr.imr_interface.s_addr = htonl(local_address->data.ip4.address);
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_SO_IP_MEMBERSHIP_DROP, &imr, sizeof(imr));
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_IP_MEMBERSHIP_DROP, &imr, sizeof(imr));
}
else if (multicast_address->family.native == TCS_FAMILY_IP6.native)
{
Expand Down Expand Up @@ -6764,7 +6754,7 @@ TcsResult tcs_opt_ip_no_delay_set(TcsSocket socket_ctx, bool use_no_delay)
return TCS_ERROR_INVALID_ARGUMENT;

int b = use_no_delay ? 1 : 0;
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_SO_IP_NODELAY, &b, sizeof(b));
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_TCP_NODELAY, &b, sizeof(b));
}

TcsResult tcs_opt_ip_no_delay_get(TcsSocket socket_ctx, bool* is_no_delay_used)
Expand All @@ -6773,7 +6763,7 @@ TcsResult tcs_opt_ip_no_delay_get(TcsSocket socket_ctx, bool* is_no_delay_used)
return TCS_ERROR_INVALID_ARGUMENT;
int b = 0;
size_t s = sizeof(b);
TcsResult sts = tcs_opt_get(socket_ctx, TCS_SOL_IP, TCS_SO_IP_NODELAY, &b, &s);
TcsResult sts = tcs_opt_get(socket_ctx, TCS_SOL_IP, TCS_TCP_NODELAY, &b, &s);
*is_no_delay_used = b;
return sts;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tinycsocket_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ TcsResult tcs_opt_ip_no_delay_set(TcsSocket socket_ctx, bool use_no_delay)
return TCS_ERROR_INVALID_ARGUMENT;

int b = use_no_delay ? 1 : 0;
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_SO_IP_NODELAY, &b, sizeof(b));
return tcs_opt_set(socket_ctx, TCS_SOL_IP, TCS_TCP_NODELAY, &b, sizeof(b));
}

TcsResult tcs_opt_ip_no_delay_get(TcsSocket socket_ctx, bool* is_no_delay_used)
Expand All @@ -765,7 +765,7 @@ TcsResult tcs_opt_ip_no_delay_get(TcsSocket socket_ctx, bool* is_no_delay_used)
return TCS_ERROR_INVALID_ARGUMENT;
int b = 0;
size_t s = sizeof(b);
TcsResult sts = tcs_opt_get(socket_ctx, TCS_SOL_IP, TCS_SO_IP_NODELAY, &b, &s);
TcsResult sts = tcs_opt_get(socket_ctx, TCS_SOL_IP, TCS_TCP_NODELAY, &b, &s);
*is_no_delay_used = b;
return sts;
}
Expand Down
42 changes: 19 additions & 23 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.75";
static const char* const TCS_VERSION_TXT = "v0.3.76";
static const char* const TCS_LICENSE_TXT =
"Copyright 2018 Markus Lindelöw\n"
"\n"
Expand Down Expand Up @@ -71,7 +71,7 @@ static const char* const TCS_LICENSE_TXT =
* - TcsResult tcs_connect_str(TcsSocket socket_ctx, const char* remote_address, uint16_t port);
* - TcsResult tcs_listen(TcsSocket socket_ctx, int backlog);
* - TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct TcsAddress* address);
* - TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction);
* - TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction);
*
* Data Transfer:
* - TcsResult tcs_send(TcsSocket socket_ctx, const uint8_t* buffer, size_t buffer_size, uint32_t flags, size_t* bytes_sent);
Expand Down Expand Up @@ -268,8 +268,9 @@ struct TcsAddress
{
TcsInterfaceId
interface_id; /**< Local interface index, use tcs_interface_list() to find valid interfaces. Native type. */
uint16_t protocol; /**< Host byte order. E.i. TCS_ETH_P_ALL, 0 (block all until bind), ETH_P_TSN etc. */
uint8_t mac[6]; /**< Typical destination mac address or local mac address when joining groups */
uint16_t
protocol; /**< Host byte order. E.g. TCS_PROTOCOL_ETH_ALL, 0 (block all until bind), ETH_P_TSN etc. */
uint8_t mac[6]; /**< Typical destination mac address or local mac address when joining groups */
} packet;
} data;
};
Expand Down Expand Up @@ -352,12 +353,7 @@ typedef uint16_t TcsProtocol;

static const TcsProtocol TCS_PROTOCOL_IP_TCP = 6; /**< TCP, IANA-assigned (RFC 9293). Use with TCS_SOCK_STREAM. */
static const TcsProtocol TCS_PROTOCOL_IP_UDP = 17; /**< UDP, IANA-assigned (RFC 768). Use with TCS_SOCK_DGRAM. */

// Ethernet protocols (host byte order)
static const TcsProtocol TCS_ETH_P_ALL = 0x0003; /**< Receive all protocols. Use with TCS_FAMILY_PACKET for capture. */

// Flags
extern const uint32_t TCS_AI_PASSIVE; /**< Use this flag for pure listening sockets */
static const TcsProtocol TCS_PROTOCOL_ETH_ALL = 3; /**< Receive all protocols. Use with TCS_FAMILY_PACKET. */

// Recv flags
extern const uint32_t TCS_MSG_PEEK;
Expand All @@ -373,10 +369,10 @@ extern const int TCS_BACKLOG_MAX; /**< Max number of queued sockets when listeni
// Socket Direction
typedef enum
{
TCS_SD_RECEIVE, /**< To shutdown incoming packets for socket */
TCS_SD_SEND, /**< To shutdown outgoing packets for socket */
TCS_SD_BOTH, /**< To shutdown both incoming and outgoing packets for socket */
} TcsSocketDirection;
TCS_SHUTDOWN_RECEIVE, /**< To shutdown incoming packets for socket */
TCS_SHUTDOWN_SEND, /**< To shutdown outgoing packets for socket */
TCS_SHUTDOWN_BOTH, /**< To shutdown both incoming and outgoing packets for socket */
} TcsShutdownDirection;

// Option levels
extern const int TCS_SOL_SOCKET; /**< Socket option level for socket options */
Expand All @@ -391,19 +387,19 @@ extern const int TCS_SO_REUSEADDR;
extern const int TCS_SO_REUSEPORT;
extern const int TCS_SO_RCVBUF; /**< Byte size of receiving buffer */
extern const int TCS_SO_RCVTIMEO;
extern const int TCS_SO_SNDBUF; /**< Byte size of receiving buffer */
extern const int TCS_SO_SNDBUF; /**< Byte size of sending buffer */
extern const int TCS_SO_OOBINLINE;
extern const int TCS_SO_PRIORITY;

// IP options
extern const int TCS_SO_IP_NODELAY;
extern const int TCS_SO_IP_MEMBERSHIP_ADD;
extern const int TCS_SO_IP_MEMBERSHIP_DROP;
extern const int TCS_SO_IP_MULTICAST_LOOP;
extern const int TCS_TCP_NODELAY;
extern const int TCS_IP_MEMBERSHIP_ADD;
extern const int TCS_IP_MEMBERSHIP_DROP;
extern const int TCS_IP_MULTICAST_LOOP;

// Packet options
extern const int TCS_SO_PACKET_MEMBERSHIP_ADD;
extern const int TCS_SO_PACKET_MEMBERSHIP_DROP;
extern const int TCS_PACKET_MEMBERSHIP_ADD;
extern const int TCS_PACKET_MEMBERSHIP_DROP;

// Use for timeout to wait until infinity happens
extern const int TCS_WAIT_INF;
Expand Down Expand Up @@ -1108,7 +1104,7 @@ TcsResult tcs_accept(TcsSocket socket_ctx, TcsSocket* out_child_socket, struct T
* @param direction defines in which direction you want to turn off the communication.
* @return #TCS_SUCCESS if successful, otherwise the error code.
*/
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsSocketDirection direction);
TcsResult tcs_shutdown(TcsSocket socket_ctx, TcsShutdownDirection direction);

/**
* @brief Sends data on a socket, blocking
Expand Down Expand Up @@ -1394,7 +1390,7 @@ TcsResult tcs_opt_set(TcsSocket socket_ctx,
* @code
* uint8_t c;
* size_t a = sizeof(c);
* tcs_opt_get(socket, TCS_SOL_IP, TCS_SO_IP_MULTICAST_LOOP, &c, &a);
* tcs_opt_get(socket, TCS_SOL_IP, TCS_IP_MULTICAST_LOOP, &c, &a);
* @endcode
*
* @param socket_ctx is your in-out socket context.
Expand Down
Loading
Loading