Fix bug in Rhizome fetch - byte order of sin_port

Also improve some debug logging
This commit is contained in:
Andrew Bettison 2012-07-11 14:21:49 +09:30
parent 6556637251
commit 4eb3910dc1
3 changed files with 22 additions and 17 deletions

View File

@ -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"); 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); DEBUGF("Getting ready to fetch file %s for manifest %s", m->fileHexHash, id);
if (filesize > 0 && m->fileHexHash[0]) if (filesize > 0 && m->fileHexHash[0])
@ -642,26 +642,32 @@ int rhizome_queue_manifest_import(rhizome_manifest *m, struct sockaddr_in *peeri
close(sock); close(sock);
return -1; return -1;
} }
struct sockaddr_in addr = *peerip;
addr.sin_family = AF_INET;
INFOF("HTTP CONNECT family=%u port=%u addr=%u.%u.%u.%u", INFOF("HTTP CONNECT family=%u port=%u addr=%u.%u.%u.%u",
peerip->sin_family, peerip->sin_port, addr.sin_family, ntohs(addr.sin_port),
((unsigned char*)&peerip->sin_addr.s_addr)[0], ((unsigned char*)&addr.sin_addr.s_addr)[0],
((unsigned char*)&peerip->sin_addr.s_addr)[1], ((unsigned char*)&addr.sin_addr.s_addr)[1],
((unsigned char*)&peerip->sin_addr.s_addr)[2], ((unsigned char*)&addr.sin_addr.s_addr)[2],
((unsigned char*)&peerip->sin_addr.s_addr)[3] ((unsigned char*)&addr.sin_addr.s_addr)[3]
); );
if (connect(sock, (struct sockaddr*)peerip, sizeof *peerip) == -1 && errno != EINPROGRESS) { if (connect(sock, (struct sockaddr*)&addr, sizeof addr) == -1) {
WHY_perror("connect"); if (errno == EINPROGRESS) {
WHY("Failed to open socket to peer's rhizome web server"); if (debug & DEBUG_RHIZOMESYNC)
close(sock); DEBUGF("connect() returned EINPROGRESS");
return -1; } 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]; rhizome_file_fetch_record *q=&file_fetch_queue[rhizome_file_fetch_queue_count];
q->manifest = m; q->manifest = m;
*manifest_kept = 1; *manifest_kept = 1;
q->alarm.poll.fd=sock; q->alarm.poll.fd=sock;
strncpy(q->fileid, m->fileHexHash, RHIZOME_FILEHASH_STRLEN + 1); strncpy(q->fileid, m->fileHexHash, RHIZOME_FILEHASH_STRLEN + 1);
snprintf(q->request,1024,"GET /rhizome/file/%s HTTP/1.0\r\n\r\n", snprintf(q->request,1024,"GET /rhizome/file/%s HTTP/1.0\r\n\r\n", q->fileid);
q->fileid);
q->request_len=strlen(q->request); q->request_len=strlen(q->request);
q->request_ofs=0; q->request_ofs=0;
q->state=RHIZOME_FETCH_CONNECTING; q->state=RHIZOME_FETCH_CONNECTING;

View File

@ -270,7 +270,6 @@ void rhizome_client_poll(struct sched_ent *alarm)
void rhizome_server_poll(struct sched_ent *alarm) void rhizome_server_poll(struct sched_ent *alarm)
{ {
if (debug & DEBUG_RHIZOMESYNC) D;
struct sockaddr addr; struct sockaddr addr;
unsigned int addr_len = sizeof addr; unsigned int addr_len = sizeof addr;
int sock; int sock;

View File

@ -335,14 +335,14 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
int ofs=0; int ofs=0;
int ad_frame_type=f->payload->bytes[ofs++]; int ad_frame_type=f->payload->bytes[ofs++];
struct sockaddr_in httpaddr = *(struct sockaddr_in *)f->recvaddr; 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; int manifest_length;
rhizome_manifest *m=NULL; rhizome_manifest *m=NULL;
switch (ad_frame_type) { switch (ad_frame_type) {
case 3: case 3:
/* The same as type=1, but includes the source HTTP port number */ /* 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; ofs += 2;
// FALL THROUGH ... // FALL THROUGH ...
case 1: case 1: