From: alex Date: Sun, 6 Mar 2022 23:07:51 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=a4928cdf098b4ffc0ccd40b21ae1ea2abcbb95b4;p=seeder ... --- diff --git a/inc/net.h b/inc/net.h index c530d1d..bdc33a4 100644 --- a/inc/net.h +++ b/inc/net.h @@ -2,6 +2,7 @@ #define __NET_H_ #include +#include #include #include @@ -34,7 +35,7 @@ struct net_info { hints.ai_next = NULL; \ } -void net_handler(); +void net_handler(int,int,struct peer*); int net_listen_sock(); void net_loop(); int net_setup(struct net_info*); diff --git a/inc/peer.h b/inc/peer.h index 038e3ab..98ef929 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -1,6 +1,14 @@ #ifndef __PEER_H_ #define __PEER_H_ +struct peer { + unsigned int choked : 1; + unsigned int interested: 1; + + uint8_t infohash[20]; + unsigned char peer_id[20]; +}; + enum peer_message { PEER_MESSAGE_CHOKE = 0, PEER_MESSAGE_UNCHOKE = 1, @@ -17,6 +25,7 @@ enum peer_message { PEER_MESSAGE_HASH_REJECT = 23 }; -int peer_handshake(int,const uint8_t*,size_t,const uint8_t*,size_t); +int peer_handshake(int,struct peer*); +int peer_init(struct peer**); #endif diff --git a/inc/session.h b/inc/session.h index 8054421..ebc3066 100644 --- a/inc/session.h +++ b/inc/session.h @@ -14,6 +14,7 @@ struct session_torrents { struct session { struct session_torrents torrents; + unsigned char peer_id[20]; }; extern struct session session; diff --git a/src/net/handler.c b/src/net/handler.c index ae16468..e2fe32a 100644 --- a/src/net/handler.c +++ b/src/net/handler.c @@ -1,18 +1,32 @@ #include -void net_handler(int sock) { - char buf[5]; +void net_handler(int epoll_fd, int sock, struct peer *peer_info) { + struct epoll_event ev; + unsigned char buf[4]; ssize_t i; unsigned int len; - i = recv(sock,buf,5,MSG_PEEK); + if(NULL==peer_info) { + if(peer_init(&peer_info)<0) { goto close; } + if(peer_handshake(sock,peer_info)<0) { goto close; } + goto epoll; + } + + i = recv(sock,buf,4,MSG_PEEK); if(-1==i) { if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { perror("recv"); return; } + goto epoll; } + /* + * message should at least have 4 bytes (length prefix of + * the following message) + */ + if(i<4) { goto epoll; } + len = 0; for(size_t i=0;i<4;i++) { len <<= 1; @@ -53,4 +67,17 @@ void net_handler(int sock) { } return; +close: + if(peer_info!=NULL) { peer_free(peer_info); } + close(sock); + return; +epoll: + ev.events = EPOLLIN | EPOLLET; + ev.data.ptr = peer_info; + ev.data.fd = sock; + + if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&ev)) { + perror("epoll_ctl"); + return; + } } diff --git a/src/net/loop.c b/src/net/loop.c index 7d8221f..ee45b53 100644 --- a/src/net/loop.c +++ b/src/net/loop.c @@ -32,6 +32,7 @@ static void net_await_epoll_event(struct net_info *info) { setnonblocking(conn_sock); ev.events = EPOLLIN | EPOLLET; + ev.data.ptr = NULL; ev.data.fd = conn_sock; if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) { @@ -39,7 +40,7 @@ static void net_await_epoll_event(struct net_info *info) { return; } } else { - net_handler(events[n].data.fd); + net_handler(info->epoll_fd,events[n].data.fd,events[n].data.ptr); } } } diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 6325b62..d19063f 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -3,9 +3,12 @@ #define HANDSHAKE_STRING "\x13" "BitTorrent protocol\0\0\0\0\0\0\0\0"; #define HANDSHAKE_INFOHASH_SIZE 20 -int peer_handshake(int sock, const uint8_t *infohash, size_t infohash_size const uint8_t *peer_id, size_t peer_id_size) { +int peer_handshake(int sock, struct peer **info) { + ssize_t i; const char header[64] = HANDSHAKE_STRING; + + assert(HANDSHAKE_INFOHASH_SIZE==infohash_size); assert(PEER_ID_SIZE==peer_id_size);