diff --git a/fdqueue.c b/fdqueue.c index 89a170b9..02674902 100644 --- a/fdqueue.c +++ b/fdqueue.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "strbuf.h" #include #define MAX_WATCHED_FDS 128 @@ -169,7 +170,15 @@ int fd_poll() { struct call_stats call_stats; fd_func_enter(&call_stats); - r=poll(fds, fdcount, ms); + strbuf b = strbuf_alloca(1024); + int i; + for (i = 0; i < fdcount; ++i) { + if (i) + strbuf_puts(b, ", "); + strbuf_sprintf(b, "%d:%04x:%04x", fds[i].fd, fds[i].events, fds[i].revents); + } + r = poll(fds, fdcount, ms); + DEBUGF("poll(fds=(%s), fdcount=%d, ms=%d) = %d", strbuf_str(b), fdcount, ms, r); fd_func_exit(&call_stats, &poll_stats); } diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 29a08a5c..1f87efd2 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -755,17 +755,16 @@ int rhizome_fetch_close(rhizome_file_fetch_record *q){ } void rhizome_fetch_write(rhizome_file_fetch_record *q){ - int bytes; - bytes=write(q->alarm.poll.fd,&q->request[q->request_ofs], - q->request_len-q->request_ofs); - if (bytes>0) { - + int bytes = write_nonblock(q->alarm.poll.fd, &q->request[q->request_ofs], q->request_len-q->request_ofs); + if (bytes == -1) { + WHY("Got error while sending HTTP request. Closing."); + rhizome_fetch_close(q); + } else { // reset timeout unschedule(&q->alarm); q->alarm.alarm=overlay_gettime_ms() + RHIZOME_IDLE_TIMEOUT; schedule(&q->alarm); q->request_ofs+=bytes; - if (q->request_ofs>=q->request_len) { /* Sent all of request. Switch to listening for HTTP response headers. */ @@ -775,9 +774,6 @@ void rhizome_fetch_write(rhizome_file_fetch_record *q){ watch(&q->alarm); }else if(q->state==RHIZOME_FETCH_CONNECTING) q->state = RHIZOME_FETCH_SENDINGHTTPREQUEST; - } else if (errno!=EAGAIN) { - WHY("Got error while sending HTTP request. Closing."); - rhizome_fetch_close(q); } } diff --git a/rhizome_http.c b/rhizome_http.c index d4e9a1d3..9f8b7a46 100644 --- a/rhizome_http.c +++ b/rhizome_http.c @@ -187,7 +187,7 @@ int rhizome_http_server_start() return WHY("Failed to start rhizome HTTP server"); } - INFOF("Started Rhizome HTTP server on port %d", port); + INFOF("Started Rhizome HTTP server on port %d, fd = %d", port, rhizome_server_socket); /* Add Rhizome HTTPd server to list of file descriptors to watch */ server_alarm.function = rhizome_server_poll; @@ -270,30 +270,41 @@ void rhizome_client_poll(struct sched_ent *alarm) void rhizome_server_poll(struct sched_ent *alarm) { + if (debug & DEBUG_RHIZOMESYNC) D; struct sockaddr addr; - unsigned int addr_len=0; + unsigned int addr_len = sizeof addr; int sock; - - /* Deal with any new requests */ - - while ((sock=accept(rhizome_server_socket,&addr,&addr_len))>-1) - { - rhizome_http_request *request = calloc(sizeof(rhizome_http_request),1); - - /* We are now trying to read the HTTP request */ - request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING; - request->alarm.function = rhizome_client_poll; - connection_stats.name="rhizome_client_poll"; - request->alarm.stats=&connection_stats; - request->alarm.poll.fd=sock; - request->alarm.poll.events=POLLIN; - request->alarm.alarm = overlay_gettime_ms()+RHIZOME_IDLE_TIMEOUT; - // watch for the incoming http request - watch(&request->alarm); - // set an inactivity timeout to close the connection - schedule(&request->alarm); + while ((sock = accept(rhizome_server_socket, &addr, &addr_len)) != -1) { + if (addr.sa_family == AF_INET) { + struct sockaddr_in *peerip = (struct sockaddr_in *)&addr; + INFOF("HTTP ACCEPT addrlen=%u family=%u port=%u addr=%u.%u.%u.%u", + addr_len, peerip->sin_family, peerip->sin_port, + ((unsigned char*)&peerip->sin_addr.s_addr)[0], + ((unsigned char*)&peerip->sin_addr.s_addr)[1], + ((unsigned char*)&peerip->sin_addr.s_addr)[2], + ((unsigned char*)&peerip->sin_addr.s_addr)[3] + ); + } else { + INFOF("HTTP ACCEPT addrlen=%u family=%u data=%s", + addr_len, addr.sa_family, alloca_tohex((unsigned char *)addr.sa_data, sizeof addr.sa_data)); } - + rhizome_http_request *request = calloc(sizeof(rhizome_http_request),1); + /* We are now trying to read the HTTP request */ + request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING; + request->alarm.function = rhizome_client_poll; + connection_stats.name="rhizome_client_poll"; + request->alarm.stats=&connection_stats; + request->alarm.poll.fd=sock; + request->alarm.poll.events=POLLIN; + request->alarm.alarm = overlay_gettime_ms()+RHIZOME_IDLE_TIMEOUT; + // watch for the incoming http request + watch(&request->alarm); + // set an inactivity timeout to close the connection + schedule(&request->alarm); + } + if (errno != EAGAIN) { + WARN_perror("accept"); + } } int rhizome_server_free_http_request(rhizome_http_request *r)