15 #include <sys/types.h>
16 #ifdef HAVE_SYS_SOCKET_H
17 # include <sys/socket.h>
18 # define OPTVAL_T(t) (t)
19 # define OPTVAL_GT(t) (t)
21 #ifdef HAVE_SYS_IOCTL_H
22 #include <sys/ioctl.h>
24 #ifdef HAVE_WS2TCPIP_H
26 # define OPTVAL_T(t) (const char*)(t)
27 # define OPTVAL_GT(t) (char*)(t)
29 # define CMSG_DATA WSA_CMSG_DATA
34 return !COAP_DISABLE_TCP;
56 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
60 "coap_socket_connect_tcp1: socket: %s\n",
72 "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
77 switch (server->
addr.
sa.sa_family) {
79 if (connect_addr.
addr.
sin.sin_port == 0)
80 connect_addr.
addr.
sin.sin_port = htons(default_port);
83 if (connect_addr.
addr.
sin6.sin6_port == 0)
84 connect_addr.
addr.
sin6.sin6_port = htons(default_port);
87 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
89 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
98 if (local_if && local_if->
addr.
sa.sa_family) {
100 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
102 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
104 if (bind(sock->
fd, &local_if->
addr.
sa,
105 local_if->
addr.
sa.sa_family == AF_INET ?
106 (socklen_t)
sizeof(
struct sockaddr_in) :
113 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
118 if (WSAGetLastError() == WSAEWOULDBLOCK) {
120 if (errno == EINPROGRESS) {
159 int optlen = (int)
sizeof( error );
161 socklen_t optlen = (socklen_t)
sizeof( error );
166 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
174 "coap_socket_finish_connect_tcp: connect failed: %s\n",
205 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
223 if (setsockopt (sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
226 "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
229 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
232 "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
235 switch (listen_addr->
addr.
sa.sa_family) {
241 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
243 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
251 if (bind(sock->
fd, &listen_addr->
addr.
sa,
252 listen_addr->
addr.
sa.sa_family == AF_INET ?
253 (socklen_t)
sizeof(
struct sockaddr_in) :
260 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
261 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
295 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
303 if (getsockname( new_client->
fd, &local_addr->
addr.
sa, &local_addr->
size) < 0)
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Pulls together all the internal only header files.
void coap_socket_close(coap_socket_t *sock)
const char * coap_socket_strerror(void)
const char * coap_socket_format_errno(int error)
#define COAP_SOCKET_ERROR
#define COAP_INVALID_SOCKET
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
#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(level,...)
Logging function.
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