root/trunk/wifidog/src/gateway.c @ 34

Revision 34, 4.9 KB (checked in by alexcv, 9 years ago)

re-indented

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/********************************************************************\
2 * This program is free software; you can redistribute it and/or    *
3 * modify it under the terms of the GNU General Public License as   *
4 * published by the Free:Software Foundation; either version 2 of   *
5 * the License, or (at your option) any later version.              *
6 *                                                                  *
7 * This program is distributed in the hope that it will be useful,  *
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
10 * GNU General Public License for more details.                     *
11 *                                                                  *
12 * You should have received a copy of the GNU General Public License*
13 * along with this program; if not, contact:                        *
14 *                                                                  *
15 * Free Software Foundation           Voice:  +1-617-542-5942       *
16 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
17 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
18 *                                                                  *
19 \********************************************************************/
20
21/* $Header$ */
22/** @internal
23  @file gateway.c
24  @brief Main loop
25  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
26 */
27
28#include "common.h"
29
30extern s_config config;
31
32void main_loop(void)
33{
34        struct timeval tv;
35        time_t last_checked;
36        httpd * webserver;
37        int result;
38
39        /* Initialize the linked list */
40        node_init();
41
42        // Initialize the web server
43        debug(D_LOG_DEBUG, "Creating web server on %s:%d", 
44                        config.gw_address, config.gw_port);
45        webserver = httpdCreate(config.gw_address, config.gw_port);
46        if (webserver == NULL) {
47                debug(D_LOG_ERR, "Could not create web server");
48                exit(1);
49        }
50        debug(D_LOG_DEBUG, "Assigning callbacks to web server");
51        httpdAddCContent(webserver, "/wifidog", "about", 0, NULL,
52                        http_callback_about);
53        httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL,
54                        http_callback_auth);
55        httpdAddC404Content(webserver, http_callback_404);
56
57        // Init the signals to catch chld/quit/etc
58        init_signals();
59
60        // Reset the firewall
61        fw_init();
62
63        last_checked = time(NULL);
64
65        debug(D_LOG_DEBUG, "Waiting for connections");
66        while(1) {
67                tv.tv_sec = config.checkinterval;
68                tv.tv_usec = 0;
69                result = httpdGetConnection(webserver, &tv);
70                if (result < 0) {
71                        /*
72                         * fixme
73                         * An error occurred - should we abort? reboot the device ?
74                         */
75                        debug(D_LOG_ERR, "httpdGetConnection returned %d",
76                                result);
77                        exit(1);
78                } else if (result > 0) {
79                        /*
80                         * We got a connection
81                         */
82                        debug(D_LOG_DEBUG, "Received connection from %s",
83                                webserver->clientAddr);
84                        if (httpdReadRequest(webserver) >=0) {
85                                /*
86                                 * We read the request fine
87                                 */
88                                debug(D_LOG_DEBUG, "Processing request from "
89                                        "%s", webserver->clientAddr);
90                                httpdProcessRequest(webserver);
91                        }
92                        else {
93                                debug(D_LOG_ERR, "No valid request received "
94                                        "from %s", webserver->clientAddr);
95                        }
96                        debug(D_LOG_DEBUG, "Closing connection with %s",
97                                webserver->clientAddr);
98                        httpdEndRequest(webserver);
99                }
100
101                if (time(NULL) - last_checked > config.checkinterval) {
102                        fw_counter();
103                        last_checked = time(NULL);
104                }
105        }
106
107        fw_destroy();
108}
109
110        int
111main(int argc, char **argv)
112{
113        config_init();
114
115        parse_commandline(argc, argv);
116
117        config_read(config.configfile);
118        config_validate();
119
120        if (config.daemon) {
121                struct sigaction sa;
122                int childPid;
123
124                debug(D_LOG_INFO, "Forking into background");
125
126                sa.sa_handler = sigchld_handler;
127                sigemptyset(&sa.sa_mask);
128                sa.sa_flags = SA_RESTART;
129                if (sigaction(SIGCHLD, &sa, NULL) == -1) {
130                        debug(D_LOG_ERR, "sigaction(): %s", strerror(errno));
131                        exit(1);
132                }
133
134                switch((childPid = fork())) {
135                case -1: /* error */
136                        debug(D_LOG_ERR, "fork(): %s", strerror(errno));
137                        exit(1);
138                        break;
139
140                case 0: /* parent */
141                        main_loop();
142                        break;
143
144                default: /* child */
145                        exit(0);
146                        break;
147                }
148        } else {
149                main_loop();
150        }
151
152        return(0);
153}
154
155void termination_handler(int s)
156{
157        fw_destroy();
158
159        debug(D_LOG_INFO, "Exiting...");
160        exit(0);
161}
162
163void init_signals()
164{
165        struct sigaction sa;
166
167        sa.sa_handler = sigchld_handler;
168        sigemptyset(&sa.sa_mask);
169        sa.sa_flags = SA_RESTART;
170        if (sigaction(SIGCHLD, &sa, NULL) == -1) {
171                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno));
172                exit(1);
173        }
174
175        sa.sa_handler = termination_handler;
176        sigemptyset(&sa.sa_mask);
177        sa.sa_flags = SA_RESTART;
178
179        /* Trap SIGTERM */
180        if (sigaction(SIGTERM, &sa, NULL) == -1) {
181                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno));
182                exit(1);
183        }
184
185        /* Trap SIGQUIT */
186        if (sigaction(SIGQUIT, &sa, NULL) == -1) {
187                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno));
188                exit(1);
189        }
190
191        /* Trap SIGINT */
192        if (sigaction(SIGINT, &sa, NULL) == -1) {
193                debug(D_LOG_ERR, "sigaction(): %s", strerror(errno));
194                exit(1);
195        }
196}
197
Note: See TracBrowser for help on using the browser.