int main(gint argc, gchar **argv) { // int i; fw_debug_set(5); fw_debug_funcs_set(5); fw_debug_filepos_set(5); // fw_debug_off(); // fw_debug_funcs_off(); // fw_debug_filepos_off(); fw_init(argc, argv); int i; for (i = 0; i < 1000000; i++) { printf("%d\n", fwrules_read()); printf("==================================\n"); } // fwrules_entry_add(1, // ENTRY_SESSION, // "lo", "incoming", "tcp", "0/0", 10000, "ACCEPT", "wegorz", "*", "*", 10); fwrules_print(stdout); fwrules_write(); // fwrules_entries_print(); // fwrules_entry_print(0); // fwrules_entry_move(1, 0); // printf("Action: %d\n", fwrules_entry_get_action(-2, "eth0", DIR_INCOMING, PROTO_UDP, "0/0", 53, "*")); return 0; }
void fw_enable(void) { struct fwohci_softc *sc; int i; if (fw_initialized == 0) fw_init(); for (i = 0; i < MAX_OHCI; i ++) { sc = &fwinfo[i]; if (sc->state != FWOHCI_STATE_INIT) break; sc->config_rom = (uint32_t *) (((uint32_t)sc->config_rom_buf + (CROMSIZE - 1)) & ~(CROMSIZE - 1)); #if 0 printf("configrom: %08p %08p\n", sc->config_rom_buf, sc->config_rom); #endif if (fwohci_init(sc, 0) == 0) { sc->state = FWOHCI_STATE_ENABLED; fw_busreset(sc); } else sc->state = FWOHCI_STATE_DEAD; } }
int main (int argc, char **argv) { GMainLoop *loop; GIOChannel *sock; /* Initialize signal handlers */ signal(SIGPIPE, SIG_IGN); signal(SIGINT, handle_sigint); /* read nocat.conf */ read_conf_file( NC_CONF_PATH "/nocat.conf" ); initialize_driver(); /* initialize the firewall */ fw_init( nocat_conf ); /* initialize the peer table */ peer_tab = g_hash_new(); /* initialize the listen socket */ sock = http_bind_socket( CONF("GatewayAddr"), CONFd("GatewayPort"), CONFd("ListenQueue") ); /* initialize the main loop and handlers */ loop = g_main_new(FALSE); g_io_add_watch( sock, G_IO_IN, (GIOFunc) handle_accept, NULL ); g_timeout_add( 30000, (GSourceFunc) check_peers, NULL ); g_timeout_add( 1000, (GSourceFunc) check_sigint, loop ); /* Go! */ g_message("starting main loop"); g_main_run( loop ); g_message("exiting main loop"); return 0; }
int main(void) { apr_status_t rv = APR_SUCCESS; apr_status_t s = APR_SUCCESS; properties_pt config = NULL; char *autoStart = NULL; apr_pool_t *pool = NULL; bundle_pt fwBundle = NULL; rv = apr_initialize(); if (rv != APR_SUCCESS) { return CELIX_START_ERROR; } apr_pool_t *memoryPool; s = apr_pool_create(&memoryPool, NULL); if (s != APR_SUCCESS) { return CELIX_START_ERROR; } struct framework * framework = NULL; celix_status_t status = CELIX_SUCCESS; status = framework_create(&framework, memoryPool, config); if (status == CELIX_SUCCESS) { status = fw_init(framework); if (status == CELIX_SUCCESS) { // Start the system bundle framework_getFrameworkBundle(framework, &fwBundle); bundle_start(fwBundle); bundle_context_pt context = NULL; bundle_getContext(fwBundle, &context); // do some stuff bundle_pt bundle = NULL; status = CELIX_DO_IF(status, bundleContext_installBundle(context, "../test_bundle1/test_bundle1.zip", &bundle)); status = CELIX_DO_IF(status, bundle_start(bundle)); // Stop the system bundle, sleep a bit to let stuff settle down sleep(5); bundle_stop(fwBundle); framework_destroy(framework); } } if (status != CELIX_SUCCESS) { printf("Problem creating framework\n"); } apr_pool_destroy(memoryPool); apr_terminate(); printf("LAUNCHER: Exit\n"); return 0; }
int main (int argc, char **argv) { GMainLoop *loop; GIOChannel *sock; /* read nocat.conf */ read_conf_file( NC_CONF_PATH "/nocat.conf" ); if (argc < 2 || strncmp(argv[1], "-D", 2) != 0) daemonize(); /* initalize the log */ initialize_log(); /* set network parameters */ set_network_defaults( nocat_conf ); /* initialize the gateway type driver */ initialize_driver(); /* initialize the firewall */ fw_init( nocat_conf ); /* initialize the peer table */ peer_tab = g_hash_new(); /* initialize the listen socket */ sock = http_bind_socket( CONF("GatewayAddr"), CONFd("GatewayPort"), CONFd("ListenQueue") ); /* initialize the main loop and handlers */ loop = g_main_new(FALSE); g_io_add_watch( sock, G_IO_IN, (GIOFunc) handle_accept, NULL ); g_timeout_add( 30000, (GSourceFunc) check_peers, NULL ); g_timeout_add( 1000, (GSourceFunc) check_exit_signal, loop ); /* Go! */ g_message("starting main loop"); g_main_run( loop ); g_message("exiting main loop"); return 0; }
/**************************************************************************//** @brief hogehogehoge. @param[in] argc arguments count @param[in] argv arguments value @retval 0 success @retval <0 error occurred *****************************************************************************/ int main(int argc, char **argv) { (void)argc; /* unused */ (void)argv; /* unused */ int ret = 0; /* Step 1: initialize process */ if (fw_init() != 0) { printf("Initialize failed.\n"); exit(EXIT_FAILURE); } /* Step 2: main loop */ TAPP_DEBUG("main loop start\n"); while (fw_is_exit() == FALSE) { // if (fw_is_exit() == FALSE) { //modified by CJ 2014/08/21 9:55AM /* === dialog === */ if (fw_cmd_dialog() < 0) { ret = -1; break; } usleep(10000); //modified by CJ 2014/08/21 10:17AM /* === body function === */ /* tapp_cam_main(); tapp_audio_main(); tapp_util_main(); tapp_vana_main(); tapp_extd_main(); tapp_af_main(); tapp_streaming_main(); */ } /* Step 3: exit process */ TAPP_DEBUG("tappd exit\n"); fw_exit(); return ret; }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); request *r; void **params; /* Set the time when wifidog started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* If we don't have the Gateway IP address, get it. Can't fail. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_address); } /* If we don't have the Gateway ID, construct it from the internal MAC address. * "Can't fail" so exit() if the impossible happens. */ if (!config->gw_mac) { debug(LOG_DEBUG, "Finding MAC address of %s", config->gw_interface); if ((config->gw_mac = get_iface_mac(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_mac); } /* Initializes the web server */ debug(LOG_NOTICE, "Creating web server on %s:%d", config->gw_address, config->gw_port); if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } debug(LOG_DEBUG, "Assigning callbacks to web server"); httpdAddCContent(webserver, "/", "ctbrihuang", 0, NULL, http_callback_wifidog); httpdAddCContent(webserver, "/ctbrihuang", "", 0, NULL, http_callback_wifidog); httpdAddCContent(webserver, "/debug", "", 0, NULL, http_callback_404); httpdAddCContent(webserver, "/ctbrihuang", "about", 0, NULL, http_callback_about); httpdAddCContent(webserver, "/ctbrihuang", "status", 0, NULL, http_callback_status); httpdAddCContent(webserver, "/smartwifi", "auth", 0, NULL, http_callback_auth); /*httpdAddCContent(webserver, "/ctbrihuang", "logout", 0, NULL, http_callback_logout); */ httpdAddC404Content(webserver, http_callback_404); fw_destroy(); if (!fw_init()) { debug(LOG_ERR, "FATAL: Failed to initialize firewall"); exit(1); } /* Start update thread */ result = pthread_create(&tid_update, NULL, (void *)thread_update, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (update) - exiting"); termination_handler(0); } pthread_detach(tid_update); /* Start clean up thread */ result = pthread_create(&tid_fw_counter, NULL, (void *)thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (fw_counter) - exiting"); termination_handler(0); } pthread_detach(tid_fw_counter); /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_wdctl, (void *)safe_strdup(config->wdctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (wdctl) - exiting"); termination_handler(0); } pthread_detach(tid); /* Start heartbeat thread */ result = pthread_create(&tid_ping, NULL, (void *)thread_ping, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (ping) - exiting"); termination_handler(0); } pthread_detach(tid_ping); result = pthread_create(&tid_ding, NULL, (void *)thread_ding, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (ding) - exiting"); termination_handler(0); } pthread_detach(tid_ding); result = pthread_create(&tid_authlog, NULL, (void *)thread_client_timeout_log, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread authlog - exiting"); termination_handler(0); } pthread_detach(tid_authlog); debug(LOG_NOTICE, "Waiting for connections"); while(1) { webserver->lastError = 0; r = httpdGetConnection(webserver, NULL); /* We can't convert this to a switch because there might be * values that are not -1, 0 or 1. */ if (webserver->lastError == -1) { /* Interrupted system call */ debug(LOG_DEBUG, "lastError is -1"); continue; /* restart loop */ } else if (webserver->lastError < -1) { /* * FIXME * An error occurred - should we abort? * reboot the device ? */ debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError); termination_handler(0); } else if (r != NULL) { /* * We got a connection * * We should create another thread */ debug(LOG_INFO, "Received connection from %s, spawning worker thread", r->clientAddr); /* The void**'s are a simulation of the normal C * function calling sequence. */ params = safe_malloc(2 * sizeof(void *)); *params = webserver; *(params + 1) = r; result = pthread_create(&tid, NULL, (void *)thread_httpd, (void *)params); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (httpd) - exiting"); termination_handler(0); } pthread_detach(tid); } else { debug(LOG_DEBUG, "lastError=%d", webserver->lastError); /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } } /* never reached */ }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); struct timespec wait_time; int msec; request *r; void **params; int* thread_serial_num_p; /* Set the time when nodogsplash started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* If we don't have the Gateway IP address, get it. Exit on failure. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } if ((config->gw_mac = get_iface_mac(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_NOTICE, "Detected gateway %s at %s (%s)", config->gw_interface, config->gw_address, config->gw_mac); } /* Initializes the web server */ if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } debug(LOG_NOTICE, "Created web server on %s:%d", config->gw_address, config->gw_port); /* Set web root for server */ debug(LOG_DEBUG, "Setting web root: %s",config->webroot); httpdSetFileBase(webserver,config->webroot); /* Add images files to server: any file in config->imagesdir can be served */ debug(LOG_DEBUG, "Setting images subdir: %s",config->imagesdir); httpdAddWildcardContent(webserver,config->imagesdir,NULL,config->imagesdir); /* Add pages files to server: any file in config->pagesdir can be served */ debug(LOG_DEBUG, "Setting pages subdir: %s",config->pagesdir); httpdAddWildcardContent(webserver,config->pagesdir,NULL,config->pagesdir); debug(LOG_DEBUG, "Registering callbacks to web server"); httpdAddCContent(webserver, "/", "", 0, NULL, http_nodogsplash_callback_index); httpdAddCWildcardContent(webserver, config->authdir, NULL, http_nodogsplash_callback_auth); httpdAddCWildcardContent(webserver, config->denydir, NULL, http_nodogsplash_callback_deny); httpdAddC404Content(webserver, http_nodogsplash_callback_404); /* Reset the firewall (cleans it, in case we are restarting after nodogsplash crash) */ fw_destroy(); /* Then initialize it */ debug(LOG_NOTICE, "Initializing firewall rules"); if( fw_init() != 0 ) { debug(LOG_ERR, "Error initializing firewall rules! Cleaning up"); fw_destroy(); debug(LOG_ERR, "Exiting because of error initializing firewall rules"); exit(1); } /* Start client statistics and timeout clean-up thread */ result = pthread_create(&tid_client_check, NULL, (void *)thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_client_timeout_check - exiting"); termination_handler(0); } pthread_detach(tid_client_check); /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_ndsctl, (void *)safe_strdup(config->ndsctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_ndsctl - exiting"); termination_handler(0); } pthread_detach(tid); /* * Enter the httpd request handling loop */ debug(LOG_NOTICE, "Waiting for connections"); while(1) { r = httpdGetConnection(webserver, NULL); /* We can't convert this to a switch because there might be * values that are not -1, 0 or 1. */ if (webserver->lastError == -1) { /* Interrupted system call */ continue; /* continue loop from the top */ } else if (webserver->lastError < -1) { /* * FIXME * An error occurred - should we abort? * reboot the device ? */ debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError); termination_handler(0); } else if (r != NULL) { /* We got a connection */ handle_http_request(webserver, r); } else { /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } } /* never reached */ }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); request *r; void **params; /* Set the time when wifidog started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* save the pid file if needed */ if ((!config) && (!config->pidfile)) save_pid_file(config->pidfile); /* If we don't have the Gateway IP address, get it. Can't fail. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_address); } /* If we don't have the Gateway ID, construct it from the internal MAC address. * "Can't fail" so exit() if the impossible happens. */ if (!config->gw_id) { debug(LOG_DEBUG, "Finding MAC address of %s", config->external_interface); if ((config->gw_id = get_iface_mac(config->external_interface)) == NULL) { debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->external_interface); exit(1); } debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_id); } /* Initializes the web server */ debug(LOG_NOTICE, "Creating web server on %s:%d", config->gw_address, config->gw_port); if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } register_fd_cleanup_on_fork(webserver->serverSock); debug(LOG_DEBUG, "Assigning callbacks to web server"); httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about); httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status); httpdAddCContent(webserver, "/wifidog", "release", 0, NULL, http_callback_release); httpdAddCContent(webserver, "/wifidog", "allow", 0, NULL, http_callback_allow_redirect); httpdSetErrorFunction(webserver, 404, http_callback_404); /* Set the auth server ip address. */ set_auth_svr_lastip(config); /* Reset the firewall (if WiFiDog crashed) */ fw_destroy(); /* Then initialize it */ if (!fw_init()) { debug(LOG_ERR, "FATAL: Failed to initialize firewall"); exit(1); } /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_wdctl, (void *)safe_strdup(config->wdctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (wdctl) - exiting"); termination_handler(0); } pthread_detach(tid); debug(LOG_NOTICE, "Waiting for connections"); while (1) { r = httpdGetConnection(webserver, NULL); /* We can't convert this to a switch because there might be * values that are not -1, 0 or 1. */ if (webserver->lastError == -1) { /* Interrupted system call */ if (NULL != r) { httpdEndRequest(r); } } else if (webserver->lastError < -1) { /* * FIXME * An error occurred - should we abort? * reboot the device ? */ debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError); termination_handler(0); } else if (r != NULL) { /* * We got a connection * * We should create another thread */ debug(LOG_INFO, "Received connection from %s, spawning worker thread", r->clientAddr); /* The void**'s are a simulation of the normal C * function calling sequence. */ params = safe_malloc(2 * sizeof(void *)); *params = webserver; *(params + 1) = r; result = pthread_create(&tid, NULL, (void *)thread_httpd, (void *)params); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (httpd) - exiting"); termination_handler(0); } pthread_detach(tid); } else { /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } //update the auth server ip update_auth_svr_lastip(config); } /* never reached */ }
/**@internal * Main execution loop */ static void main_loop(void) { int result = 0; pthread_t tid; s_config *config; config = config_get_config(); /* Set the time when nodogsplash started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* If we don't have the Gateway IP address, get it. Exit on failure. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } } if ((config->gw_mac = get_iface_mac(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_NOTICE, "Detected gateway %s at %s (%s)", config->gw_interface, config->gw_address, config->gw_mac); /* Initializes the web server */ if ((webserver = MHD_start_daemon( MHD_USE_EPOLL_INTERNALLY, config->gw_port, NULL, NULL, libmicrohttpd_cb, NULL, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_LISTENING_ADDRESS_REUSE, 1, MHD_OPTION_END)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } /* TODO: set listening socket */ debug(LOG_NOTICE, "Created web server on %s:%d", config->gw_address, config->gw_port); /* httpdAddCContent(webserver, "/", "", 0, NULL, http_nodogsplash_callback_index); httpdAddCWildcardContent(webserver, config->authdir, NULL, http_nodogsplash_callback_auth); httpdAddCWildcardContent(webserver, config->denydir, NULL, http_nodogsplash_callback_deny); httpdAddC404Content(webserver, http_nodogsplash_callback_404); */ /* Reset the firewall (cleans it, in case we are restarting after nodogsplash crash) */ fw_destroy(); /* Then initialize it */ debug(LOG_NOTICE, "Initializing firewall rules"); if (fw_init() != 0) { debug(LOG_ERR, "Error initializing firewall rules! Cleaning up"); fw_destroy(); debug(LOG_ERR, "Exiting because of error initializing firewall rules"); exit(1); } /* Start client statistics and timeout clean-up thread */ result = pthread_create(&tid_client_check, NULL, thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_client_timeout_check - exiting"); termination_handler(0); } pthread_detach(tid_client_check); /* Start control thread */ result = pthread_create(&tid, NULL, thread_ndsctl, (void *)(config->ndsctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_ndsctl - exiting"); termination_handler(1); } result = pthread_join(tid, NULL); if (result) { debug(LOG_INFO, "Failed to wait for nodogsplash thread."); } MHD_stop_daemon(webserver); termination_handler(result); }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); struct timespec wait_time; int msec; request *r; void **params; FILE *fh; int* thread_serial_num_p; /* Set the time when nodogsplash started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* If we don't have the Gateway IP address, get it. Exit on failure. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_NOTICE, "Detected gateway %s at %s", config->gw_interface, config->gw_address); } /* Initializes the web server */ if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } debug(LOG_NOTICE, "Created web server on %s:%d", config->gw_address, config->gw_port); /* Set web root for server */ debug(LOG_DEBUG, "Setting web root: %s",config->webroot); httpdSetFileBase(webserver,config->webroot); /* Add images files to server: any file in config->imagesdir can be served */ debug(LOG_DEBUG, "Setting images subdir: %s",config->imagesdir); httpdAddWildcardContent(webserver,config->imagesdir,NULL,config->imagesdir); /* Add pages files to server: any file in config->pagesdir can be served */ debug(LOG_DEBUG, "Setting pages subdir: %s",config->pagesdir); httpdAddWildcardContent(webserver,config->pagesdir,NULL,config->pagesdir); debug(LOG_DEBUG, "Registering callbacks to web server"); httpdAddCContent(webserver, "/", "", 0, NULL, http_nodogsplash_callback_index); httpdAddCWildcardContent(webserver, config->authdir, NULL, http_nodogsplash_callback_auth); httpdAddCWildcardContent(webserver, config->denydir, NULL, http_nodogsplash_callback_deny); httpdAddC404Content(webserver, http_nodogsplash_callback_404); /* Reset the firewall (cleans it, in case we are restarting after nodogsplash crash) */ fw_destroy(); /* Then initialize it */ debug(LOG_NOTICE, "Initializing firewall rules"); if( fw_init() != 0 ) { debug(LOG_ERR, "Error initializing firewall rules! Cleaning up"); fw_destroy(); debug(LOG_ERR, "Exiting because of error initializing firewall rules"); exit(1); } /* Start client statistics and timeout clean-up thread */ result = pthread_create(&tid_client_check, NULL, (void *)thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_client_timeout_check - exiting"); termination_handler(0); } pthread_detach(tid_client_check); /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_ndsctl, (void *)safe_strdup(config->ndsctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_ndsctl - exiting"); termination_handler(0); } pthread_detach(tid); /* * Enter the httpd request handling loop */ debug(LOG_NOTICE, "Waiting for connections"); created_httpd_threads = 0; current_httpd_threads = 0; while(1) { r = httpdGetConnection(webserver, NULL); /* We can't convert this to a switch because there might be * values that are not -1, 0 or 1. */ if (webserver->lastError == -1) { /* Interrupted system call */ continue; /* continue loop from the top */ } else if (webserver->lastError < -1) { /* * FIXME * An error occurred - should we abort? * reboot the device ? */ debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError); termination_handler(0); } else if (r != NULL) { /* * We got a connection * * We create another thread to handle the request, * possibly sleeping first if there are too many already */ debug(LOG_DEBUG,"%d current httpd threads.", current_httpd_threads); if(config->decongest_httpd_threads && current_httpd_threads >= config->httpd_thread_threshold) { msec = current_httpd_threads * config->httpd_thread_delay_ms; wait_time.tv_sec = msec / 1000; wait_time.tv_nsec = (msec % 1000) * 1000000; debug(LOG_INFO, "Httpd thread creation delayed %ld sec %ld nanosec for congestion.", wait_time.tv_sec, wait_time.tv_nsec); nanosleep(&wait_time,NULL); } thread_serial_num_p = (int*) malloc(sizeof(int)); /* thread_httpd() must free */ *thread_serial_num_p = created_httpd_threads; debug(LOG_INFO, "Creating httpd request thread %d for %s", *thread_serial_num_p, r->clientAddr); /* The void**'s are a simulation of the normal C * function calling sequence. */ params = safe_malloc(3 * sizeof(void *)); /* thread_httpd() must free */ *params = webserver; *(params + 1) = r; *(params + 2) = thread_serial_num_p; created_httpd_threads++; result = pthread_create(&tid, NULL, (void *)thread_httpd, (void *)params); if (result != 0) { debug(LOG_ERR, "FATAL: pthread_create failed to create httpd request thread - exiting..."); termination_handler(0); } pthread_detach(tid); } else { /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } } /* never reached */ }