- }
-
- last_ping_time = time(NULL);
-cp
- return 0;
-}
-
-/*
- end all connections that did not respond
- to the ping probe in time
-*/
-int check_dead_connections(void)
-{
- conn_list_t *p;
-cp
- for(p = conn_list; p != NULL; p = p->next)
- {
- if(p->status.remove)
- continue;
- if(p->status.active && p->status.meta && p->status.pinged && !p->status.got_pong)
- {
- syslog(LOG_INFO, "%s (" IP_ADDR_S ") didn't respond to ping",
- p->hostname, IP_ADDR_V(p->vpn_ip));
- p->status.timeout = 1;
- terminate_connection(p);
- }
- }
-cp
- return 0;
-}
-
-/*
- accept a new tcp connect and create a
- new connection
-*/
-int handle_new_meta_connection(conn_list_t *cl)
-{
- conn_list_t *ncn;
- struct sockaddr client;
- int nfd, len = sizeof(client);
-cp
- if((nfd = accept(cl->meta_socket, &client, &len)) < 0)
- {
- syslog(LOG_ERR, "Accepting a new connection failed: %m");
- return -1;
- }
-
- if((ncn = create_new_connection(nfd)) == NULL)
- {
- shutdown(nfd, 2);
- close(nfd);
- syslog(LOG_NOTICE, "Closed attempted connection.");
- return 0;
- }
-
- ncn->status.meta = 1;
- ncn->next = conn_list;
- conn_list = ncn;
-cp
- return 0;
-}
-
-/*
- dispatch any incoming meta requests
-*/
-int handle_incoming_meta_data(conn_list_t *cl)
-{
- int x, l = sizeof(x);
- int request, oldlen, i;
- int lenin = 0;
-cp
- if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
- {
- syslog(LOG_ERR, "This is a bug: %s:%d: %d:%m", __FILE__, __LINE__, cl->meta_socket);
- return -1;
- }
- if(x)
- {
- syslog(LOG_ERR, "Metadata socket error: %s", sys_errlist[x]);
- return -1;
- }
-
- if(cl->buflen >= MAXBUFSIZE)
- {
- syslog(LOG_ERR, "Metadata read buffer full! Discarding contents.");
- cl->buflen = 0;
- }
-
- lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
-
- if(lenin<=0)
- {
- syslog(LOG_ERR, "Metadata socket read error: %m");
- return -1;
- }
-
- oldlen = cl->buflen;
- cl->buflen += lenin;
-
- for(;;)
- {
- cl->reqlen = 0;
-
- for(i = oldlen; i < cl->buflen; i++)
- {
- if(cl->buffer[i] == '\n')
- {
- cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
- cl->reqlen = i + 1;
- break;
- }
- }
-
- if(cl->reqlen)
- {
- if(sscanf(cl->buffer, "%d", &request) == 1)
- {
- if(request_handlers[request] == NULL)
- {
- syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
- return 0;
- }
-
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "Got request: %s", cl->buffer);
-
- request_handlers[request](cl);
- }
- else
- {
- syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
- }
-
- cl->buflen -= cl->reqlen;
- memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
- oldlen = 0;
- }
- else
- {
- break;
- }
- }
-cp
- return 0;
-}
-
-/*
- check all connections to see if anything
- happened on their sockets
-*/
-void check_network_activity(fd_set *f)
-{
- conn_list_t *p;
- int x, l = sizeof(x);
-cp
- for(p = conn_list; p != NULL; p = p->next)
- {
- if(p->status.remove)
- continue;
-
- if(p->status.dataopen)
- if(FD_ISSET(p->socket, f))
- {
- /*
- The only thing that can happen to get us here is apparently an
- error on this outgoing(!) UDP socket that isn't immediate (i.e.
- something that will not trigger an error directly on send()).
- I've once got here when it said `No route to host'.
- */
- getsockopt(p->socket, SOL_SOCKET, SO_ERROR, &x, &l);
- syslog(LOG_ERR, "Outgoing data socket error: %s", sys_errlist[x]);
- terminate_connection(p);
- return;
- }
-
- if(p->status.meta)
- if(FD_ISSET(p->meta_socket, f))
- if(handle_incoming_meta_data(p) < 0)
- {
- terminate_connection(p);
- return;
- }
- }
-
- if(FD_ISSET(myself->socket, f))
- handle_incoming_vpn_data(myself);
-
- if(FD_ISSET(myself->meta_socket, f))
- handle_new_meta_connection(myself);
-cp
-}
-
-/*
- read, encrypt and send data that is
- available through the ethertap device
-*/
-void handle_tap_input(void)
-{
- vpn_packet_t vp;
- ip_t from, to;
- int ether_type, lenin;
-cp
- memset(&vp, 0, sizeof(vp));
- if((lenin = read(tap_fd, &vp, MTU)) <= 0)
- {
- syslog(LOG_ERR, "Error while reading from tapdevice: %m");
- return;
- }
-
- total_tap_in += lenin;
-
- ether_type = ntohs(*((unsigned short*)(&vp.data[12])));
- if(ether_type != 0x0800)
- {
- if(debug_lvl > 0)
- syslog(LOG_INFO, "Non-IP ethernet frame %04x from " MAC_ADDR_S,
- ether_type, MAC_ADDR_V(vp.data[6]));
- return;
- }
-
- if(lenin < 32)
- {
- if(debug_lvl > 0)
- syslog(LOG_INFO, "Dropping short packet");
- return;
- }
-
- from = ntohl(*((unsigned long*)(&vp.data[26])));
- to = ntohl(*((unsigned long*)(&vp.data[30])));
-
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "An IP packet (%04x) for " IP_ADDR_S " from " IP_ADDR_S,
- ether_type, IP_ADDR_V(to), IP_ADDR_V(from));
- if(debug_lvl > 4)
- syslog(LOG_DEBUG, MAC_ADDR_S " to " MAC_ADDR_S,
- MAC_ADDR_V(vp.data[0]), MAC_ADDR_V(vp.data[6]));
-
- vp.len = (length_t)lenin - 2;
-
- strip_mac_addresses(&vp);
-
- send_packet(to, &vp);
-cp