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.41 2000/10/20 15:34:35 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>
42 #include LINUX_IF_TUN_H
60 int total_tap_out = 0;
61 int total_socket_in = 0;
62 int total_socket_out = 0;
64 int upstreamindex = 0;
65 static int seconds_till_retry;
70 strip off the MAC adresses of an ethernet frame
72 void strip_mac_addresses(vpn_packet_t *p)
75 memmove(p->data, p->data + 12, p->len -= 12);
80 reassemble MAC addresses
82 void add_mac_addresses(vpn_packet_t *p)
85 memcpy(p->data + 12, p->data, p->len);
87 p->data[0] = p->data[6] = 0xfe;
88 p->data[1] = p->data[7] = 0xfd;
89 /* Really evil pointer stuff just below! */
90 *((ip_t*)(&p->data[2])) = (ip_t)(htonl(myself->address));
91 *((ip_t*)(&p->data[8])) = *((ip_t*)(&p->data[26]));
95 int xsend(conn_list_t *cl, vpn_packet_t *inpkt)
100 outpkt.len = inpkt->len;
101 EVP_EncryptInit(cl->cipher_pktctx, cl->cipher_pkttype, cl->cipher_pktkey, NULL);
102 EVP_EncryptUpdate(cl->cipher_pktctx, outpkt.data, &outlen, inpkt->data, inpkt->len);
103 EVP_EncryptFinal(cl->cipher_pktctx, outpkt.data + outlen, &outpad);
107 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
108 outlen, cl->name, cl->hostname);
110 total_socket_out += outlen;
114 if((send(cl->socket, (char *) &(outpkt.len), outlen + 2, 0)) < 0)
116 syslog(LOG_ERR, _("Error sending packet to %s (%s): %m"),
117 cl->name, cl->hostname);
124 int xrecv(vpn_packet_t *inpkt)
130 syslog(LOG_ERR, _("Receiving packet of %d bytes"),
133 outpkt.len = inpkt->len;
134 EVP_DecryptInit(myself->cipher_pktctx, myself->cipher_pkttype, myself->cipher_pktkey, NULL);
135 EVP_DecryptUpdate(myself->cipher_pktctx, outpkt.data, &outlen, inpkt->data, inpkt->len);
136 /* FIXME: grok DecryptFinal
137 EVP_DecryptFinal(myself->cipher_pktctx, outpkt.data + outlen, &outpad);
140 add_mac_addresses(&outpkt);
142 if(write(tap_fd, outpkt.data, outpkt.len) < 0)
143 syslog(LOG_ERR, _("Can't write to tap device: %m"));
145 total_tap_out += outpkt.len;
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_list_ipv4(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 /* FIXME - check for indirection and reprogram it The Right Way(tm) this time. */
295 if(!cl->status.dataopen)
296 if(setup_vpn_connection(cl) < 0)
298 syslog(LOG_ERR, _("Could not open UDP connection to %s (%s)"),
299 cl->name, cl->hostname);
303 if(!cl->status.validkey)
306 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
307 cl->name, cl->hostname);
308 add_queue(&(cl->sq), packet, packet->len + 2);
309 if(!cl->status.waitingforkey)
310 send_req_key(myself, cl); /* Keys should be sent to the host running the tincd */
314 if(!cl->status.active)
317 syslog(LOG_INFO, _("%s (%s) is not ready, queueing packet"),
318 cl->name, cl->hostname);
319 add_queue(&(cl->sq), packet, packet->len + 2);
320 return 0; /* We don't want to mess up, do we? */
323 /* can we send it? can we? can we? huh? */
325 return xsend(cl, packet);
329 open the local ethertap device
331 int setup_tap_fd(void)
334 const char *tapfname;
341 if((cfg = get_config_val(config, tapdevice)))
342 tapfname = cfg->data.ptr;
345 tapfname = "/dev/misc/net/tun";
347 tapfname = "/dev/tap0";
350 if((nfd = open(tapfname, O_RDWR | O_NONBLOCK)) < 0)
352 syslog(LOG_ERR, _("Could not open %s: %m"), tapfname);
361 /* Ok now check if this is an old ethertap or a new tun/tap thingie */
362 memset(&ifr, 0, sizeof(ifr));
364 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
366 strncpy(ifr.ifr_name, netname, IFNAMSIZ);
368 if (!ioctl(tap_fd, TUNSETIFF, (void *) &ifr))
370 syslog(LOG_INFO, _("%s is a new style tun/tap device"), tapfname);
372 if((cfg = get_config_val(config, tapsubnet)) == NULL)
373 syslog(LOG_INFO, _("tun/tap device will be left unconfigured"));
375 /* Setup inetaddr/netmask etc */;
384 set up the socket that we listen on for incoming
387 int setup_listen_meta_socket(int port)
390 struct sockaddr_in a;
394 if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
396 syslog(LOG_ERR, _("Creating metasocket failed: %m"));
400 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
402 syslog(LOG_ERR, _("setsockopt: %m"));
406 if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
408 syslog(LOG_ERR, _("setsockopt: %m"));
412 flags = fcntl(nfd, F_GETFL);
413 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
415 syslog(LOG_ERR, _("fcntl: %m"));
419 if((cfg = get_config_val(config, interface)))
421 if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, cfg->data.ptr, strlen(cfg->data.ptr)))
423 syslog(LOG_ERR, _("Unable to bind listen socket to interface %s: %m"), cfg->data.ptr);
428 memset(&a, 0, sizeof(a));
429 a.sin_family = AF_INET;
430 a.sin_port = htons(port);
432 if((cfg = get_config_val(config, interfaceip)))
433 a.sin_addr.s_addr = htonl(cfg->data.ip->ip);
435 a.sin_addr.s_addr = htonl(INADDR_ANY);
437 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
439 syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
445 syslog(LOG_ERR, _("listen: %m"));
453 setup the socket for incoming encrypted
456 int setup_vpn_in_socket(int port)
459 struct sockaddr_in a;
462 if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
464 syslog(LOG_ERR, _("Creating socket failed: %m"));
468 if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
470 syslog(LOG_ERR, _("setsockopt: %m"));
474 flags = fcntl(nfd, F_GETFL);
475 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
477 syslog(LOG_ERR, _("fcntl: %m"));
481 memset(&a, 0, sizeof(a));
482 a.sin_family = AF_INET;
483 a.sin_port = htons(port);
484 a.sin_addr.s_addr = htonl(INADDR_ANY);
486 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
488 syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
496 setup an outgoing meta (tcp) socket
498 int setup_outgoing_meta_socket(conn_list_t *cl)
501 struct sockaddr_in a;
505 syslog(LOG_INFO, _("Trying to connect to %s"), cl->hostname);
507 if((cfg = get_config_val(cl->config, port)) == NULL)
510 cl->port = cfg->data.val;
512 cl->meta_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
513 if(cl->meta_socket == -1)
515 syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
516 cl->hostname, cl->port);
520 a.sin_family = AF_INET;
521 a.sin_port = htons(cl->port);
522 a.sin_addr.s_addr = htonl(cl->address);
524 if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
526 syslog(LOG_ERR, _("%s port %hd: %m"), cl->hostname, cl->port);
530 flags = fcntl(cl->meta_socket, F_GETFL);
531 if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
533 syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
534 cl->hostname, cl->port);
539 syslog(LOG_INFO, _("Connected to %s port %hd"),
540 cl->hostname, cl->port);
548 setup an outgoing connection. It's not
549 necessary to also open an udp socket as
550 well, because the other host will initiate
551 an authentication sequence during which
552 we will do just that.
554 int setup_outgoing_connection(char *name)
562 syslog(LOG_ERR, _("Invalid name for outgoing connection"));
566 ncn = new_conn_list();
567 asprintf(&ncn->name, "%s", name);
569 if(read_host_config(ncn))
571 syslog(LOG_ERR, _("Error reading host configuration file for %s"));
576 if(!(cfg = get_config_val(ncn->config, address)))
578 syslog(LOG_ERR, _("No address specified for %s"));
583 if(!(h = gethostbyname(cfg->data.ptr)))
585 syslog(LOG_ERR, _("Error looking up `%s': %m"), cfg->data.ptr);
590 ncn->address = ntohl(*((ip_t*)(h->h_addr_list[0])));
591 ncn->hostname = hostlookup(htonl(ncn->address));
593 if(setup_outgoing_meta_socket(ncn) < 0)
595 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
601 ncn->status.outgoing = 1;
602 ncn->buffer = xmalloc(MAXBUFSIZE);
604 ncn->last_ping_time = time(NULL);
615 set up the local sockets (listen only)
617 int setup_myself(void)
621 myself = new_conn_list();
623 asprintf(&myself->hostname, "MYSELF"); /* FIXME? Do hostlookup on ourselves? */
625 myself->protocol_version = PROT_CURRENT;
627 if(!(cfg = get_config_val(config, tincname))) /* Not acceptable */
629 syslog(LOG_ERR, _("Name for tinc daemon required!"));
633 asprintf(&myself->name, "%s", (char*)cfg->data.val);
635 if(check_id(myself->name))
637 syslog(LOG_ERR, _("Invalid name for myself!"));
641 if(!(cfg = get_config_val(config, privatekey)))
643 syslog(LOG_ERR, _("Private key for tinc daemon required!"));
648 myself->rsa_key = RSA_new();
649 BN_hex2bn(&myself->rsa_key->d, cfg->data.ptr);
650 BN_hex2bn(&myself->rsa_key->e, "FFFF");
653 if(read_host_config(myself))
655 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
659 if(!(cfg = get_config_val(myself->config, publickey)))
661 syslog(LOG_ERR, _("Public key for tinc daemon required!"));
666 BN_hex2bn(&myself->rsa_key->n, cfg->data.ptr);
669 if(RSA_check_key(myself->rsa_key) != 1)
671 syslog(LOG_ERR, _("Invalid public/private keypair!"));
675 if(!(cfg = get_config_val(myself->config, port)))
678 myself->port = cfg->data.val;
680 if((cfg = get_config_val(myself->config, indirectdata)))
681 if(cfg->data.val == stupid_true)
682 myself->flags |= EXPORTINDIRECTDATA;
684 if((cfg = get_config_val(myself->config, tcponly)))
685 if(cfg->data.val == stupid_true)
686 myself->flags |= TCPONLY;
688 if((myself->meta_socket = setup_listen_meta_socket(myself->port)) < 0)
690 syslog(LOG_ERR, _("Unable to set up a listening socket!"));
694 if((myself->socket = setup_vpn_in_socket(myself->port)) < 0)
696 syslog(LOG_ERR, _("Unable to set up an incoming vpn data socket!"));
697 close(myself->meta_socket);
701 myself->status.active = 1;
703 syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
709 sigalrm_handler(int a)
713 cfg = get_next_config_val(config, connectto, upstreamindex++);
715 if(!upstreamindex && !cfg)
716 /* No upstream IP given, we're listen only. */
721 if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
723 signal(SIGALRM, SIG_IGN);
726 cfg = get_next_config_val(config, connectto, upstreamindex++); /* Or else we try the next ConnectTo line */
729 signal(SIGALRM, sigalrm_handler);
731 seconds_till_retry += 5;
732 if(seconds_till_retry > MAXTIMEOUT) /* Don't wait more than MAXTIMEOUT seconds. */
733 seconds_till_retry = MAXTIMEOUT;
734 syslog(LOG_ERR, _("Still failed to connect to other, will retry in %d seconds"),
736 alarm(seconds_till_retry);
741 setup all initial network connections
743 int setup_network_connections(void)
747 if((cfg = get_config_val(config, pingtimeout)) == NULL)
750 timeout = cfg->data.val;
752 if(setup_tap_fd() < 0)
755 if(setup_myself() < 0)
758 if((cfg = get_next_config_val(config, connectto, upstreamindex++)) == NULL)
759 /* No upstream IP given, we're listen only. */
764 if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
766 cfg = get_next_config_val(config, connectto, upstreamindex++); /* Or else we try the next ConnectTo line */
769 signal(SIGALRM, sigalrm_handler);
771 seconds_till_retry = MAXTIMEOUT;
772 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), seconds_till_retry);
773 alarm(seconds_till_retry);
779 close all open network connections
781 void close_network_connections(void)
785 for(p = conn_list; p != NULL; p = p->next)
787 if(p->status.dataopen)
789 shutdown(p->socket, 0); /* No more receptions */
795 shutdown(p->meta_socket, 0); /* No more receptions */
796 close(p->meta_socket);
801 if(myself->status.active)
803 close(myself->meta_socket);
804 close(myself->socket);
810 syslog(LOG_NOTICE, _("Terminating"));
816 create a data (udp) socket
818 int setup_vpn_connection(conn_list_t *cl)
821 struct sockaddr_in a;
824 syslog(LOG_DEBUG, _("Opening UDP socket to %s"), cl->hostname);
826 nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
829 syslog(LOG_ERR, _("Creating UDP socket failed: %m"));
833 a.sin_family = AF_INET;
834 a.sin_port = htons(cl->port);
835 a.sin_addr.s_addr = htonl(cl->address);
837 if(connect(nfd, (struct sockaddr *)&a, sizeof(a)) == -1)
839 syslog(LOG_ERR, _("Connecting to %s port %d failed: %m"),
840 cl->hostname, cl->port);
844 flags = fcntl(nfd, F_GETFL);
845 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
847 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, nfd,
848 cl->name, cl->hostname);
853 cl->status.dataopen = 1;
859 handle an incoming tcp connect call and open
862 conn_list_t *create_new_connection(int sfd)
865 struct sockaddr_in ci;
866 int len = sizeof(ci);
870 if(getpeername(sfd, &ci, &len) < 0)
872 syslog(LOG_ERR, _("Error: getpeername: %m"));
877 p->address = ntohl(ci.sin_addr.s_addr);
878 p->hostname = hostlookup(ci.sin_addr.s_addr);
879 p->meta_socket = sfd;
881 p->buffer = xmalloc(MAXBUFSIZE);
883 p->last_ping_time = time(NULL);
887 syslog(LOG_NOTICE, _("Connection from %s port %d"),
888 p->hostname, htons(ci.sin_port));
890 p->allow_request = ID;
896 put all file descriptors in an fd_set array
898 void build_fdset(fd_set *fs)
904 for(p = conn_list; p != NULL; p = p->next)
907 FD_SET(p->meta_socket, fs);
908 if(p->status.dataopen)
909 FD_SET(p->socket, fs);
912 FD_SET(myself->meta_socket, fs);
913 FD_SET(myself->socket, fs);
919 receive incoming data from the listening
920 udp socket and write it to the ethertap
921 device after being decrypted
923 int handle_incoming_vpn_data()
927 int x, l = sizeof(x);
929 if(getsockopt(myself->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
931 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"),
932 __FILE__, __LINE__, myself->socket);
937 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
941 if(recvfrom(myself->socket, (char *) &(pkt.len), MTU, 0, NULL, NULL) <= 0)
943 syslog(LOG_ERR, _("Receiving packet failed: %m"));
952 terminate a connection and notify the other
953 end before closing the sockets
955 void terminate_connection(conn_list_t *cl)
960 if(cl->status.remove)
964 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
965 cl->name, cl->hostname);
970 close(cl->meta_socket);
972 cl->status.remove = 1;
974 /* If this cl isn't active, don't send any DEL_HOSTs. */
976 /* FIXME: reprogram this.
977 if(cl->status.active)
978 notify_others(cl,NULL,send_del_host);
982 /* Find all connections that were lost because they were behind cl
983 (the connection that was dropped). */
985 for(p = conn_list; p != NULL; p = p->next)
987 if((p->nexthop == cl) && (p != cl))
989 if(cl->status.active && p->status.active)
990 /* FIXME: reprogram this
991 notify_others(p,cl,send_del_host);
995 p->status.active = 0;
996 p->status.remove = 1;
1000 cl->status.active = 0;
1002 if(cl->status.outgoing)
1004 signal(SIGALRM, sigalrm_handler);
1005 seconds_till_retry = 5;
1006 alarm(seconds_till_retry);
1007 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
1013 Check if the other end is active.
1014 If we have sent packets, but didn't receive any,
1015 then possibly the other end is dead. We send a
1016 PING request over the meta connection. If the other
1017 end does not reply in time, we consider them dead
1018 and close the connection.
1020 int check_dead_connections(void)
1026 for(p = conn_list; p != NULL; p = p->next)
1028 if(p->status.remove)
1030 if(p->status.active && p->status.meta)
1032 if(p->last_ping_time + timeout < now)
1034 if(p->status.pinged && !p->status.got_pong)
1037 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
1038 p->name, p->hostname);
1039 p->status.timeout = 1;
1040 terminate_connection(p);
1042 else if(p->want_ping)
1045 p->last_ping_time = now;
1046 p->status.pinged = 1;
1047 p->status.got_pong = 0;
1057 accept a new tcp connect and create a
1060 int handle_new_meta_connection()
1063 struct sockaddr client;
1064 int nfd, len = sizeof(client);
1066 if((nfd = accept(myself->meta_socket, &client, &len)) < 0)
1068 syslog(LOG_ERR, _("Accepting a new connection failed: %m"));
1072 if(!(ncn = create_new_connection(nfd)))
1076 syslog(LOG_NOTICE, _("Closed attempted connection"));
1080 ncn->status.meta = 1;
1081 ncn->next = conn_list;
1088 check all connections to see if anything
1089 happened on their sockets
1091 void check_network_activity(fd_set *f)
1094 int x, l = sizeof(x);
1096 for(p = conn_list; p != NULL; p = p->next)
1098 if(p->status.remove)
1101 if(p->status.dataopen)
1102 if(FD_ISSET(p->socket, f))
1105 The only thing that can happen to get us here is apparently an
1106 error on this outgoing(!) UDP socket that isn't immediate (i.e.
1107 something that will not trigger an error directly on send()).
1108 I've once got here when it said `No route to host'.
1110 getsockopt(p->socket, SOL_SOCKET, SO_ERROR, &x, &l);
1111 syslog(LOG_ERR, _("Outgoing data socket error for %s (%s): %s"),
1112 p->name, p->hostname, strerror(x));
1113 terminate_connection(p);
1118 if(FD_ISSET(p->meta_socket, f))
1119 if(receive_meta(p) < 0)
1121 terminate_connection(p);
1126 if(FD_ISSET(myself->socket, f))
1127 handle_incoming_vpn_data();
1129 if(FD_ISSET(myself->meta_socket, f))
1130 handle_new_meta_connection();
1135 read, encrypt and send data that is
1136 available through the ethertap device
1138 void handle_tap_input(void)
1142 int ether_type, lenin;
1144 memset(&vp, 0, sizeof(vp));
1148 if((lenin = read(tap_fd, vp.data, MTU)) <= 0)
1150 syslog(LOG_ERR, _("Error while reading from tapdevice: %m"));
1157 if((lenin = read(tap_fd, &vp, MTU)) <= 0)
1159 syslog(LOG_ERR, _("Error while reading from tapdevice: %m"));
1165 total_tap_in += lenin;
1167 ether_type = ntohs(*((unsigned short*)(&vp.data[12])));
1168 if(ether_type != 0x0800)
1171 syslog(LOG_INFO, _("Non-IP ethernet frame %04x from %02x:%02x:%02x:%02x:%02x:%02x"), ether_type, MAC_ADDR_V(vp.data[6]));
1178 syslog(LOG_INFO, _("Dropping short packet from %02x:%02x:%02x:%02x:%02x:%02x"), MAC_ADDR_V(vp.data[6]));
1182 from = ntohl(*((unsigned long*)(&vp.data[26])));
1183 to = ntohl(*((unsigned long*)(&vp.data[30])));
1185 send_packet(to, &vp);
1190 this is where it all happens...
1192 void main_loop(void)
1197 time_t last_ping_check;
1199 last_ping_check = time(NULL);
1203 tv.tv_sec = timeout;
1209 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
1211 if(errno != EINTR) /* because of alarm */
1213 syslog(LOG_ERR, _("Error while waiting for input: %m"));
1221 /* FIXME: reprogram this.
1223 syslog(LOG_INFO, _("Rereading configuration file"));
1224 close_network_connections();
1226 if(read_config_file(&config, configfilename))
1228 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
1232 setup_network_connections();
1237 if(last_ping_check + timeout < time(NULL))
1238 /* Let's check if everybody is still alive */
1240 check_dead_connections();
1241 last_ping_check = time(NULL);
1246 check_network_activity(&fset);
1248 /* local tap data */
1249 if(FD_ISSET(tap_fd, &fset))