static gboolean initialize_device(struct upnp_device_descriptor *device_def, struct upnp_device *result_device, const char *ip_address, unsigned short port) { int rc; char *buf; rc = UpnpInit(ip_address, port); if (UPNP_E_SUCCESS != rc) { Log_error("upnp", "UpnpInit(ip=%s, port=%d) Error: %s (%d)", ip_address, port, UpnpGetErrorMessage(rc), rc); return FALSE; } Log_info("upnp", "Registered IP=%s port=%d\n", UpnpGetServerIpAddress(), UpnpGetServerPort()); rc = UpnpEnableWebserver(TRUE); if (UPNP_E_SUCCESS != rc) { Log_error("upnp", "UpnpEnableWebServer() Error: %s (%d)", UpnpGetErrorMessage(rc), rc); return FALSE; } if (!webserver_register_callbacks()) return FALSE; rc = UpnpAddVirtualDir("/upnp"); if (UPNP_E_SUCCESS != rc) { Log_error("upnp", "UpnpAddVirtualDir() Error: %s (%d)", UpnpGetErrorMessage(rc), rc); return FALSE; } buf = upnp_create_device_desc(device_def); rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, buf, strlen(buf), 1, &event_handler, result_device, &(result_device->device_handle)); free(buf); if (UPNP_E_SUCCESS != rc) { Log_error("upnp", "UpnpRegisterRootDevice2() Error: %s (%d)", UpnpGetErrorMessage(rc), rc); return FALSE; } rc = UpnpSendAdvertisement(result_device->device_handle, 100); if (UPNP_E_SUCCESS != rc) { Log_error("unpp", "Error sending advertisements: %s (%d)", UpnpGetErrorMessage(rc), rc); return FALSE; } return TRUE; }
int TvDeviceStart(char *ip_address, unsigned short port, const char *desc_doc_name, const char *web_dir_path, print_string pfun, int combo) { int ret = UPNP_E_SUCCESS; char desc_doc_url[DESC_URL_SIZE]; ithread_mutex_init(&TVDevMutex, NULL); SampleUtil_Initialize(pfun); SampleUtil_Print("Initializing UPnP Sdk with\n" "\tipaddress = %s port = %u\n", ip_address ? ip_address : "{NULL}", port); ret = UpnpInit2(ip_address, port); if (ret != UPNP_E_SUCCESS) { SampleUtil_Print("Error with UpnpInit2 -- %d\n", ret); UpnpFinish(); return ret; } ip_address = UpnpGetServerIpAddress(); port = UpnpGetServerPort(); SampleUtil_Print("UPnP Initialized\n" "\tipaddress = %s port = %u\n", ip_address ? ip_address : "{NULL}", port); if (!desc_doc_name) { if (combo) { desc_doc_name = "tvcombodesc.xml"; } else { desc_doc_name = "tvdevicedesc.xml"; } } if (!web_dir_path) { web_dir_path = DEFAULT_WEB_DIR; } snprintf(desc_doc_url, DESC_URL_SIZE, "http://%s:%d/%s", ip_address, port, desc_doc_name); SampleUtil_Print("Specifying the webserver root directory -- %s\n", web_dir_path); ret = UpnpSetWebServerRootDir(web_dir_path); if (ret != UPNP_E_SUCCESS) { SampleUtil_Print ("Error specifying webserver root directory -- %s: %d\n", web_dir_path, ret); UpnpFinish(); return ret; } SampleUtil_Print("Registering the RootDevice\n" "\t with desc_doc_url: %s\n", desc_doc_url); ret = UpnpRegisterRootDevice(desc_doc_url, TvDeviceCallbackEventHandler, &device_handle, &device_handle); if (ret != UPNP_E_SUCCESS) { SampleUtil_Print("Error registering the rootdevice : %d\n", ret); UpnpFinish(); return ret; } else { SampleUtil_Print("RootDevice Registered\n" "Initializing State Table\n"); TvDeviceStateTableInit(desc_doc_url); SampleUtil_Print("State Table Initialized\n"); ret = UpnpSendAdvertisement(device_handle, default_advr_expire); if (ret != UPNP_E_SUCCESS) { SampleUtil_Print("Error sending advertisements : %d\n", ret); UpnpFinish(); return ret; } SampleUtil_Print("Advertisements Sent\n"); } return UPNP_E_SUCCESS; }
int main (int argc, char** argv) { int ret = UPNP_E_SUCCESS; int signal; char descDocUrl[50]; char descDocName[20]; char xmlPath[50]; char intIpAddress[32]; // Server internal ip address char extIpAddress[32]; sigset_t sigsToCatch; pid_t pid,sid; if (argc != 3) { printf("Usage: upnpd <external ifname> <internal ifname>\n"); printf("Example: upnpd ppp0 eth0\n"); printf("Example: upnpd eth1 eth0\n"); exit(0); } parseConfigFile(&g_forwardRules,&g_debug,g_iptables, g_forwardChainName,g_preroutingChainName, g_upstreamBitrate,g_downstreamBitrate, descDocName,xmlPath); // Save the interface names for later uses strcpy(g_extInterfaceName, argv[1]); strcpy(g_intInterfaceName, argv[2]); // Get the internal and external ip address to start the daemon on /* Added by Yanhua */ while( (GetIpAddressStr(intIpAddress, g_intInterfaceName)==0) || (GetIpAddressStr(extIpAddress, g_extInterfaceName)==0) ) { sleep(2); } #if 0 // Put igd in the background as a daemon process. pid = vfork(); if (pid < 0) { perror("Error forking a new process."); exit(EXIT_FAILURE); } if (pid > 0) exit(EXIT_SUCCESS); if ((sid = setsid()) < 0) { perror("Error running setsid"); exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { perror("Error setting root directory"); exit(EXIT_FAILURE); } umask(0); close(STDERR_FILENO); close (STDIN_FILENO); close (STDOUT_FILENO); #endif // End Daemon initialization // Initialize UPnP SDK on the internal Interface if (g_debug) syslog(LOG_DEBUG, "Initializing UPnP SDK ... "); if ( (ret = UpnpInit(intIpAddress,0) ) != UPNP_E_SUCCESS) { syslog (LOG_ERR, "Error Initializing UPnP SDK on IP %s ",intIpAddress); syslog (LOG_ERR, " UpnpInit returned %d", ret); UpnpFinish(); exit(1); } if (g_debug) syslog(LOG_DEBUG, "UPnP SDK Successfully Initialized."); // Set the Device Web Server Base Directory if (g_debug) syslog(LOG_DEBUG, "Setting the Web Server Root Directory to %s",xmlPath); if ( (ret = UpnpSetWebServerRootDir(xmlPath)) != UPNP_E_SUCCESS ) { syslog (LOG_ERR, "Error Setting Web Server Root Directory to: %s", xmlPath); syslog (LOG_ERR, " UpnpSetWebServerRootDir returned %d", ret); UpnpFinish(); exit(1); } if (g_debug) syslog(LOG_DEBUG, "Succesfully set the Web Server Root Directory."); // Form the Description Doc URL to pass to RegisterRootDevice sprintf(descDocUrl, "http://%s:%d/%s", UpnpGetServerIpAddress(), UpnpGetServerPort(), descDocName); // Register our IGD as a valid UPnP Root device if (g_debug) syslog(LOG_DEBUG, "Registering the root device with descDocUrl %s", descDocUrl); if ( (ret = UpnpRegisterRootDevice(descDocUrl, EventHandler, &deviceHandle, &deviceHandle)) != UPNP_E_SUCCESS ) { syslog(LOG_ERR, "Error registering the root device with descDocUrl: %s", descDocUrl); syslog(LOG_ERR, " UpnpRegisterRootDevice returned %d", ret); UpnpFinish(); exit(1); } syslog (LOG_DEBUG, "IGD root device successfully registered."); // Initialize the state variable table. StateTableInit(descDocUrl); // Record the startup time, for uptime /* Modified by Yanhua */ #if 0 startup_time = time(NULL); #endif { struct sysinfo info; sysinfo(&info); startup_time = (long int)info.uptime; } // Send out initial advertisements of our device's services with timeouts of 30 minutes if ( (ret = UpnpSendAdvertisement(deviceHandle, 1800) != UPNP_E_SUCCESS )) { syslog(LOG_ERR, "Error Sending Advertisements. Exiting ..."); UpnpFinish(); exit(1); } syslog(LOG_DEBUG, "Advertisements Sent. Listening for requests ... "); // Loop until program exit signals recieved sigemptyset(&sigsToCatch); sigaddset(&sigsToCatch, SIGINT); sigaddset(&sigsToCatch, SIGTERM); //sigwait(&sigsToCatch, &signal); pthread_sigmask(SIG_SETMASK, &sigsToCatch, NULL); sigwait(&sigsToCatch, &signal); syslog(LOG_DEBUG, "Shutting down on signal %d...\n", signal); // Cleanup UPnP SDK and free memory pmlist_FreeList(); UpnpUnRegisterRootDevice(deviceHandle); UpnpFinish(); // Exit normally return (1); }
int wphoto_upnp_handshake(void) { int ret = -1, err; char descurl[256]; const char *desc_xml = "MobileDevDesc.xml"; struct timespec timer; int camera_responded_save; char *camera_url_save; int pinged_camera; ithread_mutex_init(&state_mutex, NULL); ithread_cond_init(&state_cond, NULL); camera_url = NULL; camera_responded = 0; err = UpnpInit(NULL, 0); if (err != UPNP_E_SUCCESS) { upnp_perror("UpnpInit", err); goto err_init; } server_ip = UpnpGetServerIpAddress(); server_port = UpnpGetServerPort(); if (init_xml_docs() < 0) { perror("init_xml_docs"); goto err_init; } printf("address: %s:%d\n", server_ip, server_port); snprintf(descurl, sizeof(descurl), "http://%s:%d/%s", server_ip, server_port, desc_xml); err = web_add_callback("/MobileDevDesc.xml", web_MobileDevDesc, NULL); if (err) { perror("web_add_callback"); goto err_init; } err = web_add_callback("/desc_iml/CameraConnectedMobile.xml", web_CameraConnectedMobile, NULL); if (err) { perror("web_add_callback"); goto err_init; } if (web_start() < 0) { printf("web_init error\n"); goto err_init; } err = UpnpRegisterRootDevice(descurl, upnp_device_event_handler, &device_handle, &device_handle); if (err != UPNP_E_SUCCESS) { upnp_perror("UpnpRegisterRootDevice", err); goto err_init; } err = UpnpRegisterClient(upnp_client_event_handler, &client_handle, &client_handle); if (err != UPNP_E_SUCCESS) { upnp_perror("UpnpRegisterClient", err); goto err_register; } clock_gettime(CLOCK_REALTIME, &timer); discovery_timeout = 1; camera_responded_save = 0; camera_url_save = NULL; pinged_camera = 0; do { int wait_err; if (!camera_responded_save) { err = UpnpSendAdvertisement(device_handle, 0); if (err != UPNP_E_SUCCESS) { upnp_perror("UpnpSendAdvertisement", err); goto err_register; } printf("NOTIFY sent\n"); } if (camera_url_save && !pinged_camera) if (ping_camera(camera_url_save) == 0) pinged_camera = 1; timer.tv_sec += ADVERTISEMENT_INTERVAL; wait: ithread_mutex_lock(&state_mutex); wait_err = 0; while (camera_responded == camera_responded_save && strcmp_null(camera_url, camera_url_save) == 0 && !discovery_timeout && wait_err == 0) wait_err = ithread_cond_timedwait( &state_cond, &state_mutex, &timer); camera_responded_save = camera_responded; if (strcmp_null(camera_url, camera_url_save) != 0) { free(camera_url_save); camera_url_save = strdup(camera_url); } /* * Once we have the camera url, we stop sending M-SEARCH * requests */ if (discovery_timeout && !camera_url_save) { err = UpnpSearchAsync(client_handle, MSEARCH_INTERVAL, CAMERA_SERVICE_NAME, (void*)42); if (err != UPNP_E_SUCCESS) { upnp_perror("UpnpSearchAsync", err); goto err_register; } printf("M-SEARCH sent\n"); } discovery_timeout = 0; ithread_mutex_unlock(&state_mutex); if (wait_err != ETIMEDOUT && (!pinged_camera || !camera_responded_save)) goto wait; } while (!pinged_camera || !camera_responded_save); return 0; err_register: UpnpUnRegisterRootDevice(device_handle); err_init: UpnpFinish(); return ret; }
int main(int argc, char *argv[]) { UpnpDevice_Handle upnp_handle; UpnpDevice_Handle upnp_device; char *device_desc = NULL; int rv = EXIT_FAILURE; struct sigaction sa; shutdown_flag = 0; /* init options, preset some defaults and parse command line arguments */ options = options_init(); options_parse_cli(argc, argv, options); memset(&sa, 0, sizeof(sa)); sa.sa_handler = signal_handler; sa.sa_flags = 0; sigfillset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) < 0) { perror("sigaction"); return EXIT_FAILURE; } if (sigaction(SIGTERM, &sa, NULL) < 0) { perror("sigaction"); return EXIT_FAILURE; } #ifdef UPNP_ENABLE_IPV6 rv = UpnpInit2(options->interface, 0); #else if (options->interface) fprintf(stderr, "Warning: ignoring interface argument, not supported by libupnp.\n"); rv = UpnpInit(NULL, 0); #endif if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "UpnpInit failed: %d\n", rv); return EXIT_FAILURE; } rv = UpnpEnableWebserver(1); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Could not enabled UPnP's internal HTTP server.\n"); goto upnp_finish; } device_desc = generate_device_desc(options); if (!device_desc) { fprintf(stderr, "Could not generated the UPnP device description.\n"); goto upnp_finish; } rv = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, device_desc, strlen(device_desc), 1, upnp_callback, &upnp_device, &upnp_device); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Failed to register UPnP root device.\n"); goto free_out; } rv = UpnpSendAdvertisement(upnp_device, UPNP_ALIVE_INTERVAL); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Failed to announce UPnP device.\n"); goto upnp_unregister; } while (!shutdown_flag) pause(); rv = EXIT_SUCCESS; upnp_unregister: UpnpUnRegisterRootDevice(upnp_device); free_out: if (device_desc) free(device_desc); upnp_finish: UpnpFinish(); err_out: options_free(options); return rv; }
int main (int argc, char** argv) { char descDocUrl[7+15+1+5+1+sizeof(g_vars.descDocName)+1]; // http://ipaddr:port/docName<null> char intIpAddress[16]; // Server internal ip address sigset_t sigsToCatch; int ret, signum, arg = 1, foreground = 0; if (argc < 3 || argc > 4) { printf("Usage: upnpd [-f] <external ifname> <internal ifname>\n"); printf(" -f\tdon't daemonize\n"); printf("Example: upnpd ppp0 eth0\n"); exit(0); } parseConfigFile(&g_vars); // check for '-f' option if (strcmp(argv[arg], "-f") == 0) { foreground = 1; arg++; } // Save interface names for later use strncpy(g_vars.extInterfaceName, argv[arg++], IFNAMSIZ); strncpy(g_vars.intInterfaceName, argv[arg++], IFNAMSIZ); // Get the internal ip address to start the daemon on if (GetIpAddressStr(intIpAddress, g_vars.intInterfaceName) == 0) { fprintf(stderr, "Invalid internal interface name '%s'\n", g_vars.intInterfaceName); exit(EXIT_FAILURE); } if (!foreground) { struct rlimit resourceLimit = { 0, 0 }; pid_t pid, sid; unsigned int i; // Put igd in the background as a daemon process. pid = fork(); if (pid < 0) { perror("Error forking a new process."); exit(EXIT_FAILURE); } if (pid > 0) exit(EXIT_SUCCESS); // become session leader if ((sid = setsid()) < 0) { perror("Error running setsid"); exit(EXIT_FAILURE); } // close all file handles resourceLimit.rlim_max = 0; ret = getrlimit(RLIMIT_NOFILE, &resourceLimit); if (ret == -1) /* shouldn't happen */ { perror("error in getrlimit()"); exit(EXIT_FAILURE); } if (0 == resourceLimit.rlim_max) { fprintf(stderr, "Max number of open file descriptors is 0!!\n"); exit(EXIT_FAILURE); } for (i = 0; i < resourceLimit.rlim_max; i++) close(i); // fork again so child can never acquire a controlling terminal pid = fork(); if (pid < 0) { perror("Error forking a new process."); exit(EXIT_FAILURE); } if (pid > 0) exit(EXIT_SUCCESS); if ((chdir("/")) < 0) { perror("Error setting root directory"); exit(EXIT_FAILURE); } } umask(0); // End Daemon initialization openlog("upnpd", LOG_CONS | LOG_NDELAY | LOG_PID | (foreground ? LOG_PERROR : 0), LOG_LOCAL6); // Initialize UPnP SDK on the internal Interface trace(3, "Initializing UPnP SDK ... "); if ( (ret = UpnpInit(intIpAddress,0) ) != UPNP_E_SUCCESS) { syslog (LOG_ERR, "Error Initializing UPnP SDK on IP %s ",intIpAddress); syslog (LOG_ERR, " UpnpInit returned %d", ret); UpnpFinish(); exit(1); } trace(2, "UPnP SDK Successfully Initialized."); // Set the Device Web Server Base Directory trace(3, "Setting the Web Server Root Directory to %s",g_vars.xmlPath); if ( (ret = UpnpSetWebServerRootDir(g_vars.xmlPath)) != UPNP_E_SUCCESS ) { syslog (LOG_ERR, "Error Setting Web Server Root Directory to: %s", g_vars.xmlPath); syslog (LOG_ERR, " UpnpSetWebServerRootDir returned %d", ret); UpnpFinish(); exit(1); } trace(2, "Succesfully set the Web Server Root Directory."); //initialize the timer thread for expiration of mappings if (ExpirationTimerThreadInit()!=0) { syslog(LOG_ERR,"ExpirationTimerInit failed"); UpnpFinish(); exit(1); } // Form the Description Doc URL to pass to RegisterRootDevice sprintf(descDocUrl, "http://%s:%d/%s", UpnpGetServerIpAddress(), UpnpGetServerPort(), g_vars.descDocName); // Register our IGD as a valid UPnP Root device trace(3, "Registering the root device with descDocUrl %s", descDocUrl); if ( (ret = UpnpRegisterRootDevice(descDocUrl, EventHandler, &deviceHandle, &deviceHandle)) != UPNP_E_SUCCESS ) { syslog(LOG_ERR, "Error registering the root device with descDocUrl: %s", descDocUrl); syslog(LOG_ERR, " UpnpRegisterRootDevice returned %d", ret); UpnpFinish(); exit(1); } trace(2, "IGD root device successfully registered."); // Initialize the state variable table. StateTableInit(descDocUrl); // Record the startup time, for uptime startup_time = time(NULL); // Send out initial advertisements of our device's services with timeouts of 30 minutes if ( (ret = UpnpSendAdvertisement(deviceHandle, 1800) != UPNP_E_SUCCESS )) { syslog(LOG_ERR, "Error Sending Advertisements. Exiting ..."); UpnpFinish(); exit(1); } trace(2, "Advertisements Sent. Listening for requests ... "); // Loop until program exit signals received do { sigemptyset(&sigsToCatch); sigaddset(&sigsToCatch, SIGINT); sigaddset(&sigsToCatch, SIGTERM); sigaddset(&sigsToCatch, SIGUSR1); pthread_sigmask(SIG_SETMASK, &sigsToCatch, NULL); sigwait(&sigsToCatch, &signum); trace(3, "Caught signal %d...\n", signum); switch (signum) { case SIGUSR1: DeleteAllPortMappings(); break; default: break; } } while (signum!=SIGTERM && signum!=SIGINT); trace(2, "Shutting down on signal %d...\n", signum); // Cleanup UPnP SDK and free memory DeleteAllPortMappings(); ExpirationTimerThreadShutdown(); UpnpUnRegisterRootDevice(deviceHandle); UpnpFinish(); // Exit normally return (0); }
struct device_private *upnp_device_init(struct device *device_def, const char *ip_address) { int rc; #ifdef HAVE_LIBUPNP short int port = 0; #endif struct service *srv; struct icon *icon_entry; char *buf; struct device_private *priv = NULL; int i; assert(device_def != NULL); if (device_def->init_function) { rc = device_def->init_function(); if (rc != 0) { goto out; } } priv = malloc(sizeof(*priv)); priv->upnp_device = device_def; #ifdef HAVE_LIBUPNP ithread_mutex_init(&(priv->device_mutex), NULL); #endif //upnp_device = device_def; /* register icons in web server */ for (i=0; (icon_entry = device_def->icons[i]); i++) { webserver_register_file(icon_entry->url, "image/png"); } /* generate and register service schemas in web server */ for (i=0; (srv = device_def->services[i]); i++) { buf = upnp_get_scpd(srv); assert(buf != NULL); printf("registering '%s'\n", srv->scpd_url); webserver_register_buf(srv->scpd_url,buf,"text/xml"); } #ifdef HAVE_LIBUPNP rc = UpnpInit(ip_address, port); if (UPNP_E_SUCCESS != rc) { printf("UpnpInit() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpEnableWebserver(TRUE); if (UPNP_E_SUCCESS != rc) { printf("UpnpEnableWebServer() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpSetVirtualDirCallbacks(&virtual_dir_callbacks); if (UPNP_E_SUCCESS != rc) { printf("UpnpSetVirtualDirCallbacks() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpAddVirtualDir("/upnp"); if (UPNP_E_SUCCESS != rc) { printf("UpnpAddVirtualDir() Error: %d\n", rc); goto upnp_err_out; } buf = upnp_get_device_desc(device_def); rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, buf, strlen(buf), 1, &event_handler, priv, &(priv->device_handle)); if (UPNP_E_SUCCESS != rc) { printf("UpnpRegisterRootDevice2() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpSendAdvertisement(priv->device_handle, 100); if (UPNP_E_SUCCESS != rc) { fprintf(stderr, "Error sending advertisements: %d\n", rc); goto upnp_err_out; } #endif goto out; #ifdef HAVE_LIBUPNP upnp_err_out: UpnpFinish(); #endif out: return priv; }
int main (int argc, char** argv) { int ret = UPNP_E_SUCCESS; int signal; char descDocUrl[50]; char descDocName[20]; char xmlPath[50]; char intIpAddress[16]; // Server internal ip address char extIpAddress[16]; // Server internal ip address sigset_t sigsToCatch; FILE *f; pid_t pid,sid; if (argc != 3) { printf("Usage: upnpd <external ifname> <internal ifname>\n"); printf("Example: upnpd ppp0 eth0\n"); printf("Example: upnpd eth1 eth0\n"); exit(0); } parseConfigFile(&g_forwardRules,&g_debug,g_iptables, g_forwardChainName,g_preroutingChainName, g_upstreamBitrate,g_downstreamBitrate, descDocName,xmlPath); // Save the interface names for later uses strcpy(g_extInterfaceName, argv[1]); strcpy(g_intInterfaceName, argv[2]); openlog("upnpd", LOG_PID | LOG_CONS, LOG_USER); // Get the internal ip address to start the daemon on GetIpAddressStr(intIpAddress, g_intInterfaceName); GetIpAddressStr(extIpAddress, g_extInterfaceName); // Put igd in the background as a daemon process. pid = fork(); if (pid < 0) { perror("Error forking a new process."); cleanup(); exit(EXIT_FAILURE); } if (pid > 0) exit(EXIT_SUCCESS); /* if we are here, we know we are the demonized version */ //open a state file f = fopen(STATE_FILE, "w"); if (!f) { syslog(LOG_ERR, "failed to open %s: %m", STATE_FILE); } else { fprintf(f, "external %s %s\ninternal %s %s\n", g_extInterfaceName, extIpAddress, g_intInterfaceName, intIpAddress); fclose(f); } atexit(cleanup); if ((sid = setsid()) < 0) { perror("Error running setsid"); exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { perror("Error setting root directory"); exit(EXIT_FAILURE); } umask(0); close(STDERR_FILENO); close (STDIN_FILENO); close (STDOUT_FILENO); // End Daemon initialization // Initialize UPnP SDK on the internal Interface if (g_debug) syslog(LOG_DEBUG, "Initializing UPnP SDK ... "); if ( (ret = UpnpInit(intIpAddress,0) ) != UPNP_E_SUCCESS) { syslog (LOG_ERR, "Error Initializing UPnP SDK on IP %s ",intIpAddress); syslog (LOG_ERR, " UpnpInit returned %d", ret); UpnpFinish(); exit(1); } if (g_debug) syslog(LOG_DEBUG, "UPnP SDK Successfully Initialized."); // Set the Device Web Server Base Directory if (g_debug) syslog(LOG_DEBUG, "Setting the Web Server Root Directory to %s",xmlPath); if ( (ret = UpnpSetWebServerRootDir(xmlPath)) != UPNP_E_SUCCESS ) { syslog (LOG_ERR, "Error Setting Web Server Root Directory to: %s", xmlPath); syslog (LOG_ERR, " UpnpSetWebServerRootDir returned %d", ret); UpnpFinish(); exit(1); } if (g_debug) syslog(LOG_DEBUG, "Succesfully set the Web Server Root Directory."); // Form the Description Doc URL to pass to RegisterRootDevice sprintf(descDocUrl, "http://%s:%d/%s", UpnpGetServerIpAddress(), UpnpGetServerPort(), descDocName); // Register our IGD as a valid UPnP Root device if (g_debug) syslog(LOG_DEBUG, "Registering the root device with descDocUrl %s", descDocUrl); if ( (ret = UpnpRegisterRootDevice(descDocUrl, EventHandler, &deviceHandle, &deviceHandle)) != UPNP_E_SUCCESS ) { syslog(LOG_ERR, "Error registering the root device with descDocUrl: %s", descDocUrl); syslog(LOG_ERR, " UpnpRegisterRootDevice returned %d", ret); UpnpFinish(); exit(1); } syslog (LOG_DEBUG, "IGD root device successfully registered."); // Initialize the state variable table. StateTableInit(descDocUrl); // Record the startup time, for uptime startup_time = time(NULL); // Send out initial advertisements of our device's services with timeouts of 30 minutes if ( (ret = UpnpSendAdvertisement(deviceHandle, 1800) != UPNP_E_SUCCESS )) { syslog(LOG_ERR, "Error Sending Advertisements. Exiting ..."); UpnpFinish(); exit(1); } syslog(LOG_DEBUG, "Advertisements Sent. Listening for requests ... "); // Loop until program exit signals recieved // and now also recreate the current portmappings on SIGUSR1 while (1) { sigemptyset(&sigsToCatch); sigaddset(&sigsToCatch, SIGINT); sigaddset(&sigsToCatch, SIGTERM); sigaddset(&sigsToCatch, SIGQUIT); sigaddset(&sigsToCatch, SIGABRT); sigaddset(&sigsToCatch, SIGHUP); sigaddset(&sigsToCatch, SIGUSR1); sigaddset(&sigsToCatch, SIGUSR2); //sigwait(&sigsToCatch, &signal); pthread_sigmask(SIG_SETMASK, &sigsToCatch, NULL); sigwait(&sigsToCatch, &signal); if (signal == SIGUSR1) { syslog(LOG_DEBUG, "signal SIGUSR1 received - rebuilding portmappings\n"); //rebuild all the portmappings pmlist_RecreateAll(); } else if (signal == SIGHUP || signal == SIGUSR2) { //nothing } else { break; } } syslog(LOG_DEBUG, "Shutting down on signal %d...\n", signal); // Cleanup UPnP SDK and free memory pmlist_FreeList(); UpnpUnRegisterRootDevice(deviceHandle); UpnpFinish(); // Exit normally return (1); }
int upnp_device_init(struct device *device_def, char *ip_address) { int rc; int result = -1; short int port = 0; struct service *srv; struct icon *icon_entry; char *buf; int i; if (device_def->init_function) { rc = device_def->init_function(); if (rc != 0) { goto out; } } upnp_device = device_def; /* register icons in web server */ for (i=0; (icon_entry = upnp_device->icons[i]); i++) { webserver_register_file(icon_entry->url, "image/png"); } /* generate and register service schemas in web server */ for (i=0; (srv = upnp_device->services[i]); i++) { buf = upnp_get_scpd(srv); debug_printf(MSG_INFO, "registering '%s'\n", srv->scpd_url); webserver_register_buf(srv->scpd_url,buf,"text/xml"); } rc = UpnpInit(ip_address, port); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "UpnpInit() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpEnableWebserver(TRUE); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "UpnpEnableWebServer() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpSetVirtualDirCallbacks(&virtual_dir_callbacks); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "UpnpSetVirtualDirCallbacks() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpAddVirtualDir("/upnp"); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "UpnpAddVirtualDir() Error: %d\n", rc); goto upnp_err_out; } buf = upnp_get_device_desc(device_def); rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, buf, strlen(buf), 1, &event_handler, &device_def, &device_handle); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "UpnpRegisterRootDevice2() Error: %d\n", rc); goto upnp_err_out; } rc = UpnpSendAdvertisement(device_handle, 100); if (UPNP_E_SUCCESS != rc) { debug_printf(MSG_ERROR, "Error sending advertisements: %d\n", rc); goto upnp_err_out; } result = 0; goto out; upnp_err_out: UpnpFinish(); out: return result; }