[Pkg-privacy-commits] [onioncat] 18/340: thread start helper improved

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:04:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch debian
in repository onioncat.

commit c7cc61203237b3ee140741bae043692bf33e3906
Author: eagle <eagle at 58e1ccc2-750e-0410-8d0d-f93ca75ab447>
Date:   Mon Feb 11 20:43:10 2008 +0000

    thread start helper improved
    
    git-svn-id: http://www.cypherpunk.at/svn/onioncat/trunk@134 58e1ccc2-750e-0410-8d0d-f93ca75ab447
---
 ocat.c       |   3 +-
 ocat.h       |   1 +
 ocatroute.c  | 131 ++++++++---------------------------------------------------
 ocatthread.c |  61 +++++++++++++++++++++++-----
 ocattun.c    |   3 ++
 5 files changed, 73 insertions(+), 126 deletions(-)

diff --git a/ocat.c b/ocat.c
index 5029e55..5384f31 100644
--- a/ocat.c
+++ b/ocat.c
@@ -104,6 +104,7 @@ int main(int argc, char *argv[])
    if (!argv[optind])
       usage(argv[0]), exit(1);
 
+   // init main thread
    (void) init_ocat_thread("main");
 
    if (urlconv == 2)
@@ -171,7 +172,7 @@ int main(int argc, char *argv[])
    run_ocat_thread("connector", socks_connector);
    // start packet dequeuer
    run_ocat_thread("dequeuer", packet_dequeuer);
-
+   // start controller socket thread
    run_ocat_thread("controller", ocat_controller);
 
    // start forwarding packets from tunnel
diff --git a/ocat.h b/ocat.h
index eb2add1..99acd29 100644
--- a/ocat.h
+++ b/ocat.h
@@ -88,6 +88,7 @@ typedef struct OcatThread
    pthread_t handle;
    int id;
    char name[THREAD_NAME_LEN];
+   void *(*entry)(void*);
 } OcatThread_t;
 
 typedef struct SocksQueue
diff --git a/ocatroute.c b/ocatroute.c
index f1f0eff..6166eb9 100644
--- a/ocatroute.c
+++ b/ocatroute.c
@@ -27,9 +27,6 @@ static int sockfd_;
 // file descriptors of socket_receiver pipe
 // used for internal communication
 static int lpfd_[2];
-// file descriptors of socks_connector pipe
-// used for internal communication
-//static int cpfd_[2];
 // array of active peers
 static OcatPeer_t peer_[MAXPEERS];
 // mutex for locking array of peers
@@ -48,6 +45,7 @@ static pthread_mutex_t socks_queue_mutex_ = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t socks_queue_cond_ = PTHREAD_COND_INITIALIZER;
 
 // frame header of local OS in network byte order
+// it is initialized in ocattun.c
 uint32_t fhd_key_ = 0;
 
 uint16_t tor_socks_port_ = TOR_SOCKS_PORT;
@@ -96,15 +94,6 @@ void delete_peer(OcatPeer_t *peer)
    memset(peer, 0, sizeof(OcatPeer_t));
 }
 
-/*
-void mk_ocat_frame(const struct in6_addr *addr, const struct ip6_hdr *ihd, OcatHdr_t *ohd)
-{
-   memcpy(ohd, ihd, sizeof(struct ip6_hdrctl));
-   memcpy(ohd->oh_srcid, (char*)addr + 6, 10);
-   memcpy(ohd + 1, ihd + 1, ihd->ip6_plen);
-}
-*/
-
 
 void rewrite_framehdr(char *buf, int len)
 {
@@ -196,8 +185,6 @@ void *packet_dequeuer(void *p)
    int rc, timed = 0;
    time_t delay;
 
-   (void) init_ocat_thread(p);
-
    for (;;)
    {
       pthread_mutex_lock(&queue_mutex_);
@@ -240,18 +227,6 @@ void *packet_dequeuer(void *p)
 }
 
 
-/*
-void init_packet_dequeuer(void)
-{
-   pthread_t thread;
-   int rc;
-
-   if ((rc = pthread_create(&thread, NULL, packet_dequeuer, NULL)))
-      log_msg(L_FATAL, "[init_packet_dequeuer] could not start socket_receiver thread: \"%s\"", strerror(rc));
-}
-*/
-
-
 const static char hdigit_[] = "0123456789abcdef";
 
 void hex_code_header(const char *frame, int len, char *buf)
@@ -272,24 +247,11 @@ void hex_code_header(const char *frame, int len, char *buf)
 int validate_frame(const struct ip6_hdr *ihd, int len)
 {
    char buf[INET6_ADDRSTRLEN];
-   //struct ip6_hdr *ihd = (struct ip6_hdr*) (frame + 4);
    char hexbuf[IP6HLEN * 3 + 1];
 
    hex_code_header((char*) ihd, len > IP6HLEN ? IP6HLEN : len, hexbuf);
    log_msg(L_DEBUG, "[validate_frame] header \"%s\"", hexbuf);
 
-   /*
-   if (len < IP6HLEN + 4)
-   {
-      log_msg(L_ERROR, "[validate_frame] frame too short: %d bytes", len);
-      return 0;
-   }
-   if (*((uint16_t*) &frame[2]) != htons(0x86dd))
-   {
-      log_msg(L_ERROR, "[validate_frame] ethertype is not IPv6");
-      return 0;
-   }
-   */
    if (!has_tor_prefix(&ihd->ip6_dst))
    {
       log_msg(L_ERROR, "[validate_frame] destination %s unreachable", inet_ntop(AF_INET6, &ihd->ip6_dst, buf, INET6_ADDRSTRLEN));
@@ -330,8 +292,6 @@ void *socket_receiver(void *p)
    struct ip6_hdr *ihd;
    ihd = (struct ip6_hdr*) &buf[4];
 
-   (void) init_ocat_thread(p);
-
    if (pipe(lpfd_) < 0)
       log_msg(L_FATAL, "[init_socket_receiver] could not create pipe for socket_receiver: \"%s\"", strerror(errno)), exit(1);
 
@@ -435,21 +395,6 @@ void *socket_receiver(void *p)
 }
 
 
-/*
-void init_socket_receiver(void)
-{
-   pthread_t thread;
-   int rc;
-
-   if (pipe(lpfd_) < 0)
-      log_msg(L_FATAL, "[init_socket_receiver] could not create pipe for socket_receiver: \"%s\"", strerror(errno)), exit(1);
-
-   if ((rc = pthread_create(&thread, NULL, socket_receiver, NULL)))
-      log_msg(L_FATAL, "[init_socket_receiver] could not start socket_receiver thread: \"%s\"", strerror(rc));
-}
-*/
-
-
 void set_nonblock(int fd)
 {
    long flags;
@@ -459,10 +404,8 @@ void set_nonblock(int fd)
       log_msg(L_ERROR, "could not get socket flags for %d: \"%s\"", fd, strerror(errno));
       flags = 0;
    }
-
    log_msg(L_DEBUG, "O_NONBLOCK currently is %x", flags & O_NONBLOCK);
 
-   //if ((fcntl(socket, F_SETFL, flags | O_NONBLOCK)) == -1)
    if ((fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1)
       log_msg(L_ERROR, "[set_nonblock] could not set O_NONBLOCK for %d: \"%s\"", fd, strerror(errno));
 }
@@ -504,10 +447,6 @@ void *socket_acceptor(void *p)
    int fd;
    struct sockaddr_in in;
 
-   (void) init_ocat_thread(p);
-
-   log_msg(L_NOTICE, "[socket_acceptor] running");
-
    memset(&in, 0, sizeof(in));
    in.sin_family = AF_INET;
    in.sin_port = htons(ocat_listen_port_);
@@ -541,39 +480,6 @@ void *socket_acceptor(void *p)
 }
 
 
-/*
-void init_socket_acceptor(void)
-{
-   struct sockaddr_in in;
-   pthread_t thread;
-   int rc;
-
-   memset(&in, 0, sizeof(in));
-   in.sin_family = AF_INET;
-   in.sin_port = htons(ocat_listen_port_);
-   in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-#ifndef linux
-   in.sin_len = sizeof(in);
-#endif
-
-   if ((sockfd_ = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-      log_msg(L_FATAL, "[init_socket_acceptor] could not create listener socker: \"%s\"", strerror(errno)), exit(1);
-
-   if (bind(sockfd_, (struct sockaddr*) &in, sizeof(struct sockaddr_in)) < 0)
-      log_msg(L_FATAL, "[init_socket_acceptor] could not bind listener: \"%s\"", strerror(errno)), exit(1);
-
-   if (listen(sockfd_, 32) < 0)
-      log_msg(L_FATAL, "[init_socket_acceptor] could not bring listener to listening state: \"%s\"", strerror(errno)), exit(1);
-   
-   log_msg(L_NOTICE, "[init_socket_acceptor] created local listener on port %d", ocat_listen_port_);
-
-   if ((rc = pthread_create(&thread, NULL, socket_acceptor, NULL)))
-      log_msg(L_FATAL, "[init_socket_acceptor] could not create socket_acceptor: \"%s\"", strerror(rc)), exit(1);
-}
-*/
-
-
-//int socks_connect(const char *onion)
 int socks_connect(const struct in6_addr *addr)
 {
    struct sockaddr_in in /* = {AF_INET, htons(tor_socks_port_), {htonl(INADDR_LOOPBACK)}}*/;
@@ -676,9 +582,7 @@ void *socks_connector(void *p)
 {
    OcatPeer_t *peer;
    SocksQueue_t **squeue, *sq;
-   int i, rc, run = 1;
-
-   (void) init_ocat_thread(p);
+   int i, rc, ps, run = 1;
 
    if ((rc = pthread_detach(pthread_self())))
       log_msg(L_ERROR, "couldn't detach: \"%s\"", rc);
@@ -709,8 +613,8 @@ void *socks_connector(void *p)
       pthread_mutex_unlock(&peer_mutex_);
 
       if (!peer)
-         for (i = 0; i < SOCKS_MAX_RETRY; i++)
-            socks_connect(&(*squeue)->addr);
+         for (i = 0, ps = -1; i < SOCKS_MAX_RETRY && ps < 0; i++)
+            ps = socks_connect(&(*squeue)->addr);
       else
          log_msg(L_NOTICE, "peer already exists, ignoring");
 
@@ -731,7 +635,6 @@ void *socks_connector(void *p)
 void packet_forwarder(void)
 {
    char buf[FRAME_SIZE];
-   //char addr[INET6_ADDRSTRLEN];
    struct ip6_hdr *ihd;
    int rlen;
 
@@ -739,8 +642,19 @@ void packet_forwarder(void)
 
    for (;;)
    {
-      //rlen = receive_packet(tunfd_, data);
-      rlen = read(tunfd_[0], buf, FRAME_SIZE);
+      if ((rlen = read(tunfd_[0], buf, FRAME_SIZE)) == -1)
+      {
+         rlen = errno;
+         log_msg(L_DEBUG, "read from tun %d returned on error: \"%s\"", strerror(rlen));
+         if (rlen == EINTR)
+         {
+            log_msg(L_DEBUG, "signal caught, exiting");
+            return;
+         }
+         log_msg(L_DEBUG, "restart reading");
+         continue;
+      }
+
       log_msg(L_DEBUG, "[packet_forwarder] received on tunfd %d, framesize %d", tunfd_[0], rlen);
 
       if (!validate_frame(ihd, rlen - 4))
@@ -753,12 +667,6 @@ void packet_forwarder(void)
       if (!forward_packet(&ihd->ip6_dst, buf, rlen))
       {
          log_msg(L_NOTICE, "[packet_forwarder] establishing new socks peer");
-         //push_socks_connector(&ihd->ip6_dst);
-         /*
-         log_msg(L_DEBUG, "[packet_forwarder] writing %s to socks connector pipe %d", inet_ntop(AF_INET6, &ihd->ip6_dst, addr, INET6_ADDRSTRLEN), cpfd_[1]);
-         if (write(cpfd_[1], &ihd->ip6_dst, sizeof(struct in6_addr)) != sizeof(struct in6_addr))
-            log_msg(L_ERROR, "couldn't write %d bytes to SOCKS connector pipe %d", sizeof(struct in6_addr), cpfd_[1]);
-            */
          socks_queue(&ihd->ip6_dst);
          log_msg(L_DEBUG, "[packet_forwarder] queuing packet");
          queue_packet(&ihd->ip6_dst, buf, rlen);
@@ -771,9 +679,6 @@ void *socket_cleaner(void *p)
 {
    int i;
 
-   (void) init_ocat_thread(p);
-
-   log_msg(L_NOTICE, "running");
    for (;;)
    {
       sleep(CLEANER_WAKEUP);
@@ -802,8 +707,6 @@ void *ocat_controller(void *p)
    struct tm *tm;
    OcatThread_t *th;
 
-   (void) init_ocat_thread(p);
-
    memset(&in, 0, sizeof(in));
    in.sin_family = AF_INET;
    in.sin_port = htons(OCAT_CTRL_PORT);
diff --git a/ocatthread.c b/ocatthread.c
index 11eee0e..73aa735 100644
--- a/ocatthread.c
+++ b/ocatthread.c
@@ -10,6 +10,7 @@
 #include <stdlib.h>
 #include <pthread.h>
 #include <errno.h>
+#include <signal.h>
 
 #include "ocat.h"
 
@@ -19,21 +20,16 @@ static int thread_id_ = 0;
 pthread_mutex_t thread_mutex_ = PTHREAD_MUTEX_INITIALIZER;
 OcatThread_t *octh_ = NULL;
 
-/*
-void init_threads(void)
-{
-   memset(octh_, 0, sizeof(OcatThread_t) * MAX_THREADS);
-}
-*/
-
 
 const OcatThread_t *init_ocat_thread(const char *name)
 {
    OcatThread_t *th;
 
+   // get memory for the ocat internal thread structure
    if (!(th = malloc(sizeof(OcatThread_t))))
       return NULL;
 
+   // init ocat thread structure
    pthread_mutex_lock(&thread_mutex_);
    th->id = thread_id_++;
    strncpy(th->name, name, THREAD_NAME_LEN);
@@ -43,7 +39,6 @@ const OcatThread_t *init_ocat_thread(const char *name)
    octh_ = th;
    pthread_mutex_unlock(&thread_mutex_);
 
-   log_msg(L_NOTICE, "running");
    return th;
 }
 
@@ -51,7 +46,34 @@ const OcatThread_t *init_ocat_thread(const char *name)
 
 void *thread_run(void *p)
 {
-   (void) init_ocat_thread(p);
+   OcatThread_t **tl;
+   void *r;
+   sigset_t ss;
+
+   // block all signals for the thread
+   sigfillset(&ss);
+   pthread_sigmask(SIG_BLOCK, &ss, NULL);
+
+   // init internal ocat thread structure
+   (void) init_ocat_thread(((OcatThread_t *)p)->name);
+
+   // call thread entry function
+   log_msg(L_NOTICE, "running");
+   r = ((OcatThread_t*)p)->entry(NULL);
+   log_msg(L_NOTICE, "terminating");
+
+   pthread_mutex_lock(&thread_mutex_);
+   for (tl = &octh_; *tl; tl = &(*tl)->next)
+      if ((*tl)->handle == ((OcatThread_t*)p)->handle)
+         break;
+   free(p);
+   if ((p = *tl))
+   {
+      *tl = (*tl)->next;
+      free(p);
+   }
+   pthread_mutex_unlock(&thread_mutex_);
+
    return NULL;
 }
 
@@ -59,11 +81,28 @@ void *thread_run(void *p)
 int run_ocat_thread(const char *name, void *(*thfunc)(void*))
 {
    int rc;
-   pthread_t th;
+   OcatThread_t *th;
+
+   // we need a helper structure on startup.
+   // this is because pthread_create pushes only one arg.
+   // the helper struct is freed again from the thread
+   // (within thread_run()).
+   if (!(th = malloc(sizeof(OcatThread_t))))
+   {
+      rc = errno;
+      log_msg(L_FATAL, "could not create thread %s: \"%s\"", name, strerror(errno));
+      return rc;
+   }
+
+   strncpy(th->name, name, THREAD_NAME_LEN);
+   th->entry = thfunc;
 
    log_msg(L_DEBUG, "starting [%s]", name);
-   if ((rc = pthread_create(&th, NULL, thfunc, (void*) name)))
+   if ((rc = pthread_create(&th->handle, NULL, thread_run, th)))
+   {
       log_msg(L_FATAL, "could not start thread %s: \"%s\"", name, strerror(rc));
+      free(th);
+   }
 
    return rc;
 }
diff --git a/ocattun.c b/ocattun.c
index 8821966..f56e468 100644
--- a/ocattun.c
+++ b/ocattun.c
@@ -92,6 +92,9 @@ int tun_alloc(char *dev, struct in6_addr addr)
 
 #ifdef TEST_TUN_HDR
 
+/*! This is a test function which detects the frame
+ *  header of the local OS by sending a ping into
+ *  the tun by a call to system("ping6..."). */
 void test_tun_hdr(void)
 {
    struct in6_addr addr;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onioncat.git



More information about the Pkg-privacy-commits mailing list