Repository navigation
Expand file tree
/
Copy pathminiupnpd.c
More file actions
2065 lines (1936 loc) · 50.3 KB
/
Copy pathminiupnpd.c
File metadata and controls
2065 lines (1936 loc) · 50.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: miniupnpd.c,v 1.183 2014/02/06 09:52:01 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2014 Thomas Bernard
* This software is subject to the conditions detailed
* in the LICENCE file provided within the distribution */
#include "config.h"
/* Experimental support for NFQUEUE interfaces */
#ifdef ENABLE_NFQUEUE
/* apt-get install libnetfilter-queue-dev */
#include <netinet/ip.h>
#include <netinet/udp.h>
#if 0
#include <linux/netfilter_ipv4.h> /* Defines verdicts (NF_ACCEPT, etc) */
#endif
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <linux/netfilter/nfnetlink_queue.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/file.h>
#include <syslog.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <sys/param.h>
#if defined(sun)
#include <kstat.h>
#else
/* for BSD's sysctl */
#include <sys/sysctl.h>
#endif
/* unix sockets */
#ifdef USE_MINIUPNPDCTL
#include <sys/un.h>
#endif
#include "macros.h"
#include "upnpglobalvars.h"
#include "upnphttp.h"
#include "upnpdescgen.h"
#include "miniupnpdpath.h"
#include "getifaddr.h"
#include "upnpsoap.h"
#include "options.h"
#include "minissdp.h"
#include "upnpredirect.h"
#include "upnppinhole.h"
#include "miniupnpdtypes.h"
#include "daemonize.h"
#include "upnpevents.h"
#ifdef ENABLE_NATPMP
#include "natpmp.h"
#ifdef ENABLE_PCP
#include "pcpserver.h"
#else
#define PCP_MAX_LEN 32
#endif
#endif
#include "commonrdr.h"
#include "upnputils.h"
#ifdef USE_IFACEWATCHER
#include "ifacewatcher.h"
#endif
#ifdef ENABLE_6FC_SERVICE
#ifdef USE_NETFILTER
void init_iptpinhole(void);
#endif
#endif
#ifndef DEFAULT_CONFIG
#define DEFAULT_CONFIG "/etc/miniupnpd.conf"
#endif
#ifdef USE_MINIUPNPDCTL
struct ctlelem {
int socket;
LIST_ENTRY(ctlelem) entries;
};
#endif
#ifdef ENABLE_NFQUEUE
/* globals */
static struct nfq_handle *nfqHandle;
static struct sockaddr_in ssdp;
/* prototypes */
static int nfqueue_cb( struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) ;
int identify_ip_protocol (char *payload);
int get_udp_dst_port (char *payload);
#endif
/* variables used by signals */
static volatile sig_atomic_t quitting = 0;
volatile sig_atomic_t should_send_public_address_change_notif = 0;
/* OpenAndConfHTTPSocket() :
* setup the socket used to handle incoming HTTP connections. */
static int
OpenAndConfHTTPSocket(unsigned short port)
{
int s;
int i = 1;
#ifdef ENABLE_IPV6
struct sockaddr_in6 listenname6;
struct sockaddr_in listenname4;
#else
struct sockaddr_in listenname;
#endif
socklen_t listenname_len;
if( (s = socket(
#ifdef ENABLE_IPV6
ipv6_enabled ? PF_INET6 : PF_INET,
#else
PF_INET,
#endif
SOCK_STREAM, 0)) < 0)
{
syslog(LOG_ERR, "socket(http): %m");
return -1;
}
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
{
syslog(LOG_WARNING, "setsockopt(http, SO_REUSEADDR): %m");
}
#if 0
/* enable this to force IPV6 only for IPV6 socket.
* see http://www.ietf.org/rfc/rfc3493.txt section 5.3 */
if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &i, sizeof(i)) < 0)
{
syslog(LOG_WARNING, "setsockopt(http, IPV6_V6ONLY): %m");
}
#endif
if(!set_non_blocking(s))
{
syslog(LOG_WARNING, "set_non_blocking(http): %m");
}
#ifdef ENABLE_IPV6
if(ipv6_enabled)
{
memset(&listenname6, 0, sizeof(struct sockaddr_in6));
listenname6.sin6_family = AF_INET6;
listenname6.sin6_port = htons(port);
listenname6.sin6_addr = in6addr_any;
listenname_len = sizeof(struct sockaddr_in6);
} else {
memset(&listenname4, 0, sizeof(struct sockaddr_in));
listenname4.sin_family = AF_INET;
listenname4.sin_port = htons(port);
listenname4.sin_addr.s_addr = htonl(INADDR_ANY);
listenname_len = sizeof(struct sockaddr_in);
}
#else
memset(&listenname, 0, sizeof(struct sockaddr_in));
listenname.sin_family = AF_INET;
listenname.sin_port = htons(port);
listenname.sin_addr.s_addr = htonl(INADDR_ANY);
listenname_len = sizeof(struct sockaddr_in);
#endif
#ifdef ENABLE_IPV6
if(bind(s,
ipv6_enabled ? (struct sockaddr *)&listenname6 : (struct sockaddr *)&listenname4,
listenname_len) < 0)
#else
if(bind(s, (struct sockaddr *)&listenname, listenname_len) < 0)
#endif
{
syslog(LOG_ERR, "bind(http): %m");
close(s);
return -1;
}
if(listen(s, 5) < 0)
{
syslog(LOG_ERR, "listen(http): %m");
close(s);
return -1;
}
return s;
}
#ifdef ENABLE_NFQUEUE
int identify_ip_protocol(char *payload) {
return payload[9];
}
/*
* This function returns the destination port of the captured packet UDP
*/
int get_udp_dst_port(char *payload) {
char *pkt_data_ptr = NULL;
pkt_data_ptr = payload + sizeof(struct ip);
/* Cast the UDP Header from the raw packet */
struct udphdr *udp = (struct udphdr *) pkt_data_ptr;
/* get the dst port of the packet */
return(ntohs(udp->dest));
}
static int
OpenAndConfNFqueue(){
struct nfq_q_handle *myQueue;
struct nfnl_handle *netlinkHandle;
int fd = 0, e = 0;
inet_pton(AF_INET, "239.255.255.250", &(ssdp.sin_addr));
/* Get a queue connection handle from the module */
if (!(nfqHandle = nfq_open())) {
syslog(LOG_ERR, "Error in nfq_open(): %m");
return -1;
}
/* Unbind the handler from processing any IP packets
Not totally sure why this is done, or if it's necessary... */
if ((e = nfq_unbind_pf(nfqHandle, AF_INET)) < 0) {
syslog(LOG_ERR, "Error in nfq_unbind_pf(): %m");
return -1;
}
/* Bind this handler to process IP packets... */
if (nfq_bind_pf(nfqHandle, AF_INET) < 0) {
syslog(LOG_ERR, "Error in nfq_bind_pf(): %m");
return -1;
}
/* Install a callback on queue -Q */
if (!(myQueue = nfq_create_queue(nfqHandle, nfqueue, &nfqueue_cb, NULL))) {
syslog(LOG_ERR, "Error in nfq_create_queue(): %m");
return -1;
}
/* Turn on packet copy mode */
if (nfq_set_mode(myQueue, NFQNL_COPY_PACKET, 0xffff) < 0) {
syslog(LOG_ERR, "Error setting packet copy mode (): %m");
return -1;
}
netlinkHandle = nfq_nfnlh(nfqHandle);
fd = nfnl_fd(netlinkHandle);
return fd;
}
static int nfqueue_cb(
struct nfq_q_handle *qh,
struct nfgenmsg *nfmsg,
struct nfq_data *nfa,
void *data) {
char *pkt;
struct nfqnl_msg_packet_hdr *ph;
ph = nfq_get_msg_packet_hdr(nfa);
if ( ph ) {
int id = 0, size = 0;
id = ntohl(ph->packet_id);
size = nfq_get_payload(nfa, &pkt);
struct ip *iph = (struct ip *) pkt;
int id_protocol = identify_ip_protocol(pkt);
int dport = get_udp_dst_port(pkt);
int x = sizeof (struct ip) + sizeof (struct udphdr);
/* packets we are interested in are UDP multicast to 239.255.255.250:1900
* and start with a data string M-SEARCH
*/
if ( (dport == 1900) && (id_protocol == IPPROTO_UDP)
&& (ssdp.sin_addr.s_addr == iph->ip_dst.s_addr) ) {
/* get the index that the packet came in on */
u_int32_t idx = nfq_get_indev(nfa);
int i = 0;
for ( ;i < n_nfqix ; i++) {
if ( nfqix[i] == idx ) {
struct udphdr *udp = (struct udphdr *) (pkt + sizeof(struct ip));
char *dd = pkt + x;
struct sockaddr_in sendername;
sendername.sin_family = AF_INET;
sendername.sin_port = udp->source;
sendername.sin_addr.s_addr = iph->ip_src.s_addr;
/* printf("pkt found %s\n",dd);*/
ProcessSSDPData (sudp, dd, size - x,
&sendername, (unsigned short) 5555);
}
}
}
nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
} else {
syslog(LOG_ERR,"nfq_get_msg_packet_hdr failed");
return 1;
/* from nfqueue source: 0 = ok, >0 = soft error, <0 hard error */
}
return 0;
}
static void ProcessNFQUEUE(int fd){
char buf[4096];
socklen_t len_r;
struct sockaddr_in sendername;
len_r = sizeof(struct sockaddr_in);
int res = recvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr *)&sendername, &len_r);
nfq_handle_packet(nfqHandle, buf, res);
}
#endif
/* Functions used to communicate with miniupnpdctl */
#ifdef USE_MINIUPNPDCTL
static int
OpenAndConfCtlUnixSocket(const char * path)
{
struct sockaddr_un localun;
int s;
s = socket(AF_UNIX, SOCK_STREAM, 0);
localun.sun_family = AF_UNIX;
strncpy(localun.sun_path, path,
sizeof(localun.sun_path));
if(bind(s, (struct sockaddr *)&localun,
sizeof(struct sockaddr_un)) < 0)
{
syslog(LOG_ERR, "bind(sctl): %m");
close(s);
s = -1;
}
else if(listen(s, 5) < 0)
{
syslog(LOG_ERR, "listen(sctl): %m");
close(s);
s = -1;
}
return s;
}
static void
write_upnphttp_details(int fd, struct upnphttp * e)
{
char buffer[256];
int len;
write(fd, "HTTP :\n", 7);
while(e)
{
len = snprintf(buffer, sizeof(buffer),
"%d %d %s req_buf=%p(%dbytes) res_buf=%p(%dbytes alloc)\n",
e->socket, e->state, e->HttpVer,
e->req_buf, e->req_buflen,
e->res_buf, e->res_buf_alloclen);
write(fd, buffer, len);
e = e->entries.le_next;
}
}
static void
write_ctlsockets_list(int fd, struct ctlelem * e)
{
char buffer[256];
int len;
write(fd, "CTL :\n", 6);
while(e)
{
len = snprintf(buffer, sizeof(buffer),
"struct ctlelem: socket=%d\n", e->socket);
write(fd, buffer, len);
e = e->entries.le_next;
}
}
#ifndef DISABLE_CONFIG_FILE
static void
write_option_list(int fd)
{
char buffer[256];
int len;
unsigned int i;
write(fd, "Options :\n", 10);
for(i=0; i<num_options; i++)
{
len = snprintf(buffer, sizeof(buffer),
"opt=%02d %s\n",
ary_options[i].id, ary_options[i].value);
write(fd, buffer, len);
}
}
#endif
static void
write_command_line(int fd, int argc, char * * argv)
{
char buffer[256];
int len;
int i;
write(fd, "Command Line :\n", 15);
for(i=0; i<argc; i++)
{
len = snprintf(buffer, sizeof(buffer),
"argv[%02d]='%s'\n",
i, argv[i]);
write(fd, buffer, len);
}
}
#endif
/* Handler for the SIGTERM signal (kill)
* SIGINT is also handled */
static void
sigterm(int sig)
{
UNUSED(sig);
/*int save_errno = errno; */
/*signal(sig, SIG_IGN);*/ /* Ignore this signal while we are quitting */
/* Note : isn't it useless ? */
#if 0
/* calling syslog() is forbidden in signal handler according to
* signal(3) */
syslog(LOG_NOTICE, "received signal %d, good-bye", sig);
#endif
quitting = 1;
/*errno = save_errno;*/
}
/* Handler for the SIGUSR1 signal indicating public IP address change. */
static void
sigusr1(int sig)
{
UNUSED(sig);
#if 0
/* calling syslog() is forbidden in signal handler according to
* signal(3) */
syslog(LOG_INFO, "received signal %d, public ip address change", sig);
#endif
should_send_public_address_change_notif = 1;
}
/* record the startup time, for returning uptime */
static void
set_startup_time(int sysuptime)
{
startup_time = time(NULL);
if(sysuptime)
{
/* use system uptime instead of daemon uptime */
#if defined(__linux__)
char buff[64];
int uptime = 0, fd;
fd = open("/proc/uptime", O_RDONLY);
if(fd < 0)
{
syslog(LOG_ERR, "open(\"/proc/uptime\" : %m");
}
else
{
memset(buff, 0, sizeof(buff));
if(read(fd, buff, sizeof(buff) - 1) < 0)
{
syslog(LOG_ERR, "read(\"/proc/uptime\" : %m");
}
else
{
uptime = atoi(buff);
syslog(LOG_INFO, "system uptime is %d seconds", uptime);
}
close(fd);
startup_time -= uptime;
}
#elif defined(SOLARIS_KSTATS)
kstat_ctl_t *kc;
kc = kstat_open();
if(kc != NULL)
{
kstat_t *ksp;
ksp = kstat_lookup(kc, "unix", 0, "system_misc");
if(ksp && (kstat_read(kc, ksp, NULL) != -1))
{
void *ptr = kstat_data_lookup(ksp, "boot_time");
if(ptr)
memcpy(&startup_time, ptr, sizeof(startup_time));
else
syslog(LOG_ERR, "cannot find boot_time kstat");
}
else
syslog(LOG_ERR, "cannot open kstats for unix/0/system_misc: %m");
kstat_close(kc);
}
#else
struct timeval boottime;
size_t size = sizeof(boottime);
int name[2] = { CTL_KERN, KERN_BOOTTIME };
if(sysctl(name, 2, &boottime, &size, NULL, 0) < 0)
{
syslog(LOG_ERR, "sysctl(\"kern.boottime\") failed");
}
else
{
startup_time = boottime.tv_sec;
}
#endif
}
}
/* structure containing variables used during "main loop"
* that are filled during the init */
struct runtime_vars {
/* LAN IP addresses for SSDP traffic and HTTP */
/* moved to global vars */
int port; /* HTTP Port */
int notify_interval; /* seconds between SSDP announces */
/* unused rules cleaning related variables : */
int clean_ruleset_threshold; /* threshold for removing unused rules */
int clean_ruleset_interval; /* (minimum) interval between checks */
};
/* parselanaddr()
* parse address with mask
* ex: 192.168.1.1/24 or 192.168.1.1/255.255.255.0
* When MULTIPLE_EXTERNAL_IP is enabled, the ip address of the
* external interface associated with the lan subnet follows.
* ex : 192.168.1.1/24 81.21.41.11
*
* Can also use the interface name (ie eth0)
*
* return value :
* 0 : ok
* -1 : error */
static int
parselanaddr(struct lan_addr_s * lan_addr, const char * str)
{
const char * p;
int n;
char tmp[16];
memset(lan_addr, 0, sizeof(struct lan_addr_s));
p = str;
while(*p && *p != '/' && !isspace(*p))
p++;
n = p - str;
if(!isdigit(str[0]) && n < (int)sizeof(lan_addr->ifname))
{
/* not starting with a digit : suppose it is an interface name */
memcpy(lan_addr->ifname, str, n);
lan_addr->ifname[n] = '\0';
if(getifaddr(lan_addr->ifname, lan_addr->str, sizeof(lan_addr->str),
&lan_addr->addr, &lan_addr->mask) < 0)
goto parselan_error;
}
else
{
if(n>15)
goto parselan_error;
memcpy(lan_addr->str, str, n);
lan_addr->str[n] = '\0';
if(!inet_aton(lan_addr->str, &lan_addr->addr))
goto parselan_error;
}
if(*p == '/')
{
const char * q = ++p;
while(*p && isdigit(*p))
p++;
if(*p=='.')
{
/* parse mask in /255.255.255.0 format */
while(*p && (*p=='.' || isdigit(*p)))
p++;
n = p - q;
if(n>15)
goto parselan_error;
memcpy(tmp, q, n);
tmp[n] = '\0';
if(!inet_aton(tmp, &lan_addr->mask))
goto parselan_error;
}
else
{
/* it is a /24 format */
int nbits = atoi(q);
if(nbits > 32 || nbits < 0)
goto parselan_error;
lan_addr->mask.s_addr = htonl(nbits ? (0xffffffffu << (32 - nbits)) : 0);
}
}
else if(lan_addr->mask.s_addr == 0)
{
/* by default, networks are /24 */
lan_addr->mask.s_addr = htonl(0xffffff00u);
}
#ifdef MULTIPLE_EXTERNAL_IP
/* skip spaces */
while(*p && isspace(*p))
p++;
if(*p) {
/* parse the exteral ip address to associate with this subnet */
n = 0;
while(p[n] && !isspace(*p))
n++;
if(n<=15) {
memcpy(lan_addr->ext_ip_str, p, n);
lan_addr->ext_ip_str[n] = '\0';
if(!inet_aton(lan_addr->ext_ip_str, &lan_addr->ext_ip_addr)) {
/* error */
fprintf(stderr, "Error parsing address : %s\n", lan_addr->ext_ip_str);
}
}
}
#endif
#ifdef ENABLE_IPV6
if(lan_addr->ifname[0] != '\0')
{
lan_addr->index = if_nametoindex(lan_addr->ifname);
if(lan_addr->index == 0)
fprintf(stderr, "Cannot get index for network interface %s",
lan_addr->ifname);
}
else
{
fprintf(stderr, "Warning: please specify LAN network interface by name instead of IPv4 address\n");
}
#endif
return 0;
parselan_error:
fprintf(stderr, "Error parsing address/mask (or interface name) : %s\n",
str);
return -1;
}
/* fill uuidvalue_wan and uuidvalue_wcd based on uuidvalue_igd */
void complete_uuidvalues(void)
{
size_t len;
len = strlen(uuidvalue_igd);
memcpy(uuidvalue_wan, uuidvalue_igd, len+1);
switch(uuidvalue_wan[len-1]) {
case '9':
uuidvalue_wan[len-1] = 'a';
break;
case 'f':
uuidvalue_wan[len-1] = '0';
break;
default:
uuidvalue_wan[len-1]++;
}
memcpy(uuidvalue_wcd, uuidvalue_wan, len+1);
switch(uuidvalue_wcd[len-1]) {
case '9':
uuidvalue_wcd[len-1] = 'a';
break;
case 'f':
uuidvalue_wcd[len-1] = '0';
break;
default:
uuidvalue_wcd[len-1]++;
}
}
/* init phase :
* 1) read configuration file
* 2) read command line arguments
* 3) daemonize
* 4) open syslog
* 5) check and write pid file
* 6) set startup time stamp
* 7) compute presentation URL
* 8) set signal handlers */
static int
init(int argc, char * * argv, struct runtime_vars * v)
{
int i;
int pid;
int debug_flag = 0;
int openlog_option;
struct sigaction sa;
/*const char * logfilename = 0;*/
const char * presurl = 0;
#ifndef DISABLE_CONFIG_FILE
int options_flag = 0;
const char * optionsfile = DEFAULT_CONFIG;
#endif /* DISABLE_CONFIG_FILE */
struct lan_addr_s * lan_addr;
struct lan_addr_s * lan_addr2;
/* only print usage if -h is used */
for(i=1; i<argc; i++)
{
if(0 == strcmp(argv[i], "-h"))
goto print_usage;
}
#ifndef DISABLE_CONFIG_FILE
/* first check if "-f" option is used */
for(i=2; i<argc; i++)
{
if(0 == strcmp(argv[i-1], "-f"))
{
optionsfile = argv[i];
options_flag = 1;
break;
}
}
#endif /* DISABLE_CONFIG_FILE */
/* set initial values */
SETFLAG(ENABLEUPNPMASK); /* UPnP is enabled by default */
LIST_INIT(&lan_addrs);
v->port = -1;
v->notify_interval = 30; /* seconds between SSDP announces */
v->clean_ruleset_threshold = 20;
v->clean_ruleset_interval = 0; /* interval between ruleset check. 0=disabled */
#ifndef DISABLE_CONFIG_FILE
/* read options file first since
* command line arguments have final say */
if(readoptionsfile(optionsfile) < 0)
{
/* only error if file exists or using -f */
if(access(optionsfile, F_OK) == 0 || options_flag)
fprintf(stderr, "Error reading configuration file %s\n", optionsfile);
}
else
{
for(i=0; i<(int)num_options; i++)
{
switch(ary_options[i].id)
{
case UPNPEXT_IFNAME:
ext_if_name = ary_options[i].value;
break;
case UPNPEXT_IP:
use_ext_ip_addr = ary_options[i].value;
break;
case UPNPLISTENING_IP:
lan_addr = (struct lan_addr_s *) malloc(sizeof(struct lan_addr_s));
if (lan_addr == NULL)
{
fprintf(stderr, "malloc(sizeof(struct lan_addr_s)): %m");
break;
}
if(parselanaddr(lan_addr, ary_options[i].value) != 0)
{
fprintf(stderr, "can't parse \"%s\" as valid lan address\n", ary_options[i].value);
free(lan_addr);
break;
}
LIST_INSERT_HEAD(&lan_addrs, lan_addr, list);
break;
case UPNPPORT:
v->port = atoi(ary_options[i].value);
break;
case UPNPBITRATE_UP:
upstream_bitrate = strtoul(ary_options[i].value, 0, 0);
break;
case UPNPBITRATE_DOWN:
downstream_bitrate = strtoul(ary_options[i].value, 0, 0);
break;
case UPNPPRESENTATIONURL:
presurl = ary_options[i].value;
break;
#ifdef ENABLE_MANUFACTURER_INFO_CONFIGURATION
case UPNPFRIENDLY_NAME:
strncpy(friendly_name, ary_options[i].value, FRIENDLY_NAME_MAX_LEN);
friendly_name[FRIENDLY_NAME_MAX_LEN-1] = '\0';
break;
case UPNPMANUFACTURER_NAME:
strncpy(manufacturer_name, ary_options[i].value, MANUFACTURER_NAME_MAX_LEN);
manufacturer_name[MANUFACTURER_NAME_MAX_LEN-1] = '\0';
break;
case UPNPMANUFACTURER_URL:
strncpy(manufacturer_url, ary_options[i].value, MANUFACTURER_URL_MAX_LEN);
manufacturer_url[MANUFACTURER_URL_MAX_LEN-1] = '\0';
break;
case UPNPMODEL_NAME:
strncpy(model_name, ary_options[i].value, MODEL_NAME_MAX_LEN);
model_name[MODEL_NAME_MAX_LEN-1] = '\0';
break;
case UPNPMODEL_DESCRIPTION:
strncpy(model_description, ary_options[i].value, MODEL_DESCRIPTION_MAX_LEN);
model_description[MODEL_DESCRIPTION_MAX_LEN-1] = '\0';
break;
case UPNPMODEL_URL:
strncpy(model_url, ary_options[i].value, MODEL_URL_MAX_LEN);
model_url[MODEL_URL_MAX_LEN-1] = '\0';
break;
#endif
#ifdef USE_NETFILTER
case UPNPFORWARDCHAIN:
miniupnpd_forward_chain = ary_options[i].value;
break;
case UPNPNATCHAIN:
miniupnpd_nat_chain = ary_options[i].value;
break;
#endif
case UPNPNOTIFY_INTERVAL:
v->notify_interval = atoi(ary_options[i].value);
break;
case UPNPSYSTEM_UPTIME:
if(strcmp(ary_options[i].value, "yes") == 0)
SETFLAG(SYSUPTIMEMASK); /*sysuptime = 1;*/
break;
#if defined(USE_PF) || defined(USE_IPF)
case UPNPPACKET_LOG:
if(strcmp(ary_options[i].value, "yes") == 0)
SETFLAG(LOGPACKETSMASK); /*logpackets = 1;*/
break;
#endif
case UPNPUUID:
strncpy(uuidvalue_igd+5, ary_options[i].value,
strlen(uuidvalue_igd+5) + 1);
complete_uuidvalues();
break;
case UPNPSERIAL:
strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN);
serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
break;
case UPNPMODEL_NUMBER:
strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN);
modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
break;
case UPNPCLEANTHRESHOLD:
v->clean_ruleset_threshold = atoi(ary_options[i].value);
break;
case UPNPCLEANINTERVAL:
v->clean_ruleset_interval = atoi(ary_options[i].value);
break;
#ifdef USE_PF
case UPNPANCHOR:
anchor_name = ary_options[i].value;
break;
case UPNPQUEUE:
queue = ary_options[i].value;
break;
case UPNPTAG:
tag = ary_options[i].value;
break;
#endif
#ifdef ENABLE_NATPMP
case UPNPENABLENATPMP:
if(strcmp(ary_options[i].value, "yes") == 0)
SETFLAG(ENABLENATPMPMASK); /*enablenatpmp = 1;*/
else
if(atoi(ary_options[i].value))
SETFLAG(ENABLENATPMPMASK);
/*enablenatpmp = atoi(ary_options[i].value);*/
break;
#endif
#ifdef ENABLE_PCP
case UPNPPCPMINLIFETIME:
min_lifetime = atoi(ary_options[i].value);
if (min_lifetime > 120 ) {
min_lifetime = 120;
}
break;
case UPNPPCPMAXLIFETIME:
max_lifetime = atoi(ary_options[i].value);
if (max_lifetime > 86400 ) {
max_lifetime = 86400;
}
break;
#endif
#ifdef PF_ENABLE_FILTER_RULES
case UPNPQUICKRULES:
if(strcmp(ary_options[i].value, "no") == 0)
SETFLAG(PFNOQUICKRULESMASK);
break;
#endif
case UPNPENABLE:
if(strcmp(ary_options[i].value, "yes") != 0)
CLEARFLAG(ENABLEUPNPMASK);
break;
case UPNPSECUREMODE:
if(strcmp(ary_options[i].value, "yes") == 0)
SETFLAG(SECUREMODEMASK);
break;
#ifdef ENABLE_LEASEFILE
case UPNPLEASEFILE:
lease_file = ary_options[i].value;
break;
#endif
case UPNPMINISSDPDSOCKET:
minissdpdsocketpath = ary_options[i].value;
break;
default:
fprintf(stderr, "Unknown option in file %s\n",
optionsfile);
}
}
#ifdef ENABLE_PCP
/* if lifetimes are inverse */
if (min_lifetime >= max_lifetime) {
fprintf(stderr, "Minimum lifetime (%lu) is greater than or equal to maximum lifetime (%lu).\n", min_lifetime, max_lifetime);
fprintf(stderr, "Check your configuration file.\n");
return 1;
}
#endif
}
#endif /* DISABLE_CONFIG_FILE */
/* command line arguments processing */
for(i=1; i<argc; i++)
{
if(argv[i][0]!='-')
{
fprintf(stderr, "Unknown option: %s\n", argv[i]);
}
else switch(argv[i][1])
{
case 'o':
if(i+1 < argc)
use_ext_ip_addr = argv[++i];
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
break;
case 't':
if(i+1 < argc)
v->notify_interval = atoi(argv[++i]);
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
break;
case 'r':
if(i+1 < argc)
v->clean_ruleset_interval = atoi(argv[++i]);
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
break;
case 'u':
if(i+1 < argc) {
strncpy(uuidvalue_igd+5, argv[++i], strlen(uuidvalue_igd+5) + 1);
complete_uuidvalues();
} else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
break;
#ifdef ENABLE_MANUFACTURER_INFO_CONFIGURATION
case 'z':
if(i+1 < argc)
strncpy(friendly_name, argv[++i], FRIENDLY_NAME_MAX_LEN);
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
friendly_name[FRIENDLY_NAME_MAX_LEN-1] = '\0';
break;
#endif
case 's':
if(i+1 < argc)
strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN);
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
break;
case 'm':
if(i+1 < argc)
strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN);
else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
break;
#ifdef ENABLE_NATPMP
case 'N':
/*enablenatpmp = 1;*/
SETFLAG(ENABLENATPMPMASK);