20#ifdef HAVE_SYS_SOCKET_H
21# include <sys/socket.h>
22# define OPTVAL_T(t) (t)
23# define OPTVAL_GT(t) (t)
25#ifdef HAVE_SYS_IOCTL_H
30# define OPTVAL_T(t) (const char*)(t)
31# define OPTVAL_GT(t) (char*)(t)
33# define CMSG_DATA WSA_CMSG_DATA
38 return !COAP_DISABLE_TCP;
50#if !defined(RIOT_VERSION) && COAP_IPV6_SUPPORT
59 sock->
flags &= ~COAP_SOCKET_CONNECTED;
60 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
74 coap_log_warn(
"coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
79 switch (server->
addr.
sa.sa_family) {
82 if (connect_addr.
addr.
sin.sin_port == 0)
83 connect_addr.
addr.
sin.sin_port = htons(default_port);
88 if (connect_addr.
addr.
sin6.sin6_port == 0)
89 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
92 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
94 coap_log_warn(
"coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
99#if COAP_AF_UNIX_SUPPORT
104 coap_log_alert(
"coap_socket_connect_tcp1: unsupported sa_family\n");
108 if (local_if && local_if->
addr.
sa.sa_family) {
110 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
111 coap_log_warn(
"coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
113 if (bind(sock->
fd, &local_if->
addr.
sa,
115 local_if->
addr.
sa.sa_family == AF_INET ?
116 (socklen_t)
sizeof(
struct sockaddr_in) :
124 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
129 if (WSAGetLastError() == WSAEWOULDBLOCK) {
131 if (errno == EINPROGRESS) {
170 int optlen = (int)
sizeof(error);
172 socklen_t optlen = (socklen_t)
sizeof(error);
177 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
179 coap_log_warn(
"coap_socket_finish_connect_tcp: getsockopt: %s\n",
184 coap_log_warn(
"coap_socket_finish_connect_tcp: connect failed: %s\n",
208#if !defined(RIOT_VERSION) && COAP_IPV6_SUPPORT
215 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
233 if (setsockopt(sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
235 coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
238 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
240 coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
243 switch (listen_addr->
addr.
sa.sa_family) {
252 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
254 coap_log_alert(
"coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
259#if COAP_AF_UNIX_SUPPORT
267 if (bind(sock->
fd, &listen_addr->
addr.
sa,
269 listen_addr->
addr.
sa.sa_family == AF_INET ?
270 (socklen_t)
sizeof(
struct sockaddr_in) :
278 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
279 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
311 server->
flags &= ~COAP_SOCKET_CAN_ACCEPT;
313 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
321 if (getsockname(new_client->
fd, &local_addr->
addr.
sa, &local_addr->
size) < 0)
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Pulls together all the internal only header files.
#define COAP_IPV4_SUPPORT
const char * coap_socket_format_errno(int error)
void coap_socket_close(coap_socket_t *sock)
Function interface to close off a socket.
const char * coap_socket_strerror(void)
#define COAP_SOCKET_ERROR
#define COAP_INVALID_SOCKET
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#define COAP_SOCKET_CONNECTED
the socket is connected
int coap_tcp_is_supported(void)
Check whether TCP is available.
#define coap_log_alert(...)
#define coap_log_warn(...)
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Create a new TCP socket and then listen for new incoming TCP sessions.
int coap_socket_connect_tcp1(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new TCP socket and initiate the connection.
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
Accept a new incoming TCP session.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Complete the TCP Connection.
Multi-purpose address abstraction.
socklen_t size
size of addr
union coap_address_t::@0 addr
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values