2 net.c -- most of the network code
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: net.c,v 1.35.4.3 2000/06/25 15:16:11 guus Exp $
25 #include <arpa/inet.h>
29 #include <netinet/in.h>
33 #include <sys/signal.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
55 int total_tap_out = 0;
56 int total_socket_in = 0;
57 int total_socket_out = 0;
59 static int seconds_till_retry;
61 /* The global list of existing connections */
62 conn_list_t *conn_list = NULL;
63 conn_list_t *myself = NULL;
66 strip off the MAC adresses of an ethernet frame
68 void strip_mac_addresses(vpn_packet_t *p)
70 unsigned char tmp[MAXSIZE];
72 memcpy(tmp, p->data, p->len);
74 memcpy(p->data, &tmp[12], p->len);
79 reassemble MAC addresses
81 void add_mac_addresses(vpn_packet_t *p)
83 unsigned char tmp[MAXSIZE];
85 memcpy(&tmp[12], p->data, p->len);
87 tmp[0] = tmp[6] = 0xfe;
88 tmp[1] = tmp[7] = 0xfd;
89 *((ip_t*)(&tmp[2])) = (ip_t)(htonl(myself->vpn_ip));
90 *((ip_t*)(&tmp[8])) = *((ip_t*)(&tmp[26]));
91 memcpy(p->data, &tmp[0], p->len);
95 int xsend(conn_list_t *cl, void *packet)
100 do_encrypt((vpn_packet_t*)packet, &rp, cl->key);
101 rp.from = htonl(myself->vpn_ip);
102 rp.data.len = htons(rp.data.len);
103 rp.len = htons(rp.len);
106 syslog(LOG_ERR, _("Sent %d bytes to %lx"), ntohs(rp.len), cl->vpn_ip);
108 if((r = send(cl->socket, (char*)&rp, ntohs(rp.len), 0)) < 0)
110 syslog(LOG_ERR, _("Error sending data: %m"));
114 total_socket_out += r;
121 int xrecv(conn_list_t *cl, void *packet)
126 do_decrypt((real_packet_t*)packet, &vp, cl->key);
127 add_mac_addresses(&vp);
129 if((lenin = write(tap_fd, &vp, vp.len + sizeof(vp.len))) < 0)
130 syslog(LOG_ERR, _("Can't write to tap device: %m"));
132 total_tap_out += lenin;
135 cl->last_ping_time = time(NULL);
141 add the given packet of size s to the
142 queue q, be it the send or receive queue
144 void add_queue(packet_queue_t **q, void *packet, size_t s)
149 syslog(LOG_DEBUG, _("packet to queue: %d"), s);
151 e = xmalloc(sizeof(*e));
152 e->packet = xmalloc(s);
153 memcpy(e->packet, packet, s);
157 *q = xmalloc(sizeof(**q));
158 (*q)->head = (*q)->tail = NULL;
161 e->next = NULL; /* We insert at the tail */
163 if((*q)->tail) /* Do we have a tail? */
165 (*q)->tail->next = e;
166 e->prev = (*q)->tail;
168 else /* No tail -> no head too */
178 /* Remove a queue element */
179 void del_queue(packet_queue_t **q, queue_element_t *e)
184 if(e->next) /* There is a successor, so we are not tail */
186 if(e->prev) /* There is a predecessor, so we are not head */
188 e->next->prev = e->prev;
189 e->prev->next = e->next;
191 else /* We are head */
193 e->next->prev = NULL;
194 (*q)->head = e->next;
197 else /* We are tail (or all alone!) */
199 if(e->prev) /* We are not alone :) */
201 e->prev->next = NULL;
202 (*q)->tail = e->prev;
216 flush a queue by calling function for
217 each packet, and removing it when that
218 returned a zero exit code
220 void flush_queue(conn_list_t *cl, packet_queue_t **pq,
221 int (*function)(conn_list_t*,void*))
223 queue_element_t *p, *next = NULL;
225 for(p = (*pq)->head; p != NULL; )
229 if(!function(cl, p->packet))
236 syslog(LOG_DEBUG, _("queue flushed"));
241 flush the send&recv queues
242 void because nothing goes wrong here, packets
243 remain in the queue if something goes wrong
245 void flush_queues(conn_list_t *cl)
251 syslog(LOG_DEBUG, _("Flushing send queue for " IP_ADDR_S),
252 IP_ADDR_V(cl->vpn_ip));
253 flush_queue(cl, &(cl->sq), xsend);
259 syslog(LOG_DEBUG, _("Flushing receive queue for " IP_ADDR_S),
260 IP_ADDR_V(cl->vpn_ip));
261 flush_queue(cl, &(cl->rq), xrecv);
267 send a packet to the given vpn ip.
269 int send_packet(ip_t to, vpn_packet_t *packet)
273 if((cl = lookup_conn(to)) == NULL)
277 syslog(LOG_NOTICE, _("Trying to look up " IP_ADDR_S " in connection list failed!"),
281 /* Is this really necessary? If we can't find "to", then neither should any uplink. (GS) */
285 for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
287 { /* No open outgoing connection has been found. */
289 syslog(LOG_NOTICE, _("There is no remote host I can send this packet to!"));
294 /* If we ourselves have indirectdata flag set, we should send only to our uplink! */
296 if(myself->flags & EXPORTINDIRECTDATA)
298 for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
300 { /* No open outgoing connection has been found. */
302 syslog(LOG_NOTICE, _("There is no remote host I can send this packet to!"));
308 /* If indirectdata flag is set for the destination we just looked up,
309 * then real_ip is actually the vpn_ip of the gateway tincd
313 if(cl->flags & INDIRECTDATA)
315 if((cl = lookup_conn(cl->vpn_ip)) == NULL)
319 syslog(LOG_NOTICE, _("Indirect look up " IP_ADDR_S " in connection list failed!"),
323 /* Gateway tincd dead? Should we kill it? (GS) */
327 if(cl->flags & INDIRECTDATA) /* This should not happen */
330 syslog(LOG_NOTICE, _("double indirection for " IP_ADDR_S),
336 if(my_key_expiry <= time(NULL))
339 if(!cl->status.dataopen)
340 if(setup_vpn_connection(cl) < 0)
343 if(!cl->status.validkey)
345 add_queue(&(cl->sq), packet, packet->len + 2);
346 if(!cl->status.waitingforkey)
347 send_key_request(cl->vpn_ip); /* Keys should be sent to the host running the tincd */
351 if(!cl->status.active)
353 add_queue(&(cl->sq), packet, packet->len + 2);
355 syslog(LOG_INFO, _(IP_ADDR_S " is not ready, queueing packet"), IP_ADDR_V(cl->vpn_ip));
356 return 0; /* We don't want to mess up, do we? */
359 /* can we send it? can we? can we? huh? */
361 return xsend(cl, packet);
365 open the local ethertap device
367 int setup_tap_fd(void)
370 const char *tapfname;
373 if((cfg = get_config_val(tapdevice)) == NULL)
374 tapfname = "/dev/tap0";
376 tapfname = cfg->data.ptr;
378 if((nfd = open(tapfname, O_RDWR | O_NONBLOCK)) < 0)
380 syslog(LOG_ERR, _("Could not open %s: %m"), tapfname);
390 set up the socket that we listen on for incoming
393 int setup_listen_meta_socket(int port)
396 struct sockaddr_in a;
399 if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
401 syslog(LOG_ERR, _("Creating metasocket failed: %m"));
405 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
407 syslog(LOG_ERR, _("setsockopt: %m"));
411 flags = fcntl(nfd, F_GETFL);
412 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
414 syslog(LOG_ERR, _("fcntl: %m"));
418 memset(&a, 0, sizeof(a));
419 a.sin_family = AF_INET;
420 a.sin_port = htons(port);
421 a.sin_addr.s_addr = htonl(INADDR_ANY);
423 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
425 syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
431 syslog(LOG_ERR, _("listen: %m"));
439 setup the socket for incoming encrypted
442 int setup_vpn_in_socket(int port)
445 struct sockaddr_in a;
448 if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
450 syslog(LOG_ERR, _("Creating socket failed: %m"));
454 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
456 syslog(LOG_ERR, _("setsockopt: %m"));
460 flags = fcntl(nfd, F_GETFL);
461 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
463 syslog(LOG_ERR, _("fcntl: %m"));
467 memset(&a, 0, sizeof(a));
468 a.sin_family = AF_INET;
469 a.sin_port = htons(port);
470 a.sin_addr.s_addr = htonl(INADDR_ANY);
472 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
474 syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
482 setup an outgoing meta (tcp) socket
484 int setup_outgoing_meta_socket(conn_list_t *cl)
487 struct sockaddr_in a;
490 if((cfg = get_config_val(upstreamport)) == NULL)
493 cl->port = cfg->data.val;
495 cl->meta_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
496 if(cl->meta_socket == -1)
498 syslog(LOG_ERR, _("Creating socket failed: %m"));
502 a.sin_family = AF_INET;
503 a.sin_port = htons(cl->port);
504 a.sin_addr.s_addr = htonl(cl->real_ip);
506 if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
508 syslog(LOG_ERR, _(IP_ADDR_S ":%d: %m"), IP_ADDR_V(cl->real_ip), cl->port);
512 flags = fcntl(cl->meta_socket, F_GETFL);
513 if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
515 syslog(LOG_ERR, _("fcntl: %m"));
519 syslog(LOG_INFO, _("Connected to " IP_ADDR_S ":%hd"),
520 IP_ADDR_V(cl->real_ip), cl->port);
526 setup an outgoing connection. It's not
527 necessary to also open an udp socket as
528 well, because the other host will initiate
529 an authentication sequence during which
530 we will do just that.
532 int setup_outgoing_connection(ip_t ip)
536 ncn = new_conn_list();
539 if(setup_outgoing_meta_socket(ncn) < 0)
541 syslog(LOG_ERR, _("Could not set up a meta connection!"));
542 free_conn_element(ncn);
546 ncn->status.meta = 1;
547 ncn->status.outgoing = 1;
548 ncn->next = conn_list;
555 set up the local sockets (listen only)
557 int setup_myself(void)
561 myself = new_conn_list();
563 if(!(cfg = get_config_val(myvpnip)))
565 syslog(LOG_ERR, _("No value for my VPN IP given"));
569 myself->vpn_ip = cfg->data.ip->ip;
570 myself->vpn_mask = cfg->data.ip->mask;
573 if(!(cfg = get_config_val(listenport)))
576 myself->port = cfg->data.val;
578 if(cfg = get_config_val(indirectdata))
580 myself->flags |= EXPORTINDIRECTDATA;
582 if((myself->meta_socket = setup_listen_meta_socket(myself->port)) < 0)
584 syslog(LOG_ERR, _("Unable to set up a listening socket"));
588 if((myself->socket = setup_vpn_in_socket(myself->port)) < 0)
590 syslog(LOG_ERR, _("Unable to set up an incoming vpn data socket"));
591 close(myself->meta_socket);
595 myself->status.active = 1;
597 syslog(LOG_NOTICE, _("Ready: listening on port %d"), myself->port);
603 sigalrm_handler(int a)
607 cfg = get_config_val(upstreamip);
609 if(!setup_outgoing_connection(cfg->data.ip->ip))
611 signal(SIGALRM, SIG_IGN);
615 signal(SIGALRM, sigalrm_handler);
616 seconds_till_retry += 5;
617 if(seconds_till_retry>300) /* Don't wait more than 5 minutes. */
618 seconds_till_retry = 300;
619 alarm(seconds_till_retry);
620 syslog(LOG_ERR, _("Still failed to connect to other, will retry in %d seconds"),
627 setup all initial network connections
629 int setup_network_connections(void)
633 if((cfg = get_config_val(pingtimeout)) == NULL)
636 timeout = cfg->data.val;
638 if(setup_tap_fd() < 0)
641 if(setup_myself() < 0)
644 if((cfg = get_config_val(upstreamip)) == NULL)
645 /* No upstream IP given, we're listen only. */
648 if(setup_outgoing_connection(cfg->data.ip->ip))
650 signal(SIGALRM, sigalrm_handler);
651 seconds_till_retry = 300;
652 alarm(seconds_till_retry);
653 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 minutes"));
660 close all open network connections
662 void close_network_connections(void)
666 for(p = conn_list; p != NULL; p = p->next)
668 if(p->status.dataopen)
670 shutdown(p->socket, 0); /* No more receptions */
676 shutdown(p->meta_socket, 0); /* No more receptions */
677 close(p->meta_socket);
682 if(myself->status.active)
684 close(myself->meta_socket);
685 close(myself->socket);
691 syslog(LOG_NOTICE, _("Terminating"));
697 create a data (udp) socket
699 int setup_vpn_connection(conn_list_t *cl)
702 struct sockaddr_in a;
705 syslog(LOG_DEBUG, _("Opening UDP socket to " IP_ADDR_S), IP_ADDR_V(cl->real_ip));
707 nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
710 syslog(LOG_ERR, _("Creating data socket failed: %m"));
714 a.sin_family = AF_INET;
715 a.sin_port = htons(cl->port);
716 a.sin_addr.s_addr = htonl(cl->real_ip);
718 if(connect(nfd, (struct sockaddr *)&a, sizeof(a)) == -1)
720 syslog(LOG_ERR, _("Connecting to " IP_ADDR_S ":%d failed: %m"),
721 IP_ADDR_V(cl->real_ip), cl->port);
725 flags = fcntl(nfd, F_GETFL);
726 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
728 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"), __FILE__, __LINE__, nfd);
733 cl->status.dataopen = 1;
739 handle an incoming tcp connect call and open
742 conn_list_t *create_new_connection(int sfd)
745 struct sockaddr_in ci;
746 int len = sizeof(ci);
750 if(getpeername(sfd, &ci, &len) < 0)
752 syslog(LOG_ERR, _("Error: getpeername: %m"));
756 p->real_ip = ntohl(ci.sin_addr.s_addr);
757 p->meta_socket = sfd;
760 p->last_ping_time = time(NULL);
763 syslog(LOG_NOTICE, _("Connection from " IP_ADDR_S ":%d"),
764 IP_ADDR_V(p->real_ip), htons(ci.sin_port));
766 if(send_basic_info(p) < 0)
776 put all file descriptors in an fd_set array
778 void build_fdset(fd_set *fs)
784 for(p = conn_list; p != NULL; p = p->next)
787 FD_SET(p->meta_socket, fs);
788 if(p->status.dataopen)
789 FD_SET(p->socket, fs);
792 FD_SET(myself->meta_socket, fs);
793 FD_SET(myself->socket, fs);
799 receive incoming data from the listening
800 udp socket and write it to the ethertap
801 device after being decrypted
803 int handle_incoming_vpn_data(conn_list_t *cl)
807 int x, l = sizeof(x);
810 if(getsockopt(cl->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
812 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"), __FILE__, __LINE__, cl->socket);
817 syslog(LOG_ERR, _("Incoming data socket error: %s"), sys_errlist[x]);
822 lenin = recvfrom(cl->socket, &rp, MTU, 0, NULL, NULL);
825 syslog(LOG_ERR, _("Receiving data failed: %m"));
828 total_socket_in += lenin;
830 rp.data.len = ntohs(rp.data.len);
831 rp.len = ntohs(rp.len);
832 rp.from = ntohl(rp.from);
836 f = lookup_conn(rp.from);
838 syslog(LOG_DEBUG, _("packet from " IP_ADDR_S " (len %d)"),
839 IP_ADDR_V(rp.from), rp.len);
842 syslog(LOG_ERR, _("Got packet from unknown source " IP_ADDR_S),
847 if(f->status.validkey)
851 add_queue(&(f->rq), &rp, rp.len);
852 if(!cl->status.waitingforkey)
853 send_key_request(rp.from);
856 if(my_key_expiry <= time(NULL))
864 terminate a connection and notify the other
865 end before closing the sockets
867 void terminate_connection(conn_list_t *cl)
872 if(cl->status.remove)
876 syslog(LOG_NOTICE, _("Closing connection with " IP_ADDR_S " (" IP_ADDR_S ")"),
877 IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
879 if(cl->status.timeout)
881 else if(!cl->status.termreq)
886 close(cl->meta_socket);
888 if(cl->status.outgoing)
890 signal(SIGALRM, sigalrm_handler);
891 seconds_till_retry = 5;
892 alarm(seconds_till_retry);
893 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
896 cl->status.active = 0;
897 cl->status.remove = 1;
900 /* Find all connections that were lost because they were behind cl
901 (the connection that was dropped). */
902 for(p = conn_list; p != NULL; p = p->next)
905 p->status.active = 0;
906 p->status.remove = 1;
910 /* Then send a notification about all these connections to all hosts
911 that are still connected to us. */
912 for(p = conn_list; p != NULL; p = p->next)
913 if(!p->status.remove && p->status.meta)
914 for(q = conn_list; q != NULL; q = q->next)
922 Check if the other end is active.
923 If we have sent packets, but didn't receive any,
924 then possibly the other end is dead. We send a
925 PING request over the meta connection. If the other
926 end does not reply in time, we consider them dead
927 and close the connection.
929 int check_dead_connections(void)
935 for(p = conn_list; p != NULL; p = p->next)
939 if(p->status.active && p->status.meta)
941 if(p->last_ping_time + timeout < now)
943 if(p->status.pinged && !p->status.got_pong)
946 syslog(LOG_INFO, _(IP_ADDR_S " (" IP_ADDR_S ") didn't respond to ping"),
947 IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->real_ip));
948 p->status.timeout = 1;
949 terminate_connection(p);
951 else if(p->want_ping)
954 p->last_ping_time = now;
955 p->status.pinged = 1;
956 p->status.got_pong = 0;
966 accept a new tcp connect and create a
969 int handle_new_meta_connection(conn_list_t *cl)
972 struct sockaddr client;
973 int nfd, len = sizeof(client);
975 if((nfd = accept(cl->meta_socket, &client, &len)) < 0)
977 syslog(LOG_ERR, _("Accepting a new connection failed: %m"));
981 if((ncn = create_new_connection(nfd)) == NULL)
985 syslog(LOG_NOTICE, _("Closed attempted connection"));
989 ncn->status.meta = 1;
990 ncn->next = conn_list;
997 dispatch any incoming meta requests
999 int handle_incoming_meta_data(conn_list_t *cl)
1001 int x, l = sizeof(x);
1002 int request, oldlen, i;
1005 if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
1007 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"), __FILE__, __LINE__, cl->meta_socket);
1012 syslog(LOG_ERR, _("Metadata socket error: %s"), sys_errlist[x]);
1016 if(cl->buflen >= MAXBUFSIZE)
1018 syslog(LOG_ERR, _("Metadata read buffer overflow!"));
1022 lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
1026 syslog(LOG_ERR, _("Metadata socket read error: %m"));
1030 oldlen = cl->buflen;
1031 cl->buflen += lenin;
1037 for(i = oldlen; i < cl->buflen; i++)
1039 if(cl->buffer[i] == '\n')
1041 cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
1050 syslog(LOG_DEBUG, _("Got request from " IP_ADDR_S " (" IP_ADDR_S "): %s"),
1051 IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip), cl->buffer);
1052 if(sscanf(cl->buffer, "%d", &request) == 1)
1054 if((request < 0) || (request > 255) || (request_handlers[request] == NULL))
1056 syslog(LOG_ERR, _("Unknown request from " IP_ADDR_S " (" IP_ADDR_S ")"),
1057 IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
1061 if(request_handlers[request](cl)) /* Something went wrong. Probably scriptkiddies. Terminate. */
1063 syslog(LOG_ERR, _("Error while processing request from " IP_ADDR_S " (" IP_ADDR_S ")"),
1064 IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
1070 syslog(LOG_ERR, _("Bogus data received from " IP_ADDR_S " (" IP_ADDR_S ")"),
1071 IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
1075 cl->buflen -= cl->reqlen;
1076 memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
1085 cl->last_ping_time = time(NULL);
1092 check all connections to see if anything
1093 happened on their sockets
1095 void check_network_activity(fd_set *f)
1098 int x, l = sizeof(x);
1100 for(p = conn_list; p != NULL; p = p->next)
1102 if(p->status.remove)
1105 if(p->status.dataopen)
1106 if(FD_ISSET(p->socket, f))
1109 The only thing that can happen to get us here is apparently an
1110 error on this outgoing(!) UDP socket that isn't immediate (i.e.
1111 something that will not trigger an error directly on send()).
1112 I've once got here when it said `No route to host'.
1114 getsockopt(p->socket, SOL_SOCKET, SO_ERROR, &x, &l);
1115 syslog(LOG_ERR, _("Outgoing data socket error: %s"), sys_errlist[x]);
1116 terminate_connection(p);
1121 if(FD_ISSET(p->meta_socket, f))
1122 if(handle_incoming_meta_data(p) < 0)
1124 terminate_connection(p);
1129 if(FD_ISSET(myself->socket, f))
1130 handle_incoming_vpn_data(myself);
1132 if(FD_ISSET(myself->meta_socket, f))
1133 handle_new_meta_connection(myself);
1138 read, encrypt and send data that is
1139 available through the ethertap device
1141 void handle_tap_input(void)
1145 int ether_type, lenin;
1147 memset(&vp, 0, sizeof(vp));
1148 if((lenin = read(tap_fd, &vp, MTU)) <= 0)
1150 syslog(LOG_ERR, _("Error while reading from tapdevice: %m"));
1154 total_tap_in += lenin;
1156 ether_type = ntohs(*((unsigned short*)(&vp.data[12])));
1157 if(ether_type != 0x0800)
1160 syslog(LOG_INFO, _("Non-IP ethernet frame %04x from " MAC_ADDR_S),
1161 ether_type, MAC_ADDR_V(vp.data[6]));
1168 syslog(LOG_INFO, _("Dropping short packet"));
1172 from = ntohl(*((unsigned long*)(&vp.data[26])));
1173 to = ntohl(*((unsigned long*)(&vp.data[30])));
1176 syslog(LOG_DEBUG, _("An IP packet (%04x) for " IP_ADDR_S " from " IP_ADDR_S),
1177 ether_type, IP_ADDR_V(to), IP_ADDR_V(from));
1179 syslog(LOG_DEBUG, _(MAC_ADDR_S " to " MAC_ADDR_S),
1180 MAC_ADDR_V(vp.data[0]), MAC_ADDR_V(vp.data[6]));
1182 vp.len = (length_t)lenin - 2;
1184 strip_mac_addresses(&vp);
1186 send_packet(to, &vp);
1191 this is where it all happens...
1193 void main_loop(void)
1198 time_t last_ping_check;
1200 last_ping_check = time(NULL);
1204 tv.tv_sec = timeout;
1210 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
1212 if(errno == EINTR) /* because of alarm */
1214 syslog(LOG_ERR, _("Error while waiting for input: %m"));
1218 if(last_ping_check + timeout < time(NULL))
1219 /* Let's check if everybody is still alive */
1221 check_dead_connections();
1222 last_ping_check = time(NULL);
1226 check_network_activity(&fset);
1228 /* local tap data */
1229 if(FD_ISSET(tap_fd, &fset))