1/*
2 * Copyright 2004, Broadcom Corporation
3 * All Rights Reserved.
4 *
5 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
9 *
10 * $Id: linux_main.c,v 1.1.1.1 2008/10/15 03:31:35 james26_jang Exp $
11 */
12
13#include <errno.h>	    // for errno, of course.
14#include <error.h>	    // for perror
15#include <signal.h>	    // for signal, etc.
16#include <assert.h>	    // for assert, of course.
17#include <stdlib.h>	    // for malloc, free, etc.
18#include <string.h>	    // for memset, strncasecmp, etc.
19#include <stdarg.h>	    // for va_list, etc.
20#include <stdio.h>	    // for printf, perror, fopen, fclose, etc.
21#include <net/if.h>	    // for struct ifreq, etc.
22#include <sys/ioctl.h>	    // for SIOCGIFCONF, etc.
23#include <fcntl.h>	    // for fcntl, F_GETFL, etc.
24#include <unistd.h>	    // for read, write, etc.
25#include <arpa/inet.h>	    // for inet_aton, inet_addr, etc.
26#include <time.h>	    // for time
27#include <netinet/in.h>	    // for sockaddr_in
28#include <wait.h>	    // for sockaddr_in
29
30
31#include "ctype.h"
32#include "upnp_dbg.h"
33#include "upnp.h"
34
35
36extern void define_variable(char *name, char *value);
37extern void uuidstr_create(char *);
38extern char *strip_chars(char *, char *);
39extern void init_devices();
40void init_event_queue(int);
41
42extern struct net_connection *net_connections;
43extern int global_exit_now;
44extern struct iface *global_lans;
45
46static void
47reap(int sig)
48{
49	pid_t pid;
50
51	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
52		UPNP_TRACE(("Reaped %d\n", pid));
53}
54
55
56int main(int argc, char *argv[])
57{
58    extern char g_wandevs[];
59    extern char g_landevs[];
60    extern DeviceTemplate IGDeviceTemplate;
61    char **argp = &argv[1];
62    char *wanif = NULL;
63    char *lanif = NULL;
64    int daemonize = 0;
65
66    while (argp < &argv[argc]) {
67	if (strcasecmp(*argp, "-L") == 0) {
68	    lanif = *++argp;
69	}
70	else if (strcasecmp(*argp, "-W") == 0) {
71	    wanif = *++argp;
72	    strcpy(g_wandevs, wanif);
73	}
74	else if (strcasecmp(*argp, "-D") == 0) {
75	    daemonize = 1;
76	}
77	argp++;
78    }
79
80    init_event_queue(40);
81
82    if (lanif == NULL || wanif == NULL) {
83	fprintf(stderr, "usage: %s -L lan_ifname -W wan_ifname\n", argv[0]);
84    } else {
85	if (daemonize && daemon(1, 1) == -1) {
86	    perror("daemon");
87	    exit(errno);
88	}
89
90	/* We need to have a reaper for child processes we may create.
91	   That happens when we send signals to the dhcp process to
92	   release an renew a lease on the external interface. */
93	signal(SIGCHLD, reap);
94
95	/* For some reason that I do not understand, this process gets
96	   a SIGTERM after sending SIGUSR1 to the dhcp process (to
97	   renew a lease).  Ignore SIGTERM to avoid being killed when
98	   this happens.  */
99	//	signal(SIGTERM, SIG_IGN);
100	signal(SIGUSR1, SIG_IGN);
101
102	fprintf(stderr, "calling upnp_main\n");
103	upnp_main(&IGDeviceTemplate, lanif);
104    }
105
106    return 0;
107}
108
109