oui: cleanup parser
[netsniff-ng.git] / ct_server.c
blob5e2f2d11945b8e5bfb257e1f5f6207e4b1194654
1 /*
2 * curvetun - the cipherspace wormhole creator
3 * Part of the netsniff-ng project
4 * By Daniel Borkmann <daniel@netsniff-ng.org>
5 * Copyright 2011 Daniel Borkmann <daniel@netsniff-ng.org>,
6 * Subject to the GPL, version 2.
7 */
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <pthread.h>
17 #include <syslog.h>
18 #include <signal.h>
19 #include <netdb.h>
20 #include <stdint.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <netinet/udp.h>
24 #include <sys/poll.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/wait.h>
28 #include <sys/epoll.h>
29 #include <arpa/inet.h>
30 #include <linux/if_tun.h>
32 #include "die.h"
33 #include "xutils.h"
34 #include "xio.h"
35 #include "xmalloc.h"
36 #include "curvetun.h"
37 #include "curve.h"
38 #include "built_in.h"
39 #include "ct_usermgmt.h"
40 #include "ct_cpusched.h"
41 #include "trie.h"
43 struct parent_info {
44 int efd;
45 int refd;
46 int tunfd;
47 int ipv4;
48 int udp;
51 struct worker_struct {
52 pthread_t trid;
53 int efd[2];
54 unsigned int cpu;
55 struct parent_info parent;
56 int (*handler)(int fd, const struct worker_struct *ws,
57 char *buff, size_t len);
58 struct curve25519_struct *c;
61 static struct worker_struct *threadpool = NULL;
63 static int auth_log = 1;
65 extern volatile sig_atomic_t sigint;
67 static int handler_udp_tun_to_net(int fd, const struct worker_struct *ws,
68 char *buff, size_t len) __pure;
69 static int handler_udp_net_to_tun(int fd, const struct worker_struct *ws,
70 char *buff, size_t len) __pure;
71 static int handler_udp(int fd, const struct worker_struct *ws,
72 char *buff, size_t len) __pure;
73 static int handler_tcp_tun_to_net(int fd, const struct worker_struct *ws,
74 char *buff, size_t len) __pure;
75 static int handler_tcp_net_to_tun(int fd, const struct worker_struct *ws,
76 char *buff, size_t len) __pure;
77 static int handler_tcp(int fd, const struct worker_struct *ws,
78 char *buff, size_t len) __pure;
79 ssize_t handler_tcp_read(int fd, char *buff, size_t len);
80 static void *worker(void *self) __pure;
82 static int handler_udp_tun_to_net(int fd, const struct worker_struct *ws,
83 char *buff, size_t len)
85 int dfd, keep = 1;
86 char *cbuff;
87 ssize_t rlen, err, clen;
88 struct ct_proto *hdr;
89 struct curve25519_proto *p;
90 struct sockaddr_storage naddr;
91 socklen_t nlen;
92 size_t off = sizeof(struct ct_proto) + crypto_box_zerobytes;
94 if (!buff || len <= off)
95 return 0;
97 memset(buff, 0, len);
98 while ((rlen = read(fd, buff + off, len - off)) > 0) {
99 dfd = -1; nlen = 0; p = NULL;
101 memset(&naddr, 0, sizeof(naddr));
103 hdr = (struct ct_proto *) buff;
104 memset(hdr, 0, sizeof(*hdr));
105 hdr->flags = 0;
107 trie_addr_lookup(buff + off, rlen, ws->parent.ipv4, &dfd, &naddr,
108 (size_t *) &nlen);
109 if (unlikely(dfd < 0 || nlen == 0)) {
110 memset(buff, 0, len);
111 continue;
114 err = get_user_by_sockaddr(&naddr, nlen, &p);
115 if (unlikely(err || !p)) {
116 memset(buff, 0, len);
117 continue;
120 clen = curve25519_encode(ws->c, p, (unsigned char *) (buff + off -
121 crypto_box_zerobytes), (rlen +
122 crypto_box_zerobytes), (unsigned char **)
123 &cbuff);
124 if (unlikely(clen <= 0)) {
125 memset(buff, 0, len);
126 continue;
129 hdr->payload = htons((uint16_t) clen);
131 set_udp_cork(dfd);
133 sendto(dfd, hdr, sizeof(struct ct_proto), 0, (struct sockaddr *)
134 &naddr, nlen);
135 sendto(dfd, cbuff, clen, 0, (struct sockaddr *) &naddr, nlen);
137 set_udp_uncork(dfd);
139 memset(buff, 0, len);
142 return keep;
145 static void handler_udp_notify_close(int fd, struct sockaddr_storage *addr)
147 struct ct_proto hdr;
149 memset(&hdr, 0, sizeof(hdr));
150 hdr.flags |= PROTO_FLAG_EXIT;
151 hdr.payload = 0;
153 sendto(fd, &hdr, sizeof(hdr), 0, (struct sockaddr *) addr,
154 sizeof(*addr));
157 static int handler_udp_net_to_tun(int fd, const struct worker_struct *ws,
158 char *buff, size_t len)
160 int keep = 1;
161 char *cbuff;
162 ssize_t rlen, err, clen;
163 struct ct_proto *hdr;
164 struct curve25519_proto *p;
165 struct sockaddr_storage naddr;
166 socklen_t nlen = sizeof(naddr);
168 if (!buff || !len)
169 return 0;
171 memset(&naddr, 0, sizeof(naddr));
172 while ((rlen = recvfrom(fd, buff, len, 0, (struct sockaddr *) &naddr,
173 &nlen)) > 0) {
174 p = NULL;
176 hdr = (struct ct_proto *) buff;
178 if (unlikely(rlen < sizeof(struct ct_proto)))
179 goto close;
180 if (unlikely(rlen - sizeof(*hdr) != ntohs(hdr->payload)))
181 goto close;
182 if (unlikely(ntohs(hdr->payload) == 0))
183 goto close;
184 if (hdr->flags & PROTO_FLAG_EXIT) {
185 close:
186 remove_user_by_sockaddr(&naddr, nlen);
187 trie_addr_remove_addr(&naddr, nlen);
188 handler_udp_notify_close(fd, &naddr);
190 return keep;
192 if (hdr->flags & PROTO_FLAG_INIT) {
193 syslog_maybe(auth_log, LOG_INFO, "Got initial userhash "
194 "from remote end!\n");
196 if (unlikely(rlen - sizeof(*hdr) <
197 sizeof(struct username_struct)))
198 goto close;
200 err = try_register_user_by_sockaddr(ws->c,
201 buff + sizeof(struct ct_proto),
202 rlen - sizeof(struct ct_proto),
203 &naddr, nlen, auth_log);
204 if (unlikely(err))
205 goto close;
207 goto next;
210 err = get_user_by_sockaddr(&naddr, nlen, &p);
211 if (unlikely(err || !p))
212 goto close;
214 clen = curve25519_decode(ws->c, p, (unsigned char *) buff +
215 sizeof(struct ct_proto),
216 rlen - sizeof(struct ct_proto),
217 (unsigned char **) &cbuff, NULL);
218 if (unlikely(clen <= 0))
219 goto close;
221 cbuff += crypto_box_zerobytes;
222 clen -= crypto_box_zerobytes;
224 err = trie_addr_maybe_update(cbuff, clen, ws->parent.ipv4,
225 fd, &naddr, nlen);
226 if (unlikely(err))
227 goto next;
229 err = write(ws->parent.tunfd, cbuff, clen);
230 next:
231 nlen = sizeof(naddr);
232 memset(&naddr, 0, sizeof(naddr));
235 return keep;
238 static int handler_udp(int fd, const struct worker_struct *ws,
239 char *buff, size_t len)
241 int ret = 0;
243 if (fd == ws->parent.tunfd)
244 ret = handler_udp_tun_to_net(fd, ws, buff, len);
245 else
246 ret = handler_udp_net_to_tun(fd, ws, buff, len);
248 return ret;
251 static int handler_tcp_tun_to_net(int fd, const struct worker_struct *ws,
252 char *buff, size_t len)
254 int dfd, keep = 1;
255 char *cbuff;
256 ssize_t rlen, err, clen;
257 struct ct_proto *hdr;
258 struct curve25519_proto *p;
259 socklen_t nlen;
260 size_t off = sizeof(struct ct_proto) + crypto_box_zerobytes;
262 if (!buff || len <= off)
263 return 0;
265 memset(buff, 0, len);
266 while ((rlen = read(fd, buff + off, len - off)) > 0) {
267 dfd = -1; p = NULL;
269 hdr = (struct ct_proto *) buff;
270 memset(hdr, 0, sizeof(*hdr));
271 hdr->flags = 0;
273 trie_addr_lookup(buff + off, rlen, ws->parent.ipv4, &dfd, NULL,
274 (size_t *) &nlen);
275 if (unlikely(dfd < 0)) {
276 memset(buff, 0, len);
277 continue;
280 err = get_user_by_socket(dfd, &p);
281 if (unlikely(err || !p)) {
282 memset(buff, 0, len);
283 continue;
286 clen = curve25519_encode(ws->c, p, (unsigned char *) (buff + off -
287 crypto_box_zerobytes), (rlen +
288 crypto_box_zerobytes), (unsigned char **)
289 &cbuff);
290 if (unlikely(clen <= 0)) {
291 memset(buff, 0, len);
292 continue;
295 hdr->payload = htons((uint16_t) clen);
297 set_tcp_cork(dfd);
299 write_exact(dfd, hdr, sizeof(struct ct_proto), 0);
300 write_exact(dfd, cbuff, clen, 0);
302 set_tcp_uncork(dfd);
304 memset(buff, 0, len);
307 return keep;
310 ssize_t handler_tcp_read(int fd, char *buff, size_t len)
312 ssize_t rlen;
313 struct ct_proto *hdr = (struct ct_proto *) buff;
315 if (!buff || !len)
316 return 0;
318 /* May exit on EAGAIN if 0 Byte read */
319 rlen = read_exact(fd, buff, sizeof(struct ct_proto), 1);
320 if (rlen < 0)
321 return rlen;
322 if (unlikely(ntohs(hdr->payload) > len - sizeof(struct ct_proto))) {
323 errno = ENOMEM;
324 return 1; /* Force server to close connection */
327 /* May not exit on EAGAIN if 0 Byte read */
328 rlen = read_exact(fd, buff + sizeof(struct ct_proto),
329 ntohs(hdr->payload), 0);
330 if (rlen < 0)
331 return rlen;
333 return sizeof(struct ct_proto) + rlen;
336 static void handler_tcp_notify_close(int fd)
338 struct ct_proto hdr;
340 memset(&hdr, 0, sizeof(hdr));
341 hdr.flags |= PROTO_FLAG_EXIT;
342 hdr.payload = 0;
344 if (write(fd, &hdr, sizeof(hdr))) { ; }
347 static int handler_tcp_net_to_tun(int fd, const struct worker_struct *ws,
348 char *buff, size_t len)
350 int keep = 1, count = 0;
351 char *cbuff;
352 ssize_t rlen, err, clen;
353 struct ct_proto *hdr;
354 struct curve25519_proto *p;
356 if (!buff || !len)
357 return 0;
359 while ((rlen = handler_tcp_read(fd, buff, len)) > 0) {
360 p = NULL;
362 hdr = (struct ct_proto *) buff;
364 if (unlikely(rlen < sizeof(struct ct_proto)))
365 goto close;
366 if (unlikely(rlen - sizeof(*hdr) != ntohs(hdr->payload)))
367 goto close;
368 if (unlikely(ntohs(hdr->payload) == 0))
369 goto close;
370 if (hdr->flags & PROTO_FLAG_EXIT) {
371 close:
372 remove_user_by_socket(fd);
373 trie_addr_remove(fd);
374 handler_tcp_notify_close(fd);
375 rlen = write(ws->parent.efd, &fd, sizeof(fd));
377 keep = 0;
378 return keep;
380 if (hdr->flags & PROTO_FLAG_INIT) {
381 syslog_maybe(auth_log, LOG_INFO, "Got initial userhash "
382 "from remote end!\n");
384 if (unlikely(rlen - sizeof(*hdr) <
385 sizeof(struct username_struct)))
386 goto close;
388 err = try_register_user_by_socket(ws->c,
389 buff + sizeof(struct ct_proto),
390 rlen - sizeof(struct ct_proto),
391 fd, auth_log);
392 if (unlikely(err))
393 goto close;
395 continue;
398 err = get_user_by_socket(fd, &p);
399 if (unlikely(err || !p))
400 continue;
402 clen = curve25519_decode(ws->c, p, (unsigned char *) buff +
403 sizeof(struct ct_proto),
404 rlen - sizeof(struct ct_proto),
405 (unsigned char **) &cbuff, NULL);
406 if (unlikely(clen <= 0))
407 continue;
409 cbuff += crypto_box_zerobytes;
410 clen -= crypto_box_zerobytes;
412 err = trie_addr_maybe_update(cbuff, clen, ws->parent.ipv4,
413 fd, NULL, 0);
414 if (unlikely(err))
415 continue;
417 err = write(ws->parent.tunfd, cbuff, clen);
419 count++;
420 if (count == 10) {
421 write_exact(ws->efd[1], &fd, sizeof(fd), 1);
422 /* Read later next data and let others process */
423 return keep;
427 return keep;
430 static int handler_tcp(int fd, const struct worker_struct *ws,
431 char *buff, size_t len)
433 int ret = 0;
435 if (fd == ws->parent.tunfd)
436 ret = handler_tcp_tun_to_net(fd, ws, buff, len);
437 else
438 ret = handler_tcp_net_to_tun(fd, ws, buff, len);
440 return ret;
443 static void *worker(void *self)
445 int fd, old_state;
446 ssize_t ret;
447 size_t blen = TUNBUFF_SIZ; //FIXME
448 const struct worker_struct *ws = self;
449 struct pollfd fds;
450 char *buff;
452 fds.fd = ws->efd[0];
453 fds.events = POLLIN;
455 ret = curve25519_alloc_or_maybe_die(ws->c);
456 if (ret < 0)
457 syslog_panic("Cannot init curve25519!\n");
459 buff = xmalloc_aligned(blen, 64);
461 syslog(LOG_INFO, "curvetun thread on CPU%u up!\n", ws->cpu);
463 pthread_cleanup_push(xfree_func, ws->c);
464 pthread_cleanup_push(curve25519_free, ws->c);
465 pthread_cleanup_push(xfree_func, buff);
467 while (likely(!sigint)) {
468 poll(&fds, 1, -1);
469 if ((fds.revents & POLLIN) != POLLIN)
470 continue;
472 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
474 while ((ret = read_exact(ws->efd[0], &fd, sizeof(fd), 1)) > 0) {
475 if (ret != sizeof(fd)) {
476 sched_yield();
477 continue;
480 ret = ws->handler(fd, ws, buff, blen);
481 if (ret)
482 write_exact(ws->parent.refd, &fd, sizeof(fd), 1);
485 pthread_setcancelstate(old_state, NULL);
488 syslog(LOG_INFO, "curvetun thread on CPU%u down!\n", ws->cpu);
490 pthread_cleanup_pop(1);
491 pthread_cleanup_pop(1);
492 pthread_cleanup_pop(1);
494 pthread_exit((void *) ((long) ws->cpu));
497 static void thread_spawn_or_panic(unsigned int cpus, int efd, int refd,
498 int tunfd, int ipv4, int udp)
500 int i, ret;
501 cpu_set_t cpuset;
502 unsigned int threads;
504 threads = cpus * THREADS_PER_CPU;
506 for (i = 0; i < threads; ++i) {
507 CPU_ZERO(&cpuset);
508 threadpool[i].cpu = i % cpus;
509 CPU_SET(threadpool[i].cpu, &cpuset);
511 ret = pipe2(threadpool[i].efd, O_NONBLOCK);
512 if (ret < 0)
513 syslog_panic("Cannot create event socket!\n");
515 threadpool[i].c = xmalloc_aligned(sizeof(*threadpool[i].c), 64);
516 threadpool[i].parent.efd = efd;
517 threadpool[i].parent.refd = refd;
518 threadpool[i].parent.tunfd = tunfd;
519 threadpool[i].parent.ipv4 = ipv4;
520 threadpool[i].parent.udp = udp;
521 threadpool[i].handler = udp ? handler_udp : handler_tcp;
523 ret = pthread_create(&threadpool[i].trid, NULL,
524 worker, &threadpool[i]);
525 if (ret < 0)
526 syslog_panic("Thread creation failed!\n");
528 ret = pthread_setaffinity_np(threadpool[i].trid,
529 sizeof(cpuset), &cpuset);
530 if (ret < 0)
531 syslog_panic("Thread CPU migration failed!\n");
533 pthread_detach(threadpool[i].trid);
536 sleep(1);
539 static void thread_finish(unsigned int cpus)
541 int i;
542 unsigned int threads;
544 threads = cpus * THREADS_PER_CPU;
546 for (i = 0; i < threads; ++i) {
547 while (pthread_join(threadpool[i].trid, NULL) < 0)
550 close(threadpool[i].efd[0]);
551 close(threadpool[i].efd[1]);
555 int server_main(char *home, char *dev, char *port, int udp, int ipv4, int log)
557 int lfd = -1, kdpfd, nfds, nfd, curfds, efd[2], refd[2], tunfd, i;
558 unsigned int cpus = 0, threads, udp_cpu = 0;
559 ssize_t ret;
560 struct epoll_event *events;
561 struct addrinfo hints, *ahead, *ai;
563 auth_log = !!log;
564 openlog("curvetun", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_DAEMON);
566 syslog(LOG_INFO, "curvetun server booting!\n");
567 syslog_maybe(!auth_log, LOG_INFO, "curvetun user logging disabled!\n");
569 parse_userfile_and_generate_user_store_or_die(home);
571 memset(&hints, 0, sizeof(hints));
572 hints.ai_family = PF_UNSPEC;
573 hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
574 hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
575 hints.ai_flags = AI_PASSIVE;
577 ret = getaddrinfo(NULL, port, &hints, &ahead);
578 if (ret < 0)
579 syslog_panic("Cannot get address info!\n");
581 for (ai = ahead; ai != NULL && lfd < 0; ai = ai->ai_next) {
582 lfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
583 if (lfd < 0)
584 continue;
585 if (ai->ai_family == AF_INET6) {
586 #ifdef IPV6_V6ONLY
587 ret = set_ipv6_only(lfd);
588 if (ret < 0) {
589 close(lfd);
590 lfd = -1;
591 continue;
593 #else
594 close(lfd);
595 lfd = -1;
596 continue;
597 #endif /* IPV6_V6ONLY */
600 set_reuseaddr(lfd);
601 set_mtu_disc_dont(lfd);
603 ret = bind(lfd, ai->ai_addr, ai->ai_addrlen);
604 if (ret < 0) {
605 close(lfd);
606 lfd = -1;
607 continue;
610 if (!udp) {
611 ret = listen(lfd, 5);
612 if (ret < 0) {
613 close(lfd);
614 lfd = -1;
615 continue;
619 if (ipv4 == -1) {
620 ipv4 = (ai->ai_family == AF_INET6 ? 0 :
621 (ai->ai_family == AF_INET ? 1 : -1));
624 syslog_maybe(auth_log, LOG_INFO, "curvetun on IPv%d via %s "
625 "on port %s!\n", ai->ai_family == AF_INET ? 4 : 6,
626 udp ? "UDP" : "TCP", port);
627 syslog_maybe(auth_log, LOG_INFO, "Allowed overlay proto is "
628 "IPv%d!\n", ipv4 ? 4 : 6);
631 freeaddrinfo(ahead);
633 if (lfd < 0 || ipv4 < 0)
634 syslog_panic("Cannot create socket!\n");
636 tunfd = tun_open_or_die(dev ? dev : DEVNAME_SERVER, IFF_TUN | IFF_NO_PI);
638 pipe_or_die(efd, O_NONBLOCK);
639 pipe_or_die(refd, O_NONBLOCK);
641 set_nonblocking(lfd);
643 events = xzmalloc(MAX_EPOLL_SIZE * sizeof(*events));
644 for (i = 0; i < MAX_EPOLL_SIZE; ++i)
645 events[i].data.fd = -1;
647 kdpfd = epoll_create(MAX_EPOLL_SIZE);
648 if (kdpfd < 0)
649 syslog_panic("Cannot create socket!\n");
651 set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, lfd,
652 udp ? EPOLLIN | EPOLLET | EPOLLONESHOT : EPOLLIN);
653 set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, efd[0], EPOLLIN);
654 set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, refd[0], EPOLLIN);
655 set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, tunfd,
656 EPOLLIN | EPOLLET | EPOLLONESHOT);
657 curfds = 4;
659 trie_init();
661 cpus = get_number_cpus_online();
662 threads = cpus * THREADS_PER_CPU;
663 if (!ispow2(threads))
664 syslog_panic("Thread number not power of two!\n");
666 threadpool = xzmalloc(sizeof(*threadpool) * threads);
667 thread_spawn_or_panic(cpus, efd[1], refd[1], tunfd, ipv4, udp);
669 init_cpusched(threads);
671 register_socket(tunfd);
672 register_socket(lfd);
674 syslog(LOG_INFO, "curvetun up and running!\n");
676 while (likely(!sigint)) {
677 nfds = epoll_wait(kdpfd, events, curfds, -1);
678 if (nfds < 0) {
679 syslog(LOG_ERR, "epoll_wait error: %s\n",
680 strerror(errno));
681 break;
684 for (i = 0; i < nfds; ++i) {
685 if (unlikely(events[i].data.fd < 0))
686 continue;
688 if (events[i].data.fd == lfd && !udp) {
689 int ncpu;
690 char hbuff[256], sbuff[256];
691 struct sockaddr_storage taddr;
692 socklen_t tlen;
694 tlen = sizeof(taddr);
695 nfd = accept(lfd, (struct sockaddr *) &taddr,
696 &tlen);
697 if (nfd < 0) {
698 syslog(LOG_ERR, "accept error: %s\n",
699 strerror(errno));
700 continue;
703 if (curfds + 1 > MAX_EPOLL_SIZE) {
704 close(nfd);
705 continue;
708 curfds++;
710 ncpu = register_socket(nfd);
712 memset(hbuff, 0, sizeof(hbuff));
713 memset(sbuff, 0, sizeof(sbuff));
714 getnameinfo((struct sockaddr *) &taddr, tlen,
715 hbuff, sizeof(hbuff),
716 sbuff, sizeof(sbuff),
717 NI_NUMERICHOST | NI_NUMERICSERV);
719 syslog_maybe(auth_log, LOG_INFO, "New connection "
720 "from %s:%s with id %d on CPU%d, %d "
721 "active!\n", hbuff, sbuff, nfd, ncpu,
722 curfds);
724 set_nonblocking(nfd);
725 set_socket_keepalive(nfd);
726 set_tcp_nodelay(nfd);
727 ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_ADD,
728 nfd, EPOLLIN | EPOLLET | EPOLLONESHOT);
729 if (ret < 0) {
730 close(nfd);
731 curfds--;
732 continue;
734 } else if (events[i].data.fd == refd[0]) {
735 int fd_one;
737 ret = read_exact(refd[0], &fd_one,
738 sizeof(fd_one), 1);
739 if (ret != sizeof(fd_one) || fd_one <= 0)
740 continue;
742 ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_MOD,
743 fd_one, EPOLLIN | EPOLLET | EPOLLONESHOT);
744 if (ret < 0) {
745 close(fd_one);
746 continue;
748 } else if (events[i].data.fd == efd[0]) {
749 int fd_del, test;
751 ret = read_exact(efd[0], &fd_del,
752 sizeof(fd_del), 1);
753 if (ret != sizeof(fd_del) || fd_del <= 0)
754 continue;
756 ret = read(fd_del, &test, sizeof(test));
757 if (ret < 0 && errno == EBADF)
758 continue;
760 ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_DEL,
761 fd_del, 0);
762 if (ret < 0) {
763 close(fd_del);
764 continue;
767 close(fd_del);
768 curfds--;
769 unregister_socket(fd_del);
771 syslog_maybe(auth_log, LOG_INFO, "Closed connection "
772 "with id %d, %d active!\n", fd_del,
773 curfds);
774 } else {
775 int cpu, fd_work = events[i].data.fd;
777 if (!udp)
778 cpu = socket_to_cpu(fd_work);
779 else
780 udp_cpu = (udp_cpu + 1) & (threads - 1);
782 write_exact(threadpool[udp ? udp_cpu : cpu].efd[1],
783 &fd_work, sizeof(fd_work), 1);
788 syslog(LOG_INFO, "curvetun prepare shut down!\n");
790 close(lfd);
791 close(efd[0]);
792 close(efd[1]);
793 close(refd[0]);
794 close(refd[1]);
795 close(tunfd);
797 thread_finish(cpus);
799 xfree(threadpool);
800 xfree(events);
802 unregister_socket(lfd);
803 unregister_socket(tunfd);
805 destroy_cpusched();
807 trie_cleanup();
809 destroy_user_store();
811 syslog(LOG_INFO, "curvetun shut down!\n");
812 closelog();
814 return 0;