...
authoralex <[email protected]>
Sat, 26 Mar 2022 21:17:48 +0000 (14:17 -0700)
committeralex <[email protected]>
Sat, 26 Mar 2022 21:17:48 +0000 (14:17 -0700)
Makefile.am
inc/peer.h
src/net/cache.c [new file with mode: 0644]
src/net/loop.c
src/net/send.c
src/net/wait.c
src/peer/free.c [new file with mode: 0644]
src/peer/init.c [new file with mode: 0644]

index d591b4dc4b3c0353720cdf86afc50cc44fb0cf4f..b50172e00cb8da8a5c31b1a713f71de0a108f4a2 100644 (file)
@@ -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 \
index 36c117e72554afeec0804479f3524e834432ed79..f70f891cc48de9d954f87a4ed2cccde1cfd9ae7e 100644 (file)
@@ -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 (file)
index 0000000..875009c
--- /dev/null
@@ -0,0 +1,22 @@
+#include<net.h>
+
+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;
+}
+
index f20404918d9ee39b48cc17022908f064743188fc..59c60549d7e5b97a055b0e31bd6eb36b432e245e 100644 (file)
@@ -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;
                        }
index 468154f0644acd9382a61ca12cb3da6f47a6d747..e878ff1bb3b15949cec522d2c756151e27f11d3e 100644 (file)
@@ -1,25 +1,19 @@
 #include<net.h>
 
 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(i<size) {
-               if(i<0) {
-                       perror("send");
-                       return -1;
-               }
-
-               info->out = malloc(size-i);
-               if(NULL==info->out) {
-                       perror("malloc");
-                       return -1;
-               }
-
-               memcpy(info->out,p,size);
-               info->out_size = size;
-               return 0;
-       }
+       if(i<size) { goto cache; }
 
        return 1;
+cache:
+       if(net_cache(
+               &(info->out), /* void **prev */
+               &(info->out_size), /* size_t *prev_size */
+               &(p[i]), /* void *p */
+               (size-i) /* size_t size */
+               )<0) { return -1; }
+       return 0;
 }
index cfa5936be490febdcf06f5d8ea35f766c3bd13a9..636f52c84b2bf414fdd886d2f955d32f6d6f46a3 100644 (file)
@@ -1,28 +1,7 @@
 #include<net.h>
 
-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 (file)
index 0000000..0479f8d
--- /dev/null
@@ -0,0 +1,10 @@
+#include<peer.h>
+
+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 (file)
index 0000000..b51ea38
--- /dev/null
@@ -0,0 +1,23 @@
+#include<peer.h>
+
+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;
+}