int main(int argc, char *argv[]){ struct ev_loop *loop = ev_default_loop(0); int i; struct ev_io socket_accept; get_param(argc, argv); // options.faddr = "0.0.0.0:8765"; // options.baddr = ""; // options.cryptoMethod = "RC4"; // options.secret = "secret"; // options.clientMod = 1; build_server(); ev_io_init(&socket_accept, accept_cb, serv_sock, EV_READ); ev_io_start(loop, &socket_accept); while(1) { ev_loop(loop, 0); } return 0; }
/* process HTTP or SSDP requests */ int main(int argc, char * * argv) { int i; int sudp = -1, shttpl = -1; int snotify[MAX_LAN_ADDR]; fd_set readset; /* for select() */ fd_set writeset; struct timeval timeout, timeofday, lastnotifytime = {0, 0}; int max_fd = -1; pid_t tcplistener_pid = 0; if(init(argc, argv) != 0) return 1; DPRINTF(E_WARN, L_GENERAL, "Starting " SERVER_NAME " version " DLNAPROXY_VERSION ".\n"); sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr); if(sudp < 0) { DPRINTF(E_INFO, L_GENERAL, "Failed to open socket for receiving SSDP. Trying to use MiniSSDPd\n"); if(SubmitServicesToMiniSSDPD(lan_addr[0].str, runtime_vars.port) < 0) { DPRINTF(E_FATAL, L_GENERAL, "Failed to connect to MiniSSDPd. EXITING"); return 1; } } /* open socket for sending notifications */ if(OpenAndConfSSDPNotifySockets(snotify) < 0) { DPRINTF(E_FATAL, L_GENERAL, "Failed to open sockets for sending SSDP notify " "messages. EXITING\n"); } SendSSDPGoodbye(snotify, n_lan_addr); /* create tcp tunnel for Http forwarding*/ tcplistener_pid = fork(); if( tcplistener_pid == 0) // child (listener) process { if(build_server() == 0) { while(!quitting) { /* process tunnel packets */ if (wait_for_clients() == 0) { if (build_tunnel() == 0) { use_tunnel(); } } } } close_server(); DPRINTF(E_INFO, L_GENERAL, "Shutting down tcp tunnel thread\n"); exit(EXIT_SUCCESS); }else if( tcplistener_pid < 0) { DPRINTF(E_ERROR, L_GENERAL, "Failed to create tcp tunnel thread\n"); exit(EXIT_SUCCESS); }else{ /* main loop */ while(!quitting) { /* Check if we need to send SSDP NOTIFY messages and do it if * needed */ if(gettimeofday(&timeofday, 0) < 0) { DPRINTF(E_ERROR, L_GENERAL, "gettimeofday(): %s\n", strerror(errno)); timeout.tv_sec = runtime_vars.notify_interval; timeout.tv_usec = 0; } else { /* the comparaison is not very precise but who cares ? */ if(timeofday.tv_sec >= (lastnotifytime.tv_sec + runtime_vars.notify_interval)) { //SendSSDPNotifies3(snotify, "store", (unsigned short)runtime_vars.port, "/desc/device.xml", (runtime_vars.notify_interval << 1)+10); SendSSDPNotifies2(snotify, (unsigned short)runtime_vars.port, runtime_vars.path, (runtime_vars.notify_interval << 1)+10); memcpy(&lastnotifytime, &timeofday, sizeof(struct timeval)); timeout.tv_sec = runtime_vars.notify_interval; timeout.tv_usec = 0; } else { timeout.tv_sec = lastnotifytime.tv_sec + runtime_vars.notify_interval - timeofday.tv_sec; if(timeofday.tv_usec > lastnotifytime.tv_usec) { timeout.tv_usec = 1000000 + lastnotifytime.tv_usec - timeofday.tv_usec; timeout.tv_sec--; } else { timeout.tv_usec = lastnotifytime.tv_usec - timeofday.tv_usec; } } } /* select open sockets (SSDP, HTTP listen, and all HTTP soap sockets) */ FD_ZERO(&readset); if (sudp >= 0) { FD_SET(sudp, &readset); max_fd = MAX(max_fd, sudp); } FD_ZERO(&writeset); upnpevents_selectfds(&readset, &writeset, &max_fd); if(select(max_fd+1, &readset, &writeset, 0, &timeout) < 0) { if(quitting) goto shutdown; DPRINTF(E_ERROR, L_GENERAL, "select(all): %s\n", strerror(errno)); DPRINTF(E_FATAL, L_GENERAL, "Failed to select open sockets. EXITING\n"); } upnpevents_processfds(&readset, &writeset); /* process SSDP packets */ if(sudp >= 0 && FD_ISSET(sudp, &readset)) { /*DPRINTF(E_DEBUG, L_GENERAL, "Received UDP Packet\n");*/ //ProcessSSDPRequest(sudp, "store", (unsigned short)runtime_vars.port, "/desc/device.xml"); ProcessSSDPRequest(sudp, (unsigned short)runtime_vars.port, runtime_vars.path); } } } shutdown: /* kill the listener */ DPRINTF(E_DEBUG, L_GENERAL, "Trying to kill tunnel thread: %d\n", tcplistener_pid); if( tcplistener_pid ) { kill(tcplistener_pid, 9); } /* close out open sockets */ if (sudp >= 0) close(sudp); if (shttpl >= 0) close(shttpl); if(SendSSDPGoodbye(snotify, n_lan_addr) < 0) { DPRINTF(E_ERROR, L_GENERAL, "Failed to broadcast good-bye notifications\n"); } for(i=0; i<n_lan_addr; i++) close(snotify[i]); if(unlink(pidfilename) < 0) { DPRINTF(E_ERROR, L_GENERAL, "Failed to remove pidfile %s: %s\n", pidfilename, strerror(errno)); } freeoptions(); exit(EXIT_SUCCESS); }