-/*
- receive incoming data from the listening
- udp socket and write it to the ethertap
- device after being decrypted
-*/
-int handle_incoming_vpn_data(void)
-{
- vpn_packet_t pkt;
- int x, l = sizeof(x);
- int lenin;
- struct sockaddr_in from;
- socklen_t fromlen = sizeof(from);
- connection_t *cl;
-cp
- if(getsockopt(myself->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
- {
- syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"),
- __FILE__, __LINE__, myself->socket);
- return -1;
- }
- if(x)
- {
- syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
- return -1;
- }
-
- if((lenin = recvfrom(myself->socket, (char *) &(pkt.len), MTU, 0, (struct sockaddr *)&from, &fromlen)) <= 0)
- {
- syslog(LOG_ERR, _("Receiving packet failed: %m"));
- return -1;
- }
-
- cl = lookup_connection(ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
-
- if(!cl)
- {
- syslog(LOG_WARNING, _("Received UDP packets on port %d from unknown source %lx:%d"), ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
- return 0;
- }
-
- if(debug_lvl >= DEBUG_TRAFFIC)
- {
- syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), lenin,
- cl->name, cl->hostname);
- }
-
-cp
- return xrecv(cl, &pkt);
-}
-
-/*
- terminate a connection and notify the other
- end before closing the sockets
-*/
-void terminate_connection(connection_t *cl)
-{
- connection_t *p;
- subnet_t *subnet;
- rbl_t *rbl;
-cp
- if(cl->status.remove)
- return;
-
- cl->status.remove = 1;
-
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
- cl->name, cl->hostname);
-
- if(cl->socket)
- close(cl->socket);
- if(cl->status.meta)
- close(cl->meta_socket);
-
- /* Find all connections that were lost because they were behind cl
- (the connection that was dropped). */
-
- if(cl->status.meta)
- RBL_FOREACH(connection_tree, rbl)
- {
- p = (connection_t *)rbl->data;
- if(p->nexthop == cl && p != cl)
- terminate_connection(p);
- }
-
- /* Inform others of termination if it was still active */
-
- if(cl->status.active)
- RBL_FOREACH(connection_tree, rbl)
- {
- p = (connection_t *)rbl->data;
- if(p->status.meta && p->status.active && p!=cl)
- send_del_host(p, cl); /* Sounds like recursion, but p does not have a meta connection :) */
- }
-
- /* Remove the associated subnets */
-
- RBL_FOREACH(cl->subnet_tree, rbl)
- {
- subnet = (subnet_t *)rbl->data;
- subnet_del(subnet);
- }
-
- /* Check if this was our outgoing connection */
-
- if(cl->status.outgoing && cl->status.active)
- {
- signal(SIGALRM, sigalrm_handler);
- seconds_till_retry = 5;
- alarm(seconds_till_retry);
- syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
- }
-
- /* Inactivate */
-
- cl->status.active = 0;
-cp