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.32 2000/09/26 14:06:04 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>
56 int total_tap_out = 0;
57 int total_socket_in = 0;
58 int total_socket_out = 0;
60 int upstreamindex = 0;
61 static int seconds_till_retry;
63 /* The global list of existing connections */
64 conn_list_t *conn_list = NULL;
65 conn_list_t *myself = NULL;
68 strip off the MAC adresses of an ethernet frame
70 void strip_mac_addresses(vpn_packet_t *p)
72 unsigned char tmp[MAXSIZE];
74 memcpy(tmp, p->data, p->len);
76 memcpy(p->data, &tmp[12], p->len);
81 reassemble MAC addresses
83 void add_mac_addresses(vpn_packet_t *p)
85 unsigned char tmp[MAXSIZE];
87 memcpy(&tmp[12], p->data, p->len);
89 tmp[0] = tmp[6] = 0xfe;
90 tmp[1] = tmp[7] = 0xfd;
91 *((ip_t*)(&tmp[2])) = (ip_t)(htonl(myself->vpn_ip));
92 *((ip_t*)(&tmp[8])) = *((ip_t*)(&tmp[26]));
93 memcpy(p->data, &tmp[0], p->len);
97 int xsend(conn_list_t *cl, void *packet)
101 do_encrypt((vpn_packet_t*)packet, &rp, cl->datakey);
102 rp.from = htonl(myself->vpn_ip);
103 rp.data.len = htons(rp.data.len);
104 rp.len = htons(rp.len);
107 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
108 ntohs(rp.len), cl->name, cl->hostname);
110 total_socket_out += ntohs(rp.len);
114 if((cl->flags | myself->flags) & TCPONLY)
115 return send_tcppacket(cl, (void*)&rp, ntohs(rp.len));
117 if((send(cl->socket, (char*)&rp, ntohs(rp.len), 0)) < 0)
119 syslog(LOG_ERR, _("Error sending packet to %s (%s): %m"),
120 cl->name, cl->hostname);
127 int xrecv(conn_list_t *cl, void *packet)
132 do_decrypt((real_packet_t*)packet, &vp, cl->datakey);
133 add_mac_addresses(&vp);
136 syslog(LOG_ERR, _("Receiving packet of %d bytes from %s (%s)"),
137 ((real_packet_t*)packet)->len, cl->name, cl->hostname);
139 if((lenin = write(tap_fd, &vp, vp.len + sizeof(vp.len))) < 0)
140 syslog(LOG_ERR, _("Can't write to tap device: %m"));
142 total_tap_out += lenin;
145 cl->last_ping_time = time(NULL);
151 add the given packet of size s to the
152 queue q, be it the send or receive queue
154 void add_queue(packet_queue_t **q, void *packet, size_t s)
158 e = xmalloc(sizeof(*e));
159 e->packet = xmalloc(s);
160 memcpy(e->packet, packet, s);
164 *q = xmalloc(sizeof(**q));
165 (*q)->head = (*q)->tail = NULL;
168 e->next = NULL; /* We insert at the tail */
170 if((*q)->tail) /* Do we have a tail? */
172 (*q)->tail->next = e;
173 e->prev = (*q)->tail;
175 else /* No tail -> no head too */
185 /* Remove a queue element */
186 void del_queue(packet_queue_t **q, queue_element_t *e)
191 if(e->next) /* There is a successor, so we are not tail */
193 if(e->prev) /* There is a predecessor, so we are not head */
195 e->next->prev = e->prev;
196 e->prev->next = e->next;
198 else /* We are head */
200 e->next->prev = NULL;
201 (*q)->head = e->next;
204 else /* We are tail (or all alone!) */
206 if(e->prev) /* We are not alone :) */
208 e->prev->next = NULL;
209 (*q)->tail = e->prev;
223 flush a queue by calling function for
224 each packet, and removing it when that
225 returned a zero exit code
227 void flush_queue(conn_list_t *cl, packet_queue_t **pq,
228 int (*function)(conn_list_t*,void*))
230 queue_element_t *p, *next = NULL;
232 for(p = (*pq)->head; p != NULL; )
236 if(!function(cl, p->packet))
243 syslog(LOG_DEBUG, _("Queue flushed"));
248 flush the send&recv queues
249 void because nothing goes wrong here, packets
250 remain in the queue if something goes wrong
252 void flush_queues(conn_list_t *cl)
258 syslog(LOG_DEBUG, _("Flushing send queue for %s (%s)"),
259 cl->name, cl->hostname);
260 flush_queue(cl, &(cl->sq), xsend);
266 syslog(LOG_DEBUG, _("Flushing receive queue for %s (%s)"),
267 cl->name, cl->hostname);
268 flush_queue(cl, &(cl->rq), xrecv);
274 send a packet to the given vpn ip.
276 int send_packet(ip_t to, vpn_packet_t *packet)
280 if((cl = lookup_conn(to)) == NULL)
284 syslog(LOG_NOTICE, _("Trying to look up %d.%d.%d.%d in connection list failed!"),
291 /* If we ourselves have indirectdata flag set, we should send only to our uplink! */
293 /* The next few lines will be obsoleted, if we are going indirect, matching subnet_t
294 should point to only our uplink as the recepient
297 if(myself->flags & EXPORTINDIRECTDATA)
299 for(cl = conn_list; cl != NULL && !cl->status.outgoing; cl = cl->next);
301 { /* No open outgoing connection has been found. */
303 syslog(LOG_NOTICE, _("There is no remote host I can send this packet to!"));
309 /* If indirectdata flag is set for the destination we just looked up,
310 * then real_ip is actually the vpn_ip of the gateway tincd
314 if(cl->flags & INDIRECTDATA)
317 syslog(LOG_NOTICE, _("Indirect packet to %s via %s"),
318 cl->name, cl->hostname);
319 if((cl = lookup_conn(cl->real_ip)) == NULL)
322 syslog(LOG_NOTICE, _("Indirect look up %d.%d.%d.%d in connection list failed!"), IP_ADDR_V(to));
324 /* Gateway tincd dead? Should we kill it? (GS) */
328 if(cl->flags & INDIRECTDATA) /* This should not happen */
331 syslog(LOG_NOTICE, _("Double indirection for %d.%d.%d.%d"), IP_ADDR_V(to));
336 if(my_key_expiry <= time(NULL))
339 if(!cl->status.dataopen)
340 if(setup_vpn_connection(cl) < 0)
342 syslog(LOG_ERR, _("Could not open UDP connection to %s (%s)"),
343 cl->name, cl->hostname);
347 if(!cl->status.validkey)
350 syslog(LOG_INFO, _("%s (%s) has no valid key, queueing packet"),
351 cl->name, cl->hostname);
352 add_queue(&(cl->sq), packet, packet->len + 2);
353 if(!cl->status.waitingforkey)
354 send_key_request(cl->vpn_ip); /* Keys should be sent to the host running the tincd */
358 if(!cl->status.active)
361 syslog(LOG_INFO, _("%s (%s) is not ready, queueing packet"),
362 cl->name, cl->hostname);
363 add_queue(&(cl->sq), packet, packet->len + 2);
364 return 0; /* We don't want to mess up, do we? */
367 /* can we send it? can we? can we? huh? */
369 return xsend(cl, packet);
373 open the local ethertap device
375 int setup_tap_fd(void)
378 const char *tapfname;
381 if((cfg = get_config_val(tapdevice)) == NULL)
382 tapfname = "/dev/tap0";
384 tapfname = cfg->data.ptr;
386 if((nfd = open(tapfname, O_RDWR | O_NONBLOCK)) < 0)
388 syslog(LOG_ERR, _("Could not open %s: %m"), tapfname);
398 set up the socket that we listen on for incoming
401 int setup_listen_meta_socket(int port)
404 struct sockaddr_in a;
408 if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
410 syslog(LOG_ERR, _("Creating metasocket failed: %m"));
414 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
416 syslog(LOG_ERR, _("setsockopt: %m"));
420 if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
422 syslog(LOG_ERR, _("setsockopt: %m"));
426 flags = fcntl(nfd, F_GETFL);
427 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
429 syslog(LOG_ERR, _("fcntl: %m"));
433 if((cfg = get_config_val(interface)))
435 if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, cfg->data.ptr, strlen(cfg->data.ptr)))
437 syslog(LOG_ERR, _("Unable to bind listen socket to interface %s: %m"), cfg->data.ptr);
442 memset(&a, 0, sizeof(a));
443 a.sin_family = AF_INET;
444 a.sin_port = htons(port);
446 if((cfg = get_config_val(interfaceip)))
447 a.sin_addr.s_addr = htonl(cfg->data.ip->ip);
449 a.sin_addr.s_addr = htonl(INADDR_ANY);
451 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
453 syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
459 syslog(LOG_ERR, _("listen: %m"));
467 setup the socket for incoming encrypted
470 int setup_vpn_in_socket(int port)
473 struct sockaddr_in a;
476 if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
478 syslog(LOG_ERR, _("Creating socket failed: %m"));
482 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
484 syslog(LOG_ERR, _("setsockopt: %m"));
488 flags = fcntl(nfd, F_GETFL);
489 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
491 syslog(LOG_ERR, _("fcntl: %m"));
495 memset(&a, 0, sizeof(a));
496 a.sin_family = AF_INET;
497 a.sin_port = htons(port);
498 a.sin_addr.s_addr = htonl(INADDR_ANY);
500 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
502 syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
510 setup an outgoing meta (tcp) socket
512 int setup_outgoing_meta_socket(conn_list_t *cl)
515 struct sockaddr_in a;
519 syslog(LOG_INFO, _("Trying to connect to %s"), cl->hostname);
521 if((cfg = get_config_val(upstreamport)) == NULL)
524 cl->port = cfg->data.val;
526 cl->meta_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
527 if(cl->meta_socket == -1)
529 syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
530 cl->hostname, cl->port);
534 a.sin_family = AF_INET;
535 a.sin_port = htons(cl->port);
536 a.sin_addr.s_addr = htonl(cl->real_ip);
538 if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
540 syslog(LOG_ERR, _("%s port %hd: %m"), cl->hostname, cl->port);
544 flags = fcntl(cl->meta_socket, F_GETFL);
545 if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
547 syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
548 cl->hostname, cl->port);
553 syslog(LOG_INFO, _("Connected to %s port %hd"),
554 cl->hostname, cl->port);
560 setup an outgoing connection. It's not
561 necessary to also open an udp socket as
562 well, because the other host will initiate
563 an authentication sequence during which
564 we will do just that.
566 int setup_outgoing_connection(char *hostname)
571 if(!(h = gethostbyname(hostname)))
573 syslog(LOG_ERR, _("Error looking up `%s': %m"), hostname);
577 ncn = new_conn_list();
578 ncn->real_ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
579 ncn->hostname = hostlookup(htonl(ncn->real_ip));
581 if(setup_outgoing_meta_socket(ncn) < 0)
583 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
585 free_conn_element(ncn);
589 ncn->status.meta = 1;
590 ncn->status.outgoing = 1;
591 ncn->next = conn_list;
598 set up the local sockets (listen only)
600 int setup_myself(void)
604 myself = new_conn_list();
606 if(!(cfg = get_config_val(myvpnip)))
608 syslog(LOG_ERR, _("No value for my VPN IP given"));
612 myself->vpn_ip = cfg->data.ip->ip;
613 myself->hostname = hostlookup(htonl(myself->vpn_ip));
614 myself->vpn_mask = cfg->data.ip->mask;
617 if(!(cfg = get_config_val(tincname)))
618 asprintf(&(myself->name), IP_ADDR_S, IP_ADDR_V(myself->vpn_ip));
620 myself->name = (char*)cfg->data.val;
622 if(!(cfg = get_config_val(listenport)))
625 myself->port = cfg->data.val;
627 if((cfg = get_config_val(indirectdata)))
628 if(cfg->data.val == stupid_true)
629 myself->flags |= EXPORTINDIRECTDATA;
631 if((cfg = get_config_val(tcponly)))
632 if(cfg->data.val == stupid_true)
633 myself->flags |= TCPONLY;
635 if((myself->meta_socket = setup_listen_meta_socket(myself->port)) < 0)
637 syslog(LOG_ERR, _("Unable to set up a listening socket"));
641 if((myself->socket = setup_vpn_in_socket(myself->port)) < 0)
643 syslog(LOG_ERR, _("Unable to set up an incoming vpn data socket"));
644 close(myself->meta_socket);
648 myself->status.active = 1;
650 syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
656 sigalrm_handler(int a)
660 cfg = get_next_config_val(upstreamip, upstreamindex++);
664 if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
666 signal(SIGALRM, SIG_IGN);
669 cfg = get_next_config_val(upstreamip, upstreamindex++); /* Or else we try the next ConnectTo line */
672 signal(SIGALRM, sigalrm_handler);
674 seconds_till_retry += 5;
675 if(seconds_till_retry > MAXTIMEOUT) /* Don't wait more than MAXTIMEOUT seconds. */
676 seconds_till_retry = MAXTIMEOUT;
677 syslog(LOG_ERR, _("Still failed to connect to other, will retry in %d seconds"),
679 alarm(seconds_till_retry);
684 setup all initial network connections
686 int setup_network_connections(void)
690 if((cfg = get_config_val(pingtimeout)) == NULL)
693 timeout = cfg->data.val;
695 if(setup_tap_fd() < 0)
698 if(setup_myself() < 0)
701 if((cfg = get_next_config_val(upstreamip, upstreamindex++)) == NULL)
702 /* No upstream IP given, we're listen only. */
707 if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
709 cfg = get_next_config_val(upstreamip, upstreamindex++); /* Or else we try the next ConnectTo line */
712 signal(SIGALRM, sigalrm_handler);
714 seconds_till_retry = MAXTIMEOUT;
715 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), seconds_till_retry);
716 alarm(seconds_till_retry);
722 close all open network connections
724 void close_network_connections(void)
728 for(p = conn_list; p != NULL; p = p->next)
730 if(p->status.dataopen)
732 shutdown(p->socket, 0); /* No more receptions */
738 shutdown(p->meta_socket, 0); /* No more receptions */
739 close(p->meta_socket);
744 if(myself->status.active)
746 close(myself->meta_socket);
747 close(myself->socket);
753 syslog(LOG_NOTICE, _("Terminating"));
759 create a data (udp) socket
761 int setup_vpn_connection(conn_list_t *cl)
764 struct sockaddr_in a;
767 syslog(LOG_DEBUG, _("Opening UDP socket to %s"), cl->hostname);
769 nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
772 syslog(LOG_ERR, _("Creating UDP socket failed: %m"));
776 a.sin_family = AF_INET;
777 a.sin_port = htons(cl->port);
778 a.sin_addr.s_addr = htonl(cl->real_ip);
780 if(connect(nfd, (struct sockaddr *)&a, sizeof(a)) == -1)
782 syslog(LOG_ERR, _("Connecting to %s port %d failed: %m"),
783 cl->hostname, cl->port);
787 flags = fcntl(nfd, F_GETFL);
788 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
790 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, nfd,
791 cl->name, cl->hostname);
796 cl->status.dataopen = 1;
802 handle an incoming tcp connect call and open
805 conn_list_t *create_new_connection(int sfd)
808 struct sockaddr_in ci;
809 int len = sizeof(ci);
813 if(getpeername(sfd, &ci, &len) < 0)
815 syslog(LOG_ERR, _("Error: getpeername: %m"));
819 p->real_ip = ntohl(ci.sin_addr.s_addr);
820 p->hostname = hostlookup(ci.sin_addr.s_addr);
821 p->meta_socket = sfd;
824 p->last_ping_time = time(NULL);
828 syslog(LOG_NOTICE, _("Connection from %s port %d"),
829 p->hostname, htons(ci.sin_port));
831 if(send_basic_info(p) < 0)
833 free_conn_element(p);
841 put all file descriptors in an fd_set array
843 void build_fdset(fd_set *fs)
849 for(p = conn_list; p != NULL; p = p->next)
852 FD_SET(p->meta_socket, fs);
853 if(p->status.dataopen)
854 FD_SET(p->socket, fs);
857 FD_SET(myself->meta_socket, fs);
858 FD_SET(myself->socket, fs);
864 receive incoming data from the listening
865 udp socket and write it to the ethertap
866 device after being decrypted
868 int handle_incoming_vpn_data(conn_list_t *cl)
872 int x, l = sizeof(x);
875 if(getsockopt(cl->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
877 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"),
878 __FILE__, __LINE__, cl->socket,
879 cl->name, cl->hostname);
884 syslog(LOG_ERR, _("Incoming data socket error for %s (%s): %s"),
885 cl->name, cl->hostname, strerror(x));
890 lenin = recvfrom(cl->socket, &rp, MTU, 0, NULL, NULL);
893 syslog(LOG_ERR, _("Receiving packet from %s (%s) failed: %m"),
894 cl->name, cl->hostname);
897 total_socket_in += lenin;
899 rp.data.len = ntohs(rp.data.len);
900 rp.len = ntohs(rp.len);
901 rp.from = ntohl(rp.from);
905 f = lookup_conn(rp.from);
908 syslog(LOG_ERR, _("Got packet from %s (%s) with unknown origin %d.%d.%d.%d?"),
909 cl->name, cl->hostname, IP_ADDR_V(rp.from));
913 if(f->status.validkey)
917 add_queue(&(f->rq), &rp, rp.len);
918 if(!cl->status.waitingforkey)
919 send_key_request(rp.from);
922 if(my_key_expiry <= time(NULL))
930 terminate a connection and notify the other
931 end before closing the sockets
933 void terminate_connection(conn_list_t *cl)
938 if(cl->status.remove)
942 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
943 cl->name, cl->hostname);
945 if(cl->status.timeout)
947 /* else if(!cl->status.termreq)
954 close(cl->meta_socket);
956 cl->status.remove = 1;
958 /* If this cl isn't active, don't send any DEL_HOSTs. */
959 if(cl->status.active)
960 notify_others(cl,NULL,send_del_host);
963 /* Find all connections that were lost because they were behind cl
964 (the connection that was dropped). */
966 for(p = conn_list; p != NULL; p = p->next)
968 if((p->nexthop == cl) && (p != cl))
970 if(cl->status.active && p->status.active)
971 notify_others(p,cl,send_del_host);
974 p->status.active = 0;
975 p->status.remove = 1;
979 cl->status.active = 0;
981 if(cl->status.outgoing)
983 signal(SIGALRM, sigalrm_handler);
984 seconds_till_retry = 5;
985 alarm(seconds_till_retry);
986 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
992 Check if the other end is active.
993 If we have sent packets, but didn't receive any,
994 then possibly the other end is dead. We send a
995 PING request over the meta connection. If the other
996 end does not reply in time, we consider them dead
997 and close the connection.
999 int check_dead_connections(void)
1005 for(p = conn_list; p != NULL; p = p->next)
1007 if(p->status.remove)
1009 if(p->status.active && p->status.meta)
1011 if(p->last_ping_time + timeout < now)
1013 if(p->status.pinged && !p->status.got_pong)
1016 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
1017 p->name, p->hostname);
1018 p->status.timeout = 1;
1019 terminate_connection(p);
1021 else if(p->want_ping)
1024 p->last_ping_time = now;
1025 p->status.pinged = 1;
1026 p->status.got_pong = 0;
1036 accept a new tcp connect and create a
1039 int handle_new_meta_connection(conn_list_t *cl)
1042 struct sockaddr client;
1043 int nfd, len = sizeof(client);
1045 if((nfd = accept(cl->meta_socket, &client, &len)) < 0)
1047 syslog(LOG_ERR, _("Accepting a new connection failed: %m"));
1051 if(!(ncn = create_new_connection(nfd)))
1055 syslog(LOG_NOTICE, _("Closed attempted connection"));
1059 ncn->status.meta = 1;
1060 ncn->next = conn_list;
1067 check all connections to see if anything
1068 happened on their sockets
1070 void check_network_activity(fd_set *f)
1073 int x, l = sizeof(x);
1075 for(p = conn_list; p != NULL; p = p->next)
1077 if(p->status.remove)
1080 if(p->status.dataopen)
1081 if(FD_ISSET(p->socket, f))
1084 The only thing that can happen to get us here is apparently an
1085 error on this outgoing(!) UDP socket that isn't immediate (i.e.
1086 something that will not trigger an error directly on send()).
1087 I've once got here when it said `No route to host'.
1089 getsockopt(p->socket, SOL_SOCKET, SO_ERROR, &x, &l);
1090 syslog(LOG_ERR, _("Outgoing data socket error for %s (%s): %s"),
1091 p->name, p->hostname, strerror(x));
1092 terminate_connection(p);
1097 if(FD_ISSET(p->meta_socket, f))
1098 if(receive_meta(p) < 0)
1100 terminate_connection(p);
1105 if(FD_ISSET(myself->socket, f))
1106 handle_incoming_vpn_data(myself);
1108 if(FD_ISSET(myself->meta_socket, f))
1109 handle_new_meta_connection(myself);
1114 read, encrypt and send data that is
1115 available through the ethertap device
1117 void handle_tap_input(void)
1121 int ether_type, lenin;
1123 memset(&vp, 0, sizeof(vp));
1124 if((lenin = read(tap_fd, &vp, MTU)) <= 0)
1126 syslog(LOG_ERR, _("Error while reading from tapdevice: %m"));
1130 total_tap_in += lenin;
1132 ether_type = ntohs(*((unsigned short*)(&vp.data[12])));
1133 if(ether_type != 0x0800)
1136 syslog(LOG_INFO, _("Non-IP ethernet frame %04x from %02x:%02x:%02x:%02x:%02x:%02x"), ether_type, MAC_ADDR_V(vp.data[6]));
1143 syslog(LOG_INFO, _("Dropping short packet from %02x:%02x:%02x:%02x:%02x:%02x"), MAC_ADDR_V(vp.data[6]));
1147 from = ntohl(*((unsigned long*)(&vp.data[26])));
1148 to = ntohl(*((unsigned long*)(&vp.data[30])));
1150 vp.len = (length_t)lenin - 2;
1152 strip_mac_addresses(&vp);
1154 send_packet(to, &vp);
1159 this is where it all happens...
1161 void main_loop(void)
1166 time_t last_ping_check;
1168 last_ping_check = time(NULL);
1172 tv.tv_sec = timeout;
1178 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
1180 if(errno != EINTR) /* because of alarm */
1182 syslog(LOG_ERR, _("Error while waiting for input: %m"));
1191 syslog(LOG_INFO, _("Rereading configuration file"));
1192 close_network_connections();
1194 if(read_config_file(configfilename))
1196 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
1200 setup_network_connections();
1204 if(last_ping_check + timeout < time(NULL))
1205 /* Let's check if everybody is still alive */
1207 check_dead_connections();
1208 last_ping_check = time(NULL);
1213 check_network_activity(&fset);
1215 /* local tap data */
1216 if(FD_ISSET(tap_fd, &fset))