usock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * usock - socket helper functions
  3. *
  4. * Copyright (C) 2010 Steven Barth <steven@midlink.org>
  5. * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
  6. *
  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.
  10. *
  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.
  18. */
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <sys/un.h>
  22. #include <netdb.h>
  23. #include <poll.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <poll.h>
  32. #include "usock.h"
  33. #include "utils.h"
  34. static void usock_set_flags(int sock, unsigned int type)
  35. {
  36. if (!(type & USOCK_NOCLOEXEC))
  37. fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
  38. if (type & USOCK_NONBLOCK)
  39. fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
  40. }
  41. static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
  42. {
  43. int sock;
  44. sock = socket(family, socktype, 0);
  45. if (sock < 0)
  46. return -1;
  47. usock_set_flags(sock, type);
  48. if (server) {
  49. const int one = 1;
  50. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  51. if (!bind(sock, sa, sa_len) &&
  52. (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
  53. return sock;
  54. } else {
  55. if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
  56. return sock;
  57. }
  58. close(sock);
  59. return -1;
  60. }
  61. static int usock_unix(int type, const char *host)
  62. {
  63. struct sockaddr_un sun = {.sun_family = AF_UNIX};
  64. bool server = !!(type & USOCK_SERVER);
  65. int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
  66. if (strlen(host) >= sizeof(sun.sun_path)) {
  67. errno = EINVAL;
  68. return -1;
  69. }
  70. strcpy(sun.sun_path, host);
  71. return usock_connect(type, (struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
  72. }
  73. static int
  74. usock_inet_notimeout(int type, struct addrinfo *result, void *addr)
  75. {
  76. struct addrinfo *rp;
  77. int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
  78. bool server = !!(type & USOCK_SERVER);
  79. int sock;
  80. for (rp = result; rp != NULL; rp = rp->ai_next) {
  81. sock = usock_connect(type, rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
  82. if (sock >= 0) {
  83. if (addr)
  84. memcpy(addr, rp->ai_addr, rp->ai_addrlen);
  85. return sock;
  86. }
  87. }
  88. return -1;
  89. }
  90. static int poll_restart(struct pollfd *fds, int nfds, int timeout)
  91. {
  92. struct timespec ts, cur;
  93. int msec = timeout % 1000;
  94. int ret;
  95. clock_gettime(CLOCK_MONOTONIC, &ts);
  96. ts.tv_nsec += msec * 1000000;
  97. if (ts.tv_nsec > 1000000000) {
  98. ts.tv_sec++;
  99. ts.tv_nsec -= 1000000000;
  100. }
  101. ts.tv_sec += timeout / 1000;
  102. while (1) {
  103. ret = poll(fds, nfds, timeout);
  104. if (ret >= 0 || (errno != EINTR && errno != EAGAIN))
  105. return ret;
  106. clock_gettime(CLOCK_MONOTONIC, &cur);
  107. timeout = (ts.tv_sec - cur.tv_sec) * 1000;
  108. timeout += (ts.tv_nsec - cur.tv_nsec) / 1000000;
  109. if (timeout <= 0)
  110. return 0;
  111. }
  112. }
  113. int usock_inet_timeout(int type, const char *host, const char *service,
  114. void *addr, int timeout)
  115. {
  116. int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
  117. bool server = !!(type & USOCK_SERVER);
  118. struct addrinfo *result, *rp;
  119. struct addrinfo hints = {
  120. .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
  121. (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
  122. .ai_socktype = socktype,
  123. .ai_flags = AI_ADDRCONFIG
  124. | ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
  125. | ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
  126. };
  127. struct addrinfo *rp_v6 = NULL;
  128. struct addrinfo *rp_v4 = NULL;
  129. struct pollfd pfds[2] = {
  130. { .fd = -1, .events = POLLOUT },
  131. { .fd = -1, .events = POLLOUT },
  132. };
  133. int sock = -1;
  134. int i;
  135. if (getaddrinfo(host, service, &hints, &result))
  136. return -1;
  137. if (timeout <= 0 || server) {
  138. sock = usock_inet_notimeout(type, result, addr);
  139. goto free_addrinfo;
  140. }
  141. for (rp = result; rp != NULL; rp = rp->ai_next) {
  142. if (rp->ai_family == AF_INET6 && !rp_v6)
  143. rp_v6 = rp;
  144. if (rp->ai_family == AF_INET && !rp_v4)
  145. rp_v4 = rp;
  146. }
  147. if (!rp_v6 && !rp_v4)
  148. goto out;
  149. if (rp_v6) {
  150. rp = rp_v6;
  151. pfds[0].fd = usock_connect(type | USOCK_NONBLOCK, rp->ai_addr,
  152. rp->ai_addrlen, rp->ai_family,
  153. socktype, server);
  154. if (pfds[0].fd < 0) {
  155. rp_v6 = NULL;
  156. goto try_v4;
  157. }
  158. if (timeout > 300) {
  159. if (poll_restart(pfds, 1, 300) == 1) {
  160. rp = rp_v6;
  161. sock = pfds[0].fd;
  162. goto out;
  163. }
  164. }
  165. timeout -= 300;
  166. }
  167. try_v4:
  168. if (rp_v4) {
  169. rp = rp_v4;
  170. pfds[1].fd = usock_connect(type | USOCK_NONBLOCK, rp->ai_addr,
  171. rp->ai_addrlen, rp->ai_family,
  172. socktype, server);
  173. if (pfds[1].fd < 0) {
  174. rp_v4 = NULL;
  175. if (!rp_v6)
  176. goto out;
  177. goto wait;
  178. }
  179. }
  180. wait:
  181. poll_restart(pfds + !rp_v6, !!rp_v6 + !!rp_v4, timeout);
  182. if (pfds[0].revents & POLLOUT) {
  183. rp = rp_v6;
  184. sock = pfds[0].fd;
  185. goto out;
  186. }
  187. if (pfds[1].revents & POLLOUT) {
  188. rp = rp_v4;
  189. sock = pfds[1].fd;
  190. goto out;
  191. }
  192. out:
  193. for (i = 0; i < 2; i++) {
  194. int fd = pfds[i].fd;
  195. if (fd >= 0 && fd != sock)
  196. close(fd);
  197. }
  198. if (!(type & USOCK_NONBLOCK))
  199. fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) & ~O_NONBLOCK);
  200. if (addr && sock >= 0)
  201. memcpy(addr, rp->ai_addr, rp->ai_addrlen);
  202. free_addrinfo:
  203. freeaddrinfo(result);
  204. return sock;
  205. }
  206. const char *usock_port(int port)
  207. {
  208. static char buffer[sizeof("65535\0")];
  209. if (port < 0 || port > 65535)
  210. return NULL;
  211. snprintf(buffer, sizeof(buffer), "%u", port);
  212. return buffer;
  213. }
  214. int usock(int type, const char *host, const char *service) {
  215. int sock;
  216. if (type & USOCK_UNIX)
  217. sock = usock_unix(type, host);
  218. else
  219. sock = usock_inet(type, host, service, NULL);
  220. if (sock < 0)
  221. return -1;
  222. return sock;
  223. }
  224. int usock_wait_ready(int fd, int msecs) {
  225. struct pollfd fds[1];
  226. int res;
  227. fds[0].fd = fd;
  228. fds[0].events = POLLOUT;
  229. res = poll(fds, 1, msecs);
  230. if (res < 0) {
  231. return errno;
  232. } else if (res == 0) {
  233. return -ETIMEDOUT;
  234. } else {
  235. int err = 0;
  236. socklen_t optlen = sizeof(err);
  237. res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen);
  238. if (res)
  239. return errno;
  240. if (err)
  241. return err;
  242. }
  243. return 0;
  244. }