21 return !COAP_DISABLE_TCP;
24#if !COAP_DISABLE_TCP && !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
26#ifdef HAVE_SYS_SOCKET_H
27# include <sys/socket.h>
28# define OPTVAL_T(t) (t)
29# define OPTVAL_GT(t) (t)
31#ifdef HAVE_SYS_IOCTL_H
36# define OPTVAL_T(t) (const char*)(t)
37# define OPTVAL_GT(t) (char*)(t)
39# define CMSG_DATA WSA_CMSG_DATA
59 sock->
flags &= ~COAP_SOCKET_CONNECTED;
60 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
73 coap_log_warn(
"coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
77 switch (server->
addr.
sa.sa_family) {
80 if (connect_addr.
addr.
sin.sin_port == 0)
81 connect_addr.
addr.
sin.sin_port = htons(default_port);
86 if (connect_addr.
addr.
sin6.sin6_port == 0)
87 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
89 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
91 coap_log_warn(
"coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
95#if COAP_AF_UNIX_SUPPORT
100 coap_log_alert(
"coap_socket_connect_tcp1: unsupported sa_family\n");
104 if (local_if && local_if->
addr.
sa.sa_family) {
106 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
107 coap_log_warn(
"coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
109 if (bind(sock->
fd, &local_if->
addr.
sa,
111 local_if->
addr.
sa.sa_family == AF_INET ?
112 (socklen_t)
sizeof(
struct sockaddr_in) :
120 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
125 if (WSAGetLastError() == WSAEWOULDBLOCK) {
127 if (errno == EINPROGRESS) {
166 int optlen = (int)
sizeof(error);
168 socklen_t optlen = (socklen_t)
sizeof(error);
173 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
180 coap_log_warn(
"coap_socket_connect_tcp2: connect failed: %s\n",
211 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
227 if (setsockopt(sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
229 coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
232 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
234 coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
237 switch (listen_addr->
addr.
sa.sa_family) {
245 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
247 coap_log_alert(
"coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
251#if COAP_AF_UNIX_SUPPORT
259 if (bind(sock->
fd, &listen_addr->
addr.
sa,
261 listen_addr->
addr.
sa.sa_family == AF_INET ?
262 (socklen_t)
sizeof(
struct sockaddr_in) :
270 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
271 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
303 server->
flags &= ~COAP_SOCKET_CAN_ACCEPT;
305 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
313 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)
#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
Library specific build wrapper for coap_internal.h.
#define coap_log_alert(...)
#define coap_log_warn(...)
int coap_tcp_is_supported(void)
Check whether TCP is available.
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, void *extra)
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