src/net/handler.c \
src/net/listen.c \
src/net/loop.c \
+ src/net/peek.c \
src/net/queue.c \
src/net/send.c \
src/net/setup.c \
}
int net_cache(void**,size_t*,void*,size_t);
-ssize_t net_expected(int,enum peer_message*);
-void net_handler(int,int,struct peer*);
+ssize_t net_expected(struct peer*,enum peer_message*);
+void net_handler(int,struct peer*);
int net_listen_sock();
void net_loop();
-int net_queue(int,int,struct peer*);
-int net_send(int,struct peer*,void*,size_t);
+int net_peek(struct peer*,void*,size_t);
+int net_queue(int,struct peer*);
+int net_send(struct peer*,void*,size_t);
int net_setup(struct net_info*);
int net_start();
int net_tcp();
int net_udp();
-int net_wait(int,struct peer*,void*,size_t);
+int net_wait(struct peer*,void*,size_t);
#endif
#include<string.h>
struct peer {
+ int sock;
+
unsigned int handshake : 1;
unsigned int choked : 1;
unsigned int interested: 1;
- uint8_t infohash[PEER_INFOHASH_SIZE];
unsigned char peer_id[PEER_PEER_ID_SIZE];
- void *out;
- size_t out_size;
+ uint8_t infohash[PEER_INFOHASH_SIZE];
+
+ uint8_t *bitfield;
+ size_t bitfield_size;
void *in;
size_t in_size;
+
+ void *out;
+ size_t out_size;
};
enum peer_message {
#include<net.h>
#include<session.h>
-int peer_bitfield(int,struct peer*);
-int peer_bitfield_received(int,struct peer*,ssize_t);
+int peer_bitfield(struct peer*);
+int peer_bitfield_received(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);
+int peer_cancel(struct peer*);
+int peer_cancel_received(struct peer*,ssize_t);
+int peer_choke(struct peer*);
+int peer_choke_received(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_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_handshake(struct peer*);
+int peer_hash_reject(struct peer*);
+int peer_hash_reject_received(struct peer*,ssize_t);
+int peer_hash_request(struct peer*);
+int peer_hash_request_received(struct peer*,ssize_t);
+int peer_hashes(struct peer*);
+int peer_hashes_received(struct peer*,ssize_t);
+int peer_have(struct peer*,size_t);
+int peer_have_received(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);
+int peer_interested(struct peer*);
+int peer_interested_received(struct peer*,ssize_t);
+int peer_keepalive(struct peer*);
+int peer_keepalive_received(struct peer*,ssize_t);
+int peer_not_interested(struct peer*);
+int peer_not_interested_received(struct peer*,ssize_t);
+int peer_piece(struct peer*);
+int peer_piece_received(struct peer*,ssize_t);
+int peer_reject(struct peer*);
+int peer_reject_received(struct peer*,ssize_t);
+int peer_request(struct peer*);
+int peer_request_received(struct peer*,ssize_t);
+int peer_unchoke(struct peer*);
+int peer_unchoke_received(struct peer*,ssize_t);
#endif
#include<net.h>
-ssize_t net_expected(int sock, enum peer_message *type) {
- unsigned char buf[4];
+ssize_t net_expected(struct peer *info, enum peer_message *type) {
+ unsigned char buf[5];
ssize_t i;
+ if(NULL==info) { return -1; }
+
(*type) = PEER_MESSAGE_KEEPALIVE;
-
- i = recv(sock,buf,5,MSG_PEEK);
- if(-1==i) {
- if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
- perror("recv");
- return -1;
- }
- // returning 0 here won't affect anything,
- // will be treated as a keep alive (although
- // message is still coming in, but since MSG_PEEK
- // flag is used no data will have been consumed)
- // and sock will be returned to epoll
+ /*
+ * used to figure out whether 4 or 5 bytes
+ * received; 0xff is an invalid message
+ * type
+ */
+ buf[4] = 0xff;
+
+ if(net_peek(info,buf,5)<0) { return -1; }
+ if((info->in!=NULL)&&(info->in_size<4)) {
+ /*
+ * Message should at least have 4 bytes (length prefix of
+ * the following message).
+ *
+ * Returning 0 here won't affect anything,
+ * will be treated as a keep alive (although
+ * message is still coming in, but since net_peek
+ * is used no data will have been consumed)
+ * and sock will be returned to epoll.
+ */
return 0;
}
- /*
- * message should at least have 4 bytes (length prefix of
- * the following message)
- */
- if(i<4) { return 0; }
-
- if(i==5) { (*type) = (uint8_t)buf[4]; }
+ if(buf[4]!=0xff) { (*type) = (uint8_t)buf[4]; }
i = 0;
for(size_t j=0;j<4;j++) {
#include<net.h>
-static int send_remaining(int,struct peer*);
+static int send_remaining(struct peer*);
-void net_handler(int epoll_fd, int sock, struct peer *info) {
+void net_handler(int epoll_fd, struct peer *info) {
enum peer_message type;
ssize_t i;
if(epoll_fd<0) { return; }
- if(sock<0) { return; }
if(NULL==info) { goto close; }
if(info->out!=NULL) { goto remaining; }
if(!info->handshake) { goto handshake; }
- if((i = net_expected(sock,&type))<0) { goto close; }
+ if((i = net_expected(info,&type))<0) { goto close; }
switch(type) {
/* these are ordered based on enum peer_message */
case PEER_MESSAGE_KEEPALIVE:
- if(peer_keepalive_received(sock,info,i)<0) { goto close; }
+ if(peer_keepalive_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_CHOKE:
- if(peer_choke_received(sock,info,i)<0) { goto close; }
+ if(peer_choke_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_UNCHOKE:
- if(peer_unchoke_received(sock,info,i)<0) { goto close; }
+ if(peer_unchoke_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_INTERESTED:
- if(peer_interested_received(sock,info,i)<0) { goto close; }
+ if(peer_interested_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_NOT_INTERESTED:
- if(peer_not_interested_received(sock,info,i)<0) { goto close; }
+ if(peer_not_interested_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HAVE:
- if(peer_have_received(sock,info,i)<0) { goto close; }
+ if(peer_have_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_BITFIELD:
- if(peer_bitfield_received(sock,info,i)<0) { goto close; }
+ if(peer_bitfield_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_REQUEST:
- if(peer_request_received(sock,info,i)<0) { goto close; }
+ if(peer_request_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_PIECE:
- if(peer_piece_received(sock,info,i)<0) { goto close; }
+ if(peer_piece_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_CANCEL:
- if(peer_cancel_received(sock,info,i)<0) { goto close; }
+ if(peer_cancel_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_REJECT:
- if(peer_reject_received(sock,info,i)<0) { goto close; }
+ if(peer_reject_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASH_REQUEST:
- if(peer_hash_request_received(sock,info,i)<0) { goto close; }
+ if(peer_hash_request_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASHES:
- if(peer_hashes_received(sock,info,i)<0) { goto close; }
+ if(peer_hashes_received(info,i)<0) { goto close; }
break;
case PEER_MESSAGE_HASH_REJECT:
- if(peer_hash_reject_received(sock,info,i)<0) { goto close; }
+ if(peer_hash_reject_received(info,i)<0) { goto close; }
break;
default:
goto close;
goto queue;
remaining:
- if(send_remaining(sock,info)<0) { goto close; }
+ if(send_remaining(info)<0) { goto close; }
goto queue;
handshake:
- if(peer_handshake(sock,info)<0) { goto close; }
+ if(peer_handshake(info)<0) { goto close; }
goto queue;
queue:
- if(net_queue(epoll_fd,sock,info)<0) { goto close; }
+ if(net_queue(epoll_fd,info)<0) { goto close; }
return;
close:
+ close(info->sock);
peer_free(info);
- close(sock);
return;
}
-static int send_remaining(int sock, struct peer *info) {
- if(net_send(sock,info,info->out,info->out_size)<0) { return -1; }
+static int send_remaining(struct peer *info) {
+ if(net_send(info,info->out,info->out_size)<0) { return -1; }
free(info->out);
info->out = NULL;
return;
}
+ ((struct peer*)ev.data.ptr)->sock = conn_sock;
+
if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) {
peer_free(ev.data.ptr);
perror("epoll_ctl");
return;
}
} else {
- net_handler(info->epoll_fd,events[n].data.fd,events[n].data.ptr);
+ net_handler(info->epoll_fd,events[n].data.ptr);
}
}
}
--- /dev/null
+#include<net.h>
+
+int net_peek(struct peer *info, void *p, size_t size) {
+ void *next;
+ next = malloc(size);
+ if(NULL==next) { return -1; }
+
+ if(net_wait(info,next,size)<0) { return -1; }
+ if(info->in!=NULL) {
+ free(next);
+ return 0;
+ }
+
+ info->in = next;
+ info->in_size = size;
+
+ memcpy(p,next,size);
+
+ return 1;
+}
#include<net.h>
-int net_queue(int epoll_fd, int sock, struct peer *info) {
+int net_queue(int epoll_fd, struct peer *info) {
struct epoll_event ev;
if(epoll_fd<0) { return -1; }
- if(sock<0) { return -1; }
if(NULL==info) { return -1; }
+ if(info->sock<0) { return -1; }
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = info;
- ev.data.fd = sock;
+ ev.data.fd = info->sock;
- if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&ev)) {
+ if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,info->sock,&ev)) {
perror("epoll_ctl");
return -1;
}
#include<net.h>
-int net_send(int sock, struct peer *info, void *p, size_t size) {
+int net_send(struct peer *info, void *p, size_t size) {
ssize_t i;
- if(sock<0) { return -1; }
if(NULL==info) { return -1; }
+ if(info->sock<0) { return -1; }
if(NULL==p) { return -1; }
- i = send(sock,p,size,0);
+ i = send(info->sock,p,size,0);
if(-1==i) {
if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
perror("send");
static void* use_cached(struct peer*,void*,size_t*);
-int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) {
+int net_wait(struct peer *info, void *buf, size_t buf_size) {
void *p;
size_t size;
ssize_t i;
- if(sock<0) { return -1; }
if(NULL==info) { return -1; }
+ if(info->sock<0) { return -1; }
if(NULL==buf) { return -1; }
if(0==buf_size) { return 1; }
size = buf_size;
p = use_cached(info,buf,&size);
- i = recv(sock,p,size,0);
+ i = recv(info->sock,p,size,0);
if(-1==i) {
if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
perror("recv");
#include<peer.h>
-int peer_bitfield(int sock, struct peer *info) {
+int peer_bitfield(struct peer *info) {
return -1;
}
-int peer_bitfield_received(int sock, struct peer *info, ssize_t len) {
+int peer_bitfield_init(struct peer *info, size_t pieces) {
+ info->bitfield_size = pieces/8;
+
+ info->bitfield = calloc(pieces,sizeof(uint8_t));
+ if(NULL==info->bitfield) { return -1; }
+
+ return 1;
+}
+
+int peer_bitfield_received(struct peer *info, ssize_t len) {
+ uint8_t *buf;
+
+ if((len-5)!=info->bitfield_size) { return -1; }
+
+ buf = malloc(len);
+ if(NULL==buf) { return -1; }
+
+ if(net_wait(info,buf,len)<0) { goto fail; }
+ if(info->in!=NULL) { goto clean; }
+
+ for(size_t i=5;i<len;i++) {
+ info->bitfield[i] = buf[i];
+ }
+
+ return 1;
+fail:
+ free(buf);
return -1;
+clean:
+ free(buf);
+ return 0;
}
int peer_bitfield_toggle(struct peer *info, size_t index) {
- return -1;
+ size_t byte_index = index/8;
+
+ index %= 8;
+ info->bitfield[byte_index] = info->bitfield[byte_index] ^ (1<<index);
+
+ return 1;
}
#include<peer.h>
-int peer_cancel(int sock, struct peer *info) {
+int peer_cancel(struct peer *info) {
return -1;
}
-int peer_cancel_received(int sock, struct peer *info, ssize_t len) {
+int peer_cancel_received(struct peer *info, ssize_t len) {
return -1;
}
#include<peer.h>
-int peer_choke(int sock, struct peer *info) {
+int peer_choke(struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_CHOKE;
- return net_send(sock,info,buf,5);
+ return net_send(info,buf,5);
}
-int peer_choke_received(int sock, struct peer *info, ssize_t len) {
+int peer_choke_received(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(net_wait(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) {
+int peer_unchoke(struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_UNCHOKE;
- return net_send(sock,info,buf,5);
+ return net_send(info,buf,5);
}
-int peer_unchoke_received(int sock, struct peer *info, ssize_t len) {
+int peer_unchoke_received(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(net_wait(info,buf,5)<0) { return -1; }
if(info->in!=NULL) { return 0; }
info->choked = 0;
#define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE
-static int peer_handshake_receive(int,struct peer*);
+static int peer_handshake_receive(struct peer*);
-int peer_handshake(int sock, struct peer *info) {
+int peer_handshake(struct peer *info) {
char header[HEADER_LENGTH] = HANDSHAKE_STRING;
- if(sock<0) { return -1; }
-
- if(peer_handshake_receive(sock,info)<0) { return -1; }
+ if(peer_handshake_receive(info)<0) { return -1; }
if(!info->handshake) { return 0; }
// copy infohash to buffer
info->peer_id,
PEER_PEER_ID_SIZE);
- if(net_send(sock,info,header,HEADER_LENGTH)<0) { return -1; }
+ if(net_send(info,header,HEADER_LENGTH)<0) { return -1; }
return 1;
}
-static int peer_handshake_receive(int sock, struct peer *info) {
+static int peer_handshake_receive(struct peer *info) {
char header[HEADER_LENGTH];
struct torrent *p;
int i;
- if((i = net_wait(sock,info,header,HEADER_LENGTH))<=0) {
+ if((i = net_wait(info,header,HEADER_LENGTH))<=0) {
if(i<0) { return -1; }
return 0;
}
#include<peer.h>
-int peer_hash_request(int sock, struct peer *info) {
+int peer_hash_request(struct peer *info) {
return -1;
}
-int peer_hash_request_received(int sock, struct peer *info, ssize_t len) {
+int peer_hash_request_received(struct peer *info, ssize_t len) {
return -1;
}
-int peer_hash_reject(int sock, struct peer *info) {
+int peer_hash_reject(struct peer *info) {
return -1;
}
-int peer_hash_reject_received(int sock, struct peer *info, ssize_t len) {
+int peer_hash_reject_received(struct peer *info, ssize_t len) {
return -1;
}
#include<peer.h>
-int peer_hashes(int sock, struct peer *info) {
+int peer_hashes(struct peer *info) {
return -1;
}
-int peer_hashes_received(int sock, struct peer *info, ssize_t len) {
+int peer_hashes_received(struct peer *info, ssize_t len) {
return -1;
}
#include<peer.h>
-int peer_have(int sock, struct peer *info, size_t index) {
+int peer_have(struct peer *info, size_t index) {
unsigned char buf[9] = "\x00\x00\x00\x05";
buf[4] = PEER_MESSAGE_HAVE;
index >>= 8;
}
- return net_send(sock,info,buf,9);
+ return net_send(info,buf,9);
}
-int peer_have_received(int sock, struct peer *info, ssize_t len) {
+int peer_have_received(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(net_wait(info,buf,9)<0) { return -1; }
if(info->in!=NULL) { return 0; }
index = 0;
(*p)->choked = 0;
(*p)->interested = 0;
- memset((*p)->infohash,0,PEER_INFOHASH_SIZE);
memset((*p)->peer_id,0,PEER_PEER_ID_SIZE);
+
+ memset((*p)->infohash,0,PEER_INFOHASH_SIZE);
+
+ (*p)->bitfield = NULL;
+ (*p)->bitfield_size = 0;
(*p)->out = NULL;
(*p)->out_size = 0;
#include<peer.h>
-int peer_interested(int sock, struct peer *info) {
+int peer_interested(struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_INTERESTED;
- return net_send(sock,info,buf,5);
+ return net_send(info,buf,5);
}
-int peer_interested_received(int sock, struct peer *info, ssize_t len) {
+int peer_interested_received(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(net_wait(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) {
+int peer_not_interested(struct peer *info) {
unsigned char buf[5] = "\x00\x00\x00\x01";
buf[4] = PEER_MESSAGE_NOT_INTERESTED;
- return net_send(sock,info,buf,5);
+ return net_send(info,buf,5);
}
-int peer_not_interested_received(int sock, struct peer *info, ssize_t len) {
+int peer_not_interested_received(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(net_wait(info,buf,5)<0) { return -1; }
if(info->in!=NULL) { return 0; }
info->interested = 0;
#include<peer.h>
-int peer_keepalive(int sock, struct peer *info) {
+int peer_keepalive(struct peer *info) {
unsigned char buf[4] = "\x00\x00\x00\x00";
- return net_send(sock,info,buf,4);
+ return net_send(info,buf,4);
}
-int peer_keepalive_received(int sock, struct peer *info, ssize_t len) {
+int peer_keepalive_received(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(net_wait(info,buf,4)<0) { return -1; }
if(info->in!=NULL) { return 0; }
return 1;
#include<peer.h>
-int peer_piece(int sock, struct peer *info) {
+int peer_piece(struct peer *info) {
return -1;
}
-int peer_piece_received(int sock, struct peer *info, ssize_t len) {
+int peer_piece_received(struct peer *info, ssize_t len) {
return -1;
}
#include<peer.h>
-int peer_reject(int sock, struct peer *info) {
+int peer_reject(struct peer *info) {
return -1;
}
-int peer_reject_received(int sock, struct peer *info, ssize_t len) {
+int peer_reject_received(struct peer *info, ssize_t len) {
return -1;
}
#include<peer.h>
-int peer_request(int sock, struct peer *info) {
+int peer_request(struct peer *info) {
return -1;
}
-int peer_request_received(int sock, struct peer *info, ssize_t len) {
+int peer_request_received(struct peer *info, ssize_t len) {
return -1;
}
static void net_expected_basic_test();
static void net_handler_basic_test();
static void net_handler_invalid_message_test();
+static void net_peek_basic_test();
static void net_queue_basic_test();
static void net_send_basic_test();
static void net_wait_basic_test();
net_expected_basic_test();
net_handler_basic_test();
net_handler_invalid_message_test();
+ net_peek_basic_test();
net_queue_basic_test();
net_send_basic_test();
net_wait_basic_test();
enum peer_message type;
unsigned char buf[10];
- assert(1==peer_init(&info));
-
setup_sockets();
+ assert(1==peer_init(&info));
+ info->sock = read_sock;
+
/* verify messages with <4 bytes returns keepalive */
unsigned char expected1[4] = "\x00\x00\x00\x00";
assert(3==write(write_sock,expected1,3));
- assert(0==net_expected(read_sock,&type));
+ assert(0==net_expected(info,&type));
assert(PEER_MESSAGE_KEEPALIVE==type);
assert(3==read(read_sock,buf,sizeof(buf)));
/* verify keepalive returns as expected */
assert(4==write(write_sock,expected1,4));
- assert(4==net_expected(read_sock,&type));
+ assert(4==net_expected(info,&type));
assert(PEER_MESSAGE_KEEPALIVE==type);
assert(4==read(read_sock,buf,sizeof(buf)));
#define NET_HANDLER_HELPER(message,type) { \
assert(sizeof(message)==write(write_sock,message,sizeof(message))); \
\
- net_handler(epoll_fd,read_sock,info); \
+ net_handler(epoll_fd,info); \
assert(0==epoll_ctl(epoll_fd,EPOLL_CTL_DEL,read_sock,NULL)); \
assert(sizeof(message)==read(read_sock,buf,sizeof(buf))); \
\
struct peer *info;
int epoll_fd;
+ setup_sockets();
+
assert(1==peer_init(&info));
info->handshake = 1;
+ info->sock = read_sock;
epoll_fd = epoll_create1(0);
assert(epoll_fd!=-1);
- setup_sockets();
-
memset(message_count, 0, sizeof(message_count));
unsigned char invalid_message_type[5] = "\x00\x00\x00\x01\xff";
assert(5==write(write_sock,invalid_message_type,5));
- net_handler(epoll_fd,read_sock,info);
+ net_handler(epoll_fd,info);
for(size_t i=0;i<25;i++) {
assert(0==message_count[i]);
unsigned char buf[10];
int epoll_fd;
+ setup_sockets();
+
memset(message_count, 0, sizeof(message_count));
assert(1==peer_init(&info));
info->handshake = 1;
+ info->sock = read_sock;
epoll_fd = epoll_create1(0);
assert(epoll_fd!=-1);
- setup_sockets();
-
/* all peer_message enums indexes are +1 in message_count */
unsigned char keepalive_message[4] = "\x00\x00\x00\x00";
NET_HANDLER_HELPER(keepalive_message,PEER_MESSAGE_KEEPALIVE);
close(write_sock);
}
+static void net_peek_basic_test() {
+ struct peer *info;
+
+}
+
static void net_queue_basic_test() {
int epoll_fd;
int sock;
struct peer *info;
assert(1==peer_init(&info));
+ info->sock = 0;
epoll_fd = epoll_create1(0);
assert(epoll_fd!=-1);
- sock = 0;
-
- assert(-1==net_queue(-1,-1,NULL));
- assert(-1==net_queue(epoll_fd,-1,NULL));
- assert(-1==net_queue(epoll_fd,sock,NULL));
+ assert(-1==net_queue(-1,NULL));
+ assert(-1==net_queue(epoll_fd,NULL));
- assert(1==net_queue(epoll_fd,sock,info));
+ assert(1==net_queue(epoll_fd,info));
peer_free(info);
close(epoll_fd);
memset(expected,0,100);
assert(1==peer_init(&info));
+ info->sock = write_sock;
assert(-1==net_send(-1,NULL,NULL,0));
- assert(-1==net_send(write_sock,info,NULL,0));
+ assert(-1==net_send(info,NULL,0));
- assert(1==net_send(write_sock,info,buf,0));
+ assert(1==net_send(info,buf,0));
assert(-1==read(read_sock,expected,100));
- assert(1==net_send(write_sock,info,buf,50));
+ assert(1==net_send(info,buf,50));
assert(50==read(read_sock,expected,100));
- assert(1==net_send(write_sock,info,buf,100));
+ assert(1==net_send(info,buf,100));
assert(100==read(read_sock,expected,100));
close(read_sock);
memset(expected,0,100);
assert(1==peer_init(&info));
+ info->sock = read_sock;
- assert(-1==net_wait(-1,NULL,NULL,0));
- assert(-1==net_wait(read_sock,NULL,NULL,0));
- assert(-1==net_wait(read_sock,info,NULL,0));
- assert(1==net_wait(read_sock,info,expected,0));
+ assert(-1==net_wait(NULL,NULL,0));
+ assert(-1==net_wait(NULL,NULL,0));
+ assert(-1==net_wait(info,NULL,0));
+ assert(1==net_wait(info,expected,0));
- assert(0==net_wait(read_sock,info,buf,100));
+ assert(0==net_wait(info,buf,100));
assert(50==write(write_sock,expected,50));
- assert(0==net_wait(read_sock,info,buf,100));
+ assert(0==net_wait(info,buf,100));
assert(info->in!=NULL);
assert(info->in_size==50);
assert(memcmp(buf,expected,100)!=0);
assert(25==write(write_sock,expected,25));
- assert(0==net_wait(read_sock,info,buf,100));
+ assert(0==net_wait(info,buf,100));
assert(info->in!=NULL);
assert(info->in_size==75);
assert(memcmp(buf,expected,100)!=0);
assert(25==write(write_sock,expected,25));
- assert(1==net_wait(read_sock,info,buf,100));
+ assert(1==net_wait(info,buf,100));
assert(info->in==NULL);
assert(memcmp(buf,expected,100)==0);
i = 0;
info->in_size = 0;
while(i<99) {
- assert(0==net_wait(read_sock,info,buf,100));
+ assert(0==net_wait(info,buf,100));
increment = rand()%2;
i += increment;
assert(((increment>0)?1:0)==write(write_sock,expected,increment));
- assert(0==net_wait(read_sock,info,buf,100));
+ assert(0==net_wait(info,buf,100));
assert(info->in_size==i);
}
assert(1==write(write_sock,expected,1));
- assert(1==net_wait(read_sock,info,buf,100));
+ assert(1==net_wait(info,buf,100));
assert(info->in==NULL);
assert(memcmp(buf,expected,100)==0);