[Pkg-privacy-commits] [onioncat] 54/241: ocat 0.1.6

Intrigeri intrigeri at moszumanska.debian.org
Wed Aug 26 16:16:26 UTC 2015


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

intrigeri pushed a commit to branch upstream-master
in repository onioncat.

commit 3f927f4a727d190955e563e47d039082b90ce025
Author: eagle <eagle at 58e1ccc2-750e-0410-8d0d-f93ca75ab447>
Date:   Thu Jun 5 13:33:46 2008 +0000

    ocat 0.1.6
    
    git-svn-id: https://www.cypherpunk.at/svn/onioncat/trunk@219 58e1ccc2-750e-0410-8d0d-f93ca75ab447
---
 configure       |  20 ++++----
 configure.ac    |   2 +-
 src/Makefile.am |   2 +-
 src/ocat.h      |  13 ++++-
 src/ocatpeer.c  | 122 +++++++++++++++++++++++++++++++++++++++++++++
 src/ocatroute.c | 151 +++++++++++++++++---------------------------------------
 6 files changed, 192 insertions(+), 118 deletions(-)

diff --git a/configure b/configure
index 53e5171..a807748 100755
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.61 for onioncat 0.1.5.
+# Generated by GNU Autoconf 2.61 for onioncat 0.1.6.
 #
 # Report bugs to <eagle at cypherpunk.at>.
 #
@@ -574,8 +574,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
 # Identity of this package.
 PACKAGE_NAME='onioncat'
 PACKAGE_TARNAME='onioncat'
-PACKAGE_VERSION='0.1.5'
-PACKAGE_STRING='onioncat 0.1.5'
+PACKAGE_VERSION='0.1.6'
+PACKAGE_STRING='onioncat 0.1.6'
 PACKAGE_BUGREPORT='eagle at cypherpunk.at'
 
 ac_subst_vars='SHELL
@@ -1166,7 +1166,7 @@ if test "$ac_init_help" = "long"; then
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
-\`configure' configures onioncat 0.1.5 to adapt to many kinds of systems.
+\`configure' configures onioncat 0.1.6 to adapt to many kinds of systems.
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
@@ -1232,7 +1232,7 @@ fi
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
-     short | recursive ) echo "Configuration of onioncat 0.1.5:";;
+     short | recursive ) echo "Configuration of onioncat 0.1.6:";;
    esac
   cat <<\_ACEOF
 
@@ -1315,7 +1315,7 @@ fi
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
-onioncat configure 0.1.5
+onioncat configure 0.1.6
 generated by GNU Autoconf 2.61
 
 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
@@ -1329,7 +1329,7 @@ cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
-It was created by onioncat $as_me 0.1.5, which was
+It was created by onioncat $as_me 0.1.6, which was
 generated by GNU Autoconf 2.61.  Invocation command line was
 
   $ $0 $@
@@ -2019,7 +2019,7 @@ fi
 
 # Define the identity of the package.
  PACKAGE='onioncat'
- VERSION='0.1.5'
+ VERSION='0.1.6'
 
 
 cat >>confdefs.h <<_ACEOF
@@ -4243,7 +4243,7 @@ exec 6>&1
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
-This file was extended by onioncat $as_me 0.1.5, which was
+This file was extended by onioncat $as_me 0.1.6, which was
 generated by GNU Autoconf 2.61.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
@@ -4296,7 +4296,7 @@ Report bugs to <bug-autoconf at gnu.org>."
 _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF
 ac_cs_version="\\
-onioncat config.status 0.1.5
+onioncat config.status 0.1.6
 configured by $0, generated by GNU Autoconf 2.61,
   with options \\"`echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 
diff --git a/configure.ac b/configure.ac
index 8498f40..2d17241 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.61)
-AC_INIT([onioncat], [0.1.5], [eagle at cypherpunk.at])
+AC_INIT([onioncat], [0.1.6], [eagle at cypherpunk.at])
 AM_INIT_AUTOMAKE([-Wall -Werror])
 #AC_CONFIG_SRCDIR([ocatroute.c])
 AC_CONFIG_HEADERS([config.h])
diff --git a/src/Makefile.am b/src/Makefile.am
index ab0f1d7..51be3be 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,4 @@
 bin_PROGRAMS = ocat
-ocat_SOURCES = ocat.c ocatlog.c ocatroute.c ocatthread.c ocattun.c ocatv6conv.c ocatcompat.c
+ocat_SOURCES = ocat.c ocatlog.c ocatroute.c ocatthread.c ocattun.c ocatv6conv.c ocatcompat.c ocatpeer.c
 include_HEADERS = ocat.h
 
diff --git a/src/ocat.h b/src/ocat.h
index c72fd1f..17da4be 100644
--- a/src/ocat.h
+++ b/src/ocat.h
@@ -84,7 +84,7 @@ typedef struct SocksHdr
    char cmd;
    uint16_t port;
    struct in_addr addr;
-} SocksHdr_t;
+} __attribute__ ((packed)) SocksHdr_t;
 
 typedef struct OcatPeer
 {
@@ -227,5 +227,16 @@ const OcatThread_t *get_thread(void);
 size_t strlcat(char *, const char *, size_t);
 #endif
 
+/* ocatpeer.c */
+OcatPeer_t *get_first_peer(void);
+OcatPeer_t **get_first_peer_ptr(void);
+int lock_peers(void);
+int unlock_peers(void);
+int lock_peer(OcatPeer_t *);
+int unlock_peer(OcatPeer_t *);
+OcatPeer_t *search_peer(const struct in6_addr *);
+OcatPeer_t *get_empty_peer(void);
+void delete_peer(OcatPeer_t *);
+
 #endif
 
diff --git a/src/ocatpeer.c b/src/ocatpeer.c
new file mode 100644
index 0000000..c1a5f5b
--- /dev/null
+++ b/src/ocatpeer.c
@@ -0,0 +1,122 @@
+/*! ocatpeer.c
+ *  Contains functions for managing both kind of TCP peers.
+ *  Those are active SOCKS4A and passive TCP-LISTEN.
+ *
+ *  @author Bernhard Fischer <rahra _at_ cypherpunk at>
+ *  @version 2008/02/03-01
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <errno.h>
+
+#include "ocat.h"
+
+
+// array of active peers
+static OcatPeer_t *peer_ = NULL;
+// mutex for locking array of peers
+static pthread_mutex_t peer_mutex_ = PTHREAD_MUTEX_INITIALIZER;
+
+
+OcatPeer_t *get_first_peer(void)
+{
+   return peer_;
+}
+
+
+OcatPeer_t **get_first_peer_ptr(void)
+{
+   return &peer_;
+}
+
+
+int lock_peers(void)
+{
+   return pthread_mutex_lock(&peer_mutex_);
+}
+
+
+int unlock_peers(void)
+{
+   return pthread_mutex_unlock(&peer_mutex_);
+}
+
+
+int lock_peer(OcatPeer_t *peer)
+{
+   return pthread_mutex_lock(&peer->mutex);
+}
+
+
+int unlock_peer(OcatPeer_t *peer)
+{
+   return pthread_mutex_unlock(&peer->mutex);
+}
+
+
+OcatPeer_t *search_peer(const struct in6_addr *addr)
+{
+   OcatPeer_t *peer;
+
+   for (peer = peer_; peer; peer = peer->next)
+      if (!memcmp(addr, &peer->addr, sizeof(struct in6_addr)))
+         return peer;
+   return NULL;
+}
+
+
+OcatPeer_t *get_empty_peer(void)
+{
+   int rc;
+   OcatPeer_t *peer;
+
+   if (!(peer = calloc(1, sizeof(OcatPeer_t))))
+   {
+      log_msg(L_ERROR, "cannot get memory for new peer: \"%s\"", strerror(errno));
+      return NULL;
+   }
+
+   peer->fraghdr = fhd_key_;
+   if ((rc = pthread_mutex_init(&peer->mutex, NULL)))
+   {
+      log_msg(L_FATAL, "cannot init new peer mutex: \"%s\"", strerror(rc));
+      free(peer);
+      return NULL;
+   }
+
+   peer->next = peer_;
+   peer_ = peer;
+
+   return peer;  
+}
+
+
+/** peer list must be locked with lock_peers() in advance!
+ *  @param peer pointer to peer that shall be deleted.
+ */
+void delete_peer(OcatPeer_t *peer)
+{
+   int rc;
+   OcatPeer_t **p;
+
+   for (p = &peer_; *p; p = &(*p)->next)
+      if (*p == peer)
+      {
+         // unlink peer from list
+         lock_peer(peer);
+         *p = peer->next;
+         unlock_peer(peer);
+
+         // effectively delete it
+         if ((rc = pthread_mutex_destroy(&peer->mutex)))
+            log_msg(L_FATAL, "cannot destroy mutex: \"%s\"", strerror(rc));
+         free(peer);
+         return;
+      }
+}
+
diff --git a/src/ocatroute.c b/src/ocatroute.c
index ac84795..ed816d9 100644
--- a/src/ocatroute.c
+++ b/src/ocatroute.c
@@ -35,10 +35,6 @@ static int ctrlfd_[2];
 // file descriptors of socket_receiver pipe
 // used for internal communication
 static int lpfd_[2];
-// array of active peers
-static OcatPeer_t *peer_ = NULL;
-// mutex for locking array of peers
-pthread_mutex_t peer_mutex_ = PTHREAD_MUTEX_INITIALIZER;
 // packet queue pointer
 static PacketQueue_t *queue_ = NULL;
 // mutex and condition variable for packet queue
@@ -68,71 +64,15 @@ int snd_buf_size_ = 0;
 #endif
 
 
-OcatPeer_t *search_peer(const struct in6_addr *addr)
-{
-   OcatPeer_t *peer;
-
-   for (peer = peer_; peer; peer = peer->next)
-      if (!memcmp(addr, &peer->addr, sizeof(struct in6_addr)))
-         return peer;
-   return NULL;
-}
-
-
-OcatPeer_t *get_empty_peer(void)
-{
-   int rc;
-   OcatPeer_t *peer;
-
-   if (!(peer = calloc(1, sizeof(OcatPeer_t))))
-   {
-      log_msg(L_ERROR, "cannot get memory for new peer: \"%s\"", strerror(errno));
-      return NULL;
-   }
-
-   peer->fraghdr = fhd_key_;
-   if ((rc = pthread_mutex_init(&peer->mutex, NULL)))
-   {
-      log_msg(L_FATAL, "cannot init new peer mutex: \"%s\"", strerror(rc));
-      free(peer);
-      return NULL;
-   }
-
-   peer->next = peer_;
-   peer_ = peer;
-
-   return peer;  
-}
-
-
-void delete_peer(OcatPeer_t *peer)
-{
-   int rc;
-   OcatPeer_t **p;
-
-   for (p = &peer_; *p; p = &(*p)->next)
-      if (*p == peer)
-      {
-         pthread_mutex_lock(&peer->mutex);
-         *p = peer->next;
-         pthread_mutex_unlock(&peer->mutex);
-         if ((rc = pthread_mutex_destroy(&peer->mutex)))
-            log_msg(L_FATAL, "cannot destroy mutex: \"%s\"", strerror(rc));
-         free(peer);
-         return;
-      }
-}
-
-
 int forward_packet(const struct in6_addr *addr, const char *buf, int buflen)
 {
    OcatPeer_t *peer;
    int len;
 
-   pthread_mutex_lock(&peer_mutex_);
+   lock_peers();
    if ((peer = search_peer(addr)))
-      pthread_mutex_lock(&peer->mutex);
-   pthread_mutex_unlock(&peer_mutex_);
+      lock_peer(peer);
+   unlock_peers();
 
    if (!peer)
    {
@@ -148,7 +88,7 @@ int forward_packet(const struct in6_addr *addr, const char *buf, int buflen)
       if (snd_buf_size_ - len < buflen)
       {
          log_msg(L_ERROR, "OUTQ too less space, dropping packet");
-         pthread_mutex_unlock(&peer->mutex);
+         unlock_peer(peer);
          return E_FWD_NOBUF;
       }
    }
@@ -172,7 +112,7 @@ int forward_packet(const struct in6_addr *addr, const char *buf, int buflen)
       peer->time = time(NULL);
       peer->out += len;
    }
-   pthread_mutex_unlock(&peer->mutex);
+   unlock_peer(peer);
 
    return 0;
 }
@@ -323,9 +263,9 @@ void cleanup_socket(int fd, OcatPeer_t *peer)
 {
    log_msg(L_NOTICE, "fd %d reached EOF, closing.", fd);
    close(fd);
-   pthread_mutex_lock(&peer_mutex_);
+   lock_peers();
    delete_peer(peer);
-   pthread_mutex_unlock(&peer_mutex_);
+   unlock_peers();
 }
 
 
@@ -347,14 +287,14 @@ void *socket_receiver(void *p)
       maxfd = lpfd_[0];
 
       // create set for all available peers to read
-      pthread_mutex_lock(&peer_mutex_);
-      for (peer = peer_; peer; peer = peer->next)
+      lock_peers();
+      for (peer = get_first_peer(); peer; peer = peer->next)
       {
-         pthread_mutex_lock(&peer->mutex);
+         lock_peer(peer);
          // only select active peers
          if (peer->state != PEER_ACTIVE)
          {
-            pthread_mutex_unlock(&peer->mutex);
+            unlock_peer(peer);
             continue;
          }
 
@@ -364,9 +304,9 @@ void *socket_receiver(void *p)
          FD_SET(peer->tcpfd, &rset);
          if (peer->tcpfd > maxfd)
             maxfd = peer->tcpfd;
-         pthread_mutex_unlock(&peer->mutex);
+         unlock_peer(peer);
       }
-      pthread_mutex_unlock(&peer_mutex_);
+      unlock_peers();
 
       log_msg(L_DEBUG, "selecting...");
       if ((maxfd = select(maxfd + 1, &rset, NULL, NULL, NULL)) == -1)
@@ -385,29 +325,29 @@ void *socket_receiver(void *p)
       peer = NULL;
       while (maxfd)
       {
-         // the following 10 loc look somehow strange and someone may tend
+         // the following 10 locs look somehow strange and someone may tend
          // to write this as a for loop but it's necessary for thread locking!
-         pthread_mutex_lock(&peer_mutex_);
+         lock_peers();
          if (!peer)
-            peer = peer_;
+            peer = get_first_peer();
          else if (!(peer = peer->next))
          {
             log_msg(L_FATAL, "fd %d ready but no peer found");
-            pthread_mutex_unlock(&peer_mutex_);
+            unlock_peers();
             break;
          }
-         pthread_mutex_lock(&peer->mutex);
-         pthread_mutex_unlock(&peer_mutex_);
+         lock_peer(peer);
+         unlock_peers();
 
          if (peer->state != PEER_ACTIVE)
          {
-            pthread_mutex_unlock(&peer->mutex);
+            unlock_peer(peer);
             continue;
          }
 
          if (!FD_ISSET(peer->tcpfd, &rset))
          {
-            pthread_mutex_unlock(&peer->mutex);
+            unlock_peer(peer);
             continue;
          }
 
@@ -419,7 +359,7 @@ void *socket_receiver(void *p)
          {
             // this might happen on linux, see SELECT(2)
             log_msg(L_DEBUG, "spurious wakup of %d: \"%s\"", peer->tcpfd, strerror(errno));
-            pthread_mutex_unlock(&peer->mutex);
+            unlock_peer(peer);
             continue;
          }
          log_msg(L_DEBUG, "received %d bytes on %d", len, peer->tcpfd);
@@ -428,10 +368,10 @@ void *socket_receiver(void *p)
          {
             log_msg(L_NOTICE, "fd %d reached EOF, closing.", peer->tcpfd);
             close(peer->tcpfd);
-            pthread_mutex_unlock(&peer->mutex);
-            pthread_mutex_lock(&peer_mutex_);
+            unlock_peer(peer);
+            lock_peers();
             delete_peer(peer);
-            pthread_mutex_unlock(&peer_mutex_);
+            unlock_peers();
             continue;
          }
 
@@ -487,7 +427,7 @@ void *socket_receiver(void *p)
             else
                log_msg(L_DEBUG, "fragbuf empty");
          } // while (peer->fraglen >= IP6HLEN)
-         pthread_mutex_unlock(&peer->mutex);
+         unlock_peer(peer);
       } // while (maxfd)
    } // for (;;)
 }
@@ -528,15 +468,15 @@ int insert_peer(int fd, const struct in6_addr *addr, time_t dly)
 
    set_nonblock(fd);
 
-   pthread_mutex_lock(&peer_mutex_);
+   lock_peers();
    if (!(peer = get_empty_peer()))
    {
-      pthread_mutex_unlock(&peer_mutex_);
+      unlock_peers();
       log_msg(L_ERROR, "could not get new empty peer");
       return 0;
    } 
-   pthread_mutex_lock(&peer->mutex);
-   pthread_mutex_unlock(&peer_mutex_);
+   lock_peer(peer);
+   unlock_peers();
 
    peer->tcpfd = fd;
    peer->state = PEER_ACTIVE;
@@ -549,7 +489,7 @@ int insert_peer(int fd, const struct in6_addr *addr, time_t dly)
    }
    else
       peer->dir = PEER_INCOMING;
-   pthread_mutex_unlock(&peer->mutex);
+   unlock_peer(peer);
 
    // wake up socket_receiver
    log_msg(L_DEBUG, "waking up socket_receiver");
@@ -818,9 +758,9 @@ void *socks_connector(void *p)
       pthread_mutex_unlock(&socks_queue_mutex_);
 
       // search for existing peer
-      pthread_mutex_lock(&peer_mutex_);
+      lock_peers();
       peer = search_peer(&(*squeue)->addr);
-      pthread_mutex_unlock(&peer_mutex_);
+      unlock_peers();
 
       // connect via SOCKS if no peer exists
       if (!peer)
@@ -901,17 +841,17 @@ void *socket_cleaner(void *ptr)
    {
       sleep(CLEANER_WAKEUP);
       log_msg(L_DEBUG, "wakeup");
-      pthread_mutex_lock(&peer_mutex_);
-      for (p = &peer_; *p; p = &(*p)->next)
+      lock_peers();
+      for (p = get_first_peer_ptr(); *p; p = &(*p)->next)
       {
-         pthread_mutex_lock(&(*p)->mutex);
+         lock_peer(*p);
          if ((*p)->state && (*p)->time + MAX_IDLE_TIME < time(NULL))
          {
             peer = *p;
             *p = peer->next;
             log_msg(L_NOTICE, "peer %d timed out, closing.", peer->tcpfd);
             close(peer->tcpfd);
-            pthread_mutex_unlock(&peer->mutex);
+            unlock_peer(peer);
             delete_peer(peer);
             if (!(*p))
             {
@@ -920,9 +860,9 @@ void *socket_cleaner(void *ptr)
             }
          }
          else
-            pthread_mutex_unlock(&(*p)->mutex);
+            unlock_peer(*p);
       }
-      pthread_mutex_unlock(&peer_mutex_);
+      unlock_peers();
    }
 }
 
@@ -987,8 +927,9 @@ void *ctrl_handler(void *p)
       // "status"
       else if (!strncmp(buf, "status", 6))
       {
-         pthread_mutex_lock(&peer_mutex_);
-         for (peer = peer_; peer; peer = peer->next)
+         lock_peers();
+         for (peer = get_first_peer(); peer; peer = peer->next)
+            // FIXME: should peer be locked?
             if (peer->state == PEER_ACTIVE)
             {
                tm = localtime(&peer->otime);
@@ -999,13 +940,13 @@ void *ctrl_handler(void *p)
                      peer->dir == PEER_INCOMING ? "in" : "out",
                      time(NULL) - peer->time, peer->in, peer->out, peer->sdelay, timestr);
             }
-         pthread_mutex_unlock(&peer_mutex_);
+         unlock_peers();
       }
       else if (!strncmp(buf, "close ", 6))
       {
          cfd = atoi(&buf[6]);
-         pthread_mutex_lock(&peer_mutex_);
-         for (peer = peer_; peer; peer = peer->next)
+         lock_peers();
+         for (peer = get_first_peer(); peer; peer = peer->next)
             if (peer->tcpfd == cfd)
             {
                log_msg(L_NOTICE, "close request for %d", cfd);
@@ -1018,7 +959,7 @@ void *ctrl_handler(void *p)
             log_msg(L_NOTICE, "no peer with fd %d exists\n", cfd);
             fprintf(ff, "no peer with fd %d exists\n", cfd);
          }
-         pthread_mutex_unlock(&peer_mutex_);
+         unlock_peers();
       }
       else if (!strncmp(buf, "threads", 7))
       {

-- 
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