home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ircd4652.zip / ircd-df-4.6.5-os2 / src / old_s_bsd.c < prev    next >
C/C++ Source or Header  |  1998-06-06  |  68KB  |  2,645 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, ircd/s_bsd.c
  3.  *   Copyright (C) 1990 Jarkko Oikarinen and
  4.  *                      University of Oulu, Computing Center
  5.  *
  6.  *   This program is free software; you can redistribute it and/or modify
  7.  *   it under the terms of the GNU General Public License as published by
  8.  *   the Free Software Foundation; either version 1, or (at your option)
  9.  *   any later version.
  10.  *
  11.  *   This program is distributed in the hope that it will be useful,
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *   GNU General Public License for more details.
  15.  *
  16.  *   You should have received a copy of the GNU General Public License
  17.  *   along with this program; if not, write to the Free Software
  18.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* -- Jto -- 07 Jul 1990
  22.  * Added jlp@hamblin.byu.edu's debugtty fix
  23.  */
  24.  
  25. /* -- Armin -- Jun 18 1990
  26.  * Added setdtablesize() for more socket connections
  27.  * (sequent OS Dynix only) -- maybe select()-call must be changed ...
  28.  */
  29.  
  30. /* -- Jto -- 13 May 1990
  31.  * Added several fixes from msa:
  32.  *   Better error messages
  33.  *   Changes in check_access
  34.  * Added SO_REUSEADDR fix from zessel@informatik.uni-kl.de
  35.  */
  36.  
  37. #ifndef lint
  38. static  char sccsid[] = "@(#)s_bsd.c    2.78 2/7/94 (C) 1988 University of Oulu, \
  39. Computing Center and Jarkko Oikarinen";
  40. #endif
  41.  
  42. #include "struct.h"
  43. #include "common.h"
  44. #include "sys.h"
  45. #include "res.h"
  46. #include "numeric.h"
  47. #include "patchlevel.h"
  48. #ifndef _WIN32
  49. #include <sys/socket.h>
  50. #include <sys/file.h>
  51. #include <sys/ioctl.h>
  52. #ifndef __EMX__
  53. #include <utmp.h>
  54. #endif
  55. #include <sys/resource.h>
  56. #else
  57. #include <io.h>
  58. #endif
  59. #if defined(SOL20) && !defined __EMX__
  60. #include <sys/filio.h>
  61. #endif
  62. #if defined(UNIXPORT) && (!defined(SVR3) || defined(sgi) || \
  63.     defined(_SEQUENT_)) && !defined(_WIN32)
  64. # include <sys/un.h>
  65. #endif
  66. #ifdef __EMX__
  67. #include <arpa/inet.h>
  68. #else
  69. #include "inet.h"
  70. #endif
  71. #include "resolv.h"
  72. #include "nameser.h"
  73. #include <stdio.h>
  74. #include <signal.h>
  75. #include <fcntl.h>
  76. #ifdef    AIX
  77. # include <time.h>
  78. # include <arpa/nameser.h>
  79. #else
  80. #endif
  81. #include "sock.h"    /* If FD_ZERO isn't define up to this point,  */
  82.             /* define it (BSD4.2 needs this) */
  83. #include "h.h"
  84.  
  85. #ifndef IN_LOOPBACKNET
  86. #define IN_LOOPBACKNET    0x7f
  87. #endif
  88.  
  89. #ifdef _WIN32
  90. extern    HWND    hwIRCDWnd;
  91. #endif
  92. aClient    *local[MAXCONNECTIONS];
  93. int    highest_fd = 0, readcalls = 0, udpfd = -1, resfd = -1;
  94. static    struct    sockaddr_in    mysk;
  95. static    void    polludp();
  96.  
  97. static    struct    sockaddr *connect_inet PROTO((aConfItem *, aClient *, int *));
  98. static    int    completed_connection PROTO((aClient *));
  99. static    int    check_init PROTO((aClient *, char *));
  100. #ifndef _WIN32
  101. static    void    do_dns_async PROTO(()), set_sock_opts PROTO((int, aClient *));
  102. #else
  103. static    void    set_sock_opts PROTO((int, aClient *));
  104. #endif
  105. #ifdef    UNIXPORT
  106. static    struct    sockaddr *connect_unix PROTO((aConfItem *, aClient *, int *));
  107. static    void    add_unixconnection PROTO((aClient *, int));
  108. static    char    unixpath[256];
  109. #endif
  110. static    char    readbuf[8192];
  111. char zlinebuf[BUFSIZE];
  112. extern    char *version;
  113.  
  114. /*
  115.  * Try and find the correct name to use with getrlimit() for setting the max.
  116.  * number of files allowed to be open by this process.
  117.  */
  118. #ifdef RLIMIT_FDMAX
  119. # define RLIMIT_FD_MAX   RLIMIT_FDMAX
  120. #else
  121. # ifdef RLIMIT_NOFILE
  122. #  define RLIMIT_FD_MAX RLIMIT_NOFILE
  123. # else
  124. #  ifdef RLIMIT_OPEN_MAX
  125. #   define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
  126. #  else
  127. #   undef RLIMIT_FD_MAX
  128. #  endif
  129. # endif
  130. #endif
  131.  
  132. /*
  133. ** add_local_domain()
  134. ** Add the domain to hostname, if it is missing
  135. ** (as suggested by eps@TOASTER.SFSU.EDU)
  136. */
  137.  
  138. void    add_local_domain(hname, size)
  139. char    *hname;
  140. int    size;
  141. {
  142. #ifdef RES_INIT
  143.     /* try to fix up unqualified names */
  144.     if (!index(hname, '.'))
  145.         {
  146.         if (!(_res.options & RES_INIT))
  147.             {
  148.             Debug((DEBUG_DNS,"res_init()"));
  149.             res_init();
  150.             }
  151.         if (_res.defdname[0])
  152.             {
  153.             (void)strncat(hname, ".", size-1);
  154.             (void)strncat(hname, _res.defdname, size-2);
  155.             }
  156.         }
  157. #endif
  158.     return;
  159. }
  160.  
  161. /*
  162. ** Cannot use perror() within daemon. stderr is closed in
  163. ** ircd and cannot be used. And, worse yet, it might have
  164. ** been reassigned to a normal connection...
  165. */
  166.  
  167. /*
  168. ** report_error
  169. **    This a replacement for perror(). Record error to log and
  170. **    also send a copy to all *LOCAL* opers online.
  171. **
  172. **    text    is a *format* string for outputting error. It must
  173. **        contain only two '%s', the first will be replaced
  174. **        by the sockhost from the cptr, and the latter will
  175. **        be taken from sys_errlist[errno].
  176. **
  177. **    cptr    if not NULL, is the *LOCAL* client associated with
  178. **        the error.
  179. */
  180. void    report_error(text, cptr)
  181. char    *text;
  182. aClient *cptr;
  183. {
  184. #ifndef _WIN32
  185.     Reg1    int    errtmp = errno; /* debug may change 'errno' */
  186. #else
  187.     Reg1    int    errtmp = WSAGetLastError(); /* debug may change 'errno' */
  188. #endif
  189.     Reg2    char    *host;
  190.     int    err, len = sizeof(err);
  191.  
  192.     host = (cptr) ? get_client_name(cptr, FALSE) : "";
  193.  
  194.     Debug((DEBUG_ERROR, text, host, strerror(errtmp)));
  195.  
  196.     /*
  197.      * Get the *real* error from the socket (well try to anyway..).
  198.      * This may only work when SO_DEBUG is enabled but its worth the
  199.      * gamble anyway.
  200.      */
  201. #ifdef    SO_ERROR
  202.     if (cptr && !IsMe(cptr) && cptr->fd >= 0)
  203.         if (!getsockopt(cptr->fd, SOL_SOCKET, SO_ERROR, (OPT_TYPE *)&err, &len))
  204.             if (err)
  205.                 errtmp = err;
  206. #endif
  207.     sendto_ops(text, host, strerror(errtmp));
  208. #ifdef USE_SYSLOG
  209.     syslog(LOG_WARNING, text, host, strerror(errtmp));
  210. #endif
  211.     return;
  212. }
  213.  
  214. /*
  215.  * inetport
  216.  *
  217.  * Create a socket in the AF_INET domain, bind it to the port given in
  218.  * 'port' and listen to it.  Connections are accepted to this socket
  219.  * depending on the IP# mask given by 'name'.  Returns the fd of the
  220.  * socket created or -1 on error.
  221.  */
  222. int    inetport(cptr, name, port)
  223. aClient    *cptr;
  224. char    *name;
  225. int    port;
  226. {
  227.     static    struct sockaddr_in server;
  228.     int    ad[4], len = sizeof(server);
  229.     char    ipname[20];
  230.  
  231.     if (BadPtr(name))
  232.         name = "*";
  233.     ad[0] = ad[1] = ad[2] = ad[3] = 0;
  234.  
  235.     /*
  236.      * do it this way because building ip# from separate values for each
  237.      * byte requires endian knowledge or some nasty messing. Also means
  238.      * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
  239.      */
  240.     (void)sscanf(name, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
  241.     (void)sprintf(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
  242.  
  243.     if (cptr != &me)
  244.         {
  245.         (void)sprintf(cptr->sockhost, "%-.42s.%.u",
  246.             name, (unsigned int)port);
  247.         (void)strcpy(cptr->name, me.name);
  248.         }
  249.     /*
  250.      * At first, open a new socket
  251.      */
  252.     if (cptr->fd == -1)
  253.         cptr->fd = socket(AF_INET, SOCK_STREAM, 0);
  254.  
  255.     if (cptr->fd < 0)
  256.         {
  257.         report_error("opening stream socket %s:%s", cptr);
  258.         return -1;
  259.         }
  260.     else if (cptr->fd >= MAXCLIENTS)
  261.         {
  262.         sendto_ops("No more connections allowed (%s)", cptr->name);
  263. #ifndef _WIN32
  264.         (void)close(cptr->fd);
  265. #else
  266.         (void)closesocket(cptr->fd);
  267. #endif
  268.         return -1;
  269.         }
  270.     set_sock_opts(cptr->fd, cptr);
  271.     /*
  272.      * Bind a port to listen for new connections if port is non-null,
  273.      * else assume it is already open and try get something from it.
  274.      */
  275.     if (port)
  276.         {
  277.         server.sin_family = AF_INET;
  278.         /* per-port bindings, fixes /stats l */
  279.         server.sin_addr.s_addr = inet_addr(ipname);
  280. #ifdef TESTNET
  281.         server.sin_port = htons(port + 10000);
  282. #else
  283.         server.sin_port = htons(port);
  284. #endif
  285.         /*
  286.          * Try 10 times to bind the socket with an interval of 20
  287.          * seconds. Do this so we dont have to keepp trying manually
  288.          * to bind. Why ? Because a port that has closed often lingers
  289.          * around for a short time.
  290.          * This used to be the case.  Now it no longer is.
  291.          * Could cause the server to hang for too long - avalon
  292.          */
  293.         if (bind(cptr->fd, (struct sockaddr *)&server,
  294.             sizeof(server)) == -1)
  295.             {
  296.             report_error("binding stream socket %s:%s", cptr);
  297. #ifndef _WIN32
  298.             (void)close(cptr->fd);
  299. #else
  300.             (void)closesocket(cptr->fd);
  301. #endif
  302.             return -1;
  303.             }
  304.         }
  305.     if (getsockname(cptr->fd, (struct sockaddr *)&server, &len))
  306.         {
  307.         report_error("getsockname failed for %s:%s",cptr);
  308. #ifndef _WIN32
  309.         (void)close(cptr->fd);
  310. #else
  311.         (void)closesocket(cptr->fd);
  312. #endif
  313.         return -1;
  314.         }
  315.  
  316.     if (cptr == &me) /* KLUDGE to get it work... */
  317.         {
  318.         char    buf[1024];
  319.  
  320. #ifdef TESTNET
  321.         (void)sprintf(buf, rpl_str(RPL_MYPORTIS), me.name, "*",
  322.             ntohs(server.sin_port) - 10000);
  323. #else
  324.         (void)sprintf(buf, rpl_str(RPL_MYPORTIS), me.name, "*",
  325.             ntohs(server.sin_port));
  326. #endif
  327.         (void)write(0, buf, strlen(buf));
  328.         }
  329.     if (cptr->fd > highest_fd)
  330.         highest_fd = cptr->fd;
  331.     cptr->ip.s_addr = name ? inet_addr(ipname) : me.ip.s_addr;
  332. #ifdef TESTNET
  333.     cptr->port = (int)ntohs(server.sin_port) - 10000;
  334. #else
  335.     cptr->port = (int)ntohs(server.sin_port);
  336. #endif
  337.     (void)listen(cptr->fd, LISTEN_SIZE);
  338.     local[cptr->fd] = cptr;
  339.  
  340.     return 0;
  341. }
  342.  
  343. /*
  344.  * add_listener
  345.  *
  346.  * Create a new client which is essentially the stub like 'me' to be used
  347.  * for a socket that is passive (listen'ing for connections to be accepted).
  348.  */
  349. int    add_listener(aconf)
  350. aConfItem *aconf;
  351. {
  352.     aClient    *cptr;
  353.  
  354.     cptr = make_client(NULL, NULL);
  355.     cptr->flags = FLAGS_LISTEN;
  356.     cptr->acpt = cptr;
  357.     cptr->from = cptr;
  358.     SetMe(cptr);
  359.     strncpyzt(cptr->name, aconf->host, sizeof(cptr->name));
  360. #ifdef    UNIXPORT
  361.     if (*aconf->host == '/')
  362.         {
  363.         if (unixport(cptr, aconf->host, aconf->port))
  364.             cptr->fd = -2;
  365.         }
  366.     else
  367. #endif
  368.         if (inetport(cptr, aconf->host, aconf->port))
  369.             cptr->fd = -2;
  370.  
  371.     if (cptr->fd >= 0)
  372.         {
  373.         cptr->confs = make_link();
  374.         cptr->confs->next = NULL;
  375.         cptr->confs->value.aconf = aconf;
  376.         set_non_blocking(cptr->fd, cptr);
  377.         }
  378.     else
  379.         free_client(cptr);
  380.     return 0;
  381. }
  382.  
  383. #ifdef    UNIXPORT
  384. /*
  385.  * unixport
  386.  *
  387.  * Create a socket and bind it to a filename which is comprised of the path
  388.  * (directory where file is placed) and port (actual filename created).
  389.  * Set directory permissions as rwxr-xr-x so other users can connect to the
  390.  * file which is 'forced' to rwxrwxrwx (different OS's have different need of
  391.  * modes so users can connect to the socket).
  392.  */
  393. int    unixport(cptr, path, port)
  394. aClient    *cptr;
  395. char    *path;
  396. int    port;
  397. {
  398.     struct sockaddr_un un;
  399.  
  400.     if ((cptr->fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
  401.         {
  402.         report_error("error opening unix domain socket %s:%s", cptr);
  403.         return -1;
  404.         }
  405.     else if (cptr->fd >= MAXCLIENTS)
  406.         {
  407.         sendto_ops("No more connections allowed (%s)", cptr->name);
  408.         (void)close(cptr->fd);
  409.         return -1;
  410.         }
  411.  
  412.     un.sun_family = AF_UNIX;
  413.     (void)mkdir(path, 0755);
  414.     (void)sprintf(unixpath, "%s/%d", path, port);
  415.     (void)unlink(unixpath);
  416.     strncpyzt(un.sun_path, unixpath, sizeof(un.sun_path));
  417.     (void)strcpy(cptr->name, me.name);
  418.     errno = 0;
  419.     get_sockhost(cptr, unixpath);
  420.  
  421.     if (bind(cptr->fd, (struct sockaddr *)&un, strlen(unixpath)+2) == -1)
  422.         {
  423.         report_error("error binding unix socket %s:%s", cptr);
  424.         (void)close(cptr->fd);
  425.         return -1;
  426.         }
  427.     if (cptr->fd > highest_fd)
  428.         highest_fd = cptr->fd;
  429.     (void)listen(cptr->fd, 5);
  430.     (void)chmod(path, 0755);
  431.     (void)chmod(unixpath, 0777);
  432.     cptr->flags |= FLAGS_UNIX;
  433.     cptr->port = 0;
  434.     local[cptr->fd] = cptr;
  435.  
  436.     return 0;
  437. }
  438. #endif
  439.  
  440. /*
  441.  * close_listeners
  442.  *
  443.  * Close and free all clients which are marked as having their socket open
  444.  * and in a state where they can accept connections.  Unix sockets have
  445.  * the path to the socket unlinked for cleanliness.
  446.  */
  447. void    close_listeners()
  448. {
  449.     Reg1    aClient    *cptr;
  450.     Reg2    int    i;
  451.     Reg3    aConfItem *aconf;
  452.  
  453.     /*
  454.      * close all 'extra' listening ports we have and unlink the file
  455.      * name if it was a unix socket.
  456.      */
  457.     for (i = highest_fd; i >= 0; i--)
  458.         {
  459.         if (!(cptr = local[i]))
  460.             continue;
  461.         if (!IsMe(cptr) || cptr == &me || !IsListening(cptr))
  462.             continue;
  463.         aconf = cptr->confs->value.aconf;
  464.  
  465.         if (IsIllegal(aconf) && aconf->clients == 0)
  466.             {
  467. #ifdef    UNIXPORT
  468.             if (IsUnixSocket(cptr))
  469.                 {
  470.                 (void)sprintf(unixpath, "%s/%d", aconf->host,
  471.                     aconf->port);
  472.                 (void)unlink(unixpath);
  473.                 }
  474. #endif
  475.             close_connection(cptr);
  476.             }
  477.         }
  478. }
  479.  
  480. /*
  481.  * init_sys
  482.  */
  483. void    init_sys()
  484. {
  485.     Reg1    int    fd;
  486. #ifdef RLIMIT_FD_MAX
  487.     struct rlimit limit;
  488.  
  489.     if (!getrlimit(RLIMIT_FD_MAX, &limit))
  490.         {
  491. # ifdef    pyr
  492.         if (limit.rlim_cur < MAXCONNECTIONS)
  493. #else
  494.         if (limit.rlim_max < MAXCONNECTIONS)
  495. # endif
  496.             {
  497.             (void)fprintf(stderr,"ircd fd table too big\n");
  498.             (void)fprintf(stderr,"Hard Limit: %d IRC max: %d\n",
  499.                 limit.rlim_max, MAXCONNECTIONS);
  500.             (void)fprintf(stderr,"Fix MAXCONNECTIONS\n");
  501.             exit(-1);
  502.             }
  503. # ifndef    pyr
  504.         limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
  505.         if (setrlimit(RLIMIT_FD_MAX, &limit) == -1)
  506.             {
  507.             (void)fprintf(stderr,"error setting max fd's to %d\n",
  508.                     limit.rlim_cur);
  509.             exit(-1);
  510.             }
  511. # endif
  512.         }
  513. #endif
  514. #ifdef sequent
  515. # ifndef    DYNIXPTX
  516.     int    fd_limit;
  517.  
  518.     fd_limit = setdtablesize(MAXCONNECTIONS + 1);
  519.     if (fd_limit < MAXCONNECTIONS)
  520.         {
  521.         (void)fprintf(stderr,"ircd fd table too big\n");
  522.         (void)fprintf(stderr,"Hard Limit: %d IRC max: %d\n",
  523.             fd_limit, MAXCONNECTIONS);
  524.         (void)fprintf(stderr,"Fix MAXCONNECTIONS\n");
  525.         exit(-1);
  526.         }
  527. # endif
  528. #endif
  529. #if defined(PCS) || defined(DYNIXPTX) || defined(SVR3)
  530.     char    logbuf[BUFSIZ];
  531.  
  532.     (void)setvbuf(stderr,logbuf,_IOLBF,sizeof(logbuf));
  533. #else
  534. # if defined(HPUX)
  535.     (void)setvbuf(stderr, NULL, _IOLBF, 0);
  536. # else
  537. #  if !defined(SOL20) && !defined(_WIN32) && !defined(SCOUNIX) && !defined __EMX__
  538.     (void)setlinebuf(stderr);
  539. #  endif
  540. # endif
  541. #endif
  542. #ifndef _WIN32
  543.     for (fd = 3; fd < MAXCONNECTIONS; fd++)
  544.         {
  545.         (void)close(fd);
  546.         local[fd] = NULL;
  547.         }
  548.     local[1] = NULL;
  549.     (void)close(1);
  550.  
  551.     if (bootopt & BOOT_TTY)    /* debugging is going to a tty */
  552.         goto init_dgram;
  553.     if (!(bootopt & BOOT_DEBUG))
  554.         (void)close(2);
  555.  
  556.     if (((bootopt & BOOT_CONSOLE) || isatty(0)) &&
  557.         !(bootopt & (BOOT_INETD|BOOT_OPER)))
  558.         {
  559.         if (fork())
  560.             exit(0);
  561. #ifdef TIOCNOTTY
  562.         if ((fd = open("/dev/tty", O_RDWR)) >= 0)
  563.             {
  564.             (void)ioctl(fd, TIOCNOTTY, (char *)NULL);
  565.             (void)close(fd);
  566.             }
  567. #endif
  568. #if defined(HPUX) || defined(SOL20) || defined(DYNIXPTX) || \
  569.     defined(_POSIX_SOURCE) || defined(SVR4) || defined(SGI) \
  570.     || defined(SCOUNIX)
  571.         (void)setsid();
  572. #else
  573.     #ifndef __EMX__
  574.         (void)setpgrp(0, (int)getpid());
  575.     #endif
  576. #endif
  577.         (void)close(0);    /* fd 0 opened by inetd */
  578.         local[0] = NULL;
  579.         }
  580. init_dgram:
  581. #endif /*_WIN32*/
  582.     resfd = init_resolver(0x1f);
  583.  
  584.     return;
  585. }
  586.  
  587. void    write_pidfile()
  588. {
  589. #ifdef IRCD_PIDFILE
  590.     int fd;
  591.     char buff[20];
  592.     if ((fd = open(IRCD_PIDFILE, O_CREAT|O_WRONLY, 0600))>=0)
  593.         {
  594.         bzero(buff, sizeof(buff));
  595.         (void)sprintf(buff,"%5d\n", (int)getpid());
  596.         if (write(fd, buff, strlen(buff)) == -1)
  597.             Debug((DEBUG_NOTICE,"Error writing to pid file %s",
  598.                   IRCD_PIDFILE));
  599.         (void)close(fd);
  600.         return;
  601.         }
  602. #ifdef    DEBUGMODE
  603.     else
  604.         Debug((DEBUG_NOTICE,"Error opening pid file %s",
  605.             IRCD_PIDFILE));
  606. #endif
  607. #endif
  608. }
  609.         
  610. /*
  611.  * Initialize the various name strings used to store hostnames. This is set
  612.  * from either the server's sockhost (if client fd is a tty or localhost)
  613.  * or from the ip# converted into a string. 0 = success, -1 = fail.
  614.  */
  615. static    int    check_init(cptr, sockn)
  616. Reg1    aClient    *cptr;
  617. Reg2    char    *sockn;
  618. {
  619.     struct    sockaddr_in sk;
  620.     int    len = sizeof(struct sockaddr_in);
  621.  
  622. #ifdef    UNIXPORT
  623.     if (IsUnixSocket(cptr))
  624.         {
  625.         strncpyzt(sockn, cptr->acpt->sockhost, HOSTLEN+1);
  626.         get_sockhost(cptr, sockn);
  627.         return 0;
  628.         }
  629. #endif
  630.  
  631.     /* If descriptor is a tty, special checking... */
  632. #ifndef _WIN32
  633.     if (isatty(cptr->fd))
  634. #else
  635.     if (0)
  636. #endif
  637.         {
  638.         strncpyzt(sockn, me.sockhost, HOSTLEN);
  639.         bzero((char *)&sk, sizeof(struct sockaddr_in));
  640.         }
  641.     else if (getpeername(cptr->fd, (struct sockaddr *)&sk, &len) == -1)
  642.         {
  643.         report_error("connect failure: %s %s", cptr);
  644.         return -1;
  645.         }
  646.     (void)strcpy(sockn, (char *)inetntoa((char *)&sk.sin_addr));
  647.     if (inet_netof(sk.sin_addr) == IN_LOOPBACKNET)
  648.         {
  649.         cptr->hostp = NULL;
  650.         strncpyzt(sockn, me.sockhost, HOSTLEN);
  651.         }
  652.     bcopy((char *)&sk.sin_addr, (char *)&cptr->ip,
  653.         sizeof(struct in_addr));
  654. #ifdef TESTNET
  655.     cptr->port = (int)ntohs(sk.sin_port) - 10000;
  656. #else
  657.     cptr->port = (int)ntohs(sk.sin_port);
  658. #endif
  659.  
  660.     return 0;
  661. }
  662.  
  663. /*
  664.  * Ordinary client access check. Look for conf lines which have the same
  665.  * status as the flags passed.
  666.  *  0 = Success
  667.  * -1 = Access denied
  668.  * -2 = Bad socket.
  669.  */
  670. int    check_client(cptr)
  671. Reg1    aClient    *cptr;
  672. {
  673.     static    char    sockname[HOSTLEN+1];
  674.     Reg2    struct    hostent *hp = NULL;
  675.     Reg3    int    i;
  676.  
  677.     ClearAccess(cptr);
  678.     Debug((DEBUG_DNS, "ch_cl: check access for %s[%s]",
  679.         cptr->name, inetntoa((char *)&cptr->ip)));
  680.  
  681.     if (check_init(cptr, sockname))
  682.         return -2;
  683.  
  684.     if (!IsUnixSocket(cptr))
  685.         hp = cptr->hostp;
  686.     /*
  687.      * Verify that the host to ip mapping is correct both ways and that
  688.      * the ip#(s) for the socket is listed for the host.
  689.      */
  690.     if (hp)
  691.         {
  692.         for (i = 0; hp->h_addr_list[i]; i++)
  693.             if (!bcmp(hp->h_addr_list[i], (char *)&cptr->ip,
  694.                   sizeof(struct in_addr)))
  695.                 break;
  696.         if (!hp->h_addr_list[i])
  697.             {
  698.             sendto_ops("IP# Mismatch: %s != %s[%08x]",
  699.                    inetntoa((char *)&cptr->ip), hp->h_name,
  700.                    *((unsigned long *)hp->h_addr));
  701.             hp = NULL;
  702.             }
  703.         }
  704.  
  705.     if ((i = attach_Iline(cptr, hp, sockname)))
  706.         {
  707.         Debug((DEBUG_DNS,"ch_cl: access denied: %s[%s]",
  708.             cptr->name, sockname));
  709.         return i;
  710.         }
  711.  
  712.     Debug((DEBUG_DNS, "ch_cl: access ok: %s[%s]",
  713.         cptr->name, sockname));
  714.  
  715.     if (inet_netof(cptr->ip) == IN_LOOPBACKNET || IsUnixSocket(cptr) ||
  716.         inet_netof(cptr->ip) == inet_netof(mysk.sin_addr))
  717.         {
  718.         ircstp->is_loc++;
  719.         cptr->flags |= FLAGS_LOCAL;
  720.         }
  721.     return 0;
  722. }
  723.  
  724. #define    CFLAG    CONF_CONNECT_SERVER
  725. #define    NFLAG    CONF_NOCONNECT_SERVER
  726. /*
  727.  * check_server_init(), check_server()
  728.  *    check access for a server given its name (passed in cptr struct).
  729.  *    Must check for all C/N lines which have a name which matches the
  730.  *    name given and a host which matches. A host alias which is the
  731.  *    same as the server name is also acceptable in the host field of a
  732.  *    C/N line.
  733.  *  0 = Success
  734.  * -1 = Access denied
  735.  * -2 = Bad socket.
  736.  */
  737. int    check_server_init(aClient *cptr)
  738. /*aClient    *cptr;*/
  739. {
  740.     Reg1    char    *name;
  741.     Reg2    aConfItem *c_conf = NULL, *n_conf = NULL;
  742.     struct    hostent    *hp = NULL;
  743.     Link    *lp;
  744.  
  745.     name = cptr->name;
  746.     Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
  747.         name, cptr->sockhost));
  748.  
  749.     if (IsUnknown(cptr) && !attach_confs(cptr, name, CFLAG|NFLAG))
  750.         {
  751.         Debug((DEBUG_DNS,"No C/N lines for %s", name));
  752.         return -1;
  753.         }
  754.     lp = cptr->confs;
  755.     /*
  756.      * We initiated this connection so the client should have a C and N
  757.      * line already attached after passing through the connec_server()
  758.      * function earlier.
  759.      */
  760.     if (IsConnecting(cptr) || IsHandshake(cptr))
  761.         {
  762.         c_conf = find_conf(lp, name, CFLAG);
  763.         n_conf = find_conf(lp, name, NFLAG);
  764.         if (!c_conf || !n_conf)
  765.             {
  766.             sendto_ops("Connecting Error: %s[%s]", name,
  767.                    cptr->sockhost);
  768.             det_confs_butmask(cptr, 0);
  769.             return -1;
  770.             }
  771.         }
  772. #if defined UNIXPORT && !defined __EMX__
  773.     if (IsUnixSocket(cptr))
  774.         {
  775.         if (!c_conf)
  776.             c_conf = find_conf(lp, name, CFLAG);
  777.         if (!n_conf)
  778.             n_conf = find_conf(lp, name, NFLAG);
  779.         }
  780. #endif
  781.  
  782.     /*
  783.     ** If the servername is a hostname, either an alias (CNAME) or
  784.     ** real name, then check with it as the host. Use gethostbyname()
  785.     ** to check for servername as hostname.
  786.     */
  787. /*    if (!IsUnixSocket(cptr) && !cptr->hostp)*/
  788.     if (!cptr->hostp)
  789.         {
  790.         Reg1    aConfItem *aconf;
  791.  
  792.         aconf = count_cnlines(lp);
  793.         if (aconf)
  794.             {
  795.             Reg1    char    *s;
  796.             Link    lin;
  797.  
  798.             /*
  799.             ** Do a lookup for the CONF line *only* and not
  800.             ** the server connection else we get stuck in a
  801.             ** nasty state since it takes a SERVER message to
  802.             ** get us here and we cant interrupt that very
  803.             ** well.
  804.             */
  805.             ClearAccess(cptr);
  806.             lin.value.aconf = aconf;
  807.             lin.flags = ASYNC_CONF;
  808.             nextdnscheck = 1;
  809.             if ((s = index(aconf->host, '@')))
  810.                 s++;
  811.             else
  812.                 s = aconf->host;
  813.             Debug((DEBUG_DNS,"sv_ci:cache lookup (%s)",s));
  814.             hp = gethost_byname(s, &lin);
  815.             }
  816.         }
  817.     return check_server(cptr, hp, c_conf, n_conf, 0);
  818. }
  819.  
  820. int    check_server(cptr, hp, c_conf, n_conf, estab)
  821. aClient    *cptr;
  822. Reg1    aConfItem    *n_conf, *c_conf;
  823. Reg2    struct    hostent    *hp;
  824. int    estab;
  825. {
  826.     Reg3    char    *name;
  827.     char    abuff[HOSTLEN+USERLEN+2];
  828.     char    sockname[HOSTLEN+1], fullname[HOSTLEN+1];
  829.     Link    *lp = cptr->confs;
  830.     int    i;
  831.  
  832.     ClearAccess(cptr);
  833.     if (check_init(cptr, sockname))
  834.         return -2;
  835.  
  836. check_serverback:
  837.     if (hp)
  838.         {
  839.         for (i = 0; hp->h_addr_list[i]; i++)
  840.             if (!bcmp(hp->h_addr_list[i], (char *)&cptr->ip,
  841.                   sizeof(struct in_addr)))
  842.                 break;
  843.         if (!hp->h_addr_list[i])
  844.             {
  845.             sendto_ops("IP# Mismatch: %s != %s[%08x]",
  846.                    inetntoa((char *)&cptr->ip), hp->h_name,
  847.                    *((unsigned long *)hp->h_addr));
  848.             hp = NULL;
  849.             }
  850.         }
  851.     else if (cptr->hostp)
  852.         {
  853.         hp = cptr->hostp;
  854.         goto check_serverback;
  855.         }
  856.  
  857.     if (hp)
  858.         /*
  859.          * if we are missing a C or N line from above, search for
  860.          * it under all known hostnames we have for this ip#.
  861.          */
  862.         for (i=0,name = hp->h_name; name ; name = hp->h_aliases[i++])
  863.             {
  864.             strncpyzt(fullname, name, sizeof(fullname));
  865.             add_local_domain(fullname, HOSTLEN-strlen(fullname));
  866.             Debug((DEBUG_DNS, "sv_cl: gethostbyaddr: %s->%s",
  867.                 sockname, fullname));
  868.             (void)sprintf(abuff, "%s@%s",
  869.                 cptr->username, fullname);
  870.             if (!c_conf)
  871.                 c_conf = find_conf_host(lp, abuff, CFLAG);
  872.             if (!n_conf)
  873.                 n_conf = find_conf_host(lp, abuff, NFLAG);
  874.             if (c_conf && n_conf)
  875.                 {
  876.                 get_sockhost(cptr, fullname);
  877.                 break;
  878.                 }
  879.             }
  880.     name = cptr->name;
  881.  
  882.     /*
  883.      * Check for C and N lines with the hostname portion the ip number
  884.      * of the host the server runs on. This also checks the case where
  885.      * there is a server connecting from 'localhost'.
  886.      */
  887.     if (IsUnknown(cptr) && (!c_conf || !n_conf))
  888.         {
  889.         (void)sprintf(abuff, "%s@%s", cptr->username, sockname);
  890.         if (!c_conf)
  891.             c_conf = find_conf_host(lp, abuff, CFLAG);
  892.         if (!n_conf)
  893.             n_conf = find_conf_host(lp, abuff, NFLAG);
  894.         }
  895.     /*
  896.      * Attach by IP# only if all other checks have failed.
  897.      * It is quite possible to get here with the strange things that can
  898.      * happen when using DNS in the way the irc server does. -avalon
  899.      */
  900.     if (!hp)
  901.         {
  902.         if (!c_conf)
  903.             c_conf = find_conf_ip(lp, (char *)&cptr->ip,
  904.                           cptr->username, CFLAG);
  905.         if (!n_conf)
  906.             n_conf = find_conf_ip(lp, (char *)&cptr->ip,
  907.                           cptr->username, NFLAG);
  908.         }
  909.     else
  910.         for (i = 0; hp->h_addr_list[i]; i++)
  911.             {
  912.             if (!c_conf)
  913.                 c_conf = find_conf_ip(lp, hp->h_addr_list[i],
  914.                               cptr->username, CFLAG);
  915.             if (!n_conf)
  916.                 n_conf = find_conf_ip(lp, hp->h_addr_list[i],
  917.                               cptr->username, NFLAG);
  918.             }
  919.     /*
  920.      * detach all conf lines that got attached by attach_confs()
  921.      */
  922.     det_confs_butmask(cptr, 0);
  923.     /*
  924.      * if no C or no N lines, then deny access
  925.      */
  926.     if (!c_conf || !n_conf)
  927.         {
  928.         get_sockhost(cptr, sockname);
  929.         Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s] c %x n %x",
  930.             name, cptr->username, cptr->sockhost,
  931.             c_conf, n_conf));
  932.         return -1;
  933.         }
  934.     /*
  935.      * attach the C and N lines to the client structure for later use.
  936.      */
  937.     (void)attach_conf(cptr, n_conf);
  938.     (void)attach_conf(cptr, c_conf);
  939.     (void)attach_confs(cptr, name, CONF_HUB|CONF_LEAF|CONF_UWORLD);
  940.  
  941.     if ((c_conf->ipnum.s_addr == -1) && !IsUnixSocket(cptr))
  942.         bcopy((char *)&cptr->ip, (char *)&c_conf->ipnum,
  943.             sizeof(struct in_addr));
  944.     if (!IsUnixSocket(cptr))
  945.         get_sockhost(cptr, c_conf->host);
  946.  
  947.     Debug((DEBUG_DNS,"sv_cl: access ok: %s[%s]",
  948.         name, cptr->sockhost));
  949.     if (estab)
  950.         return m_server_estab(cptr);
  951.     return 0;
  952. }
  953. #undef    CFLAG
  954. #undef    NFLAG
  955.  
  956. /*
  957. ** completed_connection
  958. **    Complete non-blocking connect()-sequence. Check access and
  959. **    terminate connection, if trouble detected.
  960. **
  961. **    Return    TRUE, if successfully completed
  962. **        FALSE, if failed and ClientExit
  963. */
  964. static    int completed_connection(cptr)
  965. aClient    *cptr;
  966. {
  967.     aConfItem *aconf;
  968.  
  969.     SetHandshake(cptr);
  970.     
  971.     aconf = find_conf(cptr->confs, cptr->name, CONF_CONNECT_SERVER);
  972.     if (!aconf)
  973.         {
  974.         sendto_ops("Lost C-Line for %s", get_client_name(cptr,FALSE));
  975.         return -1;
  976.         }
  977.     if (!BadPtr(aconf->passwd))
  978.         sendto_one(cptr, "PASS :%s", aconf->passwd);
  979.  
  980.     aconf = find_conf(cptr->confs, cptr->name, CONF_NOCONNECT_SERVER);
  981.     if (!aconf)
  982.         {
  983.         sendto_ops("Lost N-Line for %s", get_client_name(cptr,FALSE));
  984.         return -1;
  985.         }
  986.     sendto_one(cptr, "SERVER %s 1 :%s",
  987.            my_name_for_link(me.name, aconf), me.info);
  988.     if (!IsDead(cptr))
  989.         start_auth(cptr);
  990.  
  991.     return (IsDead(cptr)) ? -1 : 0;
  992. }
  993.  
  994. /*
  995. ** close_connection
  996. **    Close the physical connection. This function must make
  997. **    MyConnect(cptr) == FALSE, and set cptr->from == NULL.
  998. */
  999. void    close_connection(cptr)
  1000. aClient *cptr;
  1001. {
  1002.         Reg1    aConfItem *aconf;
  1003.     Reg2    int    i,j;
  1004.     int    empty = cptr->fd;
  1005.  
  1006.     if (IsServer(cptr))
  1007.         {
  1008.         ircstp->is_sv++;
  1009.         ircstp->is_sbs += cptr->sendB;
  1010.         ircstp->is_sbr += cptr->receiveB;
  1011.         ircstp->is_sks += cptr->sendK;
  1012.         ircstp->is_skr += cptr->receiveK;
  1013.         ircstp->is_sti += time(NULL) - cptr->firsttime;
  1014.         if (ircstp->is_sbs > 1023)
  1015.             {
  1016.             ircstp->is_sks += (ircstp->is_sbs >> 10);
  1017.             ircstp->is_sbs &= 0x3ff;
  1018.             }
  1019.         if (ircstp->is_sbr > 1023)
  1020.             {
  1021.             ircstp->is_skr += (ircstp->is_sbr >> 10);
  1022.             ircstp->is_sbr &= 0x3ff;
  1023.             }
  1024.         }
  1025.     else if (IsClient(cptr))
  1026.         {
  1027.         ircstp->is_cl++;
  1028.         ircstp->is_cbs += cptr->sendB;
  1029.         ircstp->is_cbr += cptr->receiveB;
  1030.         ircstp->is_cks += cptr->sendK;
  1031.         ircstp->is_ckr += cptr->receiveK;
  1032.         ircstp->is_cti += time(NULL) - cptr->firsttime;
  1033.         if (ircstp->is_cbs > 1023)
  1034.             {
  1035.             ircstp->is_cks += (ircstp->is_cbs >> 10);
  1036.             ircstp->is_cbs &= 0x3ff;
  1037.             }
  1038.         if (ircstp->is_cbr > 1023)
  1039.             {
  1040.             ircstp->is_ckr += (ircstp->is_cbr >> 10);
  1041.             ircstp->is_cbr &= 0x3ff;
  1042.             }
  1043.         }
  1044.     else
  1045.         ircstp->is_ni++;
  1046.  
  1047.     /*
  1048.      * remove outstanding DNS queries.
  1049.      */
  1050.     del_queries((char *)cptr);
  1051.     /*
  1052.      * If the connection has been up for a long amount of time, schedule
  1053.      * a 'quick' reconnect, else reset the next-connect cycle.
  1054.      *
  1055.      * Now just hold on a minute.  We're currently doing this when a
  1056.      * CLIENT exits too?  I don't think so!  If its not a server, or
  1057.      * the SQUIT flag has been set, then we don't schedule a fast
  1058.      * reconnect.  Pisses off too many opers. :-)  -Cabal95
  1059.      */
  1060.     if (IsServer(cptr) && !(cptr->flags & FLAGS_SQUIT) &&
  1061.         (aconf = find_conf_exact(cptr->name, cptr->username,
  1062.                     cptr->sockhost, CONF_CONNECT_SERVER)))
  1063.         {
  1064.         /*
  1065.          * Reschedule a faster reconnect, if this was a automaticly
  1066.          * connected configuration entry. (Note that if we have had
  1067.          * a rehash in between, the status has been changed to
  1068.          * CONF_ILLEGAL). But only do this if it was a "good" link.
  1069.          */
  1070.         aconf->hold = time(NULL);
  1071.         aconf->hold += (aconf->hold - cptr->since > HANGONGOODLINK) ?
  1072.                 HANGONRETRYDELAY : ConfConFreq(aconf);
  1073.         if (nextconnect > aconf->hold)
  1074.             nextconnect = aconf->hold;
  1075.         }
  1076.  
  1077.     if (cptr->authfd >= 0)
  1078. #ifndef _WIN32
  1079.         (void)close(cptr->authfd);
  1080. #else
  1081.         (void)closesocket(cptr->authfd);
  1082. #endif
  1083.  
  1084.     if (cptr->fd >= 0)
  1085.         {
  1086.         flush_connections(cptr->fd);
  1087.         local[cptr->fd] = NULL;
  1088. #ifndef _WIN32
  1089.         (void)close(cptr->fd);
  1090. #else
  1091.         (void)closesocket(cptr->fd);
  1092. #endif
  1093.         cptr->fd = -2;
  1094.         DBufClear(&cptr->sendQ);
  1095.         DBufClear(&cptr->recvQ);
  1096.         bzero(cptr->passwd, sizeof(cptr->passwd));
  1097.         /*
  1098.          * clean up extra sockets from P-lines which have been
  1099.          * discarded.
  1100.          */
  1101.         if (cptr->acpt != &me && cptr->acpt != cptr)
  1102.             {
  1103.             aconf = cptr->acpt->confs->value.aconf;
  1104.             if (aconf->clients > 0)
  1105.                 aconf->clients--;
  1106.             if (!aconf->clients && IsIllegal(aconf))
  1107.                 close_connection(cptr->acpt);
  1108.             }
  1109.         }
  1110.     for (; highest_fd > 0; highest_fd--)
  1111.         if (local[highest_fd])
  1112.             break;
  1113.  
  1114.     det_confs_butmask(cptr, 0);
  1115.     cptr->from = NULL; /* ...this should catch them! >:) --msa */
  1116.  
  1117.     /*
  1118.      * fd remap to keep local[i] filled at the bottom.
  1119.      */
  1120.     if (empty > 0)
  1121.         if ((j = highest_fd) > (i = empty) &&
  1122.             (local[j]->status != STAT_LOG))
  1123.             {
  1124.             if (dup2(j,i) == -1)
  1125.                 return;
  1126.             local[i] = local[j];
  1127.             local[i]->fd = i;
  1128.             local[j] = NULL;
  1129. #ifndef _WIN32
  1130.             (void)close(j);
  1131. #else
  1132.             (void)closesocket(j);
  1133. #endif
  1134.             while (!local[highest_fd])
  1135.                 highest_fd--;
  1136.             }
  1137.     return;
  1138. }
  1139.  
  1140. /*
  1141. ** set_sock_opts
  1142. */
  1143. static    void    set_sock_opts(fd, cptr)
  1144. int    fd;
  1145. aClient    *cptr;
  1146. {
  1147.     int    opt;
  1148. #ifdef SO_REUSEADDR
  1149.     opt = 1;
  1150.     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (OPT_TYPE *)&opt, sizeof(opt)) < 0)
  1151.         report_error("setsockopt(SO_REUSEADDR) %s:%s", cptr);
  1152. #endif
  1153. #if  defined(SO_DEBUG) && defined(DEBUGMODE) && 0
  1154. /* Solaris with SO_DEBUG writes to syslog by default */
  1155. #if !defined(SOL20) || defined(USE_SYSLOG)
  1156.     opt = 1;
  1157.     if (setsockopt(fd, SOL_SOCKET, SO_DEBUG, (OPT_TYPE *)&opt, sizeof(opt)) < 0)
  1158.         report_error("setsockopt(SO_DEBUG) %s:%s", cptr);
  1159. #endif /* SOL20 */
  1160. #endif
  1161. #if defined(SO_USELOOPBACK) && !defined(_WIN32)
  1162.     opt = 1;
  1163.     if (setsockopt(fd, SOL_SOCKET, SO_USELOOPBACK, (OPT_TYPE *)&opt, sizeof(opt)) < 0)
  1164.         report_error("setsockopt(SO_USELOOPBACK) %s:%s", cptr);
  1165. #endif
  1166. #ifdef    SO_RCVBUF
  1167.     opt = 8192;
  1168.     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (OPT_TYPE *)&opt, sizeof(opt)) < 0)
  1169.         report_error("setsockopt(SO_RCVBUF) %s:%s", cptr);
  1170. #endif
  1171. #ifdef    SO_SNDBUF
  1172. # ifdef    _SEQUENT_
  1173. /* seems that Sequent freezes up if the receving buffer is a different size
  1174.  * to the sending buffer (maybe a tcp window problem too).
  1175.  */
  1176.     opt = 8192;
  1177. # else
  1178.     opt = 8192;
  1179. # endif
  1180.     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (OPT_TYPE *)&opt, sizeof(opt)) < 0)
  1181.         report_error("setsockopt(SO_SNDBUF) %s:%s", cptr);
  1182. #endif
  1183. #if defined(IP_OPTIONS) && defined(IPPROTO_IP) && !defined(_WIN32)
  1184.     {
  1185.     char    *s = readbuf, *t = readbuf + sizeof(readbuf) / 2;
  1186.  
  1187.     opt = sizeof(readbuf) / 8;
  1188.     if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS, (OPT_TYPE *)t, &opt) < 0)
  1189.         report_error("getsockopt(IP_OPTIONS) %s:%s", cptr);
  1190.     else if (opt > 0 && opt != sizeof(readbuf) / 8)
  1191.         {
  1192.         for (*readbuf = '\0'; opt > 0; opt--, s+= 3)
  1193.             (void)sprintf(s, "%02.2x:", *t++);
  1194.         *s = '\0';
  1195.         sendto_ops("Connection %s using IP opts: (%s)",
  1196.                get_client_name(cptr, TRUE), readbuf);
  1197.         }
  1198.     if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, (OPT_TYPE *)NULL, 0) < 0)
  1199.         report_error("setsockopt(IP_OPTIONS) %s:%s", cptr);
  1200.     }
  1201. #endif
  1202. }
  1203.  
  1204.  
  1205. int    get_sockerr(cptr)
  1206. aClient    *cptr;
  1207. {
  1208. #ifndef _WIN32
  1209.     int errtmp = errno, err = 0, len = sizeof(err);
  1210. #else
  1211.     int errtmp = WSAGetLastError(), err = 0, len = sizeof(err);
  1212. #endif
  1213. #ifdef    SO_ERROR
  1214.     if (cptr->fd >= 0)
  1215.         if (!getsockopt(cptr->fd, SOL_SOCKET, SO_ERROR, (OPT_TYPE *)&err, &len))
  1216.             if (err)
  1217.                 errtmp = err;
  1218. #endif
  1219.     return errtmp;
  1220. }
  1221.  
  1222. /*
  1223. ** set_non_blocking
  1224. **    Set the client connection into non-blocking mode. If your
  1225. **    system doesn't support this, you can make this a dummy
  1226. **    function (and get all the old problems that plagued the
  1227. **    blocking version of IRC--not a problem if you are a
  1228. **    lightly loaded node...)
  1229. */
  1230. void    set_non_blocking(fd, cptr)
  1231. int    fd;
  1232. aClient *cptr;
  1233. {
  1234.     int    res, nonb = 0;
  1235.  
  1236.     /*
  1237.     ** NOTE: consult ALL your relevant manual pages *BEFORE* changing
  1238.     **     these ioctl's.  There are quite a few variations on them,
  1239.     **     as can be seen by the PCS one.  They are *NOT* all the same.
  1240.     **     Heed this well. - Avalon.
  1241.     */
  1242. #ifdef    NBLOCK_POSIX
  1243.     nonb |= O_NONBLOCK;
  1244. #endif
  1245. #ifdef    NBLOCK_BSD
  1246.     nonb |= O_NDELAY;
  1247. #endif
  1248. #ifdef    NBLOCK_SYSV
  1249.     /* This portion of code might also apply to NeXT.  -LynX */
  1250.     res = 1;
  1251.  
  1252.     if (ioctl (fd, FIONBIO, &res) < 0)
  1253.         report_error("ioctl(fd,FIONBIO) failed for %s:%s", cptr);
  1254. #else
  1255. # if !defined(_WIN32)
  1256.     if ((res = fcntl(fd, F_GETFL, 0)) == -1)
  1257.         report_error("fcntl(fd, F_GETFL) failed for %s:%s",cptr);
  1258.     else if (fcntl(fd, F_SETFL, res | nonb) == -1)
  1259.         report_error("fcntl(fd, F_SETL, nonb) failed for %s:%s",cptr);
  1260. # else
  1261.     nonb=1;
  1262.     if (ioctlsocket(fd, FIONBIO, &nonb) < 0)
  1263.         report_error("ioctlsocket(fd,FIONBIO) failed for %s:%s", cptr);
  1264. # endif
  1265. #endif
  1266.     return;
  1267. }
  1268.  
  1269. /*
  1270.  * Creates a client which has just connected to us on the given fd.
  1271.  * The sockhost field is initialized with the ip# of the host.
  1272.  * The client is added to the linked list of clients but isnt added to any
  1273.  * hash tables yuet since it doesnt have a name.
  1274.  */
  1275. aClient    *add_connection(cptr, fd)
  1276. aClient    *cptr;
  1277. int    fd;
  1278. {
  1279.     Link    lin;
  1280.     aClient *acptr;
  1281.     aConfItem *aconf = NULL;
  1282.     char    *message[20];
  1283.     acptr = make_client(NULL, &me);
  1284.  
  1285.     if (cptr != &me)
  1286.         aconf = cptr->confs->value.aconf;
  1287.     /* Removed preliminary access check. Full check is performed in
  1288.      * m_server and m_user instead. Also connection time out help to
  1289.      * get rid of unwanted connections.
  1290.      */
  1291. #ifndef _WIN32
  1292.     if (isatty(fd)) /* If descriptor is a tty, special checking... */
  1293. #else
  1294.     if (0)
  1295. #endif
  1296.         get_sockhost(acptr, cptr->sockhost);
  1297.     else
  1298.         {
  1299.         Reg1    char    *s, *t;
  1300.         struct    sockaddr_in addr;
  1301.         int    len = sizeof(struct sockaddr_in);
  1302.  
  1303.         if (getpeername(fd, (struct sockaddr *) &addr, &len) == -1)
  1304.             {
  1305.             report_error("Failed in connecting to %s :%s", cptr);
  1306. add_con_refuse:
  1307.             ircstp->is_ref++;
  1308.             acptr->fd = -2;
  1309.             free_client(acptr);
  1310. #ifndef _WIN32
  1311.             (void)close(fd);
  1312. #else
  1313.             (void)closesocket(fd);
  1314. #endif
  1315.             return NULL;
  1316.             }
  1317.         /* don't want to add "Failed in connecting to" here.. */
  1318.         if (aconf && IsIllegal(aconf))
  1319.             goto add_con_refuse;
  1320.         /* Copy ascii address to 'sockhost' just in case. Then we
  1321.          * have something valid to put into error messages...
  1322.          */
  1323.         get_sockhost(acptr, (char *)inetntoa((char *)&addr.sin_addr));
  1324.         bcopy ((char *)&addr.sin_addr, (char *)&acptr->ip,
  1325.             sizeof(struct in_addr));
  1326.         /* Check for zaps -- Barubary */
  1327.         if (find_zap(acptr, 0))
  1328.         {
  1329.             set_non_blocking(fd, acptr);
  1330.             set_sock_opts(fd, acptr);
  1331.             send(fd, zlinebuf, strlen(zlinebuf), 0);
  1332.             goto add_con_refuse;
  1333.         }
  1334. #ifdef TESTNET
  1335.         acptr->port = ntohs(addr.sin_port) - 10000;
  1336. #else
  1337.         acptr->port = ntohs(addr.sin_port);
  1338. #endif
  1339. #if 0
  1340.         /*
  1341.          * Some genious along the lines of ircd took out the code
  1342.          * where ircd loads the IP mask from the P:Lines, so this
  1343.          * is useless untill that's added back. :)
  1344.          */
  1345.         /*
  1346.          * Check that this socket (client) is allowed to accept
  1347.          * connections from this IP#.
  1348.          */
  1349.         for (s = (char *)&cptr->ip, t = (char *)&acptr->ip, len = 4;
  1350.              len > 0; len--, s++, t++)
  1351.             {
  1352.             if (!*s)
  1353.                 continue;
  1354.             if (*s != *t)
  1355.                 break;
  1356.             }
  1357.  
  1358.         if (len)
  1359.             goto add_con_refuse;
  1360. #endif
  1361. #ifdef SHOWCONNECTINFO
  1362.         write(fd, REPORT_DO_DNS, R_do_dns);
  1363. #endif
  1364.         lin.flags = ASYNC_CLIENT;
  1365.         lin.value.cptr = acptr;
  1366.         Debug((DEBUG_DNS, "lookup %s",
  1367.             inetntoa((char *)&addr.sin_addr)));
  1368.         acptr->hostp = gethost_byaddr((char *)&acptr->ip, &lin);
  1369.         if (!acptr->hostp)
  1370.             SetDNS(acptr);
  1371. #ifdef SHOWCONNECTINFO
  1372.         else
  1373.             write(fd, REPORT_FIN_DNSC, R_fin_dnsc);
  1374. #endif
  1375.         nextdnscheck = 1;
  1376.         }
  1377.  
  1378.     if (aconf)
  1379.         aconf->clients++;
  1380.     acptr->fd = fd;
  1381.     if (fd > highest_fd)
  1382.         highest_fd = fd;
  1383.     local[fd] = acptr;
  1384.     acptr->acpt = cptr;
  1385.     add_client_to_list(acptr);
  1386.     set_non_blocking(acptr->fd, acptr);
  1387.     set_sock_opts(acptr->fd, acptr);
  1388.     start_auth(acptr);
  1389.     return acptr;
  1390. }
  1391.  
  1392. #ifdef    UNIXPORT
  1393. static    void    add_unixconnection(cptr, fd)
  1394. aClient    *cptr;
  1395. int    fd;
  1396. {
  1397.     aClient *acptr;
  1398.     aConfItem *aconf = NULL;
  1399.  
  1400.     acptr = make_client(NULL, NULL);
  1401.  
  1402.     /* Copy ascii address to 'sockhost' just in case. Then we
  1403.      * have something valid to put into error messages...
  1404.      */
  1405.     get_sockhost(acptr, me.sockhost);
  1406.     if (cptr != &me)
  1407.         aconf = cptr->confs->value.aconf;
  1408.     if (aconf)
  1409.         {
  1410.         if (IsIllegal(aconf))
  1411.             {
  1412.             ircstp->is_ref++;
  1413.             acptr->fd = -2;
  1414.             free_client(acptr);
  1415.             (void)close(fd);
  1416.             return;
  1417.             }
  1418.         else
  1419.             aconf->clients++;
  1420.         }
  1421.     acptr->fd = fd;
  1422.     if (fd > highest_fd)
  1423.         highest_fd = fd;
  1424.     local[fd] = acptr;
  1425.     acptr->acpt = cptr;
  1426.     SetUnixSock(acptr);
  1427.     bcopy((char *)&me.ip, (char *)&acptr->ip, sizeof(struct in_addr));
  1428.  
  1429.     add_client_to_list(acptr);
  1430.     set_non_blocking(acptr->fd, acptr);
  1431.     set_sock_opts(acptr->fd, acptr);
  1432.     SetAccess(acptr);
  1433.     return;
  1434. }
  1435. #endif
  1436.  
  1437. /*
  1438. ** read_packet
  1439. **
  1440. ** Read a 'packet' of data from a connection and process it.  Read in 8k
  1441. ** chunks to give a better performance rating (for server connections).
  1442. ** Do some tricky stuff for client connections to make sure they don't do
  1443. ** any flooding >:-) -avalon
  1444. */
  1445. static    int    read_packet(cptr, rfd)
  1446. Reg1    aClient *cptr;
  1447. fd_set    *rfd;
  1448. {
  1449.     Reg1    int    dolen = 0, length = 0, done;
  1450.     time_t    now = time(NULL);
  1451.  
  1452.     if (FD_ISSET(cptr->fd, rfd) &&
  1453.         !(IsPerson(cptr) && DBufLength(&cptr->recvQ) > 6090))
  1454.         {
  1455. #ifndef _WIN32
  1456.         errno = 0;
  1457. #else
  1458.         WSASetLastError(0);
  1459. #endif
  1460.         length = recv(cptr->fd, readbuf, sizeof(readbuf), 0);
  1461.  
  1462.         cptr->lasttime = now;
  1463.         if (cptr->lasttime > cptr->since)
  1464.             cptr->since = cptr->lasttime;
  1465.         cptr->flags &= ~(FLAGS_PINGSENT|FLAGS_NONL);
  1466.         /*
  1467.          * If not ready, fake it so it isnt closed
  1468.          */
  1469.         if (length == -1 &&
  1470. #ifndef _WIN32
  1471.             ((errno == EWOULDBLOCK) || (errno == EAGAIN)))
  1472. #else
  1473.             (WSAGetLastError() == WSAEWOULDBLOCK))
  1474. #endif
  1475.             return 1;
  1476.         if (length <= 0)
  1477.             return length;
  1478.         }
  1479.  
  1480.     /*
  1481.     ** For server connections, we process as many as we can without
  1482.     ** worrying about the time of day or anything :)
  1483.     */
  1484.     if (IsServer(cptr) || IsConnecting(cptr) || IsHandshake(cptr) ||
  1485.         IsService(cptr))
  1486.         {
  1487.         if (length > 0)
  1488.             if ((done = dopacket(cptr, readbuf, length)))
  1489.                 return done;
  1490.         }
  1491.     else
  1492.         {
  1493.         /*
  1494.         ** Before we even think of parsing what we just read, stick
  1495.         ** it on the end of the receive queue and do it when its
  1496.         ** turn comes around.
  1497.         */
  1498.         if (dbuf_put(&cptr->recvQ, readbuf, length) < 0)
  1499.             return exit_client(cptr, cptr, cptr, "dbuf_put fail");
  1500.  
  1501.         if (IsPerson(cptr) &&
  1502.             DBufLength(&cptr->recvQ) > CLIENT_FLOOD)
  1503.             {
  1504.             sendto_umode(UMODE_FLOOD|UMODE_OPER,
  1505.                 "*** Flood -- %s!%s@%s (%d) exceeds %d recvQ",
  1506.                 cptr->name[0] ? cptr->name : "*",
  1507.                 cptr->user ? cptr->user->username : "*",
  1508.                 cptr->user ? cptr->user->host : "*",
  1509.                 DBufLength(&cptr->recvQ), CLIENT_FLOOD);
  1510.             return exit_client(cptr, cptr, cptr, "Excess Flood");
  1511.             }
  1512.  
  1513.         while (DBufLength(&cptr->recvQ) && !NoNewLine(cptr) &&
  1514.                ((cptr->status < STAT_UNKNOWN) ||
  1515.             (cptr->since - now < 10)))
  1516.             {
  1517.             /*
  1518.             ** If it has become registered as a Service or Server
  1519.             ** then skip the per-message parsing below.
  1520.             */
  1521.             if (IsService(cptr) || IsServer(cptr))
  1522.                 {
  1523.                 dolen = dbuf_get(&cptr->recvQ, readbuf,
  1524.                          sizeof(readbuf));
  1525.                 if (dolen <= 0)
  1526.                     break;
  1527.                 if ((done = dopacket(cptr, readbuf, dolen)))
  1528.                     return done;
  1529.                 break;
  1530.                 }
  1531.             dolen = dbuf_getmsg(&cptr->recvQ, readbuf,
  1532.                         sizeof(readbuf));
  1533.             /*
  1534.             ** Devious looking...whats it do ? well..if a client
  1535.             ** sends a *long* message without any CR or LF, then
  1536.             ** dbuf_getmsg fails and we pull it out using this
  1537.             ** loop which just gets the next 512 bytes and then
  1538.             ** deletes the rest of the buffer contents.
  1539.             ** -avalon
  1540.             */
  1541.             while (dolen <= 0)
  1542.                 {
  1543.                 if (dolen < 0)
  1544.                     return exit_client(cptr, cptr, cptr,
  1545.                                "dbuf_getmsg fail");
  1546.                 if (DBufLength(&cptr->recvQ) < 510)
  1547.                     {
  1548.                     cptr->flags |= FLAGS_NONL;
  1549.                     break;
  1550.                     }
  1551.                 dolen = dbuf_get(&cptr->recvQ, readbuf, 511);
  1552.                 if (dolen > 0 && DBufLength(&cptr->recvQ))
  1553.                     DBufClear(&cptr->recvQ);
  1554.                 }
  1555.  
  1556.             if (dolen > 0 &&
  1557.                 (dopacket(cptr, readbuf, dolen) == FLUSH_BUFFER))
  1558.                 return FLUSH_BUFFER;
  1559.             }
  1560.         }
  1561.     return 1;
  1562. }
  1563.  
  1564.  
  1565. /*
  1566.  * Check all connections for new connections and input data that is to be
  1567.  * processed. Also check for connections with data queued and whether we can
  1568.  * write it out.
  1569.  */
  1570. int    read_message(delay)
  1571. time_t    delay; /* Don't ever use ZERO here, unless you mean to poll and then
  1572.         * you have to have sleep/wait somewhere else in the code.--msa
  1573.         */
  1574. {
  1575.     Reg1    aClient    *cptr;
  1576.     Reg2    int    nfds;
  1577.     struct    timeval    wait;
  1578. #ifdef    pyr
  1579.     struct    timeval    nowt;
  1580.     u_long    us;
  1581. #endif
  1582. #ifndef _WIN32
  1583.     fd_set    read_set, write_set;
  1584. #else
  1585.     fd_set    read_set, write_set, excpt_set;
  1586. #endif
  1587.     time_t    delay2 = delay, now;
  1588.     u_long    usec = 0;
  1589.     int    res, length, fd, i;
  1590.     int    auth = 0;
  1591.     int    sockerr;
  1592.  
  1593. #ifdef NPATH
  1594.     check_command(&delay, NULL);
  1595. #endif
  1596. #ifdef    pyr
  1597.     (void) gettimeofday(&nowt, NULL);
  1598.     now = nowt.tv_sec;
  1599. #else
  1600.     now = time(NULL);
  1601. #endif
  1602.  
  1603.     for (res = 0;;)
  1604.         {
  1605.         FD_ZERO(&read_set);
  1606.         FD_ZERO(&write_set);
  1607. #ifdef _WIN32
  1608.         FD_ZERO(&excpt_set);
  1609. #endif
  1610.  
  1611.         for (i = highest_fd; i >= 0; i--)
  1612.             {
  1613.             if (!(cptr = local[i]))
  1614.                 continue;
  1615.             if (IsLog(cptr))
  1616.                 continue;
  1617.             if (DoingAuth(cptr))
  1618.                 {
  1619.                 auth++;
  1620.                 Debug((DEBUG_NOTICE,"auth on %x %d", cptr, i));
  1621.                 FD_SET(cptr->authfd, &read_set);
  1622. #ifdef _WIN32
  1623.                 FD_SET(cptr->authfd, &excpt_set);
  1624. #endif
  1625.                 if (cptr->flags & FLAGS_WRAUTH)
  1626.                     FD_SET(cptr->authfd, &write_set);
  1627.                 }
  1628.             if (DoingDNS(cptr) || DoingAuth(cptr))
  1629.                 continue;
  1630.             if (IsMe(cptr) && IsListening(cptr))
  1631.                 {
  1632.                 FD_SET(i, &read_set);
  1633.                 }
  1634.             else if (!IsMe(cptr))
  1635.                 {
  1636.                 if (DBufLength(&cptr->recvQ) && delay2 > 2)
  1637.                     delay2 = 1;
  1638.                 if (DBufLength(&cptr->recvQ) < 4088)
  1639.                     FD_SET(i, &read_set);
  1640.                 }
  1641.  
  1642.             if (DBufLength(&cptr->sendQ) || IsConnecting(cptr) ||
  1643.                 (DoList(cptr) && IsSendable(cptr)))
  1644. #ifndef    pyr
  1645.                 FD_SET(i, &write_set);
  1646. #else
  1647.                 {
  1648.                 if (!IsBlocked(cptr))
  1649.                     FD_SET(i, &write_set);
  1650.                 else
  1651.                     delay2 = 0, usec = 500000;
  1652.                 }
  1653.             if (now - cptr->lw.tv_sec &&
  1654.                 nowt.tv_usec - cptr->lw.tv_usec < 0)
  1655.                 us = 1000000;
  1656.             else
  1657.                 us = 0;
  1658.             us += nowt.tv_usec;
  1659.             if (us - cptr->lw.tv_usec > 500000)
  1660.                 ClearBlocked(cptr);
  1661. #endif
  1662.             }
  1663.  
  1664.         if (udpfd >= 0)
  1665.             FD_SET(udpfd, &read_set);
  1666. #ifndef _WIN32
  1667.         if (resfd >= 0)
  1668.             FD_SET(resfd, &read_set);
  1669. #endif
  1670.  
  1671.         wait.tv_sec = MIN(delay2, delay);
  1672.         wait.tv_usec = usec;
  1673. #ifdef    HPUX
  1674.         nfds = select(FD_SETSIZE, (int *)&read_set, (int *)&write_set,
  1675.                 0, &wait);
  1676. #else
  1677. # ifndef _WIN32
  1678.         nfds = select(FD_SETSIZE, &read_set, &write_set, 0, &wait);
  1679. # else
  1680.         nfds = select(FD_SETSIZE, &read_set, &write_set, &excpt_set, &wait);
  1681. # endif
  1682. #endif
  1683. #ifndef _WIN32
  1684.         if (nfds == -1 && errno == EINTR)
  1685. #else
  1686.         if (nfds == -1 && WSAGetLastError() == WSAEINTR)
  1687. #endif
  1688.             return -1;
  1689.         else if (nfds >= 0)
  1690.             break;
  1691.         report_error("select %s:%s", &me);
  1692.         res++;
  1693.         if (res > 5)
  1694.             restart("too many select errors");
  1695. #ifndef _WIN32
  1696.         sleep(10);
  1697. #else
  1698.         Sleep(10);
  1699. #endif
  1700.         }
  1701.   
  1702.     if (udpfd >= 0 && FD_ISSET(udpfd, &read_set))
  1703.         {
  1704.             polludp();
  1705.             nfds--;
  1706.             FD_CLR(udpfd, &read_set);
  1707.         }
  1708. #ifndef _WIN32
  1709.     if (resfd >= 0 && FD_ISSET(resfd, &read_set))
  1710.         {
  1711.             do_dns_async();
  1712.             nfds--;
  1713.             FD_CLR(resfd, &read_set);
  1714.         }
  1715. #endif
  1716.     /*
  1717.      * Check fd sets for the auth fd's (if set and valid!) first
  1718.      * because these can not be processed using the normal loops below.
  1719.      * -avalon
  1720.      */
  1721.     for (i = highest_fd; (auth > 0) && (i >= 0); i--)
  1722.         {
  1723.         if (!(cptr = local[i]))
  1724.             continue;
  1725.         if (cptr->authfd < 0)
  1726.             continue;
  1727.         auth--;
  1728. #ifdef _WIN32
  1729.         /*
  1730.          * Because of the way windows uses select(), we have to use
  1731.          * the exception FD set to find out when a connection is
  1732.          * refused.  ie Auth ports and /connect's.  -Cabal95
  1733.          */
  1734.         if (FD_ISSET(cptr->authfd, &excpt_set))
  1735.             {
  1736.             int    err, len = sizeof(err);
  1737.  
  1738.             if (getsockopt(cptr->authfd, SOL_SOCKET, SO_ERROR,
  1739.                    (OPT_TYPE *)&err, &len) || err)
  1740.                 {
  1741.                 ircstp->is_abad++;
  1742.                 closesocket(cptr->authfd);
  1743.                 if (cptr->authfd == highest_fd)
  1744.                     while (!local[highest_fd])
  1745.                         highest_fd--;
  1746.                 cptr->authfd = -1;
  1747.                 cptr->flags &= ~(FLAGS_AUTH|FLAGS_WRAUTH);
  1748.                 if (!DoingDNS(cptr))
  1749.                     SetAccess(cptr);
  1750.                 if (nfds > 0)
  1751.                     nfds--;
  1752.                 continue;
  1753.                 }
  1754.             }
  1755. #endif
  1756.         if ((nfds > 0) && FD_ISSET(cptr->authfd, &write_set))
  1757.             {
  1758.             nfds--;
  1759.             send_authports(cptr);
  1760.             }
  1761.         else if ((nfds > 0) && FD_ISSET(cptr->authfd, &read_set))
  1762.             {
  1763.             nfds--;
  1764.             read_authports(cptr);
  1765.             }
  1766.         }
  1767.     for (i = highest_fd; i >= 0; i--)
  1768.         if ((cptr = local[i]) && FD_ISSET(i, &read_set) &&
  1769.             IsListening(cptr))
  1770.             {
  1771.             FD_CLR(i, &read_set);
  1772.             nfds--;
  1773.             cptr->lasttime = time(NULL);
  1774.             /*
  1775.             ** There may be many reasons for error return, but
  1776.             ** in otherwise correctly working environment the
  1777.             ** probable cause is running out of file descriptors
  1778.             ** (EMFILE, ENFILE or others?). The man pages for
  1779.             ** accept don't seem to list these as possible,
  1780.             ** although it's obvious that it may happen here.
  1781.             ** Thus no specific errors are tested at this
  1782.             ** point, just assume that connections cannot
  1783.             ** be accepted until some old is closed first.
  1784.             */
  1785.             if ((fd = accept(i, NULL, NULL)) < 0)
  1786.                 {
  1787.                 report_error("Cannot accept connections %s:%s",
  1788.                         cptr);
  1789.                 break;
  1790.                 }
  1791.             ircstp->is_ac++;
  1792.             if (fd >= MAXCLIENTS)
  1793.                 {
  1794.                 ircstp->is_ref++;
  1795.                 sendto_ops("All connections in use. (%s)",
  1796.                        get_client_name(cptr, TRUE));
  1797.                 (void)send(fd,
  1798.                     "ERROR :All connections in use\r\n",
  1799.                     32, 0);
  1800. #ifndef _WIN32
  1801.                 (void)close(fd);
  1802. #else
  1803.                 (void)closesocket(fd);
  1804. #endif
  1805.                 break;
  1806.                 }
  1807.             /*
  1808.              * Use of add_connection (which never fails :) meLazy
  1809.              */
  1810. #ifdef    UNIXPORT
  1811.             if (IsUnixSocket(cptr))
  1812.                 add_unixconnection(cptr, fd);
  1813.             else
  1814. #endif
  1815.                 (void)add_connection(cptr, fd);
  1816.             nextping = time(NULL);
  1817.             if (!cptr->acpt)
  1818.                 cptr->acpt = &me;
  1819.             }
  1820.  
  1821.     for (i = highest_fd; i >= 0; i--)
  1822.         {
  1823.         if (!(cptr = local[i]) || IsMe(cptr))
  1824.             continue;
  1825.         if (FD_ISSET(i, &write_set))
  1826.             {
  1827.             int    write_err = 0;
  1828.             nfds--;
  1829.             /*
  1830.             ** ...room for writing, empty some queue then...
  1831.             */
  1832.             ClearBlocked(cptr);
  1833.             if (IsConnecting(cptr))
  1834.                   write_err = completed_connection(cptr);
  1835.             if (!write_err) {
  1836.                 if (DoList(cptr) && IsSendable(cptr))
  1837.                     send_list(cptr, 32);
  1838.                 (void)send_queued(cptr);
  1839.             }
  1840.  
  1841.             if (IsDead(cptr) || write_err)
  1842.                 {
  1843. deadsocket:
  1844.                 if (FD_ISSET(i, &read_set))
  1845.                     {
  1846.                     nfds--;
  1847.                     FD_CLR(i, &read_set);
  1848.                     }
  1849.                 (void)exit_client(cptr, cptr, &me,
  1850.                           ((sockerr = get_sockerr(cptr))
  1851.                            ? strerror(sockerr)
  1852.                            : "Client exited"));
  1853.                 continue;
  1854.                 }
  1855.             }
  1856.         length = 1;    /* for fall through case */
  1857.         if (!NoNewLine(cptr) || FD_ISSET(i, &read_set))
  1858.             length = read_packet(cptr, &read_set);
  1859.         if (length > 0)
  1860.             flush_connections(i);
  1861.         if ((length != FLUSH_BUFFER) && IsDead(cptr))
  1862.             goto deadsocket;
  1863.         if (!FD_ISSET(i, &read_set) && length > 0)
  1864.             continue;
  1865.         nfds--;
  1866.         readcalls++;
  1867.         if (length > 0)
  1868.             continue;
  1869.  
  1870.         /*
  1871.         ** ...hmm, with non-blocking sockets we might get
  1872.         ** here from quite valid reasons, although.. why
  1873.         ** would select report "data available" when there
  1874.         ** wasn't... so, this must be an error anyway...  --msa
  1875.         ** actually, EOF occurs when read() returns 0 and
  1876.         ** in due course, select() returns that fd as ready
  1877.         ** for reading even though it ends up being an EOF. -avalon
  1878.         */
  1879. #ifndef _WIN32
  1880.         Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d",
  1881.             i, errno, length));
  1882. #else
  1883.         Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d",
  1884.             i, WSAGetLastError(), length));
  1885. #endif
  1886.  
  1887.         /*
  1888.         ** NOTE: if length == -2 then cptr has already been freed!
  1889.         */
  1890.         if (length != -2 && (IsServer(cptr) || IsHandshake(cptr)))
  1891.             {
  1892.             if (length == 0) {
  1893.                  sendto_ops("Server %s closed the connection",
  1894.                         get_client_name(cptr,FALSE));
  1895.                  sendto_serv_butone(&me,
  1896.                     ":%s GNOTICE :Server %s closed the connection",
  1897.                     me.name, get_client_name(cptr,FALSE));
  1898.             }
  1899.             else
  1900.                  report_error("Lost connection to %s:%s",
  1901.                           cptr);
  1902.             }
  1903.         if (length != FLUSH_BUFFER)
  1904.             (void)exit_client(cptr, cptr, &me,
  1905.                       ((sockerr = get_sockerr(cptr))
  1906.                        ? strerror(sockerr)
  1907.                        : "Client exited"));
  1908.         }
  1909.     return 0;
  1910. }
  1911.  
  1912. /*
  1913.  * connect_server
  1914.  */
  1915. int    connect_server(aconf, by, hp)
  1916. aConfItem *aconf;
  1917. aClient    *by;
  1918. struct    hostent    *hp;
  1919. {
  1920.     Reg1    struct    sockaddr *svp;
  1921.     Reg2    aClient *cptr, *c2ptr;
  1922.     Reg3    char    *s;
  1923.     int    errtmp, len;
  1924.  
  1925.     Debug((DEBUG_NOTICE,"Connect to %s[%s] @%s",
  1926.         aconf->name, aconf->host, inetntoa((char *)&aconf->ipnum)));
  1927.  
  1928.     if ((c2ptr = find_server(aconf->name, NULL)))
  1929.         {
  1930.         sendto_ops("Server %s already present from %s",
  1931.                aconf->name, get_client_name(c2ptr, TRUE));
  1932.         if (by && IsPerson(by) && !MyClient(by))
  1933.           sendto_one(by,
  1934.                              ":%s NOTICE %s :Server %s already present from %s",
  1935.                              me.name, by->name, aconf->name,
  1936.                  get_client_name(c2ptr, TRUE));
  1937.         return -1;
  1938.         }
  1939.  
  1940.     /*
  1941.      * If we dont know the IP# for this host and itis a hostname and
  1942.      * not a ip# string, then try and find the appropriate host record.
  1943.      */
  1944.     if ( ( !aconf->ipnum.s_addr )
  1945. #ifdef UNIXPORT
  1946.         && ( ( aconf->host[2] ) != '/' )  /* needed for Unix domain -- dl*/
  1947. #endif
  1948.             )
  1949.         {
  1950.             Link    lin;
  1951.  
  1952.         lin.flags = ASYNC_CONNECT;
  1953.         lin.value.aconf = aconf;
  1954.         nextdnscheck = 1;
  1955.         s = (char *)index(aconf->host, '@');
  1956.         s++; /* should NEVER be NULL */
  1957.         if ((aconf->ipnum.s_addr = inet_addr(s)) == -1)
  1958.             {
  1959.             aconf->ipnum.s_addr = 0;
  1960.             hp = gethost_byname(s, &lin);
  1961.             Debug((DEBUG_NOTICE, "co_sv: hp %x ac %x na %s ho %s",
  1962.                 hp, aconf, aconf->name, s));
  1963.             if (!hp)
  1964.                 return 0;
  1965.             bcopy(hp->h_addr, (char *)&aconf->ipnum,
  1966.                 sizeof(struct in_addr));
  1967.             }
  1968.         }
  1969.     cptr = make_client(NULL, NULL);
  1970.     cptr->hostp = hp;
  1971.     /*
  1972.      * Copy these in so we have something for error detection.
  1973.      */
  1974.     strncpyzt(cptr->name, aconf->name, sizeof(cptr->name));
  1975.     strncpyzt(cptr->sockhost, aconf->host, HOSTLEN+1);
  1976.  
  1977. #ifdef    UNIXPORT
  1978.     if (aconf->host[2] == '/') /* (/ starts a 2), Unix domain -- dl*/
  1979.         svp = connect_unix(aconf, cptr, &len);
  1980.     else
  1981.         svp = connect_inet(aconf, cptr, &len);
  1982. #else
  1983.     svp = connect_inet(aconf, cptr, &len);
  1984. #endif
  1985.  
  1986.     if (!svp)
  1987.         {
  1988.         if (cptr->fd != -1)
  1989. #ifndef _WIN32
  1990.             (void)close(cptr->fd);
  1991. #else
  1992.             (void)closesocket(cptr->fd);
  1993. #endif
  1994.         cptr->fd = -2;
  1995.         free_client(cptr);
  1996.         return -1;
  1997.         }
  1998.  
  1999.     set_non_blocking(cptr->fd, cptr);
  2000.     set_sock_opts(cptr->fd, cptr);
  2001. #ifndef _WIN32
  2002.     (void)signal(SIGALRM, dummy);
  2003.     if (connect(cptr->fd, svp, len) < 0 && errno != EINPROGRESS)
  2004.         {
  2005.         errtmp = errno; /* other system calls may eat errno */
  2006. #else
  2007.     if (connect(cptr->fd, svp, len) < 0 &&
  2008.         WSAGetLastError() != WSAEINPROGRESS &&
  2009.         WSAGetLastError() != WSAEWOULDBLOCK)
  2010.         {
  2011.         errtmp = WSAGetLastError(); /* other system calls may eat errno */
  2012. #endif
  2013.         report_error("Connect to host %s failed: %s",cptr);
  2014.                 if (by && IsPerson(by) && !MyClient(by))
  2015.                   sendto_one(by,
  2016.                              ":%s NOTICE %s :Connect to host %s failed.",
  2017.                  me.name, by->name, cptr);
  2018. #ifndef _WIN32
  2019.         (void)close(cptr->fd);
  2020. #else
  2021.         (void)closesocket(cptr->fd);
  2022. #endif
  2023.         cptr->fd = -2;
  2024.         free_client(cptr);
  2025. #ifndef _WIN32
  2026.         errno = errtmp;
  2027.         if (errno == EINTR)
  2028.             errno = ETIMEDOUT;
  2029. #else
  2030.         WSASetLastError(errtmp);
  2031.         if (errtmp == WSAEINTR)
  2032.             WSASetLastError(WSAETIMEDOUT);
  2033. #endif
  2034.         return -1;
  2035.         }
  2036.  
  2037.         /* Attach config entries to client here rather than in
  2038.          * completed_connection. This to avoid null pointer references
  2039.          * when name returned by gethostbyaddr matches no C lines
  2040.          * (could happen in 2.6.1a when host and servername differ).
  2041.          * No need to check access and do gethostbyaddr calls.
  2042.          * There must at least be one as we got here C line...  meLazy
  2043.          */
  2044.         (void)attach_confs_host(cptr, aconf->host,
  2045.                CONF_NOCONNECT_SERVER | CONF_CONNECT_SERVER);
  2046.  
  2047.     if (!find_conf_host(cptr->confs, aconf->host, CONF_NOCONNECT_SERVER) ||
  2048.         !find_conf_host(cptr->confs, aconf->host, CONF_CONNECT_SERVER))
  2049.         {
  2050.               sendto_ops("Host %s is not enabled for connecting:no C/N-line",
  2051.                aconf->host);
  2052.                 if (by && IsPerson(by) && !MyClient(by))
  2053.                   sendto_one(by,
  2054.                              ":%s NOTICE %s :Connect to host %s failed.",
  2055.                  me.name, by->name, cptr);
  2056.         det_confs_butmask(cptr, 0);
  2057. #ifndef _WIN32
  2058.         (void)close(cptr->fd);
  2059. #else
  2060.         (void)closesocket(cptr->fd);
  2061. #endif
  2062.         cptr->fd = -2;
  2063.         free_client(cptr);
  2064.                 return(-1);
  2065.         }
  2066.     /*
  2067.     ** The socket has been connected or connect is in progress.
  2068.     */
  2069.     (void)make_server(cptr);
  2070.     if (by && IsPerson(by))
  2071.         {
  2072.         (void)strcpy(cptr->serv->by, by->name);
  2073.         if (cptr->serv->user) free_user(cptr->serv->user, NULL);
  2074.         cptr->serv->user = by->user;
  2075.         by->user->refcnt++;
  2076.         }
  2077.         else
  2078.         {
  2079.         (void)strcpy(cptr->serv->by, "AutoConn.");
  2080.         if (cptr->serv->user) free_user(cptr->serv->user, NULL);
  2081.         cptr->serv->user = NULL;
  2082.         }
  2083.     (void)strcpy(cptr->serv->up, me.name);
  2084.     if (cptr->fd > highest_fd)
  2085.         highest_fd = cptr->fd;
  2086.     local[cptr->fd] = cptr;
  2087.     cptr->acpt = &me;
  2088.     SetConnecting(cptr);
  2089.  
  2090.     get_sockhost(cptr, aconf->host);
  2091.     add_client_to_list(cptr);
  2092.     nextping = time(NULL);
  2093.  
  2094.     return 0;
  2095. }
  2096.  
  2097. static    struct    sockaddr *connect_inet(aconf, cptr, lenp)
  2098. Reg1    aConfItem    *aconf;
  2099. Reg2    aClient    *cptr;
  2100. int    *lenp;
  2101. {
  2102.     static    struct    sockaddr_in    server;
  2103.     Reg3    struct    hostent    *hp;
  2104.  
  2105.     /*
  2106.      * Might as well get sockhost from here, the connection is attempted
  2107.      * with it so if it fails its useless.
  2108.      */
  2109.     cptr->fd = socket(AF_INET, SOCK_STREAM, 0);
  2110.     if (cptr->fd >= MAXCLIENTS)
  2111.         {
  2112.         sendto_ops("No more connections allowed (%s)", cptr->name);
  2113.         return NULL;
  2114.         }
  2115.     mysk.sin_port = 0;
  2116.     bzero((char *)&server, sizeof(server));
  2117.     server.sin_family = AF_INET;
  2118.     get_sockhost(cptr, aconf->host);
  2119.  
  2120.     if (cptr->fd == -1)
  2121.         {
  2122.         report_error("opening stream socket to server %s:%s", cptr);
  2123.         return NULL;
  2124.         }
  2125.     get_sockhost(cptr, aconf->host);
  2126.     server.sin_port = 0;
  2127.     server.sin_addr = me.ip;
  2128.     server.sin_family = AF_INET;
  2129.     /*
  2130.     ** Bind to a local IP# (with unknown port - let unix decide) so
  2131.     ** we have some chance of knowing the IP# that gets used for a host
  2132.     ** with more than one IP#.
  2133.     */
  2134.     /* No we don't bind it, not all OS's can handle connecting with
  2135.     ** an already bound socket, different ip# might occur anyway
  2136.     ** leading to a freezing select() on this side for some time.
  2137.     ** I had this on my Linux 1.1.88 --Run
  2138.     */
  2139.     /* We do now.  Virtual interface stuff --ns */
  2140.     if (me.ip.s_addr != INADDR_ANY)
  2141.     if (bind(cptr->fd, (struct sockaddr *)&server, sizeof(server)) == -1)
  2142.         {
  2143.         report_error("error binding to local port for %s:%s", cptr);
  2144.         return NULL;
  2145.         }
  2146.     bzero((char *)&server, sizeof(server));
  2147.     server.sin_family = AF_INET;
  2148.     /*
  2149.      * By this point we should know the IP# of the host listed in the
  2150.      * conf line, whether as a result of the hostname lookup or the ip#
  2151.      * being present instead. If we dont know it, then the connect fails.
  2152.      */
  2153.     if (isdigit(*aconf->host) && (aconf->ipnum.s_addr == -1))
  2154.         aconf->ipnum.s_addr = inet_addr(aconf->host);
  2155.     if (aconf->ipnum.s_addr == -1)
  2156.         {
  2157.         hp = cptr->hostp;
  2158.         if (!hp)
  2159.             {
  2160.             Debug((DEBUG_FATAL, "%s: unknown host", aconf->host));
  2161.             return NULL;
  2162.             }
  2163.         bcopy(hp->h_addr, (char *)&aconf->ipnum,
  2164.               sizeof(struct in_addr));
  2165.          }
  2166.     bcopy((char *)&aconf->ipnum, (char *)&server.sin_addr,
  2167.         sizeof(struct in_addr));
  2168.     bcopy((char *)&aconf->ipnum, (char *)&cptr->ip,
  2169.         sizeof(struct in_addr));
  2170. #ifdef TESTNET
  2171.     server.sin_port = htons(((aconf->port > 0) ? aconf->port : portnum)
  2172.         + 10000);
  2173. #else
  2174.     server.sin_port = htons(((aconf->port > 0) ? aconf->port : portnum));
  2175. #endif
  2176.     *lenp = sizeof(server);
  2177.     return    (struct sockaddr *)&server;
  2178. }
  2179.  
  2180. #ifdef    UNIXPORT
  2181. /* connect_unix
  2182.  *
  2183.  * Build a socket structure for cptr so that it can connet to the unix
  2184.  * socket defined by the conf structure aconf.
  2185.  */
  2186. static    struct    sockaddr *connect_unix(aconf, cptr, lenp)
  2187. aConfItem    *aconf;
  2188. aClient    *cptr;
  2189. int    *lenp;
  2190. {
  2191.     static    struct    sockaddr_un    sock;
  2192.  
  2193.     if ((cptr->fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
  2194.         {
  2195.         report_error("Unix domain connect to host %s failed: %s", cptr);
  2196.         return NULL;
  2197.         }
  2198.     else if (cptr->fd >= MAXCLIENTS)
  2199.         {
  2200.         sendto_ops("No more connections allowed (%s)", cptr->name);
  2201.         return NULL;
  2202.         }
  2203.  
  2204.     get_sockhost(cptr, aconf->host);
  2205.     /* +2 needed for working Unix domain -- dl*/
  2206.     strncpyzt(sock.sun_path, aconf->host+2, sizeof(sock.sun_path));
  2207.     sock.sun_family = AF_UNIX;
  2208.     *lenp = strlen(sock.sun_path) + 2;
  2209.  
  2210.     SetUnixSock(cptr);
  2211.     return (struct sockaddr *)&sock;
  2212. }
  2213. #endif
  2214.  
  2215. /*
  2216.  * The following section of code performs summoning of users to irc.
  2217.  */
  2218. #if defined(ENABLE_SUMMON) || defined(ENABLE_USERS)
  2219. int    utmp_open()
  2220. {
  2221. #ifdef O_NOCTTY
  2222.     return (open(UTMP, O_RDONLY|O_NOCTTY));
  2223. #else
  2224.     return (open(UTMP, O_RDONLY));
  2225. #endif
  2226. }
  2227.  
  2228. int    utmp_read(fd, name, line, host, hlen)
  2229. int    fd, hlen;
  2230. char    *name, *line, *host;
  2231. {
  2232.     struct    utmp    ut;
  2233.     while (read(fd, (char *)&ut, sizeof (struct utmp))
  2234.                == sizeof (struct utmp))
  2235.         {
  2236.         strncpyzt(name, ut.ut_name, 9);
  2237.         strncpyzt(line, ut.ut_line, 10);
  2238. #ifdef USER_PROCESS
  2239. #  if defined(HPUX) || defined(AIX)
  2240.         strncpyzt(host,(ut.ut_host[0]) ? (ut.ut_host) : me.name, 16);
  2241. #  else
  2242.         strncpyzt(host, me.name, 9);
  2243. #  endif
  2244.         if (ut.ut_type == USER_PROCESS)
  2245.             return 0;
  2246. #else
  2247.         strncpyzt(host, (ut.ut_host[0]) ? (ut.ut_host) : me.name,
  2248.             hlen);
  2249.         if (ut.ut_name[0])
  2250.             return 0;
  2251. #endif
  2252.         }
  2253.     return -1;
  2254. }
  2255.  
  2256. int    utmp_close(fd)
  2257. int    fd;
  2258. {
  2259.     return(close(fd));
  2260. }
  2261.  
  2262. #ifdef ENABLE_SUMMON
  2263. void    summon(who, namebuf, linebuf, chname)
  2264. aClient *who;
  2265. char    *namebuf, *linebuf, *chname;
  2266. {
  2267.     static    char    wrerr[] = "NOTICE %s :Write error. Couldn't summon.";
  2268.     int    fd;
  2269.     char    line[120];
  2270.     time_t    now;
  2271.     struct    tm    *tp;
  2272.  
  2273.     now = time(NULL);
  2274.     tp = localtime(&now);
  2275.     if (strlen(linebuf) > (size_t) 9)
  2276.         {
  2277.         sendto_one(who,"NOTICE %s :Serious fault in SUMMON.",
  2278.                who->name);
  2279.         sendto_one(who,
  2280.                "NOTICE %s :linebuf too long. Inform Administrator",
  2281.                who->name);
  2282.         return;
  2283.         }
  2284.     /*
  2285.      * Following line added to prevent cracking to e.g. /dev/kmem if
  2286.      * UTMP is for some silly reason writable to everyone...
  2287.      */
  2288.     if ((linebuf[0] != 't' || linebuf[1] != 't' || linebuf[2] != 'y')
  2289.         && (linebuf[0] != 'c' || linebuf[1] != 'o' || linebuf[2] != 'n')
  2290. #ifdef HPUX
  2291.         && (linebuf[0] != 'p' || linebuf[1] != 't' || linebuf[2] != 'y' ||
  2292.         linebuf[3] != '/')
  2293. #endif
  2294.         )
  2295.         {
  2296.         sendto_one(who,
  2297.               "NOTICE %s :Looks like mere mortal souls are trying to",
  2298.                who->name);
  2299.         sendto_one(who,"NOTICE %s :enter the twilight zone... ",
  2300.                who->name);
  2301.         Debug((0, "%s (%s@%s, nick %s, %s)",
  2302.               "FATAL: major security hack. Notify Administrator !",
  2303.               who->username, who->user->host,
  2304.               who->name, who->info));
  2305.         return;
  2306.         }
  2307.  
  2308.     (void)sprintf(line,"/dev/%s", linebuf);
  2309. #ifdef    O_NOCTTY
  2310.     if ((fd = open(line, O_WRONLY | O_NDELAY | O_NOCTTY)) == -1)
  2311. #else
  2312.     if ((fd = open(line, O_WRONLY | O_NDELAY)) == -1)
  2313. #endif
  2314.         {
  2315.         sendto_one(who,
  2316.                "NOTICE %s :%s seems to have disabled summoning...",
  2317.                who->name, namebuf);
  2318.         return;
  2319.         }
  2320. #if !defined(O_NOCTTY) && defined(TIOCNOTTY)
  2321.     (void) ioctl(fd, TIOCNOTTY, NULL);
  2322. #endif
  2323.     (void)sprintf(line,"\n\r\007Message from IRC_Daemon@%s at %d:%02d\n\r",
  2324.             me.name, tp->tm_hour, tp->tm_min);
  2325.     if (write(fd, line, strlen(line)) != strlen(line))
  2326.         {
  2327.         (void)close(fd);
  2328.         sendto_one(who, wrerr, who->name);
  2329.         return;
  2330.         }
  2331.     (void)strcpy(line, "ircd: You are being summoned to Internet Relay \
  2332. Chat on\n\r");
  2333.     if (write(fd, line, strlen(line)) != strlen(line))
  2334.         {
  2335.         (void)close(fd);
  2336.         sendto_one(who, wrerr, who->name);
  2337.         return;
  2338.         }
  2339.     (void)sprintf(line, "ircd: Channel %s, by %s@%s (%s) %s\n\r",
  2340.         chname, who->user->username, who->user->host, who->name, who->info);
  2341.     if (write(fd, line, strlen(line)) != strlen(line))
  2342.         {
  2343.         (void)close(fd);
  2344.         sendto_one(who, wrerr, who->name);
  2345.         return;
  2346.         }
  2347.     (void)strcpy(line,"ircd: Respond with irc\n\r");
  2348.     if (write(fd, line, strlen(line)) != strlen(line))
  2349.         {
  2350.         (void)close(fd);
  2351.         sendto_one(who, wrerr, who->name);
  2352.         return;
  2353.         }
  2354.     (void)close(fd);
  2355.     sendto_one(who, rpl_str(RPL_SUMMONING), me.name, who->name, namebuf);
  2356.     return;
  2357. }
  2358. #  endif
  2359. #endif /* ENABLE_SUMMON */
  2360.  
  2361. /*
  2362. ** find the real hostname for the host running the server (or one which
  2363. ** matches the server's name) and its primary IP#.  Hostname is stored
  2364. ** in the client structure passed as a pointer.
  2365. */
  2366. void    get_my_name(cptr, name, len)
  2367. aClient    *cptr;
  2368. char    *name;
  2369. int    len;
  2370. {
  2371.     static    char tmp[HOSTLEN+1];
  2372.     struct    hostent    *hp;
  2373.     char    *cname = cptr->name;
  2374.  
  2375.     /*
  2376.     ** Setup local socket structure to use for binding to.
  2377.     */
  2378.     bzero((char *)&mysk, sizeof(mysk));
  2379.     mysk.sin_family = AF_INET;
  2380.  
  2381.     if (gethostname(name,len) == -1)
  2382.         return;
  2383.     name[len] = '\0';
  2384.  
  2385.     /* assume that a name containing '.' is a FQDN */
  2386.     if (!index(name,'.'))
  2387.         add_local_domain(name, len - strlen(name));
  2388.  
  2389.     /*
  2390.     ** If hostname gives another name than cname, then check if there is
  2391.     ** a CNAME record for cname pointing to hostname. If so accept
  2392.     ** cname as our name.   meLazy
  2393.     */
  2394.     if (BadPtr(cname))
  2395.         return;
  2396.     if ((hp = gethostbyname(cname)) || (hp = gethostbyname(name)))
  2397.         {
  2398.         char    *hname;
  2399.         int    i = 0;
  2400.  
  2401.         for (hname = hp->h_name; hname; hname = hp->h_aliases[i++])
  2402.               {
  2403.             strncpyzt(tmp, hname, sizeof(tmp));
  2404.             add_local_domain(tmp, sizeof(tmp) - strlen(tmp));
  2405.  
  2406.             /*
  2407.             ** Copy the matching name over and store the
  2408.             ** 'primary' IP# as 'myip' which is used
  2409.             ** later for making the right one is used
  2410.             ** for connecting to other hosts.
  2411.             */
  2412.             if (!mycmp(me.name, tmp))
  2413.                 break;
  2414.              }
  2415.         if (mycmp(me.name, tmp))
  2416.             strncpyzt(name, hp->h_name, len);
  2417.         else
  2418.             strncpyzt(name, tmp, len);
  2419.         bcopy(hp->h_addr, (char *)&mysk.sin_addr,
  2420.             sizeof(struct in_addr));
  2421.         Debug((DEBUG_DEBUG,"local name is %s",
  2422.                 get_client_name(&me,TRUE)));
  2423.         }
  2424.     return;
  2425. }
  2426.  
  2427. /*
  2428. ** setup a UDP socket and listen for incoming packets
  2429. */
  2430. int    setup_ping()
  2431. {
  2432.     struct    sockaddr_in    from;
  2433.     int    on = 1;
  2434.  
  2435.     bzero((char *)&from, sizeof(from));
  2436.     from.sin_addr = me.ip;
  2437. #ifdef TESTNET
  2438.     from.sin_port = htons(17007);
  2439. #else
  2440.     from.sin_port = htons(7007);
  2441. #endif
  2442.     from.sin_family = AF_INET;
  2443.  
  2444.     if ((udpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  2445.         {
  2446. #ifndef _WIN32
  2447.         Debug((DEBUG_ERROR, "socket udp : %s", strerror(errno)));
  2448. #else
  2449.         Debug((DEBUG_ERROR, "socket udp : %s",
  2450.             strerror(WSAGetLastError())));
  2451. #endif
  2452.         return -1;
  2453.         }
  2454.     if (setsockopt(udpfd, SOL_SOCKET, SO_REUSEADDR,
  2455.             (OPT_TYPE *)&on, sizeof(on)) == -1)
  2456.         {
  2457. #ifdef    USE_SYSLOG
  2458.         syslog(LOG_ERR, "setsockopt udp fd %d : %m", udpfd);
  2459. #endif
  2460. #ifndef _WIN32
  2461.         Debug((DEBUG_ERROR, "setsockopt so_reuseaddr : %s",
  2462.             strerror(errno)));
  2463.         (void)close(udpfd);
  2464. #else
  2465.         Debug((DEBUG_ERROR, "setsockopt so_reuseaddr : %s",
  2466.             strerror(WSAGetLastError())));
  2467.         (void)closesocket(udpfd);
  2468. #endif
  2469.         udpfd = -1;
  2470.         return -1;
  2471.         }
  2472.     on = 0;
  2473.     (void) setsockopt(udpfd, SOL_SOCKET, SO_BROADCAST,
  2474.               (char *)&on, sizeof(on));
  2475.     if (bind(udpfd, (struct sockaddr *)&from, sizeof(from))==-1)
  2476.         {
  2477. #ifdef    USE_SYSLOG
  2478.         syslog(LOG_ERR, "bind udp.%d fd %d : %m",
  2479.             from.sin_port, udpfd);
  2480. #endif
  2481. #ifndef _WIN32
  2482.         Debug((DEBUG_ERROR, "bind : %s", strerror(errno)));
  2483.         (void)close(udpfd);
  2484. #else
  2485.         Debug((DEBUG_ERROR, "bind : %s", strerror(WSAGetLastError())));
  2486.         (void)closesocket(udpfd);
  2487. #endif
  2488.         udpfd = -1;
  2489.         return -1;
  2490.         }
  2491. #if !defined _WIN32 && !defined __EMX__
  2492.     if (fcntl(udpfd, F_SETFL, FNDELAY)==-1)
  2493.         {
  2494.         Debug((DEBUG_ERROR, "fcntl fndelay : %s", strerror(errno)));
  2495.         (void)close(udpfd);
  2496.         udpfd = -1;
  2497.         return -1;
  2498.         }
  2499. #endif
  2500.     return udpfd;
  2501. }
  2502. /*
  2503.  * max # of pings set to 15/sec.
  2504.  */
  2505. static    void    polludp()
  2506. {
  2507.     Reg1    char    *s;
  2508.     struct    sockaddr_in    from;
  2509.     int    n, fromlen = sizeof(from);
  2510.     static    time_t    last = 0, now;
  2511.     static    int    cnt = 0, mlen = 0;
  2512.     /*
  2513.      * find max length of data area of packet.
  2514.      */
  2515.     if (!mlen)
  2516.         {
  2517.         mlen = sizeof(readbuf) - strlen(me.name) - strlen(version);
  2518.         mlen -= 6;
  2519.         if (mlen < 0)
  2520.             mlen = 0;
  2521.         }
  2522.     Debug((DEBUG_DEBUG,"udp poll"));
  2523.     n = recvfrom(udpfd, readbuf, mlen, 0,
  2524.         (struct sockaddr *)&from, &fromlen);
  2525.     now = time(NULL);
  2526.     if (now == last)
  2527.         if (++cnt > 14)
  2528.             return;
  2529.     cnt = 0;
  2530.     last = now;
  2531.     if (n == -1)
  2532.         {
  2533. #ifndef _WIN32
  2534.         if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
  2535. #else
  2536.         if ((WSAGetLastError() == WSAEWOULDBLOCK))
  2537. #endif
  2538.             return;
  2539.         else
  2540.             {
  2541.             report_error("udp port recvfrom (%s): %s", &me);
  2542.             return;
  2543.             }
  2544.         }
  2545.     ircstp->is_udp++;
  2546.     if (n  < 19)
  2547.         return;
  2548.     s = readbuf + n;
  2549.     /*
  2550.      * attach my name and version for the reply
  2551.      */
  2552.     *readbuf |= 1;
  2553.     (void)strcpy(s, me.name);
  2554.     s += strlen(s)+1;
  2555.     (void)strcpy(s, version);
  2556.     s += strlen(s);
  2557.     (void)sendto(udpfd, readbuf, s-readbuf, 0,
  2558.         (struct sockaddr *)&from ,sizeof(from));
  2559.     return;
  2560. }
  2561. /*
  2562.  * do_dns_async
  2563.  *
  2564.  * Called when the fd returned from init_resolver() has been selected for
  2565.  * reading.
  2566.  */
  2567. #ifndef _WIN32
  2568. static    void    do_dns_async()
  2569. #else
  2570. void    do_dns_async(id)
  2571. int    id;
  2572. #endif
  2573. {
  2574.     static    Link    ln;
  2575.     aClient    *cptr;
  2576.     aConfItem    *aconf;
  2577.     struct    hostent    *hp;
  2578.     ln.flags = -1;
  2579. #ifndef _WIN32
  2580.     hp = get_res((char *)&ln);
  2581. #else
  2582.     hp = get_res((char *)&ln, id);
  2583. #endif
  2584.     while (hp != NULL)
  2585.     {
  2586.     Debug((DEBUG_DNS,"%#x = get_res(%d,%#x)",hp,ln.flags,ln.value.cptr));
  2587.     switch (ln.flags)
  2588.     {
  2589.     case ASYNC_NONE :
  2590.         /*
  2591.          * no reply was processed that was outstanding or had a client
  2592.          * still waiting.
  2593.          */
  2594.         break;
  2595.     case ASYNC_CLIENT :
  2596.         if ((cptr = ln.value.cptr))
  2597.             {
  2598.             del_queries((char *)cptr);
  2599. #ifdef SHOWCONNECTINFO
  2600.             write(cptr->fd, REPORT_FIN_DNS, R_fin_dns);
  2601. #endif
  2602.             ClearDNS(cptr);
  2603.             if (!DoingAuth(cptr))
  2604.                 SetAccess(cptr);
  2605.             cptr->hostp = hp;
  2606.             }
  2607.         break;
  2608.     case ASYNC_CONNECT :
  2609.         aconf = ln.value.aconf;
  2610.         if (hp && aconf)
  2611.             {
  2612.             bcopy(hp->h_addr, (char *)&aconf->ipnum,
  2613.                   sizeof(struct in_addr));
  2614.             (void)connect_server(aconf, NULL, hp);
  2615.             }
  2616.         else
  2617.             sendto_ops("Connect to %s failed: host lookup",
  2618.                    (aconf) ? aconf->host : "unknown");
  2619.         break;
  2620.     case ASYNC_CONF :
  2621.         aconf = ln.value.aconf;
  2622.         if (hp && aconf)
  2623.             bcopy(hp->h_addr, (char *)&aconf->ipnum,
  2624.                   sizeof(struct in_addr));
  2625.         break;
  2626.     case ASYNC_SERVER :
  2627.         cptr = ln.value.cptr;
  2628.         del_queries((char *)cptr);
  2629.         ClearDNS(cptr);
  2630.         if (check_server(cptr, hp, NULL, NULL, 1))
  2631.             (void)exit_client(cptr, cptr, &me,
  2632.                 "No Authorization");
  2633.         break;
  2634.     default :
  2635.         break;
  2636.     }
  2637.     ln.flags = -1;
  2638. #ifndef _WIN32
  2639.     hp = get_res((char *)&ln);
  2640. #else
  2641.     hp = get_res((char *)&ln, id);
  2642. #endif
  2643.     } /* while (hp != NULL) */
  2644. }
  2645.