-cp
- return p;
-}
-
-/*
- put all file descriptors in an fd_set array
-*/
-void build_fdset(fd_set *fs)
-{
- conn_list_t *p;
-cp
- FD_ZERO(fs);
-
- for(p = conn_list; p != NULL; p = p->next)
- {
- if(p->status.meta)
- FD_SET(p->meta_socket, fs);
- if(p->status.dataopen)
- FD_SET(p->socket, fs);
- }
-
- FD_SET(myself->meta_socket, fs);
- FD_SET(myself->socket, fs);
- FD_SET(tap_fd, fs);
-cp
-}
-
-/*
- receive incoming data from the listening
- udp socket and write it to the ethertap
- device after being decrypted
-*/
-int handle_incoming_vpn_data(conn_list_t *cl)
-{
- real_packet_t rp;
- int lenin;
- int x, l = sizeof(x);
- conn_list_t *f;
-cp
- if(getsockopt(cl->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
- {
- syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"), __FILE__, __LINE__, cl->socket);
- return -1;
- }
- if(x)
- {
- syslog(LOG_ERR, _("Incoming data socket error: %s"), sys_errlist[x]);
- return -1;
- }
-
- rp.len = -1;
- lenin = recvfrom(cl->socket, &rp, MTU, 0, NULL, NULL);
- if(lenin <= 0)
- {
- syslog(LOG_ERR, _("Receiving data failed: %m"));
- return -1;
- }
- total_socket_in += lenin;
-
- rp.data.len = ntohs(rp.data.len);
- rp.len = ntohs(rp.len);
- rp.from = ntohl(rp.from);
-
- if(rp.len >= 0)
- {
- f = lookup_conn(rp.from);
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, _("packet from " IP_ADDR_S " (len %d)"),
- IP_ADDR_V(rp.from), rp.len);
- if(!f)
- {
- syslog(LOG_ERR, _("Got packet from unknown source " IP_ADDR_S),
- IP_ADDR_V(rp.from));
- return -1;
- }
-
- if(f->status.validkey)
- xrecv(f, &rp);
- else
- {
- add_queue(&(f->rq), &rp, rp.len);
- if(!cl->status.waitingforkey)
- send_key_request(rp.from);
- }
-
- if(my_key_expiry <= time(NULL))
- regenerate_keys();
- }
-cp
- return 0;
-}
-
-/*
- terminate a connection and notify the other
- end before closing the sockets
-*/
-void terminate_connection(conn_list_t *cl)
-{
- conn_list_t *p, *q;
-
-cp
- if(cl->status.remove)
- return;
-
- if(debug_lvl > 0)
- syslog(LOG_NOTICE, _("Closing connection with " IP_ADDR_S " (" IP_ADDR_S ")"),
- IP_ADDR_V(cl->vpn_ip), IP_ADDR_V(cl->real_ip));
-
- if(cl->status.timeout)
- send_timeout(cl);
- else if(!cl->status.termreq)
- send_termreq(cl);
-
- close(cl->socket);
- if(cl->status.meta)
- close(cl->meta_socket);
-
- if(cl->status.outgoing)
- {
- signal(SIGALRM, sigalrm_handler);
- seconds_till_retry = 5;
- alarm(seconds_till_retry);
- syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
- }
-
- cl->status.active = 0;
- cl->status.remove = 1;
-
-cp
- /* Find all connections that were lost because they were behind cl
- (the connection that was dropped). */
- for(p = conn_list; p != NULL; p = p->next)
- if(p->nexthop == cl)
- {
- p->status.active = 0;
- p->status.remove = 1;
- }
-
-cp
- /* Then send a notification about all these connections to all hosts
- that are still connected to us. */
- for(p = conn_list; p != NULL; p = p->next)
- if(!p->status.remove && p->status.meta)
- for(q = conn_list; q != NULL; q = q->next)
- if(q->status.remove)
- send_del_host(p, q);
-