2 * usock - socket helper functions
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
5 * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
34 static void usock_set_flags(int sock
, unsigned int type
)
36 if (!(type
& USOCK_NOCLOEXEC
))
37 fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
);
39 if (type
& USOCK_NONBLOCK
)
40 fcntl(sock
, F_SETFL
, fcntl(sock
, F_GETFL
) | O_NONBLOCK
);
43 static int usock_connect(int type
, struct sockaddr
*sa
, int sa_len
, int family
, int socktype
, bool server
)
47 sock
= socket(family
, socktype
, 0);
51 usock_set_flags(sock
, type
);
55 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
57 if (!bind(sock
, sa
, sa_len
) &&
58 (socktype
!= SOCK_STREAM
|| !listen(sock
, SOMAXCONN
)))
61 if (!connect(sock
, sa
, sa_len
) || errno
== EINPROGRESS
)
69 static int usock_unix(int type
, const char *host
)
71 struct sockaddr_un sun
= {.sun_family
= AF_UNIX
};
72 bool server
= !!(type
& USOCK_SERVER
);
73 int socktype
= ((type
& 0xff) == USOCK_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
75 if (strlen(host
) >= sizeof(sun
.sun_path
)) {
79 strcpy(sun
.sun_path
, host
);
81 return usock_connect(type
, (struct sockaddr
*)&sun
, sizeof(sun
), AF_UNIX
, socktype
, server
);
84 int usock_inet(int type
, const char *host
, const char *service
, void *addr
)
86 int socktype
= ((type
& 0xff) == USOCK_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
87 bool server
= !!(type
& USOCK_SERVER
);
88 struct addrinfo
*result
, *rp
;
89 struct addrinfo hints
= {
90 .ai_family
= (type
& USOCK_IPV6ONLY
) ? AF_INET6
:
91 (type
& USOCK_IPV4ONLY
) ? AF_INET
: AF_UNSPEC
,
92 .ai_socktype
= socktype
,
93 .ai_flags
= AI_ADDRCONFIG
94 | ((type
& USOCK_SERVER
) ? AI_PASSIVE
: 0)
95 | ((type
& USOCK_NUMERIC
) ? AI_NUMERICHOST
: 0),
99 if (getaddrinfo(host
, service
, &hints
, &result
))
102 for (rp
= result
; rp
!= NULL
; rp
= rp
->ai_next
) {
103 sock
= usock_connect(type
, rp
->ai_addr
, rp
->ai_addrlen
, rp
->ai_family
, socktype
, server
);
106 memcpy(addr
, rp
->ai_addr
, rp
->ai_addrlen
);
111 freeaddrinfo(result
);
115 const char *usock_port(int port
)
117 static char buffer
[sizeof("65535\0")];
119 if (port
< 0 || port
> 65535)
122 snprintf(buffer
, sizeof(buffer
), "%u", port
);
127 int usock(int type
, const char *host
, const char *service
) {
130 if (type
& USOCK_UNIX
)
131 sock
= usock_unix(type
, host
);
133 sock
= usock_inet(type
, host
, service
, NULL
);
141 int usock_wait_ready(int fd
, int msecs
) {
142 struct pollfd fds
[1];
146 fds
[0].events
= POLLOUT
;
148 res
= poll(fds
, 1, msecs
);
151 } else if (res
== 0) {
155 socklen_t optlen
= sizeof(err
);
157 res
= getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &err
, &optlen
);