You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

daemon.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "inet.h"
  21. #include "request.h"
  22. #include "if.h"
  23. #include "pkts.h"
  24. #include "bmap.h"
  25. #include "daemon.h"
  26. #include "netsukuku.h"
  27. #include "accept.h"
  28. extern int errno;
  29. /*
  30. * prepare_listen_socket:
  31. * It creates a new socket of the desired `family' and binds it to the
  32. * specified `port'. It sets also the reuseaddr and NONBLOCK
  33. * socket options, because this new socket shall be used to listen() and
  34. * accept().
  35. * If `dev' is not null, the socket will be binded to the device named
  36. * `dev'->dev_name with the SO_BINDTODEVICE socket option.
  37. * The created socket is returned.
  38. */
  39. int
  40. prepare_listen_socket(int family, int socktype, u_short port,
  41. interface * dev)
  42. {
  43. struct addrinfo hints, *ai, *aitop;
  44. char strport[NI_MAXSERV];
  45. int err, s;
  46. setzero(&hints, sizeof(struct addrinfo));
  47. hints.ai_family = family;
  48. hints.ai_socktype = socktype;
  49. hints.ai_flags = AI_PASSIVE;
  50. snprintf(strport, NI_MAXSERV, "%u", port);
  51. err = getaddrinfo(NULL, strport, &hints, &aitop);
  52. if (err) {
  53. error("Getaddrinfo error: %s", gai_strerror(err));
  54. return -1;
  55. }
  56. for (ai = aitop; ai; ai = ai->ai_next) {
  57. if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
  58. continue;
  59. s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  60. if (s == -1)
  61. /* Maybe we can use another socket... */
  62. continue;
  63. /* Bind the created socket to the device named dev->dev_name */
  64. if (dev && (set_bindtodevice_sk(s, dev->dev_name) < 0)) {
  65. inet_close(&s);
  66. continue;
  67. }
  68. if (set_reuseaddr_sk(s) < 0) {
  69. inet_close(&s);
  70. continue;
  71. }
  72. /* Let's bind it! */
  73. if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
  74. error("Cannot bind the port %d: %s. Trying another "
  75. "socket...", port, strerror(errno));
  76. inet_close(&s);
  77. continue;
  78. }
  79. freeaddrinfo(aitop);
  80. return s;
  81. }
  82. error("Cannot open inbound socket on port %d: %s", port,
  83. strerror(errno));
  84. freeaddrinfo(aitop);
  85. return -1;
  86. }
  87. /*
  88. * sockets_all_ifs
  89. *
  90. * creates a socket for each interface which is in the `ifs' array.
  91. * The array has `ifs_n' members.
  92. * Each created socket is stored in the `dev_sk' array, which has `ifs_n'#
  93. * members.
  94. * The created socket will be bound to the relative interface.
  95. * In `max_sk_idx' is stored the index of the `dev_sk' array, which has the
  96. * biggest dev_sk.
  97. *
  98. * On error 0 is returned, otherwise the number of utilised interfaces is
  99. * returned.
  100. * If the error is fatal, a negative value is returned.
  101. */
  102. int
  103. sockets_all_ifs(int family, int socktype, u_short port,
  104. interface * ifs, int ifs_n, int *dev_sk, int *max_sk_idx)
  105. {
  106. int i, n, e = 0;
  107. *max_sk_idx = 0;
  108. for (i = 0, n = 0; i < ifs_n; i++) {
  109. dev_sk[i] = prepare_listen_socket(family, socktype, port, &ifs[i]);
  110. if (dev_sk[i] < 0) {
  111. error("Cannot create a socket on the %s interface! "
  112. "Ignoring it", ifs[i].dev_name);
  113. dev_sk[i] = 0;
  114. e++;
  115. continue;
  116. }
  117. if (dev_sk[i] >= dev_sk[*max_sk_idx])
  118. *max_sk_idx = i;
  119. n++;
  120. }
  121. if (e == ifs_n)
  122. return -1;
  123. return n;
  124. }
  125. /*
  126. * udp_exec_pkt: passes the received udp packet to pkt_exec().
  127. * `passed_argv' is a pointer to a udp_exec_pkt_argv struct
  128. */
  129. void *
  130. udp_exec_pkt(void *passed_argv)
  131. {
  132. struct udp_exec_pkt_argv argv;
  133. PACKET rpkt;
  134. const char *ntop;
  135. memcpy(&argv, passed_argv, sizeof(struct udp_exec_pkt_argv));
  136. memcpy(&rpkt, argv.recv_pkt, sizeof(PACKET));
  137. if (argv.flags & UDP_THREAD_FOR_EACH_PKT)
  138. pthread_mutex_unlock(&udp_exec_lock);
  139. /* Drop any packet we sent in broadcast */
  140. if (!memcmp(rpkt.from.data, me.cur_ip.data, MAX_IP_SZ)) {
  141. pkt_free(&rpkt, 0);
  142. return 0;
  143. }
  144. if (add_accept(rpkt.from, 1)) {
  145. ntop = inet_to_str(rpkt.from);
  146. debug(DBG_NORMAL, "ACPT: dropped UDP pkt from %s: "
  147. "Accept table full.", ntop);
  148. return 0;
  149. }
  150. pkt_exec(rpkt, argv.acpt_idx);
  151. pkt_free(&rpkt, 0);
  152. return 0;
  153. }
  154. /*
  155. * udp_daemon: Takes care of receiving udp packets.
  156. * `passed_argv' is a pointer to a udp_daemon_argv struct
  157. */
  158. void *
  159. udp_daemon(void *passed_argv)
  160. {
  161. struct udp_daemon_argv argv;
  162. struct udp_exec_pkt_argv exec_pkt_argv;
  163. interface *ifs;
  164. int max_sk_idx, dev_sk[me.cur_ifs_n];
  165. PACKET rpkt;
  166. fd_set fdset;
  167. int ret, i, err;
  168. u_short udp_port;
  169. pthread_t thread;
  170. pthread_attr_t t_attr;
  171. #ifdef DEBUG
  172. int select_errors = 0;
  173. #endif
  174. memcpy(&argv, passed_argv, sizeof(struct udp_daemon_argv));
  175. udp_port = argv.port;
  176. setzero(&exec_pkt_argv, sizeof(struct udp_exec_pkt_argv));
  177. if (argv.flags & UDP_THREAD_FOR_EACH_PKT) {
  178. pthread_attr_init(&t_attr);
  179. pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
  180. exec_pkt_argv.flags |= UDP_THREAD_FOR_EACH_PKT;
  181. }
  182. debug(DBG_SOFT, "Preparing the udp listening socket on port %d",
  183. udp_port);
  184. err = sockets_all_ifs(my_family, SOCK_DGRAM, udp_port, me.cur_ifs,
  185. me.cur_ifs_n, dev_sk, &max_sk_idx);
  186. if (!err)
  187. return NULL;
  188. else if (err < 0)
  189. fatal("Creation of the %s daemon aborted. "
  190. "Is there another ntkd running?", "udp");
  191. debug(DBG_NORMAL, "Udp daemon on port %d up & running", udp_port);
  192. pthread_mutex_unlock(&udp_daemon_lock);
  193. pthread_mutex_init(&udp_exec_lock, 0);
  194. for (;;) {
  195. FD_ZERO(&fdset);
  196. if (!me.cur_ifs_n) {
  197. /* All the devices have been removed while ntkd was
  198. * running, sleep well */
  199. sleep(1);
  200. continue;
  201. }
  202. for (i = 0; i < me.cur_ifs_n; i++)
  203. if (dev_sk[i])
  204. FD_SET(dev_sk[i], &fdset);
  205. ret = select(dev_sk[max_sk_idx] + 1, &fdset, NULL, NULL, NULL);
  206. if (sigterm_timestamp)
  207. /* NetsukukuD has been closed */
  208. break;
  209. if (ret < 0) {
  210. #ifdef DEBUG
  211. if (select_errors > 20)
  212. break;
  213. select_errors++;
  214. #endif
  215. error("daemon_udp: select error: %s", strerror(errno));
  216. continue;
  217. }
  218. for (i = 0; i < me.cur_ifs_n; i++) {
  219. ifs = &me.cur_ifs[i];
  220. if (!dev_sk[i])
  221. continue;
  222. if (!FD_ISSET(dev_sk[i], &fdset))
  223. continue;
  224. setzero(&rpkt, sizeof(PACKET));
  225. pkt_addsk(&rpkt, my_family, dev_sk[i], SKT_UDP);
  226. pkt_add_dev(&rpkt, ifs, 0);
  227. rpkt.flags = MSG_WAITALL;
  228. pkt_addport(&rpkt, udp_port);
  229. if (pkt_recv(&rpkt) < 0) {
  230. pkt_free(&rpkt, 0);
  231. continue;
  232. }
  233. exec_pkt_argv.acpt_idx = accept_idx;
  234. exec_pkt_argv.acpt_sidx = accept_sidx;
  235. if (argv.flags & UDP_THREAD_FOR_EACH_PKT) {
  236. exec_pkt_argv.recv_pkt = &rpkt;
  237. pthread_mutex_lock(&udp_exec_lock);
  238. pthread_create(&thread, &t_attr, udp_exec_pkt,
  239. &exec_pkt_argv);
  240. pthread_mutex_lock(&udp_exec_lock);
  241. pthread_mutex_unlock(&udp_exec_lock);
  242. } else {
  243. exec_pkt_argv.recv_pkt = &rpkt;
  244. udp_exec_pkt(&exec_pkt_argv);
  245. }
  246. }
  247. }
  248. destroy_accept_tbl();
  249. return NULL;
  250. }
  251. void *
  252. tcp_recv_loop(void *recv_pkt)
  253. {
  254. PACKET rpkt;
  255. int acpt_idx, acpt_sidx;
  256. acpt_idx = accept_idx;
  257. acpt_sidx = accept_sidx;
  258. memcpy(&rpkt, recv_pkt, sizeof(PACKET));
  259. pthread_mutex_unlock(&tcp_exec_lock);
  260. #if 0
  261. add_accept_pid(getpid(), acpt_idx, acpt_sidx);
  262. #endif
  263. while (pkt_recv(&rpkt) != -1) {
  264. if (pkt_exec(rpkt, acpt_idx) < 0) {
  265. goto close;
  266. break;
  267. } else
  268. pkt_free(&rpkt, 0);
  269. }
  270. close:
  271. pkt_free(&rpkt, 1);
  272. close_accept(acpt_idx, acpt_sidx);
  273. return NULL;
  274. }
  275. void *
  276. tcp_daemon(void *door)
  277. {
  278. pthread_t thread;
  279. pthread_attr_t t_attr;
  280. PACKET rpkt;
  281. struct sockaddr_storage addr;
  282. socklen_t addrlen = sizeof addr;
  283. inet_prefix ip;
  284. fd_set fdset;
  285. int fd, ret, err, i;
  286. interface *ifs;
  287. int max_sk_idx, dev_sk[me.cur_ifs_n];
  288. u_short tcp_port = *(u_short *) door;
  289. const char *ntop;
  290. pthread_attr_init(&t_attr);
  291. pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
  292. debug(DBG_SOFT, "Preparing the tcp listening socket on port %d",
  293. tcp_port);
  294. err = sockets_all_ifs(my_family, SOCK_STREAM, tcp_port, me.cur_ifs,
  295. me.cur_ifs_n, dev_sk, &max_sk_idx);
  296. if (!err)
  297. return NULL;
  298. else if (err < 0)
  299. fatal("Creation of the %s daemon aborted. "
  300. "Is there another ntkd running?", "tcp");
  301. pthread_mutex_init(&tcp_exec_lock, 0);
  302. for (i = 0; i < me.cur_ifs_n; i++) {
  303. if (!dev_sk[i])
  304. continue;
  305. /*
  306. * While we are accepting the connections we keep the socket non
  307. * blocking.
  308. */
  309. if (set_nonblock_sk(dev_sk[i])) {
  310. pthread_mutex_unlock(&tcp_daemon_lock);
  311. return NULL;
  312. }
  313. /* Shhh, it's listening... */
  314. if (listen(dev_sk[i], 5) == -1) {
  315. inet_close(&dev_sk[i]);
  316. pthread_mutex_unlock(&tcp_daemon_lock);
  317. return NULL;
  318. }
  319. }
  320. debug(DBG_NORMAL, "Tcp daemon on port %d up & running", tcp_port);
  321. pthread_mutex_unlock(&tcp_daemon_lock);
  322. for (;;) {
  323. FD_ZERO(&fdset);
  324. if (!me.cur_ifs_n) {
  325. /* All the devices have been removed while ntkd was
  326. * running, sleep well */
  327. sleep(1);
  328. continue;
  329. }
  330. for (i = 0; i < me.cur_ifs_n; i++)
  331. if (dev_sk[i])
  332. FD_SET(dev_sk[i], &fdset);
  333. ret = select(dev_sk[max_sk_idx] + 1, &fdset, NULL, NULL, NULL);
  334. if (sigterm_timestamp)
  335. /* NetsukukuD has been closed */
  336. break;
  337. if (ret < 0 && errno != EINTR)
  338. error("daemon_tcp: select error: %s", strerror(errno));
  339. if (ret < 0)
  340. continue;
  341. for (i = 0; i < me.cur_ifs_n; i++) {
  342. ifs = &me.cur_ifs[i];
  343. if (!dev_sk[i])
  344. continue;
  345. if (!FD_ISSET(dev_sk[i], &fdset))
  346. continue;
  347. fd = accept(dev_sk[i], (struct sockaddr *) &addr, &addrlen);
  348. if (fd == -1) {
  349. if (errno != EINTR && errno != EWOULDBLOCK)
  350. error("daemon_tcp: accept(): %s", strerror(errno));
  351. continue;
  352. }
  353. setzero(&rpkt, sizeof(PACKET));
  354. pkt_addsk(&rpkt, my_family, fd, SKT_TCP);
  355. pkt_add_dev(&rpkt, ifs, 0);
  356. rpkt.flags = MSG_WAITALL;
  357. pkt_addport(&rpkt, tcp_port);
  358. ntop = 0;
  359. sockaddr_to_inet((struct sockaddr *) &addr, &ip, 0);
  360. pkt_addfrom(&rpkt, &ip);
  361. if (server_opt.dbg_lvl)
  362. ntop = inet_to_str(ip);
  363. if ((ret = add_accept(ip, 0))) {
  364. debug(DBG_NORMAL, "ACPT: drop connection with %s: "
  365. "Accept table full.", ntop);
  366. /* Omg, we cannot take it anymore, go away: ACK_NEGATIVE */
  367. pkt_err(rpkt, ret, 1);
  368. inet_close(&fd);
  369. continue;
  370. } else {
  371. /*
  372. * Ok, the connection is good, send back the
  373. * ACK_AFFERMATIVE.
  374. */
  375. pkt_addto(&rpkt, &rpkt.from);
  376. send_rq(&rpkt, 0, ACK_AFFERMATIVE, 0, 0, 0, 0);
  377. }
  378. if (unset_nonblock_sk(fd))
  379. continue;
  380. pthread_mutex_lock(&tcp_exec_lock);
  381. err =
  382. pthread_create(&thread, &t_attr, tcp_recv_loop,
  383. (void *) &rpkt);
  384. pthread_detach(thread);
  385. pthread_mutex_lock(&tcp_exec_lock);
  386. pthread_mutex_unlock(&tcp_exec_lock);
  387. }
  388. }
  389. return NULL;
  390. }