static void cleanup_connections(void) { timeout_connections(waiting_connections.head, tuning.wait_timeout, debug ? "wait" : 0); timeout_connections(reading_connections.head, tuning.timeout, "read"); timeout_connections(writing_connections.head, tuning.timeout, "write"); timeout_connections(forked_connections.head, tuning.script_timeout, "script"); }
void main_loop(void) { int i, n, timeout = 250; struct connection *conn; ufds = must_calloc(thread_limit, sizeof(struct pollfd)); for (i = 0; i < thread_limit; ++i) ufds[i].fd = -1; while (head || outstanding) { start_next_comic(); n = poll(ufds, thread_limit, timeout); if (n < 0) { my_perror("poll"); continue; } if (n == 0) { timeout_connections(); if (!start_next_comic()) /* Once we have all the comics * started, increase the timeout * period. */ timeout = 1000; continue; } for (conn = comics; conn; conn = conn->next) if (!conn->poll) continue; else if (conn->poll->revents & POLLOUT) { if (!conn->connected) check_connect(conn); else { time(&conn->access); write_request(conn); } } else if (conn->poll->revents & POLLIN) { /* This check is needed for openssl */ if (!conn->connected) check_connect(conn); else read_conn(conn); } } free(ufds); }
int main(int argc, char** argv) { /* Check arguments */ if (argc != 2) fprintf(stderr, "Usage: %s <PORT>\n", argv[0]),exit(0); // Attempt to parse argument as a number long arg_port = strtol(argv[1], NULL, 10); // If cannot be parsed, exit immediately if (arg_port == 0) printf("%s: Could not interpet port number\n",argv[0]),exit(1); else if(arg_port == LONG_MAX || arg_port == LONG_MIN) perror(argv[0]),exit(1); /* PROCEED TO BEGIN TFTP SERVER */ char buff[DGRM_LEN], *temp; struct sockaddr_in serveraddr, clientaddr; socklen_t addr_size; int listenfd, nfds; unsigned short tftp_port; fd_set readset; // used for multiplexing struct timeval timeout; // used for timing out tftp_port = (unsigned short) arg_port; if ( (listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Couldn't open socket!"); exit(-1); } memset(&serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short) tftp_port); addr_size = sizeof(struct sockaddr_in); if (bind(listenfd, (struct sockaddr*) &serveraddr, sizeof(serveraddr)) < 0) { perror("couldn't bind socket"); exit(-1); } // Declare a list of connections LIST_INIT(&list); for ( ; ; ) { // zoidberg // sleep(1); // Create select file descriptor list FD_ZERO(&readset); // Set all file descriptors nfds = listenfd; register_connections(&list, &readset, &nfds); /* Reset timeout structure to time out after 1 second */ timeout.tv_sec = 1; timeout.tv_usec = 0; addr_size = sizeof(struct sockaddr_in); memset(&clientaddr, 0, addr_size); // TODO FIX THIS nfds = select(nfds, &readset, NULL, NULL, &timeout); // If 0 is returned, a timeout occurred, otherwise there are readable files if (nfds == 0) { #ifdef DEBUG puts("No incoming requests!"); #endif timeout_connections(&list); } else if (nfds > 0) { #ifdef DEBUG puts("Some incoming requests!"); #endif // Check whether connections are available to respond respond_to_conns(&list, &readset); // If main port is available, respond on it if (FD_ISSET(listenfd, &readset)) { #ifdef DEBUG puts("Incoming request on main port!"); #endif respond_msg_onmain(listenfd, &list); } } } // This never gets reached exit(0); }
int main(int argc, char *argv[]) { char *env; int i, n, timeout = 250; struct connection *conn; method = "HEAD"; while ((i = getopt(argc, argv, "hp:t:vT:")) != -1) switch ((char)i) { case 'h': usage(0); case 'p': set_proxy(optarg); break; case 't': thread_limit = strtol(optarg, NULL, 0); break; case 'v': verbose++; break; case 'T': read_timeout = strtol(optarg, NULL, 0); break; default: usage(1); } if (optind < argc) while (optind < argc) read_link_file(argv[optind++]); else read_urls(stdin); /* set_proxy will not use this if proxy already set */ env = getenv("COMICS_PROXY"); if (env) set_proxy(env); if (thread_limit == 0) { printf("You must allow at least one thread\n"); exit(1); } if (thread_limit > n_comics) thread_limit = n_comics; #ifdef _WIN32 win32_init(); #else signal(SIGTERM, dump_outstanding); signal(SIGHUP, dump_outstanding); #endif npoll = thread_limit + 1; /* add one for stdin */ ufds = must_calloc(npoll, sizeof(struct pollfd)); for (i = 0; i < npoll; ++i) ufds[i].fd = -1; while (head || outstanding) { start_next_comic(); n = poll(ufds, npoll, timeout); if (n < 0) { my_perror("poll"); continue; } if (n == 0) { timeout_connections(); if (!start_next_comic()) /* Once we have all the comics * started, increase the timeout * period. */ timeout = 1000; continue; } for (conn = comics; conn; conn = conn->next) if (!conn->poll) continue; else if (conn->poll->revents & POLLOUT) { if (!conn->connected) check_connect(conn); else { time(&conn->access); write_request(conn); } } else if (conn->poll->revents & POLLIN) { /* This check is needed for openssl */ if (!conn->connected) check_connect(conn); else read_conn(conn); } } out_results(comics, 0); return n_comics != gotit; }