[Pkg-privacy-commits] [onioncat] 106/340: transition to configurable receipient port (not finished yet)

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:04:29 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 e69b6d38b0b81ae49107b16ac4ed820e25e8ed32
Author: eagle <eagle at 58e1ccc2-750e-0410-8d0d-f93ca75ab447>
Date:   Sat Jan 3 15:19:32 2009 +0000

    transition to configurable receipient port (not finished yet)
    
    git-svn-id: http://www.cypherpunk.at/svn/onioncat/trunk@413 58e1ccc2-750e-0410-8d0d-f93ca75ab447
---
 src/ocat.c      |  24 ++++-----
 src/ocat.h      |  12 ++++-
 src/ocatlibe.c  |  73 +++++++++++++++++++++++++++
 src/ocatroute.c |  77 +++++++++++++++++++++++++++++
 src/ocatsetup.c | 151 ++++++++++++++++++++++++++++++++++++++------------------
 src/ocatsocks.c |   8 +--
 6 files changed, 278 insertions(+), 67 deletions(-)

diff --git a/src/ocat.c b/src/ocat.c
index 849567d..8ceab63 100644
--- a/src/ocat.c
+++ b/src/ocat.c
@@ -31,14 +31,14 @@ void usage(const char *s)
          "   -d <n>                set debug level to n, default = %d\n"
          "   -f <config_file>      read config from config_file\n"
          "   -i                    convert onion hostname to IPv6 and exit\n"
-         "   -l <port>             set ocat listen port, default = %d\n"
+         "   -l [<ip>:]<port>      set ocat listen address and port, default = 127.0.0.1:%d\n"
          "   -L <log_file>         log output to <log_file> (default = stderr)\n"
          "   -o <ipv6_addr>        convert IPv6 address to onion url and exit\n"
          "   -p                    use TAP device instead of TUN\n"
          "   -P <pid_file>         create pid file at location of <pid_file> (default = %s)\n"
          "   -r                    run as root, i.e. do not change uid/gid\n"
          "   -s <port>             set hidden service virtual port, default = %d\n"
-         "   -t [<ip>:]<port>      set Tor SOCKS ip and port, default = %d\n"
+         "   -t [<ip>:]<port>      set Tor SOCKS address and port, default = 127.0.0.1:%d\n"
 #ifndef WITHOUT_TUN
          "   -T <tun_device>       path to tun character device, default = \"%s\"\n"
 #endif
@@ -46,9 +46,9 @@ void usage(const char *s)
          "   -4                    enable IPv4 support (default = %d)\n"
          , PACKAGE_STRING, __DATE__, __TIME__, s,
          // option defaults start here
-         OCAT_DIR, OCAT_CONNECT_LOG, CNF(create_clog), CNF(debug_level), CNF(ocat_listen_port),
+         OCAT_DIR, OCAT_CONNECT_LOG, CNF(create_clog), CNF(debug_level), OCAT_LISTEN_PORT,
          CNF(pid_file),
-         CNF(ocat_dest_port), ((struct sockaddr_in*) CNF(socks_dst))->sin_port, 
+         CNF(ocat_dest_port), CNF(socks_dst)->sin_port, 
 #ifndef WITHOUT_TUN
          TUN_DEV,
 #endif
@@ -159,7 +159,9 @@ int main(int argc, char *argv[])
             break;
 
          case 'l':
-            CNF(ocat_listen_port) = atoi(optarg);
+            //CNF(ocat_listen_port) = atoi(optarg);
+            if (strsockaddr(optarg, (struct sockaddr*) CNF(oc_listen)) == -1)
+               exit(1);
             break;
 
          case 'L':
@@ -188,16 +190,8 @@ int main(int argc, char *argv[])
             break;
 
          case 't':
-            s = optarg;
-            if (strchr(optarg, ':'))
-            {
-               s = strtok(optarg, ":");
-               if (!inet_pton(AF_INET, optarg, &((struct sockaddr_in*) CNF(socks_dst))->sin_addr))
-                  log_msg(LOG_ALERT, "\"%s\" is not a valid IPv4 address", optarg), exit(1);
-               s = strtok(NULL, ":");
-            }
-
-            ((struct sockaddr_in*) CNF(socks_dst))->sin_port = htons(atoi(s));
+            if (strsockaddr(optarg, (struct sockaddr*) CNF(socks_dst)) == -1)
+               exit(1);
             break;
 
 #ifndef WITHOUT_TUN
diff --git a/src/ocat.h b/src/ocat.h
index b2cd48e..6c04d50 100644
--- a/src/ocat.h
+++ b/src/ocat.h
@@ -203,6 +203,8 @@
 #define IPV4_KEY 0
 #define IPV6_KEY 1
 
+#define SOCKADDR_SIZE(x) (x->sa_family == AF_INET ? sizeof(struct sockaddr_in) : x->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0)
+
 
 struct OcatSetup
 {
@@ -249,7 +251,14 @@ struct OcatSetup
    struct in6_addr root_peer[ROOT_PEERS];
    time_t uptime;
    char *frandn;
-   struct sockaddr *socks_dst;
+   //! destination socket address of Tor's SOCKS port
+   union
+   {
+      struct sockaddr_in *socks_dst;
+      struct sockaddr_in6 *socks_dst6;
+   };
+   //! local listening socket address for incoming connections
+   struct sockaddr **oc_listen;
 };
 
 #ifdef PACKET_QUEUE
@@ -526,6 +535,7 @@ void print_socks_queue(FILE *);
 /* ocatlibe.c */
 void oe_close(int);
 int oe_remtr(char *);
+int strsockaddr(const char *, struct sockaddr *);
 
 /* ocatipv6route.c */
 struct in6_addr *ipv6_lookup_route(const struct in6_addr *);
diff --git a/src/ocatlibe.c b/src/ocatlibe.c
index 03cad1e..2751983 100644
--- a/src/ocatlibe.c
+++ b/src/ocatlibe.c
@@ -63,3 +63,76 @@ int oe_remtr(char *s)
    return strlen(s);
 }
 
+
+/*! Convert character string into struct sockaddr of appropriate address family.
+ *  AF_INET and AF_INET6 are supported yet.
+ *  @param src Pointer to character string.
+ *  @param addr Pointer to struct sockaddr of appropriate type (and size).
+ *         It should be pre-initialized. strsockaddr() will not init all fields.
+ *  @return address family on success or -1 on error.
+ */
+int strsockaddr(const char *src, struct sockaddr *addr)
+{
+   char *s, buf[100];
+   int p;
+
+   strlcpy(buf, src, 100);
+   if ((s = strchr(buf, '[')))
+   {
+      s++;
+      ((struct sockaddr_in6*) addr)->sin6_family = AF_INET6;
+      s = strtok(s, "]");
+      if (!inet_pton(AF_INET6, s, &((struct sockaddr_in6*) addr)->sin6_addr))
+      {
+         log_msg(LOG_ALERT, "\"%s\" contains no valid IPv6 address", s);
+         return -1;
+      }
+      if ((s = strtok(NULL, " ")))
+      {
+         if (*s == ':')
+         {
+            s++;
+            if ((p = atoi(s)) > 0)
+               ((struct sockaddr_in6*) addr)->sin6_port = htons(p);
+         }
+      }
+      return AF_INET6;
+   }
+
+   if (strchr(buf, '.'))
+   {
+      ((struct sockaddr_in*) addr)->sin_family = AF_INET;
+      s = strtok(buf, ":");
+      if (!inet_pton(AF_INET, s, &((struct sockaddr_in*) addr)->sin_addr))
+      {
+         log_msg(LOG_ALERT, "\"%s\" is not a valid IPv4 address", s);
+         return -1;
+      }
+      s = strtok(NULL, ":");
+      if (s)
+         if ((p = atoi(s)) > 0)
+            ((struct sockaddr_in*) addr)->sin_port = htons(p);
+      return AF_INET;
+   }
+
+   if ((p = atoi(buf)) > 0)
+   {
+      switch (((struct sockaddr_in*) addr)->sin_family)
+      {
+         case AF_INET:
+            ((struct sockaddr_in*) addr)->sin_port = htons(p);
+            return AF_INET;
+
+         case AF_INET6:
+            ((struct sockaddr_in6*) addr)->sin6_port = htons(p);
+            return AF_INET;
+
+         default:
+            log_debug("adress family %04x not supported", ((struct sockaddr_in*) addr)->sin_family);
+            return -1;
+      }
+   }
+
+   return -1;
+}
+
diff --git a/src/ocatroute.c b/src/ocatroute.c
index 6a89f21..b6c405d 100644
--- a/src/ocatroute.c
+++ b/src/ocatroute.c
@@ -641,6 +641,83 @@ int create_listener(struct sockaddr *addr, int sock_len)
 }
 
 
+/** run_listeners(...) is a generic socket acceptor for
+ *  local TCP ports (IPv4+IPv6).
+ *  It listens on a given list of sockets.
+ *  Every time a connection comes in the function action_accept is
+ *  called with the incoming file descriptor as parameter.
+ *  @param addr Double pointer to sockaddr structs. It MUST be terminated by a null pointer.
+ *  @param sockfd Points to an int array. The array must contain at least 
+ *         as much entries as the sockaddr pointer has entries.
+ *  @param action_accept Function pointer to function that should be called if a
+ *         connection arrives.
+ *  @return File descriptor or -1 on error.
+ */
+int run_listeners(struct sockaddr **addr, int *sockfd, int (action_accept)(int))
+{
+   int fd;
+   struct sockaddr *saddr;
+   struct sockaddr_in6 in6;
+   fd_set rset;
+   int maxfd, i, cnt;
+   socklen_t alen;
+   char iabuf[INET6_ADDRSTRLEN];
+
+   for (i = 0, saddr = *addr; saddr; saddr = addr[i], i++)
+   {
+      log_debug("create listener");
+      if ((sockfd[i] = create_listener(saddr, SOCKADDR_SIZE(saddr))) == -1)
+         log_msg(LOG_EMERG, "exiting"), exit(1);
+   }
+   cnt = i;
+
+   for (;;)
+   {
+      log_debug("setting up fd_set");
+      FD_ZERO(&rset);
+      maxfd = 0;
+      for (i = 0; i < cnt; i++)
+      {
+         FD_SET(sockfd[i], &rset);
+         if (sockfd[i] > maxfd)
+            maxfd = sockfd[i];
+      }
+
+      log_debug("selecting locally (maxfd = %d)", maxfd);
+      if ((maxfd = select(maxfd + 1, &rset, NULL, NULL, NULL)) == -1)
+      {
+         log_debug("select returned: \"%s\"", strerror(errno));
+         continue;
+      }
+      log_debug("select returned %d fds ready", maxfd);
+
+      for (i = 0; maxfd && (i < cnt); i++)
+      {
+         log_debug("checking fd %d (maxfd = %d, i = %d)", sockfd[i], maxfd, i);
+         if (!FD_ISSET(sockfd[i], &rset))
+            continue;
+         maxfd--;
+         alen = sizeof(in6);
+         log_debug("accepting connection on %d", sockfd[i]);
+         if ((fd = accept(sockfd[i], (struct sockaddr*) &in6, &alen)) < 0)
+         {
+            log_msg(LOG_ERR, "error accepting connection on %d: \"%s\"", sockfd[i], strerror(errno));
+            // FIXME: there should be additional error handling!
+            continue;
+         }
+
+         inet_ntop(in6.sin6_family,
+               in6.sin6_family == AF_INET6 ? &in6.sin6_addr :
+               (void*) &((struct sockaddr_in*) &in6)->sin_addr,
+               iabuf, INET6_ADDRSTRLEN);
+         log_msg(LOG_INFO | LOG_FCONN, "connection %d [%d] accepted on listener %d from %s port %d", fd, i, sockfd[i], iabuf, ntohs(in6.sin6_port));
+         (void) action_accept(fd);
+      }
+   }
+   return 0;
+}
+
+
 /** run_local_listeners(...) is a generic socket acceptor for
  *  local TCP ports (IPv4+IPv6).
  *  Every time a connection comes in the function action_accept is
diff --git a/src/ocatsetup.c b/src/ocatsetup.c
index 46b8660..fd90ee6 100644
--- a/src/ocatsetup.c
+++ b/src/ocatsetup.c
@@ -25,6 +25,11 @@
 
 #include "ocat.h"
 
+
+static struct sockaddr_in6 socks_dst6_;
+static struct sockaddr_in6 oc_listen6_;
+static struct sockaddr* oc_listen_a_[] = {(struct sockaddr*) &oc_listen6_, NULL};
+
 struct OcatSetup setup_ =
 {
    // fhd_keys
@@ -32,7 +37,8 @@ struct OcatSetup setup_ =
    // fhd_key_len
    sizeof(uint32_t),
    //TOR_SOCKS_PORT, 
-   OCAT_LISTEN_PORT, OCAT_DEST_PORT, OCAT_CTRL_PORT, 
+   OCAT_LISTEN_PORT, 
+   OCAT_DEST_PORT, OCAT_CTRL_PORT, 
    //! default tunfd is stdin/stdout
    {0, 1},
    //! default debug level
@@ -53,24 +59,31 @@ struct OcatSetup setup_ =
    },
    0,
    "/dev/urandom",
-   NULL
+   {(struct sockaddr_in*) &socks_dst6_},
+   oc_listen_a_
 };
 
-static struct sockaddr_in socks_dst_;
-
 
 void init_setup(void)
 {
    setup_.logf = stderr;
    setup_.uptime = time(NULL);
 
-   socks_dst_.sin_family = AF_INET;
-   socks_dst_.sin_port = htons(TOR_SOCKS_PORT);
-   socks_dst_.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+   setup_.socks_dst->sin_family = AF_INET;
+   setup_.socks_dst->sin_port = htons(TOR_SOCKS_PORT);
+   setup_.socks_dst->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+#ifdef HAVE_SIN_LEN
+   setup_.socks_dst->sin_len = sizeof(socks_dst6_);
+#endif
+
+   /*
+   ((struct sockaddr_in*) *setup_.oc_listen)->sin_family = AF_INET;
+   setup_.oc_listen->sin_port = htons(OCAT_LISTEN_PORT);
+   setup_.oc_listen->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 #ifdef HAVE_SIN_LEN
-   socks_dst_.sin_len = sizeof(socks_dst_);
+   setup_.oc_listen->sin_len = sizeof(oc_listen6_);
 #endif
-   setup_.socks_dst = (struct sockaddr*) &socks_dst_;
+*/
 }
 
 
@@ -93,43 +106,38 @@ void print_setup_struct(FILE *f)
 
    t = time(NULL) - setup_.uptime;
 
-   inet_ntop(socks_dst_.sin_family, &socks_dst_.sin_addr, sk, SBUF);
 
    fprintf(f,
-         "fhd_key[IPV4(%d)]  = 0x%04x\n"
-         "fhd_key[IPV6(%d)]  = 0x%04x\n"
-         "fhd_key_len       = %d\n"
+         "fhd_key[IPV4(%d)]       = 0x%04x\n"
+         "fhd_key[IPV6(%d)]       = 0x%04x\n"
+         "fhd_key_len            = %d\n"
          //"tor_socks_port    = %d\n"
-         "ocat_listen_port  = %d\n"
-         "ocat_dest_port    = %d\n"
-         "ocat_ctrl_port    = %d\n"
-         "tunfd[0]          = %d\n"
-         "tunfd[1]          = %d\n"
-         "debug_level       = %d\n"
-         "usrname           = \"%s\"\n"
-         "onion_url         = \"%s\"\n"
-         "ocat_addr         = %s\n"
-         "create_clog       = %d\n"
-         "runasroot         = %d\n"
-         "controller        = %d\n"
-         "ocat_dir          = \"%s\"\n"
-         "tun_dev           = \"%s\"\n"
-         "ipv4_enable       = %d\n"
-         "ocat_addr4        = %s\n"
-         "ocat_addr4_mask   = %s\n"
-         "config_file       = \"%s\"\n"
-         "config_read       = %d\n"
-         "use_tap           = %d\n"
-         "ocat_hwaddr       = %s\n"
-         "pid_file          = \"%s\"\n"
-         "logfn             = \"%s\"\n"
-         "logf              = %s\n"
-         "daemon            = %d\n"
-         "uptime            = %d days, %d:%02d\n"
-         "socks_dst.sin_family = %d\n"
-         "socks_dst.sin_port = %d\n"
-         "socks_dst.sin_addr = %s\n",
-
+         "ocat_listen_port       = %d\n"
+         "ocat_dest_port         = %d\n"
+         "ocat_ctrl_port         = %d\n"
+         "tunfd[0]               = %d\n"
+         "tunfd[1]               = %d\n"
+         "debug_level            = %d\n"
+         "usrname                = \"%s\"\n"
+         "onion_url              = \"%s\"\n"
+         "ocat_addr              = %s\n"
+         "create_clog            = %d\n"
+         "runasroot              = %d\n"
+         "controller             = %d\n"
+         "ocat_dir               = \"%s\"\n"
+         "tun_dev                = \"%s\"\n"
+         "ipv4_enable            = %d\n"
+         "ocat_addr4             = %s\n"
+         "ocat_addr4_mask        = %s\n"
+         "config_file            = \"%s\"\n"
+         "config_read            = %d\n"
+         "use_tap                = %d\n"
+         "ocat_hwaddr            = %s\n"
+         "pid_file               = \"%s\"\n"
+         "logfn                  = \"%s\"\n"
+         "logf                   = %s\n"
+         "daemon                 = %d\n"
+         "uptime                 = %d days, %d:%02d\n",
  
          IPV4_KEY, ntohl(setup_.fhd_key[IPV4_KEY]), IPV6_KEY, ntohl(setup_.fhd_key[IPV6_KEY]),
          setup_.fhd_key_len,
@@ -158,14 +166,63 @@ void print_setup_struct(FILE *f)
          setup_.logfn,
          logf,
          setup_.daemon,
-         t / (3600 * 24), t / 3600 % 24, t / 60 % 60,
-         ((struct sockaddr_in*) setup_.socks_dst)->sin_family,
-         ntohs(((struct sockaddr_in*) setup_.socks_dst)->sin_port),
-         sk
+         t / (3600 * 24), t / 3600 % 24, t / 60 % 60
          );
 
    for (i = 0; i < ROOT_PEERS; i++)
       if (inet_ntop(AF_INET6, &setup_.root_peer[i], rp, SBUF))
          fprintf(f, "root_peer[%d]      = %s\n", i, rp);
+
+   if (setup_.socks_dst->sin_family == AF_INET)
+   {
+      inet_ntop(setup_.socks_dst->sin_family, &setup_.socks_dst->sin_addr, sk, SBUF);
+      fprintf(f,
+         "socks_dst.sin_family   = %04x\n"
+         "socks_dst.sin_port     = %d\n"
+         "socks_dst.sin_addr     = %s\n",
+         setup_.socks_dst->sin_family,
+         ntohs(setup_.socks_dst->sin_port),
+         sk
+         );
+   }
+   else
+   {
+      inet_ntop(setup_.socks_dst6->sin6_family, &setup_.socks_dst6->sin6_addr, sk, SBUF);
+      fprintf(f,
+         "socks_dst6.sin6_family = %04x\n"
+         "socks_dst6.sin6_port   = %d\n"
+         "socks_dst6.sin6_addr   = %s\n",
+         setup_.socks_dst6->sin6_family,
+         ntohs(setup_.socks_dst6->sin6_port),
+         sk
+         );
+   }
+
+   /*
+   if (setup_.oc_listen->sin_family == AF_INET)
+   {
+      inet_ntop(setup_.oc_listen->sin_family, &setup_.oc_listen->sin_addr, sk, SBUF);
+      fprintf(f,
+         "socks_dst.sin_family   = %04x\n"
+         "socks_dst.sin_port     = %d\n"
+         "socks_dst.sin_addr     = %s\n",
+         setup_.oc_listen->sin_family,
+         ntohs(setup_.oc_listen->sin_port),
+         sk
+         );
+   }
+   else
+   {
+      inet_ntop(setup_.oc_listen6->sin6_family, &setup_.oc_listen6->sin6_addr, sk, SBUF);
+      fprintf(f,
+         "oc_listen6.sin6_family = %04x\n"
+         "oc_listen6.sin6_port   = %d\n"
+         "oc_listen6.sin6_addr   = %s\n",
+         setup_.oc_listen6->sin6_family,
+         ntohs(setup_.oc_listen6->sin6_port),
+         sk
+         );
+   }
+   */
 }
 
diff --git a/src/ocatsocks.c b/src/ocatsocks.c
index 3959370..b515d46 100644
--- a/src/ocatsocks.c
+++ b/src/ocatsocks.c
@@ -38,7 +38,7 @@ int socks_connect(const SocksQueue_t *sq)
 {
 //   struct sockaddr_in in;
    int fd, t, len;
-   char buf[FRAME_SIZE], onion[ONION_NAME_SIZE];
+   char buf[FRAME_SIZE], onion[ONION_NAME_SIZE], addr[INET6_ADDRSTRLEN];
    SocksHdr_t *shdr = (SocksHdr_t*) buf;
    OcatPeer_t *peer;
 
@@ -57,13 +57,13 @@ int socks_connect(const SocksQueue_t *sq)
 
    log_msg(LOG_INFO, "trying to connect to \"%s\" [%s]", onion, inet_ntop(AF_INET6, &sq->addr, buf, FRAME_SIZE));
 
-   if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
+   if ((fd = socket(CNF(socks_dst)->sin_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0)) < 0)
       return E_SOCKS_SOCK;
 
    t = time(NULL);
-   if (connect(fd, CNF(socks_dst), sizeof(*CNF(socks_dst))) == -1)
+   if (connect(fd, (struct sockaddr*) CNF(socks_dst), sizeof(struct sockaddr_in6)) == -1)
    {
-      log_msg(LOG_ERR, "connect() to TOR's SOCKS port %d failed: \"%s\". Sleeping for %d seconds.", ntohs(((struct sockaddr_in*) CNF(socks_dst))->sin_port), strerror(errno), TOR_SOCKS_CONN_TIMEOUT);
+      log_msg(LOG_ERR, "connect() to TOR's SOCKS port %s:%d failed: \"%s\". Sleeping for %d seconds.", inet_ntop(CNF(socks_dst)->sin_family, CNF(socks_dst)->sin_family == AF_INET ? (char*) &CNF(socks_dst)->sin_addr : (char*) &CNF(socks_dst6)->sin6_addr, addr, sizeof(addr)), ntohs(CNF(socks_dst)->sin_port), strerror(errno), TOR_SOCKS_CONN_TIMEOUT);
       oe_close(fd);
       sleep(TOR_SOCKS_CONN_TIMEOUT);
       return E_SOCKS_CONN;

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