21#ifdef HAVE_SYS_SOCKET_H
22# include <sys/socket.h>
23# define OPTVAL_T(t) (t)
24# define OPTVAL_GT(t) (t)
26#ifdef HAVE_SYS_IOCTL_H
27 #include <sys/ioctl.h>
31# define OPTVAL_T(t) (const char*)(t)
32# define OPTVAL_GT(t) (char*)(t)
34# define CMSG_DATA WSA_CMSG_DATA
39 return !COAP_DISABLE_TCP;
60 sock->
flags &= ~COAP_SOCKET_CONNECTED;
61 sock->
fd = socket(server->
addr.
sa.sa_family, SOCK_STREAM, 0);
65 "coap_socket_connect_tcp1: socket: %s\n",
77 "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
82 switch (server->
addr.
sa.sa_family) {
84 if (connect_addr.
addr.
sin.sin_port == 0)
85 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),
sizeof(off)) ==
COAP_SOCKET_ERROR)
94 "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
103 if (local_if && local_if->
addr.
sa.sa_family) {
105 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) ==
COAP_SOCKET_ERROR)
107 "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
109 if (bind(sock->
fd, &local_if->
addr.
sa,
110 local_if->
addr.
sa.sa_family == AF_INET ?
111 (socklen_t)
sizeof(
struct sockaddr_in) :
118 local_addr->
addr.
sa.sa_family = server->
addr.
sa.sa_family;
123 if (WSAGetLastError() == WSAEWOULDBLOCK) {
125 if (errno == EINPROGRESS) {
164 int optlen = (int)
sizeof( error );
166 socklen_t optlen = (socklen_t)
sizeof( error );
171 if (getsockopt(sock->
fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
179 "coap_socket_finish_connect_tcp: connect failed: %s\n",
210 sock->
fd = socket(listen_addr->
addr.
sa.sa_family, SOCK_STREAM, 0);
228 if (setsockopt (sock->
fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
231 "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
234 if (setsockopt(sock->
fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
237 "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
240 switch (listen_addr->
addr.
sa.sa_family) {
246 if (setsockopt(sock->
fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) ==
COAP_SOCKET_ERROR)
248 "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
256 if (bind(sock->
fd, &listen_addr->
addr.
sa,
257 listen_addr->
addr.
sa.sa_family == AF_INET ?
258 (socklen_t)
sizeof(
struct sockaddr_in) :
265 bound_addr->
size = (socklen_t)
sizeof(*bound_addr);
266 if (getsockname(sock->
fd, &bound_addr->
addr.
sa, &bound_addr->
size) < 0) {
298 server->
flags &= ~COAP_SOCKET_CAN_ACCEPT;
300 new_client->
fd = accept(server->
fd, &remote_addr->
addr.
sa,
308 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_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