- if(p->status.meta)
- {
- send_termreq(p);
- shutdown(p->meta_socket, 0); /* No more receptions */
- close(p->meta_socket);
- }
- }
-
- if(myself)
- if(myself->status.active)
- {
- close(myself->meta_socket);
- close(myself->socket);
- }
-
- /* Execute tinc-down script right before shutting down the interface */
-
- asprintf(&scriptname, "%s/tinc-down", confbase);
-
- if(!fork())
- {
- execl(scriptname, NULL);
-
- if(errno != ENOENT)
- syslog(LOG_WARNING, _("Error while executing %s: %m"), scriptname);
-
- exit(0);
- }
-
- free(scriptname);
-
- close(tap_fd);
- destroy_conn_list();
-
- syslog(LOG_NOTICE, _("Terminating"));
-cp
- return;
-}
-
-/*
- create a data (udp) socket
-*/
-int setup_vpn_connection(conn_list_t *cl)
-{
- int nfd, flags;
- struct sockaddr_in a;
-cp
- if(debug_lvl >= DEBUG_TRAFFIC)
- syslog(LOG_DEBUG, _("Opening UDP socket to %s"), cl->hostname);
-
- nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if(nfd == -1)
- {
- syslog(LOG_ERR, _("Creating UDP socket failed: %m"));
- return -1;
- }
-
- a.sin_family = AF_INET;
- a.sin_port = htons(cl->port);
- a.sin_addr.s_addr = htonl(cl->address);
-
- if(connect(nfd, (struct sockaddr *)&a, sizeof(a)) == -1)
- {
- syslog(LOG_ERR, _("Connecting to %s port %d failed: %m"),
- cl->hostname, cl->port);
- return -1;
- }
-
- flags = fcntl(nfd, F_GETFL);
- if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, nfd,
- cl->name, cl->hostname);
- return -1;
- }
-
- cl->socket = nfd;
- cl->status.dataopen = 1;
-cp
- return 0;
-}
-
-/*
- handle an incoming tcp connect call and open
- a connection to it.
-*/
-conn_list_t *create_new_connection(int sfd)
-{
- conn_list_t *p;
- struct sockaddr_in ci;
- int len = sizeof(ci);
-cp
- p = new_conn_list();
-
- if(getpeername(sfd, &ci, &len) < 0)
- {
- syslog(LOG_ERR, _("Error: getpeername: %m"));
- return NULL;
- }
-
- p->name = unknown;
- p->address = ntohl(ci.sin_addr.s_addr);
- p->hostname = hostlookup(ci.sin_addr.s_addr);
- p->meta_socket = sfd;
- p->status.meta = 1;
- p->buffer = xmalloc(MAXBUFSIZE);
- p->buflen = 0;
- p->last_ping_time = time(NULL);
- p->want_ping = 0;
-
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Connection from %s port %d"),
- p->hostname, htons(ci.sin_port));
-
- p->allow_request = ID;
-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()
-{
- vpn_packet_t pkt;
- int lenin;
- int x, l = sizeof(x);
- struct sockaddr from;
- socklen_t fromlen = sizeof(from);
-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(recvfrom(myself->socket, (char *) &(pkt.len), MTU, 0, &from, &fromlen) <= 0)
- {
- syslog(LOG_ERR, _("Receiving packet failed: %m"));
- return -1;
- }
-/*
- if(debug_lvl >= DEBUG_TRAFFIC)
- {
- syslog(LOG_DEBUG, _("Received packet of %d bytes from %d.%d.%d.%d"), pkt.len,
- from.sa_addr[0], from.sa_addr[1], from.sa_addr[2], from.sa_addr[3]);
- }
-*/
-cp
- return xrecv(&pkt);
-}
-
-/*
- terminate a connection and notify the other
- end before closing the sockets
-*/
-void terminate_connection(conn_list_t *cl)
-{
- conn_list_t *p;
-
-cp
- if(cl->status.remove)
- return;