diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 1f87efd2..22012db5 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -598,7 +598,7 @@ int rhizome_queue_manifest_import(rhizome_manifest *m, struct sockaddr_in *peeri long long filesize = rhizome_manifest_get_ll(m, "filesize"); - if (1||debug&DEBUG_RHIZOMESYNC) + if (debug & DEBUG_RHIZOMESYNC) DEBUGF("Getting ready to fetch file %s for manifest %s", m->fileHexHash, id); if (filesize > 0 && m->fileHexHash[0]) @@ -642,32 +642,38 @@ int rhizome_queue_manifest_import(rhizome_manifest *m, struct sockaddr_in *peeri close(sock); return -1; } + struct sockaddr_in addr = *peerip; + addr.sin_family = AF_INET; INFOF("HTTP CONNECT family=%u port=%u addr=%u.%u.%u.%u", - 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] + addr.sin_family, ntohs(addr.sin_port), + ((unsigned char*)&addr.sin_addr.s_addr)[0], + ((unsigned char*)&addr.sin_addr.s_addr)[1], + ((unsigned char*)&addr.sin_addr.s_addr)[2], + ((unsigned char*)&addr.sin_addr.s_addr)[3] ); - if (connect(sock, (struct sockaddr*)peerip, sizeof *peerip) == -1 && errno != EINPROGRESS) { - WHY_perror("connect"); - WHY("Failed to open socket to peer's rhizome web server"); - close(sock); - return -1; + if (connect(sock, (struct sockaddr*)&addr, sizeof addr) == -1) { + if (errno == EINPROGRESS) { + if (debug & DEBUG_RHIZOMESYNC) + DEBUGF("connect() returned EINPROGRESS"); + } else { + WHY_perror("connect"); + WHY("Failed to open socket to peer's rhizome web server"); + close(sock); + return -1; + } } rhizome_file_fetch_record *q=&file_fetch_queue[rhizome_file_fetch_queue_count]; q->manifest = m; *manifest_kept = 1; q->alarm.poll.fd=sock; strncpy(q->fileid, m->fileHexHash, RHIZOME_FILEHASH_STRLEN + 1); - snprintf(q->request,1024,"GET /rhizome/file/%s HTTP/1.0\r\n\r\n", - q->fileid); + snprintf(q->request,1024,"GET /rhizome/file/%s HTTP/1.0\r\n\r\n", q->fileid); q->request_len=strlen(q->request); q->request_ofs=0; q->state=RHIZOME_FETCH_CONNECTING; q->file_len=-1; q->file_ofs=0; - + /* XXX Don't forget to implement resume */ /* XXX We should stream file straight into the database */ const char *id = rhizome_manifest_get(q->manifest, "id", NULL, 0); diff --git a/rhizome_http.c b/rhizome_http.c index 9f8b7a46..fc9e7e53 100644 --- a/rhizome_http.c +++ b/rhizome_http.c @@ -270,7 +270,6 @@ 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 = sizeof addr; int sock; diff --git a/rhizome_packetformats.c b/rhizome_packetformats.c index 215227cd..77c3124c 100644 --- a/rhizome_packetformats.c +++ b/rhizome_packetformats.c @@ -335,14 +335,14 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now) int ofs=0; int ad_frame_type=f->payload->bytes[ofs++]; struct sockaddr_in httpaddr = *(struct sockaddr_in *)f->recvaddr; - httpaddr.sin_port = RHIZOME_HTTP_PORT; + httpaddr.sin_port = htons(RHIZOME_HTTP_PORT); int manifest_length; rhizome_manifest *m=NULL; switch (ad_frame_type) { case 3: /* The same as type=1, but includes the source HTTP port number */ - httpaddr.sin_port = (f->payload->bytes[ofs] << 8) + f->payload->bytes[ofs + 1]; + httpaddr.sin_port = htons((f->payload->bytes[ofs] << 8) + f->payload->bytes[ofs + 1]); ofs += 2; // FALL THROUGH ... case 1: