Sfoglia il codice sorgente

IPv6 fixes

svn: 127
Bryan Drewery 23 anni fa
parent
commit
d5b39b9582
8 ha cambiato i file con 148 aggiunte e 69 eliminazioni
  1. 17 7
      src/dcc.c
  2. 63 24
      src/dccutil.c
  3. 2 2
      src/mod/ctcp.mod/ctcp.c
  4. 4 2
      src/mod/module.h
  5. 7 3
      src/mod/transfer.mod/transfer.c
  6. 4 3
      src/modules.c
  7. 49 27
      src/net.c
  8. 2 1
      src/proto.h

+ 17 - 7
src/dcc.c

@@ -432,6 +432,12 @@ static void dcc_bot_new(int idx, char *buf, int x)
     putlog(LOG_MISC, "*", "ERROR linking %s: %s", dcc[idx].nick, buf);
     killsock(dcc[idx].sock);
     lostdcc(idx);
+  } else if (strcmp(code, "")) {
+      /* Invalid password/digest */
+      putlog(LOG_WARN, "*", "%s failed encrypted link handshake", dcc[idx].nick);
+//      putlog(LOG_ERRORS, "*", "Expected elink, got %s %s", code, buf);
+      killsock(dcc[idx].sock);
+      lostdcc(idx);
   }
   /* Ignore otherwise */
 }
@@ -676,8 +682,8 @@ static void dcc_chat_pass(int idx, char *buf, int atr)
       send_timesync(idx);
     } else {
       /* Invalid password/digest */
-      putlog(LOG_MISC, "*", "%s failed encrypted link handshake", dcc[idx].nick);
-      putlog(LOG_ERRORS, "*", "Expected elinkdone, got %s", buf);
+      putlog(LOG_WARN, "*", "%s failed encrypted link handshake", dcc[idx].nick);
+//      putlog(LOG_ERRORS, "*", "Expected elinkdone, got %s", buf);
       killsock(dcc[idx].sock);
       lostdcc(idx);
     }
@@ -1264,7 +1270,9 @@ static void dcc_telnet(int idx, char *buf, int i)
   dcc[i].sock = sock;
   dcc[i].addr = ip;
 #ifdef USE_IPV6
-  strcpy(dcc[i].addr6, s);
+Context;
+  if (sockprotocol(sock) == AF_INET6)
+    strcpy(dcc[i].addr6, s);
 #endif /* USE_IPV6 */
   dcc[i].port = port;
   dcc[i].timeval = now;
@@ -1275,11 +1283,13 @@ static void dcc_telnet(int idx, char *buf, int i)
   dcc[i].u.dns->dns_type = RES_HOSTBYIP;
   dcc[i].u.dns->ibuf = dcc[idx].sock;
   dcc[i].u.dns->type = &DCC_IDENTWAIT;
+Context;
 #ifdef USE_IPV6
-  dcc_telnet_hostresolved(i);
-#else
-  dcc_dnshostbyip(ip);
+  if (sockprotocol(sock) == AF_INET6)
+    dcc_telnet_hostresolved(i);
+  else
 #endif /* USE_IPV6 */
+    dcc_dnshostbyip(ip);
 }
 
 static void dcc_telnet_hostresolved(int i)
@@ -1293,7 +1303,7 @@ static void dcc_telnet_hostresolved(int i)
     strncpyz(dcc[i].host, dcc[i].addr6, UHOSTLEN);
   else
 #endif /* USE_IPV6 */
-  strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
+    strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
 
   for (idx = 0; idx < dcc_total; idx++)
     if ((dcc[idx].type == &DCC_TELNET) &&

+ 63 - 24
src/dccutil.c

@@ -17,6 +17,9 @@
 
 extern struct dcc_t	*dcc;
 extern int		 dcc_total, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS, tands;
+#ifdef USE_IPV6
+extern unsigned long     notalloc;
+#endif /* USE_IPV6 */
 extern char		 botnetnick[], version[];
 extern time_t		 now;
 extern sock_list	*socklist;
@@ -420,7 +423,7 @@ void tell_dcc(int zidx)
   dprintf(zidx, format, "----", "--------", "-----", "---------", 
                         "-------------------------", "----");
 
-  egg_snprintf(format, sizeof format, "%%-4d %%08X %%5d %%-%us %%-17s %%s\n", 
+  egg_snprintf(format, sizeof format, "%%-4d %%08X %%5d %%-%us %%-25s %%s\n", 
                           nicklen);
   /* Show server */
   for (i = 0; i < dcc_total; i++) {
@@ -664,6 +667,9 @@ int listen_all(int lport, int off)
   int i,
     idx = (-1),
     port,
+#ifdef USE_IPV6
+    i6 = 0,
+#endif /* USE_IPV6 */
     realport;
   struct portmap *pmap = NULL,
    *pold = NULL;
@@ -676,43 +682,64 @@ Context;
       break;
     }
 
-  for (i = 0; i < dcc_total; i++)
-    if ((dcc[i].type == &DCC_TELNET) && (dcc[i].port == port))
+  for (i = 0; i < dcc_total; i++) {
+    if ((dcc[i].type == &DCC_TELNET) && (dcc[i].port == port)) {
       idx = i;
 
-  if (off) {
-    if (pmap) {
-      if (pold)
-        pold->next = pmap->next;
-      else
-        root = pmap->next;
-      nfree(pmap);
-    }
-    if (idx < 0) {
+      if (off) {
+        if (pmap) {
+          if (pold)
+            pold->next = pmap->next;
+          else
+           root = pmap->next;
+           nfree(pmap);
+        }
+#ifdef USE_IPV6
+        if (sockprotocol(dcc[idx].sock) == AF_INET6)
+          putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
+        else
+#endif /* USE_IPV6 */   
+          putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
+        killsock(dcc[idx].sock);
+        lostdcc(idx);
+        return idx;
+      }
+    }  
+  }
+  if (idx < 0) {
+    if (off) {
       putlog(LOG_ERRORS, "*", STR("No such listening port open - %d"), lport);
       return idx;
     }
-    putlog(LOG_DEBUG, "*", "Closing listening port %d", dcc[idx].port);
-    killsock(dcc[idx].sock);
-    lostdcc(idx);
-    return idx;
-  }
-  if (idx < 0) {
     /* make new one */
     if (dcc_total >= max_dcc) {
       putlog(LOG_ERRORS, "*", STR("Can't open listening port - no more DCC Slots"));
     } else {
 #ifdef USE_IPV6
-      /* dum de dum, listen needs an af_def option, on linux this will listen on
-       * both ipv6 and ipv4
-       */
-      i = open_listen_by_af(&port, AF_INET6);
+      i6 = open_listen_by_af(&port, AF_INET6);
+      if (i6 < 0)
+        putlog(LOG_ERRORS, "*", STR("Can't open IPv6 listening port %d - %s"), port, 
+               i6 == -1 ? "it's taken." : "couldn't assign ip.");
+      else {
+        idx = new_dcc(&DCC_TELNET, 0);
+        dcc[idx].addr = notalloc;
+        dcc[idx].addr6 = getmyip6();
+        dcc[idx].port = port;
+        dcc[idx].sock = i6;
+        dcc[idx].timeval = now;
+        strcpy(dcc[idx].nick, STR("(telnet6)"));
+        strcpy(dcc[idx].host, "*");
+        putlog(LOG_DEBUG, "*", STR("Listening on IPv6 at telnet port %d"), port);
+      }
+      i = open_listen_by_af(&port, AF_INET);
 #else
       i = open_listen(&port);
 #endif /* USE_IPV6 */
       if (i < 0)
-        putlog(LOG_ERRORS, "*", STR("Can't open listening port - it's taken"));
+        putlog(LOG_ERRORS, "*", STR("Can't open IPv4 listening port %d - %s"), port,
+               i == -1 ? "it's taken." : "couldn't assign ip.");
       else {
+	idx = (-1); /* now setup ipv4 listening port */
         idx = new_dcc(&DCC_TELNET, 0);
         dcc[idx].addr = iptolong(getmyip());
         dcc[idx].port = port;
@@ -720,6 +747,13 @@ Context;
         dcc[idx].timeval = now;
         strcpy(dcc[idx].nick, STR("(telnet)"));
         strcpy(dcc[idx].host, "*");
+        putlog(LOG_DEBUG, "*", STR("Listening on IPv4 at telnet port %d"), port);
+      }
+#ifdef USE_IPV6
+      if (i > 0 || i6 > 0) {
+#else
+      if (i > 0) {
+#endif /* USE_IPV6 */
         if (!pmap) {
           pmap = nmalloc(sizeof(struct portmap));
           pmap->next = root;
@@ -727,10 +761,15 @@ Context;
         }
         pmap->realport = realport;
         pmap->mappedto = port;
-        putlog(LOG_DEBUG, "*", STR("Listening at telnet port %d"), port);
       }
     }
   }
+  /* if one of the protocols failed, the one which worked will be returned
+   * if both were successful, it wont matter which idx is returned, because the 
+   * code reading listen_all will only be reading dcc[idx].port, which would be
+   * open on both protocols.
+   * -bryan (10/29/03)
+   */
   return idx;
 }
 

+ 2 - 2
src/mod/ctcp.mod/ctcp.c

@@ -454,8 +454,8 @@ static void ctcp_minutely()
 
   if (listen_time <= 0) {
     for (i = 0; i < dcc_total; i++) {
-      if ((dcc[i].type->flags & DCT_LISTEN) && (!strcmp(dcc[i].nick, "(telnet)"))) {
-        putlog(LOG_DEBUG, "*", "Closing listening port %d", dcc[i].port);
+      if ((dcc[i].type->flags & DCT_LISTEN) && (!strcmp(dcc[i].nick, "(telnet)") || !strcmp(dcc[i].nick, "(telnet6)"))) {
+        putlog(LOG_DEBUG, "*", "Closing listening port %d %s", dcc[i].port, dcc[i].nick);
 
         killsock(dcc[i].sock);
         lostdcc(i);

+ 4 - 2
src/mod/module.h

@@ -49,6 +49,7 @@
 #undef feof
 #undef user_malloc
 #undef dprintf
+#undef sdprintf
 #undef get_data_ptr
 #undef wild_match
 #undef wild_match_per
@@ -206,7 +207,7 @@
 #endif /* USE_IPV6 */
 #define killsock(x) (((void *(*)())global[83])((x),MODULE_NAME,__FILE__,__LINE__))
 /* 84 - 87 */
-#define open_listen ((int (*) (int *))global[84])
+#define open_listen_by_af ((int (*) (int *, int))global[84])
 #define open_telnet_dcc ((int (*) (int,char *,char *))global[85])
 #define get_data_ptr(x) ((void *(*)(int,char*,int))global[86])(x,__FILE__,__LINE__)
 #define open_telnet ((int (*) (char *, int))global[87])
@@ -531,10 +532,11 @@
 #define cmdprefix ((char *)(global[325]))
 #define replace ((char*(*)(char *, char *, char *))global[326])
 #define degarble ((char *(*)(int, char *))global[327])
-#define open_listen_by_af ((int (*) (int *, int))global[328])
+#define open_listen ((int (*) (int *))global[328])
 /* 329 - 332 */
 #define egg_inet_ntop ((int (*)(int af, const void *src, char *dst, socklen_t size))global[329])
 #define hostprotocol ((int (*) (char *))global[330])
+#define sdprintf (global[331])
 
 
 

+ 7 - 3
src/mod/transfer.mod/transfer.c

@@ -1558,6 +1558,8 @@ static int raw_dcc_resend_send(char *filename, char *nick, char *from,
   char *nfn, *buf = NULL;
   long dccfilesize;
   FILE *f, *dccfile;
+ 
+  sdprintf("raw_dcc_resend_send()");
   zz = (-1);
   dccfile = fopen(filename,"r");
   fseek(dccfile, 0, SEEK_END);
@@ -1566,22 +1568,24 @@ static int raw_dcc_resend_send(char *filename, char *nick, char *from,
   /* File empty?! */
   if (dccfilesize == 0)
     return DCCSEND_FEMPTY;
+
   if (reserved_port_min > 0 && reserved_port_min < reserved_port_max) {
     for (port = reserved_port_min; port <= reserved_port_max; port++)
 #ifdef USE_IPV6
-      if ((zz = open_listen_by_af(&port, AF_INET6)) != -1) /* no idea how we want to handle this -poptix 02/03/03 */
+      if ((zz = open_listen_by_af(&port, AF_INET)) != -1) /* no idea how we want to handle this -poptix 02/03/03 */
 #else
       if ((zz = open_listen(&port)) != -1)
 #endif /* USE_IPV6 */
-       break;
+        break;
   } else {
     port = reserved_port_min;
 #ifdef USE_IPV6
-    zz = open_listen_by_af(&port, AF_INET6);
+    zz = open_listen_by_af(&port, AF_INET);
 #else
     zz = open_listen(&port);
 #endif /* USE_IPV6 */
   }
+
   if (zz == (-1))
     return DCCSEND_NOSOCK;
   nfn = strrchr(dir, '/');

+ 4 - 3
src/modules.c

@@ -256,7 +256,7 @@ Function global_table[] =
   (Function) getsock,
   (Function) mod_killsock,
   /* 84 - 87 */
-  (Function) open_listen,
+  (Function) open_listen_by_af,
   (Function) open_telnet_dcc,
   (Function) _get_data_ptr,
   (Function) open_telnet,
@@ -578,8 +578,9 @@ Function global_table[] =
   (Function) replace,
   (Function) degarble,
   (Function) egg_inet_ntop,
-  (Function) open_listen_by_af,
-  (Function) hostprotocol
+  (Function) open_listen,
+  (Function) hostprotocol,
+  (Function) sdprintf
 
 
 };

+ 49 - 27
src/net.c

@@ -193,6 +193,20 @@ int expmem_net()
   return tot;
 }
 
+/* Get my ipv6 ip
+ */
+char *getmyip6()
+{
+#ifdef USE_IPV6
+  static char s[UHOSTLEN + 1];
+  egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
+  s[120] = 0;
+  return s;
+#else
+  return "";
+#endif /* USE_IPV6 */
+}
+
 /* Get my ip number
  */
 IP getmyip() {
@@ -203,7 +217,10 @@ IP getmyip() {
 void cache_my_ip()
 {
   char s[121];
-  int error, any = 0;
+  int error;
+#ifdef USE_IPV6
+  int any = 0;
+#endif /* USE_IPV6 */
 
   debug0("cache_my_ip()");
   memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
@@ -396,6 +413,7 @@ void setsock(int sock, int options)
   }
   /* Yay async i/o ! */
   fcntl(sock, F_SETFL, O_NONBLOCK);
+
 }
 
 #ifdef USE_IPV6
@@ -634,9 +652,6 @@ int open_address_listen(IP addr, int *port)
  {
   int sock = 0;
   unsigned int addrlen;
-#ifdef USE_IPV6
-  struct sockaddr_in6 name6;
-#endif /* USE_IPV6 */
   struct sockaddr_in name;
 
   if (firewall[0]) {
@@ -647,35 +662,42 @@ int open_address_listen(IP addr, int *port)
   }
 #ifdef USE_IPV6
   if (af_def == AF_INET6) {
-debug0("Opening listen socket with AF_INET6");
-      sock = getsock(SOCK_LISTEN, af_def);
-      bzero((char *) &name6, sizeof(name6));
-      name6.sin6_family = af_def;
-      name6.sin6_port = htons(*port);
-      memcpy(&name6.sin6_addr, &in6addr_any, 16);
-      if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
-        killsock(sock);
-        return -1;
-      }
-      addrlen = sizeof(name6);
-      if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
-        killsock(sock);
-        return -1;
-      }
-      *port = ntohs(name6.sin6_port);
-      if (listen(sock, 1) < 0) {
-        killsock(sock);
-        return -1;
-      }
+    struct sockaddr_in6 name6;
+    sock = getsock(SOCK_LISTEN, af_def);
+
+    if (sock < 1)
+    return -1;
+
+    debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
+    bzero((char *) &name6, sizeof(name6));
+    name6.sin6_family = af_def;
+    name6.sin6_port = htons(*port); /* 0 = just assign us a port */
+    /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
+    memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
+    if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
+      killsock(sock);
+      return -1;
+    }
+    addrlen = sizeof(name6);
+    if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
+      killsock(sock);
+      return -1;
+    }
+    *port = ntohs(name6.sin6_port);
+    if (listen(sock, 1) < 0) {
+      killsock(sock);
+      return -1;
+    }
   } else {
-    sock = getsock(SOCK_LISTEN, AF_INET);
+      sock = getsock(SOCK_LISTEN, AF_INET);
 #else
-    sock = getsock(SOCK_LISTEN);
+      sock = getsock(SOCK_LISTEN);
 #endif /* USE_IPV6 */
+
     if (sock < 1)
       return -1;
 
-debug0("Opening listen socket with AF_INET");
+    debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
     egg_bzero((char *) &name, sizeof(struct sockaddr_in));
     name.sin_family = AF_INET;
     name.sin_port = htons(*port); /* 0 = just assign us a port */

+ 2 - 1
src/proto.h

@@ -55,7 +55,6 @@ char *decryptit (char *);
 int lfprintf(FILE *, char *, ...);
 
 /* botnet.c */
-inline int open_listen_by_af(int *, int);
 void lower_bot_linked(int idx);
 void higher_bot_linked(int idx);
 void answer_local_whom(int, int);
@@ -285,6 +284,7 @@ void set_cmd_pass(char *, int);
 /* net.c */
 IP my_atoul(char *);
 unsigned long iptolong(IP);
+char *getmyip6();
 IP getmyip();
 void cache_my_ip();
 void neterror(char *);
@@ -301,6 +301,7 @@ char *hostnamefromip(unsigned long);
 void real_killsock(int, const char *, int);
 int answer(int, char *, unsigned long *, unsigned short *, int);
 inline int open_listen(int *);
+inline int open_listen_by_af(int *, int);
 #ifdef USE_IPV6
 int open_address_listen(IP addr, int af_def, int *);
 #else