| 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 | |
|---|
| 30 | extern s_config config; |
|---|
| 31 | |
|---|
| 32 | void |
|---|
| 33 | main_loop(void) |
|---|
| 34 | { |
|---|
| 35 | struct timeval tv; |
|---|
| 36 | time_t last_checked; |
|---|
| 37 | httpd * webserver; |
|---|
| 38 | int result; |
|---|
| 39 | pthread_t tid; |
|---|
| 40 | |
|---|
| 41 | /* Initialize the linked list */ |
|---|
| 42 | node_init(); |
|---|
| 43 | |
|---|
| 44 | // Initialize the web server |
|---|
| 45 | debug(D_LOG_DEBUG, "Creating web server on %s:%d", |
|---|
| 46 | config.gw_address, config.gw_port); |
|---|
| 47 | webserver = httpdCreate(config.gw_address, config.gw_port); |
|---|
| 48 | if (webserver == NULL) { |
|---|
| 49 | debug(D_LOG_ERR, "Could not create web server"); |
|---|
| 50 | exit(1); |
|---|
| 51 | } |
|---|
| 52 | debug(D_LOG_DEBUG, "Assigning callbacks to web server"); |
|---|
| 53 | httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, |
|---|
| 54 | http_callback_about); |
|---|
| 55 | httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, |
|---|
| 56 | http_callback_auth); |
|---|
| 57 | httpdAddC404Content(webserver, http_callback_404); |
|---|
| 58 | |
|---|
| 59 | // Init the signals to catch chld/quit/etc |
|---|
| 60 | init_signals(); |
|---|
| 61 | |
|---|
| 62 | // Reset the firewall |
|---|
| 63 | fw_init(); |
|---|
| 64 | |
|---|
| 65 | /* start clean up thread */ |
|---|
| 66 | pthread_create(&tid, NULL, (void *)cleanup_thread, NULL); |
|---|
| 67 | pthread_detach(tid); |
|---|
| 68 | |
|---|
| 69 | debug(D_LOG_DEBUG, "Waiting for connections"); |
|---|
| 70 | while(1) { |
|---|
| 71 | tv.tv_sec = config.checkinterval; |
|---|
| 72 | tv.tv_usec = 0; |
|---|
| 73 | result = httpdGetConnection(webserver, &tv); |
|---|
| 74 | if (result == -1) { |
|---|
| 75 | /* Interrupted system call */ |
|---|
| 76 | continue; /* restart loop */ |
|---|
| 77 | } else if (result < -1) { |
|---|
| 78 | /* |
|---|
| 79 | * FIXME |
|---|
| 80 | * An error occurred - should we abort? |
|---|
| 81 | * reboot the device ? |
|---|
| 82 | */ |
|---|
| 83 | debug(D_LOG_ERR, "httpdGetConnection returned %d", |
|---|
| 84 | result); |
|---|
| 85 | fw_destroy(); |
|---|
| 86 | exit(1); |
|---|
| 87 | } else if (result > 0) { |
|---|
| 88 | /* |
|---|
| 89 | * We got a connection |
|---|
| 90 | */ |
|---|
| 91 | debug(D_LOG_DEBUG, "Received connection from %s", |
|---|
| 92 | webserver->clientAddr); |
|---|
| 93 | if (httpdReadRequest(webserver) >=0) { |
|---|
| 94 | /* |
|---|
| 95 | * We read the request fine |
|---|
| 96 | */ |
|---|
| 97 | debug(D_LOG_DEBUG, "Processing request from " |
|---|
| 98 | "%s", webserver->clientAddr); |
|---|
| 99 | httpdProcessRequest(webserver); |
|---|
| 100 | } |
|---|
| 101 | else { |
|---|
| 102 | debug(D_LOG_ERR, "No valid request received " |
|---|
| 103 | "from %s", webserver->clientAddr); |
|---|
| 104 | } |
|---|
| 105 | debug(D_LOG_DEBUG, "Closing connection with %s", |
|---|
| 106 | webserver->clientAddr); |
|---|
| 107 | httpdEndRequest(webserver); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | fw_destroy(); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | int |
|---|
| 115 | main(int argc, char **argv) |
|---|
| 116 | { |
|---|
| 117 | config_init(); |
|---|
| 118 | |
|---|
| 119 | parse_commandline(argc, argv); |
|---|
| 120 | |
|---|
| 121 | config_read(config.configfile); |
|---|
| 122 | config_validate(); |
|---|
| 123 | |
|---|
| 124 | init_userclasses(0); |
|---|
| 125 | |
|---|
| 126 | if (config.daemon) { |
|---|
| 127 | int childPid; |
|---|
| 128 | |
|---|
| 129 | debug(D_LOG_INFO, "Forking into background"); |
|---|
| 130 | |
|---|
| 131 | switch((childPid = fork())) { |
|---|
| 132 | case -1: /* error */ |
|---|
| 133 | debug(D_LOG_ERR, "fork(): %s", strerror(errno)); |
|---|
| 134 | exit(1); |
|---|
| 135 | break; |
|---|
| 136 | |
|---|
| 137 | case 0: /* parent */ |
|---|
| 138 | main_loop(); |
|---|
| 139 | break; |
|---|
| 140 | |
|---|
| 141 | default: /* child */ |
|---|
| 142 | exit(0); |
|---|
| 143 | break; |
|---|
| 144 | } |
|---|
| 145 | } else { |
|---|
| 146 | main_loop(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return(0); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void |
|---|
| 153 | sigchld_handler(int s) |
|---|
| 154 | { |
|---|
| 155 | int status; |
|---|
| 156 | |
|---|
| 157 | wait(&status); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void |
|---|
| 161 | termination_handler(int s) |
|---|
| 162 | { |
|---|
| 163 | fw_destroy(); |
|---|
| 164 | |
|---|
| 165 | debug(D_LOG_INFO, "Exiting..."); |
|---|
| 166 | exit(0); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void |
|---|
| 170 | init_signals(void) |
|---|
| 171 | { |
|---|
| 172 | struct sigaction sa; |
|---|
| 173 | |
|---|
| 174 | debug(D_LOG_DEBUG, "Initializing signal handlers"); |
|---|
| 175 | |
|---|
| 176 | sa.sa_handler = sigchld_handler; |
|---|
| 177 | sigemptyset(&sa.sa_mask); |
|---|
| 178 | sa.sa_flags = SA_RESTART; |
|---|
| 179 | if (sigaction(SIGCHLD, &sa, NULL) == -1) { |
|---|
| 180 | debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); |
|---|
| 181 | exit(1); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | sa.sa_handler = termination_handler; |
|---|
| 185 | sigemptyset(&sa.sa_mask); |
|---|
| 186 | sa.sa_flags = SA_RESTART; |
|---|
| 187 | |
|---|
| 188 | /* Trap SIGTERM */ |
|---|
| 189 | if (sigaction(SIGTERM, &sa, NULL) == -1) { |
|---|
| 190 | debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); |
|---|
| 191 | exit(1); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /* Trap SIGQUIT */ |
|---|
| 195 | if (sigaction(SIGQUIT, &sa, NULL) == -1) { |
|---|
| 196 | debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); |
|---|
| 197 | exit(1); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /* Trap SIGINT */ |
|---|
| 201 | if (sigaction(SIGINT, &sa, NULL) == -1) { |
|---|
| 202 | debug(D_LOG_ERR, "sigaction(): %s", strerror(errno)); |
|---|
| 203 | exit(1); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|