#include<session.h>
int peer_bitfield(int,struct peer*);
+int peer_bitfield_received(int,struct peer*,ssize_t);
+int peer_bitfield_toggle(struct peer*,size_t);
int peer_cancel(int,struct peer*);
+int peer_cancel_received(int,struct peer*,ssize_t);
int peer_choke(int,struct peer*);
+int peer_choke_received(int,struct peer*,ssize_t);
void peer_free(struct peer*);
int peer_handshake(int,struct peer*);
int peer_hash_reject(int,struct peer*);
+int peer_hash_reject_received(int,struct peer*,ssize_t);
int peer_hash_request(int,struct peer*);
+int peer_hash_request_received(int,struct peer*,ssize_t);
int peer_hashes(int,struct peer*);
-int peer_have(int,struct peer*);
+int peer_hashes_received(int,struct peer*,ssize_t);
+int peer_have(int,struct peer*,size_t);
+int peer_have_received(int,struct peer*,ssize_t);
int peer_init(struct peer**);
int peer_interested(int,struct peer*);
+int peer_interested_received(int,struct peer*,ssize_t);
int peer_keepalive(int,struct peer*);
+int peer_keepalive_received(int,struct peer*,ssize_t);
int peer_not_interested(int,struct peer*);
+int peer_not_interested_received(int,struct peer*,ssize_t);
int peer_piece(int,struct peer*);
+int peer_piece_received(int,struct peer*,ssize_t);
int peer_reject(int,struct peer*);
+int peer_reject_received(int,struct peer*,ssize_t);
int peer_request(int,struct peer*);
+int peer_request_received(int,struct peer*,ssize_t);
int peer_unchoke(int,struct peer*);
+int peer_unchoke_received(int,struct peer*,ssize_t);
#endif
i += buf[j];
}
- return i;
+ return i+4;
}
static int send_remaining(int,struct peer*);
-
void net_handler(int epoll_fd, int sock, struct peer *info) {
enum peer_message type;
- uint32_t i;
+ ssize_t i;
if(epoll_fd<0) { return; }
if(sock<0) { return; }
switch(type) {
/* these are ordered based on enum peer_message */
case PEER_MESSAGE_KEEPALIVE:
- if(peer_keepalive(sock,info)<0) { goto close; }
+ if(peer_keepalive_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_CHOKE:
- if(peer_choke(sock,info)<0) { goto close; }
+ if(peer_choke_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_UNCHOKE:
- if(peer_unchoke(sock,info)<0) { goto close; }
+ if(peer_unchoke_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_INTERESTED:
- if(peer_interested(sock,info)<0) { goto close; }
+ if(peer_interested_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_NOT_INTERESTED:
- if(peer_not_interested(sock,info)<0) { goto close; }
+ if(peer_not_interested_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HAVE:
- if(peer_have(sock,info)<0) { goto close; }
+ if(peer_have_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_BITFIELD:
- if(peer_bitfield(sock,info)<0) { goto close; }
+ if(peer_bitfield_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_REQUEST:
- if(peer_request(sock,info)<0) { goto close; }
+ if(peer_request_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_PIECE:
- if(peer_piece(sock,info)<0) { goto close; }
+ if(peer_piece_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_CANCEL:
- if(peer_cancel(sock,info)<0) { goto close; }
+ if(peer_cancel_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_REJECT:
- if(peer_reject(sock,info)<0) { goto close; }
+ if(peer_reject_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASH_REQUEST:
- if(peer_hash_request(sock,info)<0) { goto close; }
+ if(peer_hash_request_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASHES:
- if(peer_hashes(sock,info)<0) { goto close; }
+ if(peer_hashes_received(sock,info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASH_REJECT:
- if(peer_hash_reject(sock,info)<0) { goto close; }
+ if(peer_hash_reject_received(sock,info,i)<0) { goto close; }
break;
default:
goto close;
int peer_bitfield(int sock, struct peer *info) {
return -1;
}
+
+int peer_bitfield_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
+
+int peer_bitfield_toggle(struct peer *info, size_t index) {
+ return -1;
+}
int peer_cancel(int sock, struct peer *info) {
return -1;
}
+
+int peer_cancel_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
return net_send(sock,info,buf,5);
}
+int peer_choke_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[5];
+
+ if(5!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,5)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ info->choked = 1;
+
+ return 1;
+}
+
int peer_unchoke(int sock, struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_UNCHOKE;
return net_send(sock,info,buf,5);
}
+
+int peer_unchoke_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[5];
+
+ if(5!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,5)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ info->choked = 0;
+
+ return 1;
+}
return -1;
}
+int peer_hash_request_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
+
int peer_hash_reject(int sock, struct peer *info) {
return -1;
}
+
+int peer_hash_reject_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
int peer_hashes(int sock, struct peer *info) {
return -1;
}
+
+int peer_hashes_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
#include<peer.h>
-int peer_have(int sock, struct peer *info) {
- return -1;
+int peer_have(int sock, struct peer *info, size_t index) {
+ unsigned char buf[9] = "\x00\x00\x00\x05";
+ buf[4] = PEER_MESSAGE_HAVE;
+
+ for(size_t i=0;i<4;i++) {
+ buf[8-i] = index & 0xff;
+ index >>= 8;
+ }
+
+ return net_send(sock,info,buf,9);
+}
+
+int peer_have_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[9];
+ size_t index;
+
+ if(9!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,9)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ index = 0;
+ for(size_t i=5;i<9;i++) {
+ index <<= 8;
+ index += buf[i];
+ }
+
+ if(peer_bitfield_toggle(info,index)<0) { return -1; }
+
+ return 1;
}
return net_send(sock,info,buf,5);
}
+int peer_interested_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[5];
+
+ if(5!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,5)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ info->interested = 1;
+
+ return 1;
+}
+
int peer_not_interested(int sock, struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_NOT_INTERESTED;
return net_send(sock,info,buf,5);
}
+
+int peer_not_interested_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[5];
+
+ if(5!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,5)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ info->interested = 0;
+
+ return 1;
+}
#include<peer.h>
int peer_keepalive(int sock, struct peer *info) {
- return -1;
+ unsigned char buf[4] = "\x00\x00\x00\x00";
+
+ return net_send(sock,info,buf,4);
+}
+
+int peer_keepalive_received(int sock, struct peer *info, ssize_t len) {
+ unsigned char buf[4];
+
+ if(4!=len) { return -1; }
+
+ if(net_wait(sock,info,buf,4)<0) { return -1; }
+ if(info->in!=NULL) { return 0; }
+
+ return 1;
}
int peer_piece(int sock, struct peer *info) {
return -1;
}
+
+int peer_piece_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
int peer_reject(int sock, struct peer *info) {
return -1;
}
+
+int peer_reject_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
int peer_request(int sock, struct peer *info) {
return -1;
}
+
+int peer_request_received(int sock, struct peer *info, ssize_t len) {
+ return -1;
+}
/* dummy functions (and array) used to test net_handler */
int message_count[25];
-int peer_bitfield(int sock,struct peer *info) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
-int peer_cancel(int sock,struct peer *info) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
-int peer_choke(int sock,struct peer *info) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
+int peer_bitfield_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
+int peer_cancel_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
+int peer_choke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
int peer_handshake(int sock,struct peer *info) { return 0; }
-int peer_hash_reject(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
-int peer_hash_request(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
-int peer_hashes(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
-int peer_have(int sock,struct peer *info) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
-int peer_interested(int sock,struct peer *info) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
-int peer_keepalive(int sock,struct peer *info) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
-int peer_not_interested(int sock,struct peer *info) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
-int peer_piece(int sock,struct peer *info) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
-int peer_reject(int sock,struct peer *info) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
-int peer_request(int sock,struct peer *info) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
-int peer_unchoke(int sock,struct peer *info) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
+int peer_hash_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
+int peer_hash_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
+int peer_hashes_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
+int peer_have_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
+int peer_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
+int peer_keepalive_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
+int peer_not_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
+int peer_piece_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
+int peer_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
+int peer_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
+int peer_unchoke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
/* end dummy functions */
static int read_sock;
/* verify keepalive returns as expected */
assert(4==write(write_sock,expected1,4));
- assert(0==net_expected(read_sock,&type));
+ assert(4==net_expected(read_sock,&type));
assert(PEER_MESSAGE_KEEPALIVE==type);
assert(4==read(read_sock,buf,sizeof(buf)));
expected2[4] = PEER_MESSAGE_CHOKE;
assert(5==write(write_sock,expected2,5));
- assert(256==net_expected(read_sock,&type));
+ assert(260==net_expected(read_sock,&type));
assert(PEER_MESSAGE_CHOKE==type);
assert(5==read(read_sock,buf,sizeof(buf)));
expected3[4] = PEER_MESSAGE_HASHES;
assert(5==write(write_sock,expected3,5));
- assert(1048832==net_expected(read_sock,&type));
+ assert(1048836==net_expected(read_sock,&type));
assert(PEER_MESSAGE_HASHES==type);
assert(5==read(read_sock,buf,sizeof(buf)));
expected4[4] = PEER_MESSAGE_UNCHOKE;
assert(5==write(write_sock,expected4,5));
- assert(4027580672==net_expected(read_sock,&type));
+ assert(4027580676==net_expected(read_sock,&type));
assert(PEER_MESSAGE_UNCHOKE==type);
assert(5==read(read_sock,buf,sizeof(buf)));