From: alex Date: Thu, 31 Mar 2022 17:49:51 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=237a91701a756e130250739ff531cefbe41ffab9;p=seeder ... --- diff --git a/src/net/expected.c b/src/net/expected.c index 5916a5f..9a4ba5b 100644 --- a/src/net/expected.c +++ b/src/net/expected.c @@ -39,8 +39,5 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) { i += buf[j]; } - // check whether invalid length/message given - if((i==0)&&(buf[4]==0xff)) { return -1; } - return i+4; } diff --git a/src/net/peek.c b/src/net/peek.c index 5617b99..2103c36 100644 --- a/src/net/peek.c +++ b/src/net/peek.c @@ -1,30 +1,23 @@ #include int net_peek(struct peer *info, void *p, size_t size) { - void *next; + int i; if(NULL==info) { return -1; } if(NULL==p) { return -1; } if(0==size) { return 0; } - next = malloc(size); - if(NULL==next) { return -1; } - - if(net_wait(info,next,size)<0) { return -1; } - if(info->in!=NULL) { - /* - * incomplete net_wait; p wasn't filled - */ - free(next); + if((i = net_wait(info,p,size))<=0) { + if(i<0) { return -1; } return 0; } - if(0==info->in_size) { return 0; } - - info->in = next; - info->in_size = size; - - memcpy(p,next,size); + if(net_cache( + &(info->in), /* void **prev */ + &(info->in_size), /* size_t *prev_size */ + p, /* void *p */ + size /* size_t size */ + )<0) { return -1; } return 1; } diff --git a/src/net/wait.c b/src/net/wait.c index e400665..2c2730b 100644 --- a/src/net/wait.c +++ b/src/net/wait.c @@ -51,10 +51,12 @@ static void *use_cached(struct peer *info,void *p,size_t *size) { /* use up all cached data */ memcpy(p,info->in,info->in_size); free(info->in); - info->in = NULL; (*size) -= info->in_size; p += info->in_size; + + info->in = NULL; + info->in_size = 0; } } diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index 36bc51b..cf0fa50 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -305,20 +305,24 @@ static void net_peek_basic_test() { assert(info->in!=NULL); assert(info->in_size==5); assert(memcmp(info->in,expected,5)==0); + for(size_t i=0;i<5;i++) { - assert(buf[i]==0); + assert(buf[i]==1); + assert(buf[i+5]==0); } - free(info->in); - info->in = NULL; assert(5==write(write_sock,expected,5)); - assert(1==net_peek(info,buf,5)); + assert(1==net_peek(info,&(buf[5]),5)); assert(info->in!=NULL); assert(info->in_size==5); assert(memcmp(info->in,expected,5)==0); - assert(memcmp(buf,expected,5)==0); + assert(memcmp(buf,expected,10)==0); free(info->in); info->in = NULL; + + peer_free(info); + close(read_sock); + close(write_sock); } static void net_queue_basic_test() {