From: alex Date: Sat, 26 Mar 2022 21:17:48 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=9f7024fba92da507df56d56f6b539739874b8f93;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index d591b4d..b50172e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,6 +33,7 @@ seederd_SOURCES = \ src/log.c \ src/main.c \ src/meta.c \ + src/net/cache.c \ src/net/handler.c \ src/net/listen.c \ src/net/loop.c \ @@ -51,6 +52,8 @@ seederd_SOURCES = \ src/opt/set.c \ src/opt/watch.c \ src/opt/worker.c \ + src/peer/free.c \ + src/peer/init.c \ src/pqueue.c \ src/rss/entry.c \ src/rss/footer.c \ diff --git a/inc/peer.h b/inc/peer.h index 36c117e..f70f891 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -35,6 +35,7 @@ enum peer_message { PEER_MESSAGE_HASH_REJECT = 23 }; +void peer_free(struct peer*); int peer_handshake(int,struct peer*); int peer_init(struct peer**); diff --git a/src/net/cache.c b/src/net/cache.c new file mode 100644 index 0000000..875009c --- /dev/null +++ b/src/net/cache.c @@ -0,0 +1,22 @@ +#include + +int net_cache(void **prev, size_t *prev_size, void *p, size_t size) { + void *next; + size_t next_size; + + next_size = ((*prev)!=NULL) ? size + (*prev_size) : size; + next = malloc(next_size); + if(NULL==next) { return -1; } + + if((*prev)!=NULL) { + memcpy(next,(*prev),(*prev_size)); + memcpy(&(next[(*prev_size)]),p,size); + free((*prev)); + } + + (*prev) = next; + (*prev_size) = next_size; + + return 1; +} + diff --git a/src/net/loop.c b/src/net/loop.c index f204049..59c6054 100644 --- a/src/net/loop.c +++ b/src/net/loop.c @@ -39,6 +39,7 @@ static void net_await_epoll_event(struct net_info *info) { } if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) { + peer_free(ev.data.ptr); perror("epoll_ctl"); return; } diff --git a/src/net/send.c b/src/net/send.c index 468154f..e878ff1 100644 --- a/src/net/send.c +++ b/src/net/send.c @@ -1,25 +1,19 @@ #include int net_send(int sock, struct peer *info, void *p, size_t size) { + void *next; ssize_t i; i = send(sock,p,size,0); - if(iout = malloc(size-i); - if(NULL==info->out) { - perror("malloc"); - return -1; - } - - memcpy(info->out,p,size); - info->out_size = size; - return 0; - } + if(iout), /* void **prev */ + &(info->out_size), /* size_t *prev_size */ + &(p[i]), /* void *p */ + (size-i) /* size_t size */ + )<0) { return -1; } + return 0; } diff --git a/src/net/wait.c b/src/net/wait.c index cfa5936..636f52c 100644 --- a/src/net/wait.c +++ b/src/net/wait.c @@ -1,28 +1,7 @@ #include -static int cache(struct peer*,void*,size_t); static void* use_cached(struct peer*,void*,size_t*); -static int cache(struct peer *info, void *p, size_t size) { - void *next; - size_t i; - - next_size = (info->in!=NULL) ? size + info->in_size : size; - next = malloc(next_size); - if(NULL==next) { return -1; } - - memcpy(next,p,size); - if(info->in!=NULL) { - memcpy(&(next[size]),info->in,info->in_size); - free(info->in); - } - - info->in = next; - info->in_size = next_size; - - return 1; -} - int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) { void *p; size_t size; @@ -43,7 +22,12 @@ int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) { return 1; cached: - cache(info,p,i+(buf_size - size)); + if(net_cache( + &(info->in), /* void **prev */ + &(info->in_size), /* size_t *prev_size */ + p, /* void *p */ + i /* size_t size */ + )<0) { return -1; } return 0; } diff --git a/src/peer/free.c b/src/peer/free.c new file mode 100644 index 0000000..0479f8d --- /dev/null +++ b/src/peer/free.c @@ -0,0 +1,10 @@ +#include + +void peer_free(struct peer *p) { + if(p!=NULL) { + if(p->out!=NULL) { free(p->out); } + if(p->in!=NULL) { free(p->in); } + + free(p); + } +} diff --git a/src/peer/init.c b/src/peer/init.c new file mode 100644 index 0000000..b51ea38 --- /dev/null +++ b/src/peer/init.c @@ -0,0 +1,23 @@ +#include + +int peer_init(struct peer **p) { + assert(p!=NULL); + + (*p) = malloc(sizeof(struct peer)); + if(NULL==(*p)) { return -1; } + + (*p)->handshake = 0; + (*p)->choked = 0; + (*p)->interested = 0; + + memset((*p)->infohash,0,PEER_INFOHASH_SIZE); + memset((*p)->peer_id,0,PEER_PEER_ID_SIZE); + + (*p)->out = NULL; + (*p)->out_size = 0; + + (*p)->in = NULL; + (*p)->in_size = 0; + + return 1; +}