1 /* $Id: miniupnpd.c,v 1.1 2008-09-15 12:28:52 winfred Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2008 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
23 #include <sys/param.h>
27 /* for BSD's sysctl */
28 #include <sys/sysctl.h>
33 #ifdef USE_MINIUPNPDCTL
37 #include "upnpglobalvars.h"
39 #include "upnpdescgen.h"
40 #include "miniupnpdpath.h"
41 #include "getifaddr.h"
45 #include "upnpredirect.h"
46 #include "miniupnpdtypes.h"
47 #include "daemonize.h"
48 #include "upnpevents.h"
52 #include "commonrdr.h"
54 #ifdef USE_MINIUPNPDCTL
57 LIST_ENTRY(ctlelem) entries;
61 /* MAX_LAN_ADDR : maximum number of interfaces
62 * to listen to SSDP traffic */
63 /*#define MAX_LAN_ADDR (4)*/
65 static volatile int quitting = 0;
67 /* OpenAndConfHTTPSocket() :
68 * setup the socket used to handle incoming HTTP connections. */
70 OpenAndConfHTTPSocket(unsigned short port)
74 struct sockaddr_in listenname;
76 if( (s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
78 syslog(LOG_ERR, "socket(http): %m");
82 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
84 syslog(LOG_WARNING, "setsockopt(http, SO_REUSEADDR): %m");
87 memset(&listenname, 0, sizeof(struct sockaddr_in));
88 listenname.sin_family = AF_INET;
89 listenname.sin_port = htons(port);
90 listenname.sin_addr.s_addr = htonl(INADDR_ANY);
92 if(bind(s, (struct sockaddr *)&listenname, sizeof(struct sockaddr_in)) < 0)
94 syslog(LOG_ERR, "bind(http): %m");
101 syslog(LOG_ERR, "listen(http): %m");
109 /* Functions used to communicate with miniupnpdctl */
110 #ifdef USE_MINIUPNPDCTL
112 OpenAndConfCtlUnixSocket(const char * path)
114 struct sockaddr_un localun;
116 s = socket(AF_UNIX, SOCK_STREAM, 0);
117 localun.sun_family = AF_UNIX;
118 strncpy(localun.sun_path, path,
119 sizeof(localun.sun_path));
120 if(bind(s, (struct sockaddr *)&localun,
121 sizeof(struct sockaddr_un)) < 0)
123 syslog(LOG_ERR, "bind(sctl): %m");
127 else if(listen(s, 5) < 0)
129 syslog(LOG_ERR, "listen(sctl): %m");
137 write_upnphttp_details(int fd, struct upnphttp * e)
143 len = snprintf(buffer, sizeof(buffer),
144 "%d %d %s req_buf=%p(%dbytes) res_buf=%p(%dbytes alloc)\n",
145 e->socket, e->state, e->HttpVer,
146 e->req_buf, e->req_buflen,
147 e->res_buf, e->res_buf_alloclen);
148 write(fd, buffer, len);
149 e = e->entries.le_next;
154 write_ctlsockets_list(int fd, struct ctlelem * e)
160 len = snprintf(buffer, sizeof(buffer),
161 "struct ctlelem: socket=%d\n", e->socket);
162 write(fd, buffer, len);
163 e = e->entries.le_next;
168 write_option_list(int fd)
173 for(i=0; i<num_options; i++)
175 len = snprintf(buffer, sizeof(buffer),
177 ary_options[i].id, ary_options[i].value);
178 write(fd, buffer, len);
184 /* Handler for the SIGTERM signal (kill)
185 * SIGINT is also handled */
189 /*int save_errno = errno;*/
190 signal(sig, SIG_IGN); /* Ignore this signal while we are quitting */
192 syslog(LOG_NOTICE, "received signal %d, good-bye", sig);
195 /*errno = save_errno;*/
198 /* record the startup time, for returning uptime */
200 set_startup_time(int sysuptime)
202 startup_time = time(NULL);
205 /* use system uptime instead of daemon uptime */
206 #if defined(__linux__)
209 fd = open("/proc/uptime", O_RDONLY);
212 syslog(LOG_ERR, "open(\"/proc/uptime\" : %m");
216 memset(buff, 0, sizeof(buff));
217 read(fd, buff, sizeof(buff) - 1);
219 syslog(LOG_INFO, "system uptime is %d seconds", uptime);
221 startup_time -= uptime;
223 #elif defined(SOLARIS_KSTATS)
229 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
230 if(ksp && (kstat_read(kc, ksp, NULL) != -1))
232 void *ptr = kstat_data_lookup(ksp, "boot_time");
234 memcpy(&startup_time, ptr, sizeof(startup_time));
236 syslog(LOG_ERR, "cannot find boot_time kstat");
239 syslog(LOG_ERR, "cannot open kstats for unix/0/system_misc: %m");
243 struct timeval boottime;
244 size_t size = sizeof(boottime);
245 int name[2] = { CTL_KERN, KERN_BOOTTIME };
246 if(sysctl(name, 2, &boottime, &size, NULL, 0) < 0)
248 syslog(LOG_ERR, "sysctl(\"kern.boottime\") failed");
252 startup_time = boottime.tv_sec;
258 /* structure containing variables used during "main loop"
259 * that are filled during the init */
260 struct runtime_vars {
261 /* LAN IP addresses for SSDP traffic and HTTP */
262 /* moved to global vars */
264 /*struct lan_addr_s lan_addr[MAX_LAN_ADDR];*/
265 int port; /* HTTP Port */
266 int notify_interval; /* seconds between SSDP announces */
267 /* unused rules cleaning related variables : */
268 int clean_ruleset_threshold; /* threshold for removing unused rules */
269 int clean_ruleset_interval; /* (minimum) interval between checks */
273 * parse address with mask
279 parselanaddr(struct lan_addr_s * lan_addr, const char * str)
285 while(*p && *p != '/' && !isspace(*p))
291 while(*p && !isspace(*p))
296 fprintf(stderr, "Error parsing address/mask : %s\n", str);
299 memcpy(lan_addr->str, str, n);
300 lan_addr->str[n] = '\0';
301 if(!inet_aton(lan_addr->str, &lan_addr->addr))
303 fprintf(stderr, "Error parsing address/mask : %s\n", str);
306 lan_addr->mask.s_addr = htonl(nbits ? (0xffffffff << (32 - nbits)) : 0);
307 #ifdef MULTIPLE_EXTERNAL_IP
308 while(*p && isspace(*p))
312 while(p[n] && !isspace(*p))
315 memcpy(lan_addr->ext_ip_str, p, n);
316 lan_addr->ext_ip_str[n] = '\0';
317 if(!inet_aton(lan_addr->ext_ip_str, &lan_addr->ext_ip_addr)) {
319 fprintf(stderr, "Error parsing address : %s\n", lan_addr->ext_ip_str);
328 * 1) read configuration file
329 * 2) read command line arguments
332 * 5) check and write pid file
333 * 6) set startup time stamp
334 * 7) compute presentation URL
335 * 8) set signal handlers */
337 init(int argc, char * * argv, struct runtime_vars * v)
342 int options_flag = 0;
345 /*const char * logfilename = 0;*/
346 const char * presurl = 0;
347 const char * optionsfile = "/etc/miniupnpd.conf";
349 /* first check if "-f" option is used */
350 for(i=2; i<argc; i++)
352 if(0 == strcmp(argv[i-1], "-f"))
354 optionsfile = argv[i];
360 /* set initial values */
361 SETFLAG(ENABLEUPNPMASK);
363 /*v->n_lan_addr = 0;*/
365 v->notify_interval = 30; /* seconds between SSDP announces */
366 v->clean_ruleset_threshold = 20;
367 v->clean_ruleset_interval = 0; /* interval between ruleset check. 0=disabled */
369 /* read options file first since
370 * command line arguments have final say */
371 if(readoptionsfile(optionsfile) < 0)
373 /* only error if file exists or using -f */
374 if(access(optionsfile, F_OK) == 0 || options_flag)
375 fprintf(stderr, "Error reading configuration file %s\n", optionsfile);
379 for(i=0; i<num_options; i++)
381 switch(ary_options[i].id)
384 ext_if_name = ary_options[i].value;
387 use_ext_ip_addr = ary_options[i].value;
389 case UPNPLISTENING_IP:
390 if(n_lan_addr < MAX_LAN_ADDR)/* if(v->n_lan_addr < MAX_LAN_ADDR)*/
392 /*if(parselanaddr(&v->lan_addr[v->n_lan_addr],*/
393 if(parselanaddr(&lan_addr[n_lan_addr],
394 ary_options[i].value) == 0)
395 n_lan_addr++; /*v->n_lan_addr++; */
399 fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n",
400 MAX_LAN_ADDR, ary_options[i].value);
404 v->port = atoi(ary_options[i].value);
407 upstream_bitrate = strtoul(ary_options[i].value, 0, 0);
409 case UPNPBITRATE_DOWN:
410 downstream_bitrate = strtoul(ary_options[i].value, 0, 0);
412 case UPNPPRESENTATIONURL:
413 presurl = ary_options[i].value;
415 case UPNPNOTIFY_INTERVAL:
416 v->notify_interval = atoi(ary_options[i].value);
418 case UPNPSYSTEM_UPTIME:
419 if(strcmp(ary_options[i].value, "yes") == 0)
420 SETFLAG(SYSUPTIMEMASK); /*sysuptime = 1;*/
423 if(strcmp(ary_options[i].value, "yes") == 0)
424 SETFLAG(LOGPACKETSMASK); /*logpackets = 1;*/
427 strncpy(uuidvalue+5, ary_options[i].value,
428 strlen(uuidvalue+5) + 1);
431 strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN);
432 serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
434 case UPNPMODEL_NUMBER:
435 strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN);
436 modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
438 case UPNPCLEANTHRESHOLD:
439 v->clean_ruleset_threshold = atoi(ary_options[i].value);
441 case UPNPCLEANINTERVAL:
442 v->clean_ruleset_interval = atoi(ary_options[i].value);
446 queue = ary_options[i].value;
449 tag = ary_options[i].value;
453 case UPNPENABLENATPMP:
454 if(strcmp(ary_options[i].value, "yes") == 0)
455 SETFLAG(ENABLENATPMPMASK); /*enablenatpmp = 1;*/
457 if(atoi(ary_options[i].value))
458 SETFLAG(ENABLENATPMPMASK);
459 /*enablenatpmp = atoi(ary_options[i].value);*/
462 #ifdef PF_ENABLE_FILTER_RULES
464 if(strcmp(ary_options[i].value, "no") == 0)
465 SETFLAG(PFNOQUICKRULESMASK);
469 if(strcmp(ary_options[i].value, "yes") != 0)
470 CLEARFLAG(ENABLEUPNPMASK);
473 if(strcmp(ary_options[i].value, "yes") == 0)
474 SETFLAG(SECUREMODEMASK);
476 #ifdef ENABLE_LEASEFILE
478 lease_file = ary_options[i].value;
483 fprintf(stderr, "Unknown option in file %s\n",
489 /* command line arguments processing */
490 for(i=1; i<argc; i++)
494 fprintf(stderr, "Unknown option: %s\n", argv[i]);
496 else switch(argv[i][1])
500 use_ext_ip_addr = argv[++i];
502 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
506 v->notify_interval = atoi(argv[++i]);
508 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
512 strncpy(uuidvalue+5, argv[++i], strlen(uuidvalue+5) + 1);
514 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
518 strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN);
520 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
521 serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
525 strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN);
527 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
528 modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
532 /*enablenatpmp = 1;*/
533 SETFLAG(ENABLENATPMPMASK);
538 SETFLAG(SYSUPTIMEMASK);
541 logfilename = argv[++i];
545 SETFLAG(LOGPACKETSMASK);
548 SETFLAG(SECUREMODEMASK);
552 ext_if_name = argv[++i];
554 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
561 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
567 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
572 v->port = atoi(argv[++i]);
574 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
578 pidfilename = argv[++i];
580 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
589 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
594 downstream_bitrate = strtoul(argv[++i], 0, 0);
595 upstream_bitrate = strtoul(argv[++i], 0, 0);
598 fprintf(stderr, "Option -%c takes two arguments.\n", argv[i][1]);
603 int address_already_there = 0;
606 for(j=0; j<n_lan_addr; j++)/* for(j=0; j<v->n_lan_addr; j++)*/
608 struct lan_addr_s tmpaddr;
609 parselanaddr(&tmpaddr, argv[i]);
610 /*if(0 == strcmp(v->lan_addr[j].str, tmpaddr.str))*/
611 if(0 == strcmp(lan_addr[j].str, tmpaddr.str))
612 address_already_there = 1;
614 if(address_already_there)
616 if(n_lan_addr < MAX_LAN_ADDR) /*if(v->n_lan_addr < MAX_LAN_ADDR)*/
618 /*v->lan_addr[v->n_lan_addr++] = argv[i];*/
619 /*if(parselanaddr(&v->lan_addr[v->n_lan_addr], argv[i]) == 0)*/
620 if(parselanaddr(&lan_addr[n_lan_addr], argv[i]) == 0)
621 n_lan_addr++; /*v->n_lan_addr++;*/
625 fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n",
626 MAX_LAN_ADDR, argv[i]);
630 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
633 i++; /* discarding, the config file is already read */
636 fprintf(stderr, "Unknown option: %s\n", argv[i]);
639 if(!ext_if_name || (/*v->*/n_lan_addr==0) || v->port<=0)
641 fprintf(stderr, "Usage:\n\t"
642 "%s [-f config_file] [-i ext_ifname] [-o ext_ip]\n"
643 #ifndef ENABLE_NATPMP
644 "\t\t[-a listening_ip] [-p port] [-d] [-L] [-U] [-S]\n"
646 "\t\t[-a listening_ip] [-p port] [-d] [-L] [-U] [-S] [-N]\n"
648 /*"[-l logfile] " not functionnal */
649 "\t\t[-u uuid] [-s serial] [-m model_number] \n"
650 "\t\t[-t notify_interval] [-P pid_filename]\n"
652 "\t\t[-B down up] [-w url] [-q queue] [-T tag]\n"
654 "\t\t[-B down up] [-w url]\n"
656 "\nNotes:\n\tThere can be one or several listening_ips.\n"
657 "\tNotify interval is in seconds. Default is 30 seconds.\n"
658 "\tDefault pid file is %s.\n"
659 "\tWith -d miniupnpd will run as a standard program.\n"
660 "\t-L sets packet log in pf and ipf on.\n"
661 "\t-S sets \"secure\" mode : clients can only add mappings to their own ip\n"
662 "\t-U causes miniupnpd to report system uptime instead "
663 "of daemon uptime.\n"
664 "\t-B sets bitrates reported by daemon in bits per second.\n"
665 "\t-w sets the presentation url. Default is http address on port 80\n"
667 "\t-q sets the ALTQ queue in pf.\n"
668 "\t-T sets the tag name in pf.\n"
670 "", argv[0], pidfilename);
690 openlog_option = LOG_PID|LOG_CONS;
693 openlog_option |= LOG_PERROR; /* also log on stderr */
696 openlog("miniupnpd", openlog_option, LOG_MINIUPNPD);
700 /* speed things up and ignore LOG_INFO and LOG_DEBUG */
701 setlogmask(LOG_UPTO(LOG_NOTICE));
704 if(checkforrunning(pidfilename) < 0)
706 syslog(LOG_ERR, "MiniUPnPd is already running. EXITING");
710 set_startup_time(GETFLAG(SYSUPTIMEMASK)/*sysuptime*/);
712 /* presentation url */
715 strncpy(presentationurl, presurl, PRESENTATIONURL_MAX_LEN);
716 presentationurl[PRESENTATIONURL_MAX_LEN-1] = '\0';
720 snprintf(presentationurl, PRESENTATIONURL_MAX_LEN,
721 "http://%s/", lan_addr[0].str);
722 /*"http://%s:%d/", lan_addr[0].str, 80);*/
723 /*"http://%s:%d/", v->lan_addr[0].str, 80);*/
726 /* set signal handler */
727 memset(&sa, 0, sizeof(struct sigaction));
728 sa.sa_handler = sigterm;
730 if (sigaction(SIGTERM, &sa, NULL))
732 syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGTERM");
735 if (sigaction(SIGINT, &sa, NULL))
737 syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGINT");
741 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
742 syslog(LOG_ERR, "Failed to ignore SIGPIPE signals");
745 if(init_redirect() < 0)
747 syslog(LOG_ERR, "Failed to init redirection engine. EXITING");
751 writepidfile(pidfilename, pid);
757 /* process HTTP or SSDP requests */
759 main(int argc, char * * argv)
762 int sudp = -1, shttpl = -1;
766 int snotify[MAX_LAN_ADDR];
767 LIST_HEAD(httplisthead, upnphttp) upnphttphead;
768 struct upnphttp * e = 0;
769 struct upnphttp * next;
770 fd_set readset; /* for select() */
774 struct timeval timeout, timeofday, lasttimeofday = {0, 0};
776 #ifdef USE_MINIUPNPDCTL
778 LIST_HEAD(ctlstructhead, ctlelem) ctllisthead;
779 struct ctlelem * ectl;
780 struct ctlelem * ectlnext;
782 struct runtime_vars v;
783 /* variables used for the unused-rule cleanup process */
784 struct rule_state * rule_list = 0;
785 struct timeval checktime = {0, 0};
787 if(init(argc, argv, &v) != 0)
790 LIST_INIT(&upnphttphead);
791 #ifdef USE_MINIUPNPDCTL
792 LIST_INIT(&ctllisthead);
797 !GETFLAG(ENABLENATPMPMASK) &&
799 !GETFLAG(ENABLEUPNPMASK) ) {
800 syslog(LOG_ERR, "Why did you run me anyway?");
804 if(GETFLAG(ENABLEUPNPMASK)/*enableupnp*/)
807 /* open socket for SSDP connections */
808 /*sudp = OpenAndConfSSDPReceiveSocket(v.n_lan_addr, v.lan_addr);*/
809 sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr);
812 syslog(LOG_ERR, "Failed to open socket for receiving SSDP. EXITING");
815 /* open socket for HTTP connections. Listen on the 1st LAN address */
816 shttpl = OpenAndConfHTTPSocket(v.port);
819 syslog(LOG_ERR, "Failed to open socket for HTTP. EXITING");
822 syslog(LOG_NOTICE, "HTTP listening on port %d", v.port);
824 /* open socket for sending notifications */
825 if(OpenAndConfSSDPNotifySockets(snotify) < 0)
827 syslog(LOG_ERR, "Failed to open sockets for sending SSDP notify "
828 "messages. EXITING");
834 /* open socket for NAT PMP traffic */
835 if(GETFLAG(ENABLENATPMPMASK))
837 snatpmp = OpenAndConfNATPMPSocket();
840 syslog(LOG_ERR, "Failed to open socket for NAT PMP.");
841 /*syslog(LOG_ERR, "Failed to open socket for NAT PMP. EXITING");
844 syslog(LOG_NOTICE, "Listening for NAT-PMP traffic on port %u",
847 ScanNATPMPforExpiration();
851 /* for miniupnpdctl */
852 #ifdef USE_MINIUPNPDCTL
853 sctl = OpenAndConfCtlUnixSocket("/var/run/miniupnpd.ctl");
859 /* Check if we need to send SSDP NOTIFY messages and do it if
861 if(gettimeofday(&timeofday, 0) < 0)
863 syslog(LOG_ERR, "gettimeofday(): %m");
864 timeout.tv_sec = v.notify_interval;
869 /* the comparaison is not very precise but who cares ? */
870 if(timeofday.tv_sec >= (lasttimeofday.tv_sec + v.notify_interval))
872 if (GETFLAG(ENABLEUPNPMASK))
873 SendSSDPNotifies2(snotify,
874 (unsigned short)v.port,
875 v.notify_interval << 1);
876 memcpy(&lasttimeofday, &timeofday, sizeof(struct timeval));
877 timeout.tv_sec = v.notify_interval;
882 timeout.tv_sec = lasttimeofday.tv_sec + v.notify_interval
884 if(timeofday.tv_usec > lasttimeofday.tv_usec)
886 timeout.tv_usec = 1000000 + lasttimeofday.tv_usec
892 timeout.tv_usec = lasttimeofday.tv_usec - timeofday.tv_usec;
896 /* remove unused rules */
897 if( v.clean_ruleset_interval
898 && (timeofday.tv_sec >= checktime.tv_sec + v.clean_ruleset_interval))
902 remove_unused_rules(rule_list);
907 rule_list = get_upnp_rules_state_list(v.clean_ruleset_threshold);
909 memcpy(&checktime, &timeofday, sizeof(struct timeval));
912 /* Remove expired NAT-PMP mappings */
913 while( nextnatpmptoclean_timestamp && (timeofday.tv_sec >= nextnatpmptoclean_timestamp + startup_time))
915 /*syslog(LOG_DEBUG, "cleaning expired NAT-PMP mappings");*/
916 if(CleanExpiredNATPMP() < 0) {
917 syslog(LOG_ERR, "CleanExpiredNATPMP() failed");
921 if(nextnatpmptoclean_timestamp && timeout.tv_sec >= (nextnatpmptoclean_timestamp + startup_time - timeofday.tv_sec))
923 /*syslog(LOG_DEBUG, "setting timeout to %d sec", nextnatpmptoclean_timestamp + startup_time - timeofday.tv_sec);*/
924 timeout.tv_sec = nextnatpmptoclean_timestamp + startup_time - timeofday.tv_sec;
929 /* select open sockets (SSDP, HTTP listen, and all HTTP soap sockets) */
934 FD_SET(sudp, &readset);
935 max_fd = MAX( max_fd, sudp);
940 FD_SET(shttpl, &readset);
941 max_fd = MAX( max_fd, shttpl);
944 i = 0; /* active HTTP connections count */
945 for(e = upnphttphead.lh_first; e != NULL; e = e->entries.le_next)
947 if((e->socket >= 0) && (e->state <= 2))
949 FD_SET(e->socket, &readset);
950 max_fd = MAX( max_fd, e->socket);
958 syslog(LOG_DEBUG, "%d active incoming HTTP connections", i);
963 FD_SET(snatpmp, &readset);
964 max_fd = MAX( max_fd, snatpmp);
967 #ifdef USE_MINIUPNPDCTL
969 FD_SET(sctl, &readset);
970 max_fd = MAX( max_fd, sctl);
973 for(ectl = ctllisthead.lh_first; ectl; ectl = ectl->entries.le_next)
975 if(ectl->socket >= 0) {
976 FD_SET(ectl->socket, &readset);
977 max_fd = MAX( max_fd, ectl->socket);
984 upnpevents_selectfds(&readset, &writeset, &max_fd);
988 if(select(max_fd+1, &readset, &writeset, 0, &timeout) < 0)
990 if(select(max_fd+1, &readset, 0, 0, &timeout) < 0)
993 if(quitting) goto shutdown;
994 syslog(LOG_ERR, "select(all): %m");
995 syslog(LOG_ERR, "Failed to select open sockets. EXITING");
996 return 1; /* very serious cause of error */
998 #ifdef USE_MINIUPNPDCTL
999 for(ectl = ctllisthead.lh_first; ectl;)
1001 ectlnext = ectl->entries.le_next;
1002 if((ectl->socket >= 0) && FD_ISSET(ectl->socket, &readset))
1006 l = read(ectl->socket, buf, sizeof(buf));
1009 /*write(ectl->socket, buf, l);*/
1010 write_option_list(ectl->socket);
1011 write_permlist(ectl->socket, upnppermlist, num_upnpperm);
1012 write_upnphttp_details(ectl->socket, upnphttphead.lh_first);
1013 write_ctlsockets_list(ectl->socket, ctllisthead.lh_first);
1014 write_ruleset_details(ectl->socket);
1015 #ifdef ENABLE_EVENTS
1016 write_events_details(ectl->socket);
1018 /* close the socket */
1019 close(ectl->socket);
1024 close(ectl->socket);
1028 if(ectl->socket < 0)
1030 LIST_REMOVE(ectl, entries);
1035 if((sctl >= 0) && FD_ISSET(sctl, &readset))
1038 struct sockaddr_un clientname;
1039 struct ctlelem * tmp;
1040 socklen_t clientnamelen = sizeof(struct sockaddr_un);
1041 //syslog(LOG_DEBUG, "sctl!");
1042 s = accept(sctl, (struct sockaddr *)&clientname,
1044 syslog(LOG_DEBUG, "sctl! : '%s'", clientname.sun_path);
1045 tmp = malloc(sizeof(struct ctlelem));
1047 LIST_INSERT_HEAD(&ctllisthead, tmp, entries);
1050 #ifdef ENABLE_EVENTS
1051 upnpevents_processfds(&readset, &writeset);
1053 #ifdef ENABLE_NATPMP
1054 /* process NAT-PMP packets */
1055 if((snatpmp >= 0) && FD_ISSET(snatpmp, &readset))
1057 ProcessIncomingNATPMPPacket(snatpmp);
1060 /* process SSDP packets */
1061 if(sudp >= 0 && FD_ISSET(sudp, &readset))
1063 /*syslog(LOG_INFO, "Received UDP Packet");*/
1064 /*ProcessSSDPRequest(sudp, v.lan_addr, v.n_lan_addr,*/
1065 ProcessSSDPRequest(sudp, (unsigned short)v.port);
1067 /* process active HTTP connections */
1068 /* LIST_FOREACH macro is not available under linux */
1069 for(e = upnphttphead.lh_first; e != NULL; e = e->entries.le_next)
1071 if( (e->socket >= 0) && (e->state <= 2)
1072 &&(FD_ISSET(e->socket, &readset)) )
1074 Process_upnphttp(e);
1077 /* process incoming HTTP connections */
1078 if(shttpl >= 0 && FD_ISSET(shttpl, &readset))
1081 socklen_t clientnamelen;
1082 struct sockaddr_in clientname;
1083 clientnamelen = sizeof(struct sockaddr_in);
1084 shttp = accept(shttpl, (struct sockaddr *)&clientname, &clientnamelen);
1087 syslog(LOG_ERR, "accept(http): %m");
1091 struct upnphttp * tmp = 0;
1092 syslog(LOG_INFO, "HTTP connection from %s:%d",
1093 inet_ntoa(clientname.sin_addr),
1094 ntohs(clientname.sin_port) );
1095 /*if (fcntl(shttp, F_SETFL, O_NONBLOCK) < 0) {
1096 syslog(LOG_ERR, "fcntl F_SETFL, O_NONBLOCK");
1098 /* Create a new upnphttp object and add it to
1099 * the active upnphttp object list */
1100 tmp = New_upnphttp(shttp);
1103 tmp->clientaddr = clientname.sin_addr;
1104 LIST_INSERT_HEAD(&upnphttphead, tmp, entries);
1108 syslog(LOG_ERR, "New_upnphttp() failed");
1113 /* delete finished HTTP connections */
1114 for(e = upnphttphead.lh_first; e != NULL; )
1116 next = e->entries.le_next;
1119 LIST_REMOVE(e, entries);
1127 /* close out open sockets */
1128 while(upnphttphead.lh_first != NULL)
1130 e = upnphttphead.lh_first;
1131 LIST_REMOVE(e, entries);
1135 if (sudp >= 0) close(sudp);
1136 if (shttpl >= 0) close(shttpl);
1137 #ifdef ENABLE_NATPMP
1144 #ifdef USE_MINIUPNPDCTL
1149 if(unlink("/var/run/miniupnpd.ctl") < 0)
1151 syslog(LOG_ERR, "unlink() %m");
1156 /*if(SendSSDPGoodbye(snotify, v.n_lan_addr) < 0)*/
1157 if (GETFLAG(ENABLEUPNPMASK))
1159 if(SendSSDPGoodbye(snotify, n_lan_addr) < 0)
1161 syslog(LOG_ERR, "Failed to broadcast good-bye notifications");
1163 for(i=0; i<n_lan_addr; i++)/* for(i=0; i<v.n_lan_addr; i++)*/
1167 if(unlink(pidfilename) < 0)
1169 syslog(LOG_ERR, "Failed to remove pidfile %s: %m", pidfilename);