net.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/types.h>
  25. #include <sys/uio.h>
  26. #include <sys/socket.h>
  27. #include <sys/un.h>
  28. #include <netinet/in.h>
  29. #include <netinet/in_systm.h>
  30. #include <netinet/ip.h>
  31. #include <arpa/inet.h>
  32. #include <errno.h>
  33. #include "babeld.h"
  34. #include "util.h"
  35. #include "net.h"
  36. int
  37. babel_socket(int port)
  38. {
  39. struct sockaddr_in6 sin6;
  40. int s, rc;
  41. int saved_errno;
  42. int one = 1, zero = 0;
  43. const int ds = 0xc0; /* CS6 - Network Control */
  44. s = socket(PF_INET6, SOCK_DGRAM, 0);
  45. if(s < 0)
  46. return -1;
  47. rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
  48. if(rc < 0)
  49. goto fail;
  50. rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  51. if(rc < 0)
  52. goto fail;
  53. rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  54. &zero, sizeof(zero));
  55. if(rc < 0)
  56. goto fail;
  57. rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
  58. &one, sizeof(one));
  59. if(rc < 0)
  60. goto fail;
  61. rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  62. &one, sizeof(one));
  63. if(rc < 0)
  64. goto fail;
  65. #ifdef IPV6_TCLASS
  66. rc = setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &ds, sizeof(ds));
  67. #else
  68. rc = -1;
  69. errno = ENOSYS;
  70. #endif
  71. if(rc < 0)
  72. perror("Couldn't set traffic class");
  73. rc = fcntl(s, F_GETFL, 0);
  74. if(rc < 0)
  75. goto fail;
  76. rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
  77. if(rc < 0)
  78. goto fail;
  79. rc = fcntl(s, F_GETFD, 0);
  80. if(rc < 0)
  81. goto fail;
  82. rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
  83. if(rc < 0)
  84. goto fail;
  85. memset(&sin6, 0, sizeof(sin6));
  86. sin6.sin6_family = AF_INET6;
  87. sin6.sin6_port = htons(port);
  88. rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
  89. if(rc < 0)
  90. goto fail;
  91. return s;
  92. fail:
  93. saved_errno = errno;
  94. close(s);
  95. errno = saved_errno;
  96. return -1;
  97. }
  98. int
  99. babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen)
  100. {
  101. struct iovec iovec;
  102. struct msghdr msg;
  103. int rc;
  104. memset(&msg, 0, sizeof(msg));
  105. iovec.iov_base = buf;
  106. iovec.iov_len = buflen;
  107. msg.msg_name = sin;
  108. msg.msg_namelen = slen;
  109. msg.msg_iov = &iovec;
  110. msg.msg_iovlen = 1;
  111. rc = recvmsg(s, &msg, 0);
  112. return rc;
  113. }
  114. int
  115. babel_send(int s,
  116. const void *buf1, int buflen1, const void *buf2, int buflen2,
  117. const struct sockaddr *sin, int slen)
  118. {
  119. struct iovec iovec[2];
  120. struct msghdr msg;
  121. int rc, count = 0;
  122. iovec[0].iov_base = (void*)buf1;
  123. iovec[0].iov_len = buflen1;
  124. iovec[1].iov_base = (void*)buf2;
  125. iovec[1].iov_len = buflen2;
  126. memset(&msg, 0, sizeof(msg));
  127. msg.msg_name = (struct sockaddr*)sin;
  128. msg.msg_namelen = slen;
  129. msg.msg_iov = iovec;
  130. msg.msg_iovlen = 2;
  131. /* The Linux kernel can apparently keep returning EAGAIN indefinitely. */
  132. again:
  133. rc = sendmsg(s, &msg, 0);
  134. if(rc < 0) {
  135. if(errno == EINTR) {
  136. count++;
  137. if(count < 100)
  138. goto again;
  139. } else if(errno == EAGAIN) {
  140. int rc2;
  141. rc2 = wait_for_fd(1, s, 5);
  142. if(rc2 > 0) {
  143. count++;
  144. if(count < 100)
  145. goto again;
  146. }
  147. errno = EAGAIN;
  148. }
  149. }
  150. return rc;
  151. }
  152. int
  153. tcp_server_socket(int port, int local)
  154. {
  155. struct sockaddr_in6 sin6;
  156. int s, rc, saved_errno;
  157. int one = 1;
  158. s = socket(PF_INET6, SOCK_STREAM, 0);
  159. if(s < 0)
  160. return -1;
  161. rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  162. if(rc < 0)
  163. goto fail;
  164. rc = fcntl(s, F_GETFL, 0);
  165. if(rc < 0)
  166. goto fail;
  167. rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
  168. if(rc < 0)
  169. goto fail;
  170. rc = fcntl(s, F_GETFD, 0);
  171. if(rc < 0)
  172. goto fail;
  173. rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
  174. if(rc < 0)
  175. goto fail;
  176. memset(&sin6, 0, sizeof(sin6));
  177. sin6.sin6_family = AF_INET6;
  178. sin6.sin6_port = htons(port);
  179. if(local) {
  180. rc = inet_pton(AF_INET6, "::1", &sin6.sin6_addr);
  181. if(rc < 0)
  182. goto fail;
  183. }
  184. rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
  185. if(rc < 0)
  186. goto fail;
  187. rc = listen(s, 2);
  188. if(rc < 0)
  189. goto fail;
  190. return s;
  191. fail:
  192. saved_errno = errno;
  193. close(s);
  194. errno = saved_errno;
  195. return -1;
  196. }
  197. int
  198. unix_server_socket(const char *path)
  199. {
  200. struct sockaddr_un sun;
  201. int s, rc, saved_errno;
  202. if(strlen(path) >= sizeof(sun.sun_path))
  203. return -1;
  204. s = socket(PF_UNIX, SOCK_STREAM, 0);
  205. if(s < 0)
  206. return -1;
  207. rc = fcntl(s, F_GETFL, 0);
  208. if(rc < 0)
  209. goto fail;
  210. rc = fcntl(s, F_SETFL, rc | O_NONBLOCK);
  211. if(rc < 0)
  212. goto fail;
  213. rc = fcntl(s, F_GETFD, 0);
  214. if(rc < 0)
  215. goto fail;
  216. rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
  217. if(rc < 0)
  218. goto fail;
  219. memset(&sun, 0, sizeof(sun));
  220. sun.sun_family = AF_UNIX;
  221. strncpy(sun.sun_path, path, sizeof(sun.sun_path));
  222. rc = bind(s, (struct sockaddr *)&sun, sizeof(sun));
  223. if(rc < 0)
  224. goto fail;
  225. rc = listen(s, 2);
  226. if(rc < 0)
  227. goto fail_unlink;
  228. return s;
  229. fail_unlink:
  230. saved_errno = errno;
  231. unlink(path);
  232. errno = saved_errno;
  233. fail:
  234. saved_errno = errno;
  235. close(s);
  236. errno = saved_errno;
  237. return -1;
  238. }