muhstik

- irc flooding solution
git clone git://git.acid.vegas/muhstik.git
Log | Files | Refs | Archive | README

load.c (9356B)

      1 /* Muhstik, Copyright (C) 2001-2002, Louis Bavoil <mulder@gmx.fr>       */
      2 /*                        2003, roadr (bigmac@home.sirklabs.hu)         */
      3 /*                        2009-2011, Leon Kaiser <literalka@gnaa.eu>    */
      4 /*                                                                      */
      5 /* This program is free software; you can redistribute it and/or        */
      6 /* modify it under the terms of the GNU Library General Public License  */
      7 /* as published by the Free Software Foundation; either version 2       */
      8 /* of the License, or (at your option) any later version.               */
      9 /*                                                                      */
     10 /* This program is distributed in the hope that it will be useful,      */
     11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
     12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
     13 /* GNU Library General Public License for more details.                 */
     14 
     15 /* {{{ Header includes */
     16 #include <pthread.h>
     17 #include <unistd.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 
     21 #include "../include/string.h"
     22 #include "../include/lists.h"
     23 #include "../include/print.h"
     24 #include "../include/load.h"
     25 /* }}} */
     26 /* {{{ External variables */
     27 extern clone_t *cl[];
     28 extern config_t conf;
     29 extern const char *strtype[];
     30 extern pthread_mutex_t mutex[];
     31 /* }}} */
     32 /* {{{ randget() */
     33 void randget(clone_t *clone, char **dest, size_t destlen, int uselist, char **list, int max)
     34 {
     35      register unsigned int i; /* ``unsigned'' is not needed, stops a warning when compiled with -Wsign-compare */
     36      char *tmp;
     37 
     38      /* Alphabet used to make random words */
     39      static char charset[] = "abcdefghijklmnopqrstuvwxyz";
     40      static size_t len;
     41 
     42      len = strlen(charset);
     43      tmp = (char *)xmalloc(destlen + 1);
     44 
     45      if (uselist && list[0])
     46      {
     47           for (i = 0; i < MAX_NICKS && list[i]; ++i) {}
     48           StrCopy(tmp, list[random() % i], destlen + 1);
     49      }
     50      else
     51      {
     52           for (i = 0; i < destlen; ++i)
     53           {
     54                tmp[i] = charset[random() % len];
     55           }
     56      }
     57      tmp[destlen] = 0;
     58      *dest = tmp;
     59 }
     60 /* }}} */
     61 /* {{{ Loading functions */
     62 int load_clone(clone_t *go)
     63 {
     64      clone_t **pcl;
     65      register int id;
     66 
     67      /* Get a free entry in cl[] */
     68      for (id = 0, pcl = cl; *pcl; ++id, ++pcl)
     69      {
     70           if (id == MAX_CLONES-1)
     71           {
     72                return 1;
     73           }
     74      }
     75 
     76      /* Allocate memory for a new clone structure */
     77      if (!(*pcl = (clone_t *)malloc(sizeof(clone_t))))
     78      {
     79           print_error("malloc");
     80           return 1;
     81      }
     82 
     83      /* Copy the current clone parameters in it */
     84      memcpy(*pcl, go, sizeof(clone_t));
     85 
     86      /* The id is useful to free the structure on exit */
     87      (*pcl)->id = id;
     88 
     89      /* Launch the clone */
     90      if (main_clone(*pcl))
     91      {
     92           free_clone(*pcl);
     93      }
     94 
     95      return 0;
     96 }
     97 
     98 int load_host(int t, char *host, int proxy_port, char *server, int server_port, char *pass, char *ident, char *save, int mode)
     99 {
    100      clone_t go;
    101 
    102      /* Initialize the current clone structure */
    103      memset(&go, 0, sizeof(clone_t));
    104      memset(&go.buffer, 0, sizeof(go.buffer));
    105      memset(&go.lastbuffer, 0, sizeof(go.lastbuffer));
    106      go.sock = -1;
    107      go.type = t;
    108      go.mode = mode;
    109 
    110      if (go.type != NOSPOOF)
    111      {
    112           go.proxy = StrDuplicate(host);
    113           go.proxy_port = proxy_port;
    114      }
    115 
    116      go.server = StrDuplicate(server);
    117      go.server_port = server_port;
    118      go.server_pass = StrDuplicate(pass);
    119      go.server_ident = StrDuplicate(ident);
    120      go.save = StrDuplicate(save);
    121 
    122      /* Make the clone's {nick,ident,realname} from a wordlist or randomly. */
    123      randget(&go, &go.nick, conf.nick_length, conf.use_wordlist, conf.nicks, MAX_NICKS);
    124      randget(&go, &go.ident, conf.ident_length, 1, conf.idents, MAX_IDENTS);
    125      randget(&go, &go.real, conf.real_length, 1, conf.names, MAX_NAMES);
    126 
    127      return load_clone(&go);
    128 }
    129 
    130 void loadlist(int t, FILE *hosts, FILE *servers, char *save)
    131 {
    132      int ret;
    133      int i = 0;
    134      int k = 0;
    135      int l = 0;
    136      int max_server = 0;
    137      int max_proxys = 0;
    138      char server[MEDBUF+1];
    139      char *serv = NULL;
    140      char *pass = NULL;
    141      char *ident = NULL;
    142      int server_port;
    143      char proxy[MEDBUF+1];
    144      int proxy_port;
    145      char buffer[MEDBUF+1];
    146      char parm[MINIBUF];
    147 
    148      if (!conf.wait_socks && hosts)
    149      {
    150           max_proxys = file_length(hosts);
    151      }
    152 
    153      while (1)
    154      {
    155           if (i == 0)
    156           {
    157                /* Get the next server on the current server list */
    158                if (!fgets(buffer, sizeof(buffer), servers))
    159                {
    160                     return;
    161                }
    162                switch (sscanf(buffer, "%"MEDBUF_TXT"[^:\r\n]:%d %d", server, &server_port, &max_server))
    163                {
    164                     default:
    165                          print(1, 0, 0, "No line to read");
    166                          return;
    167                     case 1:
    168                          print(1, 0, 0, "%s: server error (host:port)", server);
    169                          return;
    170                     case 2:
    171                          print(1, 0, 0, "%s: server error (no max)", server);
    172                          return;
    173                     case 3:
    174                          print(1, 3, 0, "%s server: %s:%d [max=%d]", strtype[t], server, server_port, max_server);
    175 
    176                          if (!StrParam(parm, sizeof(parm), buffer, 2))
    177                          {
    178                               pass = StrDuplicate(parm);
    179                          }
    180 
    181                          if (!StrParam(parm, sizeof(parm), buffer, 3))
    182                          {
    183                               ident = StrDuplicate(parm);
    184                          }
    185 
    186                          if (t != NOSPOOF && conf.rewind_socks)
    187                          {
    188                               rewind(hosts);
    189                          }
    190 
    191                          break;
    192                }
    193                if (t == NOSPOOF && max_server <= 0)
    194                {
    195                     print(1, 0, 0, "%s: server error (max must be > 0)", server);
    196                     return;
    197                }
    198                serv = StrDuplicate(server);
    199           }
    200 
    201           /* Get the next proxy on the current proxy list, if needed */
    202           if (t != NOSPOOF && k == 0)
    203           {
    204                while (get_a_line(buffer, sizeof(buffer), hosts))
    205                {
    206                     if (conf.wait_socks)
    207                     {
    208                          sleep(2);
    209                     }
    210                     else
    211                     {
    212                          return;
    213                     }
    214                }
    215                sscanf(buffer, "%"MEDBUF_TXT"s", proxy);
    216                if (t != VHOST)
    217                {
    218                     if (strchr(proxy,':'))
    219                     {
    220                          sscanf(proxy, "%*[^:]:%d", &proxy_port); /* XXX: What if ``(t == VHOST)''? -- Leon */
    221                          sscanf(proxy, "%[^:]", proxy);
    222                     }
    223                     else switch (t) /* set a default port */
    224                     {
    225                          case CISCO:
    226                               proxy_port = 23;
    227                               break;
    228                          case SOCKS4:
    229                          case SOCKS5:
    230                               proxy_port = 1080;
    231                               break;
    232                          case PROXY:
    233                               print(1, 0, 0, "%s: proxy error (host:port)", proxy);
    234                               proxy_port = 8080;
    235                               break;
    236                     }
    237                }
    238           }
    239 
    240           while ((max_server <= 0 || i < max_server) && (t == NOSPOOF || k < conf.max_clones))
    241           {
    242                pthread_mutex_lock(&mutex[0]);
    243                ret = load_host(t, proxy, proxy_port, serv, server_port, pass, ident, save, M_NORMAL);
    244                pthread_mutex_unlock(&mutex[0]);
    245                if (ret)
    246                {
    247                     pthread_exit(0);
    248                }
    249 
    250                ++i, ++k, ++l;
    251 
    252                if (l == conf.group && conf.load > 0)
    253                {
    254                     usleep(conf.load * 1000), l = 0;
    255                }
    256           }
    257 
    258           if (i == max_server)
    259           {
    260                i = 0;
    261           }
    262           if (k == conf.max_clones)
    263           {
    264                k = 0;
    265           }
    266 
    267           if (!conf.wait_socks && hosts && max_server <= 0)
    268           {
    269                if (i == max_proxys * conf.max_clones)
    270                {
    271                     i = 0;
    272                }
    273           }
    274      }
    275 }
    276 
    277 void load1(int t, FILE *hosts, FILE *servers, char *save)
    278 {
    279      loadlist(t, hosts, servers, save);
    280      if (conf.verbose)
    281      {
    282           print(1, 0, 0, "all %ss loaded.", strtype[t]);
    283      }
    284 }
    285 
    286 void load2(int t, FILE **p1, FILE **p2, char **p3)
    287 {
    288      FILE **fhost;
    289      FILE **fserv;
    290      char **csave;
    291      for (fhost = p1, fserv = p2, csave = p3; *fhost; ++fhost, ++fserv, ++csave)
    292      {
    293           load1(t, *fhost, *fserv, *csave);
    294      }
    295 }
    296 
    297 void *load_all(void *arg)
    298 {
    299      int i;
    300 
    301      for (i = 0; i < PROXYS; ++i)
    302      {
    303           if (conf.h[i][0])
    304           {
    305                load2(i, conf.h[i], conf.s[i], conf.g[i]);
    306           }
    307      }
    308 
    309      if (conf.direct[0])
    310      {
    311           load1(NOSPOOF, NULL, conf.direct[0], NULL);
    312      }
    313 
    314      return NULL;
    315 }
    316 /* }}} */