From: alex Date: Sun, 6 Mar 2022 05:04:22 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=8c5a8abb209e80ac32432a9602b771635c037f8c;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 13aa51c..d591b4d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,8 +33,10 @@ seederd_SOURCES = \ src/log.c \ src/main.c \ src/meta.c \ + src/net/handler.c \ src/net/listen.c \ src/net/loop.c \ + src/net/setup.c \ src/net/start.c \ src/net/tcp.c \ src/net/udp.c \ diff --git a/inc/net.h b/inc/net.h index 9f82f62..c530d1d 100644 --- a/inc/net.h +++ b/inc/net.h @@ -3,17 +3,21 @@ #include +#include #include #include #include #include +#include #include #include #include struct net_info { - int tcp; - int udp; + int tcp_socket; + int udp_socket; + + int epoll_fd; }; #define NET_MESSAGE_SHUTDOWN_FAILED "net thread shutdown failed\n" @@ -30,8 +34,10 @@ struct net_info { hints.ai_next = NULL; \ } +void net_handler(); int net_listen_sock(); void net_loop(); +int net_setup(struct net_info*); int net_start(); int net_tcp(); int net_udp(); diff --git a/src/net/handler.c b/src/net/handler.c new file mode 100644 index 0000000..ae16468 --- /dev/null +++ b/src/net/handler.c @@ -0,0 +1,56 @@ +#include + +void net_handler(int sock) { + char buf[5]; + ssize_t i; + unsigned int len; + + i = recv(sock,buf,5,MSG_PEEK); + if(-1==i) { + if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { + perror("recv"); + return; + } + } + + len = 0; + for(size_t i=0;i<4;i++) { + len <<= 1; + len += buf[i]; + } + + if(0==len) { return; } // message of length 0 == keep-alive + + switch(buf[4]) { + case PEER_MESSAGE_CHOKE: + break; + case PEER_MESSAGE_UNCHOKE: + break; + case PEER_MESSAGE_INTERESTED: + break; + case PEER_MESSAGE_NOT_INTERESTED: + break; + case PEER_MESSAGE_HAVE: + break; + case PEER_MESSAGE_BITFIELD: + break; + case PEER_MESSAGE_REQUEST: + break; + case PEER_MESSAGE_PIECE: + break; + case PEER_MESSAGE_CANCEL: + break; + case PEER_MESSAGE_REJECT: + break; + case PEER_MESSAGE_HASH_REQUEST: + break; + case PEER_MESSAGE_HASHES: + break; + case PEER_MESSAGE_HASH_REJECT: + break; + default: + break; + } + + return; +} diff --git a/src/net/loop.c b/src/net/loop.c index 8f7a672..7d8221f 100644 --- a/src/net/loop.c +++ b/src/net/loop.c @@ -1,5 +1,57 @@ #include -void net_loop() { +static void net_await_epoll_event(struct net_info*); +static void net_await_udp_event(int); +static void setnonblocking(int); + +void net_loop(struct net_info *info) { + net_await_epoll_event(info); + net_await_udp_event(info->udp_socket); +} + +#define MAX_EVENTS 10 + +static void net_await_epoll_event(struct net_info *info) { + struct epoll_event ev, events[MAX_EVENTS]; + int nfds, conn_sock; + + nfds = epoll_wait(info->epoll_fd,events,MAX_EVENTS,-1); + if(nfds==-1) { + perror("epoll_wait"); + return; + } + + for(int n=0;ntcp_socket) { + conn_sock = accept(info->tcp_socket,NULL,NULL); + if(-1==conn_sock) { + perror("accept"); + return; + } + + setnonblocking(conn_sock); + + ev.events = EPOLLIN | EPOLLET; + ev.data.fd = conn_sock; + + if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) { + perror("epoll_ctl"); + return; + } + } else { + net_handler(events[n].data.fd); + } + } +} + +static void net_await_udp_event(int sock) { + return; +} + +static void setnonblocking(int sock) { + if(-1==fcntl(sock,F_SETFL,O_NONBLOCK)) { + perror("fcntl"); + } + return; } diff --git a/src/net/setup.c b/src/net/setup.c new file mode 100644 index 0000000..cb3fcb3 --- /dev/null +++ b/src/net/setup.c @@ -0,0 +1,31 @@ +#include + +static int net_setup_epoll(struct net_info*); + +int net_setup(struct net_info *info) { + if((info->tcp_socket = net_tcp())<0) { return -1; } + if((info->udp_socket = net_udp())<0) { return -1; } + + if(net_setup_epoll(info)<0) { return -1; } + + return 1; +} + +static int net_setup_epoll(struct net_info *info) { + struct epoll_event ev; + + info->epoll_fd = epoll_create1(0); + if(info->epoll_fd==-1) { + perror("epoll_create1"); + return -1; + } + + ev.events = EPOLLIN; + ev.data.fd = info->tcp_socket; + if(epoll_ctl(info->epoll_fd, EPOLL_CTL_ADD, info->tcp_socket, &ev)==-1) { + perror("epoll_ctl"); + return -1; + } + + return 1; +} diff --git a/src/net/start.c b/src/net/start.c index 3ba07a2..8a26b4e 100644 --- a/src/net/start.c +++ b/src/net/start.c @@ -9,8 +9,8 @@ static void net_stop(); static void net_shutdown(void *p) { struct net_info *info = (struct net_info*)p; - close(info->tcp); - close(info->udp); + close(info->tcp_socket); + close(info->udp_socket); return; } @@ -18,14 +18,13 @@ static void net_shutdown(void *p) { static void *net_spawn(void *p) { struct net_info info; - if((info.tcp = net_tcp())<0) { return NULL; } - if((info.udp = net_udp())<0) { return NULL; } + if(net_setup(&info)<0) { return NULL; } pthread_cleanup_push(&net_shutdown,&info); while(1) { pthread_testcancel(); - net_loop(); + net_loop(&info); } pthread_cleanup_pop(1);