void _display (trieNode *root, char *word, int index) { if (root == NULL) return; if (root->isleaf == true) { word[index] = '\0'; printf("%s\n", word); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (!root->next[i]) continue; //printf("%c", ascii2char(i)); word[index] = ascii2char(i); display(root->next[i], word, index + 1); } }
static void http_writeready(http_struct *conn) { char buffer[512]; if (conn->url) snprintf(buffer, sizeof(buffer), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", conn->url, conn->zone->host); else snprintf(buffer, sizeof(buffer), "GET %s%s HTTP/1.0\r\nHost: %s\r\n\r\n", conn->zone->url, conn->ip, conn->zone->host); if (send(conn->fd, buffer, strlen(buffer), 0) == -1) { log_event(6, "HTTP -> [Socket %d] Failed to send request to %s for IP %s [Error %d: %s]", conn->fd, conn->zone->name, conn->ip, errno, strerror(errno)); conn->state = STATE_CLOSED; return; } log_event(6, "HTTP -> [Socket %d] Sent request to %s for IP %s: %s", conn->fd, conn->zone->name, conn->ip, ascii2char(buffer, strlen(buffer))); /* If write returns true, flag STATE_SENT. */ conn->state = STATE_SENT; }
static void http_check(void) { http_struct *conn; #if defined(HAVE_SYS_POLL_H) && !defined(FORCE_SELECT) static struct pollfd ufds[EVENT_CHUNK]; unsigned long size, i; #else /* select() */ fd_set w_fdset; fd_set r_fdset; struct timeval http_timeout; int highfd = 0; #endif /* HAVE_SYS_POLL_H */ #if defined(HAVE_SYS_POLL_H) && !defined(FORCE_SELECT) size = i = 0; /* Get size of list we're interested in. */ for (conn = HTTPREQUESTS; conn; conn = conn->next) { if ((conn->state == STATE_ESTABLISHED) || (conn->state == STATE_SENT)) ++size; } /* Setup each element now. */ for (conn = HTTPREQUESTS; conn; conn = conn->next) { if ((conn->state != STATE_ESTABLISHED) && (conn->state != STATE_SENT)) continue; ufds[i].events = 0; ufds[i].revents = 0; ufds[i].fd = conn->fd; /* Check for HUNG UP. */ ufds[i].events |= POLLHUP; /* Check for INVALID FD */ ufds[i].events |= POLLNVAL; switch (conn->state) { case STATE_ESTABLISHED: /* Check for NO BLOCK ON WRITE. */ ufds[i].events |= POLLOUT; break; case STATE_SENT: /* Check for data to be read. */ ufds[i].events |= POLLIN; break; } if (++i >= EVENT_CHUNK) break; } #else /* select() */ FD_ZERO(&w_fdset); FD_ZERO(&r_fdset); /* Add connections to appropriate sets. */ for (conn = HTTPREQUESTS; conn; conn = conn->next) { if (conn->state == STATE_ESTABLISHED) { if (conn->fd > highfd) highfd = conn->fd; FD_SET(conn->fd, &w_fdset); } else if (conn->state == STATE_SENT) { if (conn->fd > highfd) highfd = conn->fd; FD_SET(conn->fd, &r_fdset); } } /* No timeout. */ http_timeout.tv_sec = 0; http_timeout.tv_usec= 0; #endif /* HAVE_SYS_POLL_H */ #if defined(HAVE_SYS_POLL_H) && !defined(FORCE_SELECT) switch (poll(ufds, size, 0)) { #else /* select() */ switch (select((highfd + 1), &r_fdset, &w_fdset, 0, &http_timeout)) { #endif /* HAVE_SYS_POLL_H */ case -1: /* Error in select/poll. */ case 0: /* Nothing to do. */ return; default: /* Pass pointer to connection to handler. */ #if defined(HAVE_SYS_POLL_H) && !defined(FORCE_SELECT) for (conn = HTTPREQUESTS; conn; conn = conn->next) { for (i = 0; i < size; ++i) { if ((ufds[i].fd == conn->fd) && (conn->state != STATE_CLOSED) && (conn->state != STATE_POSITIVE)) { if (ufds[i].revents & POLLIN) http_readready(conn); if (ufds[i].revents & POLLOUT) http_writeready(conn); if (ufds[i].revents & POLLHUP) { /* Negotiation failed (read returned false). Discard the connection as a closed proxy. */ log_event(6, "HTTP -> [Socket %d] Negotiation failed on %s for IP %s [Read: %s]", conn->fd, conn->zone->name, conn->ip, (conn->bytes_read > 0) ? ascii2char(conn->response, conn->bytes_read) : "Nothing"); conn->state = STATE_CLOSED; } break; } } } #else for (conn = HTTPREQUESTS; conn; conn = conn->next) { if (conn->state == STATE_ESTABLISHED) { if (FD_ISSET(conn->fd, &w_fdset)) http_writeready(conn); } else if (conn->state == STATE_SENT) { if (FD_ISSET(conn->fd, &r_fdset)) http_readready(conn); } } #endif /* HAVE_SYS_POLL_H */ } } /********************************************************* * Poll or select returned back that this connection is * * ready for read. Get the data, and pass it to the * * protocol check handler. * *********************************************************/ static void http_readready(http_struct *conn) { int len; memset(conn->response, 0, sizeof(conn->response)); conn->bytes_read = 0; len = recv(conn->fd, conn->response, sizeof(conn->response), MSG_WAITALL); switch (len) { case -1: /* Error, or socket was closed. */ case 0: /* No data read from socket. */ return; default: if (len >= sizeof(conn->response)) { char dump[1024]; log_event(6, "HTTP -> [Socket %d] Read more than %d bytes, discarding the rest", conn->fd, sizeof(conn->response)); /* Get rid of whatever was left on the socket. */ while (recv(conn->fd, dump, sizeof(dump), MSG_WAITALL) > 0) ; } conn->bytes_read = len; log_event(6, "HTTP -> [Socket %d] Reading data from %s for IP %s: %s", conn->fd, conn->zone->name, conn->ip, ascii2char(conn->response, conn->bytes_read)); http_request_parse(conn); conn->state = STATE_CLOSED; return; } }