From: alex Date: Sun, 20 Mar 2022 01:03:33 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=15757de9d8e51d2e79c87703fca9451263b7cda8;p=seeder ... --- diff --git a/inc/net.h b/inc/net.h index bdc33a4..a3e67fe 100644 --- a/inc/net.h +++ b/inc/net.h @@ -38,9 +38,11 @@ struct net_info { void net_handler(int,int,struct peer*); int net_listen_sock(); void net_loop(); +int net_queue(int,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*,size_t); #endif diff --git a/inc/peer.h b/inc/peer.h index 98ef929..8f0bbda 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -1,12 +1,18 @@ #ifndef __PEER_H_ #define __PEER_H_ +#define PEER_INFOHASH_SIZE 20 +#define PEER_PEER_ID_SIZE 20 + struct peer { unsigned int choked : 1; unsigned int interested: 1; - uint8_t infohash[20]; - unsigned char peer_id[20]; + uint8_t infohash[PEER_INFOHASH_SIZE]; + unsigned char peer_id[PEER_PEER_ID_SIZE]; + + void *data; + size_t data_size; }; enum peer_message { diff --git a/src/net/handler.c b/src/net/handler.c index e2fe32a..4569d02 100644 --- a/src/net/handler.c +++ b/src/net/handler.c @@ -7,9 +7,8 @@ void net_handler(int epoll_fd, int sock, struct peer *peer_info) { unsigned int len; if(NULL==peer_info) { - if(peer_init(&peer_info)<0) { goto close; } if(peer_handshake(sock,peer_info)<0) { goto close; } - goto epoll; + return; } i = recv(sock,buf,4,MSG_PEEK); diff --git a/src/net/queue.c b/src/net/queue.c new file mode 100644 index 0000000..a46a5e9 --- /dev/null +++ b/src/net/queue.c @@ -0,0 +1,21 @@ +#include + +int net_queue(int sock, struct peer *info, void *p, size_t size) { + struct epoll_event ev; + + info->data = malloc(size); + if(NULL==info->data) { return -1; } + + memcpy(info->data,p,size); + + ev.events = EPOLLOUT; + ev.data.ptr = info; + ev.data.fd = sock; + + if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&ev)) { + perror("epoll_ctl"); + return -1; + } + + return 1; +} diff --git a/src/net/wait.c b/src/net/wait.c new file mode 100644 index 0000000..af59bcc --- /dev/null +++ b/src/net/wait.c @@ -0,0 +1,5 @@ +#include + +int net_wait(int sock, struct peer *info, size_t size) { + return -1; +} diff --git a/src/peer/handshake.c b/src/peer/handshake.c index d19063f..1bb7e8b 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -1,23 +1,65 @@ #include #define HANDSHAKE_STRING "\x13" "BitTorrent protocol\0\0\0\0\0\0\0\0"; -#define HANDSHAKE_INFOHASH_SIZE 20 +#define HANDSHAKE_STRING_LENGTH 28 -int peer_handshake(int sock, struct peer **info) { - ssize_t i; - const char header[64] = HANDSHAKE_STRING; +#define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE - +static int peer_handshake_receive(int); - assert(HANDSHAKE_INFOHASH_SIZE==infohash_size); - assert(PEER_ID_SIZE==peer_id_size); +int peer_handshake(int sock) { + struct peer *info; + char header[HEADER_LENGTH] = HANDSHAKE_STRING; + ssize_t i; if(sock<0) { return -1; } - memcpy(&(header[24]),infohash,HANDSHAKE_INFOHASH_SIZE); - memcpy(&(header[44]),peer_id,PEER_ID_SIZE); + if(peer_init(&info)<0) { return -1; } + + if(peer_handshake_receive(sock)<0) { return -1; } + + // copy infohash to buffer + memcpy( + &(header[HANDSHAKE_STRING_LENGTH]), + info->infohash, + PEER_INFOHASH_SIZE); + + // copy peer id to buffer + memcpy( + &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), + info->peer_id, + PEER_PEER_ID_SIZE); // send header + i = send(sock,header,HEADER_LENGTH,0); + if(i