Просмотр исходного кода

* Port [3215] to 1.2.14
* invalidate uplink_idx in lostdcc() instead of free_dcc_bot()
* Port [3216] to 1.2.14
* Fix bot not reconnecting with new ip/host when -HUPd. (fixes #340)
* Also fix for localhub -SIGUSR1/rehash so far.



svn: 3217

Bryan Drewery 19 лет назад
Родитель
Сommit
955b1919b4
11 измененных файлов с 90 добавлено и 38 удалено
  1. 1 0
      doc/UPDATES
  2. 14 5
      src/binary.c
  3. 17 1
      src/chanprog.c
  4. 2 2
      src/cmds.c
  5. 3 3
      src/conf.c
  6. 0 2
      src/dcc.c
  7. 3 0
      src/dccutil.c
  8. 6 2
      src/main.c
  9. 22 7
      src/misc.c
  10. 21 15
      src/net.c
  11. 1 1
      src/net.h

+ 1 - 0
doc/UPDATES

@@ -14,6 +14,7 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 * The socket file generated for restarting is now encrypted.
 * Delay 'op-requests'.time seconds before re-requesting op on opeless channels.
 * Fix some issues with case with pid files, nicks, conf loading. (This will break some bots upgrading to 1.2.14)
+* Fix bot not reconnecting with new ip/host when -HUPd. (fixes #340)
 
 1.2.13 - http://wraith.shatow.net/milestone/1.2.13
 * Fix cmd_chanset accepting invalid flags

+ 14 - 5
src/binary.c

@@ -16,6 +16,7 @@
 #include "misc_file.h"
 #include "tandem.h"
 #include "botnet.h"
+#include "userrec.h"
 
 #include <sys/wait.h>
 #include <sys/types.h>
@@ -572,7 +573,7 @@ void reload_bin_data() {
 
     /* Save the old conf.bot */
     oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
-    conf_bot_dup(oldbot, conf.bot);    
+    conf_bot_dup(oldbot, conf.bot);
 
     /* free up our current conf struct */
     free_conf();
@@ -582,9 +583,6 @@ void reload_bin_data() {
     /* fill up conf.bot using origbotname */
     fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
 
-    if (conf.bot)
-      free_bot(oldbot);
-
     /* add any new bots not in userfile */
     conf_add_userlist_bots();
 
@@ -644,7 +642,18 @@ void reload_bin_data() {
 
       werr(ERR_BADBOT);
     }
-  
+
+    if (oldbot) {
+      if (strcmp(conf.bot->nick, oldbot->nick)) {
+        change_handle(conf.bot->u, conf.bot->nick);
+//        var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
+//        var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
+      }
+
+
+      free_bot(oldbot);
+    }
+
     if (oldbots)
       free_conf_bots(oldbots);
   }

+ 17 - 1
src/chanprog.c

@@ -543,9 +543,25 @@ void chanprog()
   struct utsname un;
 
   /* cache our ip on load instead of every 30 seconds */
+  char *ip4 = NULL, *ip6 = NULL;
+
+  if (cached_ip) {
+    ip4 = strdup(myipstr(4));
+    ip6 = strdup(myipstr(6));
+  }
+
   cache_my_ip();
   sdprintf("ip4: %s", myipstr(4));
   sdprintf("ip6: %s", myipstr(6));
+
+  /* Check if our ip changed during a rehash */
+  if (ip4) {
+    if (strcmp(ip4, myipstr(4)) || strcmp(ip6, myipstr(6)))
+      fatal("IP changed.", 1);
+    free(ip4);
+    free(ip6);
+  }
+
   sdprintf("I am: %s", conf.bot->nick);
   if (conf.bot->hub) {
     simple_snprintf(userfile, 121, "%s/.u", conf.binpath);
@@ -568,7 +584,7 @@ void chanprog()
   }
 
   /* set our shell info */
-  uname(&un);  
+  uname(&un);
   set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
   set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
   set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);

+ 2 - 2
src/cmds.c

@@ -1832,7 +1832,7 @@ static void cmd_decrypt(int idx, char *par)
 static void cmd_restart(int idx, char *par)
 {
   putlog(LOG_CMDS, "*", "#%s# restart", dcc[idx].nick);
-  restart(idx);
+  do_restart = 1;
 }
 
 static void cmd_reload(int idx, char *par)
@@ -1846,7 +1846,7 @@ static void cmd_rehash(int idx, char *par)
 {
   putlog(LOG_CMDS, "*", "#%s# rehash", dcc[idx].nick);
   dprintf(idx, "Rehashing config data from binary...\n");
-  reload_bin_data();
+  do_restart = 2;
 }
 
 static void cmd_die(int idx, char *par)

+ 3 - 3
src/conf.c

@@ -19,6 +19,7 @@
 #include "users.h"
 #include "misc_file.h"
 #include "socket.h"
+#include "botnet.h"
 #include "userrec.h"
 #include <errno.h>
 #ifdef HAVE_PATHS_H
@@ -117,8 +118,7 @@ spawnbots(bool rehashed)
       -if updating and we find our nick, skip
       -if pid exists and not updating, bot is running and we have nothing more to do, skip.
      */
-    			/* use origbotname here because conf.bot may not exist (called from rehashing) */
-    } else if ((!egg_strcasecmp(bot->nick, origbotname) && (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
+    } else if ((!egg_strcasecmp(bot->nick, conf.bot->nick) && (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
       sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
       continue;
     } else {
@@ -1186,7 +1186,7 @@ void conf_add_userlist_bots()
 
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
     /* Don't auto-add hubs. */
-    if (!bot->hub) {
+    if (!bot->hub && tands > 0) {
       u = get_user_by_handle(userlist, bot->nick);
       if (!u) {
         putlog(LOG_MISC, "*", "Adding bot '%s' as it has been added to the binary config.", bot->nick);

+ 0 - 2
src/dcc.c

@@ -419,8 +419,6 @@ static void
 free_dcc_bot_(int n, void *x)
 {
   if (dcc[n].type == &DCC_BOT) {
-    if (n == uplink_idx)
-      uplink_idx = -1;
     unvia(n, findbot(dcc[n].nick));
     rembot(dcc[n].nick);
   }

+ 3 - 0
src/dccutil.c

@@ -521,6 +521,9 @@ lostdcc(int n)
   if (n < 0 || n >= max_dcc)
     return;
 
+  if (n == uplink_idx)
+    uplink_idx = -1;
+
   if (dcc[n].type && dcc[n].type->kill)
     dcc[n].type->kill(n, dcc[n].u.other);
   else if (dcc[n].u.other)

+ 6 - 2
src/main.c

@@ -173,8 +173,10 @@ void fatal(const char *s, int recoverable)
     listen_all(my_port, 1); /* close the listening port... */
 
   for (int i = 0; i < dcc_total; i++)
-    if (dcc[i].type && dcc[i].sock >= 0)
+    if (dcc[i].type && dcc[i].sock >= 0) {
       killsock(dcc[i].sock);
+      lostdcc(i);
+    }
 
   if (!recoverable) {
 //    if (conf.bot && conf.bot->pid_file)
@@ -983,8 +985,10 @@ printf("out: %s\n", out);
     if (do_restart) {
       if (do_restart == 1)
         restart(-1);
-      else
+      else { //rehash()
         reload_bin_data();
+        chanprog();
+      }
       do_restart = 0;
     }
   }

+ 22 - 7
src/misc.c

@@ -575,7 +575,7 @@ readsocks(const char *fname)
     fatal("CANT READ SOCKSFILE", 0);
   }
 
-  char buf[1024] = "", *nick = NULL, *bufp = NULL, *type = NULL, *buf_ptr = NULL;
+  char buf[1024] = "", *nick = NULL, *bufp = NULL, *type = NULL, *buf_ptr = NULL, *ip4 = NULL, *ip6 = NULL;
   time_t old_buildts = 0;
 
   bool enc = 0, first = 1;
@@ -606,6 +606,10 @@ readsocks(const char *fname)
       old_buildts = strtol(bufp, NULL, 10);
     else if (!strcmp(type, "+botname"))
       nick = strdup(bufp);
+    else if (!strcmp(type, "+ip4"))
+      ip4 = strdup(bufp);
+    else if (!strcmp(type, "+ip6"))
+      ip6 = strdup(bufp);
 
     if (enc)
       free(buf_ptr);
@@ -628,17 +632,25 @@ readsocks(const char *fname)
   if (servidx >= 0) {
     char nserv[50] = "";
 
-    simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
-    add_server(nserv);
-    curserv = 0;
-    rehash_server(dcc[servidx].host, nick);
-    dprintf(DP_DUMP, "VERSION\n");
-    reset_chans = 1;
+    if ((ip4 && ip6) && (strcmp(ip4, myipstr(4)) || strcmp(ip6, myipstr(6)))) {
+      fatal("IP changed.", 1);
+    } else {
+      simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
+      add_server(nserv);
+      curserv = 0;
+      rehash_server(dcc[servidx].host, nick);
+      dprintf(DP_DUMP, "VERSION\n");
+      reset_chans = 1;
+    }
   }
   if (nick)
     free(nick);
   if (socksfile)
     free(socksfile);
+  if (ip4)
+    free(ip4);
+  if (ip6)
+    free(ip6);
 }
 
 
@@ -686,6 +698,9 @@ restart(int idx)
   }
   lfprintf(socks->f, "+online_since %li\n", online_since);
   lfprintf(socks->f, "+buildts %li\n", buildts);
+  lfprintf(socks->f, "+ip4 %s\n", myipstr(4));
+  lfprintf(socks->f, "+ip6 %s\n", myipstr(6));
+
   fflush(socks->f);
   socks->my_close();
 

+ 21 - 15
src/net.c

@@ -49,7 +49,7 @@ union sockaddr_union cached_myip4_so;
 union sockaddr_union cached_myip6_so;
 #endif /* USE_IPV6 */
 
-char 	natip[121] = "";
+bool	cached_ip = 0;		/* Set to 1 after cache_my_ip is called */
 bool    identd_hack = 0;	/* identd_open() won't work on most servers, dont even bother warning. */
 char	botuser[21] = ""; 	/* Username of the user running the bot    */
 int     resolve_timeout = 10;   /* hostname/address lookup timeout */
@@ -223,22 +223,24 @@ int ssl_cleanup() {
  */
 char *myipstr(int af_type)
 {
+  if (cached_ip) {
 #ifdef USE_IPV6
-  if (af_type == 6) {
-    static char s[UHOSTLEN + 1] = "";
-
-    egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
-    s[120] = 0;
-    return s;
-  } else
-#endif /* USE_IPV6 */
-    if (af_type == 4) {
+    if (af_type == 6) {
       static char s[UHOSTLEN + 1] = "";
 
-      egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
+      egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
       s[120] = 0;
       return s;
-    }
+    } else
+#endif /* USE_IPV6 */
+      if (af_type == 4) {
+        static char s[UHOSTLEN + 1] = "";
+
+        egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
+        s[120] = 0;
+        return s;
+      }
+  }
 
   return "";
 }
@@ -246,7 +248,7 @@ char *myipstr(int af_type)
 /* Get my ip number
  */
 in_addr_t getmyip() {
-  return natip[0] ? inet_addr(natip) : cached_myip4_so.sin.sin_addr.s_addr;
+  return cached_myip4_so.sin.sin_addr.s_addr;
 }
 
 /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
@@ -308,6 +310,8 @@ void cache_my_ip()
     putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
     fatal("Hostname self-lookup failed.", 0);
   }
+
+  cached_ip = 1;
 }
 
 /* Sets/Unsets options for a specific socket.
@@ -1521,8 +1525,10 @@ void tputs(register int z, char *s, size_t len)
     inhere = 1;
 
     putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
-    s[strlen(s) - 1] = 0;
-    putlog(LOG_MISC, "*", "!-> '%s'", s);
+    if (strlen(s)) {
+      s[strlen(s) - 1] = 0;
+      putlog(LOG_MISC, "*", "!-> '%s'", s);
+    }
 
     inhere = 0;
   }

+ 1 - 1
src/net.h

@@ -158,7 +158,7 @@ extern unsigned long			notalloc;
 
 extern char				firewall[], botuser[], natip[];
 extern int				resolve_timeout, MAXSOCKS, socks_total;
-extern bool				identd_hack;
+extern bool				identd_hack, cached_ip;
 extern port_t				firewallport;
 extern jmp_buf				alarmret;
 extern sock_list			*socklist;