Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / s_bsd.c
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  * $Id$
21  */
22 #include "s_bsd.h"
23 #include "client.h"
24 #include "IPcheck.h"
25 #include "channel.h"
26 #include "class.h"
27 #include "hash.h"
28 #include "ircd_log.h"
29 #include "ircd_osdep.h"
30 #include "ircd_string.h"
31 #include "ircd.h"
32 #include "list.h"
33 #include "listener.h"
34 #include "numeric.h"
35 #include "numnicks.h"
36 #include "packet.h"
37 #include "parse.h"
38 #include "querycmds.h"
39 #include "res.h"
40 #include "s_auth.h"
41 #include "s_conf.h"
42 #include "s_debug.h"
43 #include "s_misc.h"
44 #include "s_user.h"
45 #include "send.h"
46 #include "sprintf_irc.h"
47 #include "struct.h"
48 #include "support.h"
49 #include "sys.h"
50 #include "version.h"
51
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
54 #include <assert.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <netdb.h>
58 #include <resolv.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sys/ioctl.h>
63 #include <sys/socket.h>
64 #include <sys/time.h>
65 #include <sys/utsname.h>
66 #include <unistd.h>
67
68 #ifdef USE_POLL
69 #include <sys/poll.h>
70 #endif /* USE_POLL */
71
72 #ifndef IN_LOOPBACKNET
73 #define IN_LOOPBACKNET  0x7f
74 #endif
75
76 #ifndef INADDR_NONE
77 #define INADDR_NONE 0xffffffff
78 #endif
79
80 struct Client*            LocalClientArray[MAXCONNECTIONS];
81 int                       HighestFd = -1;
82 static struct sockaddr_in virtualHost;
83 static char               readbuf[SERVER_TCP_WINDOW];
84 static int                running_in_background;
85
86 /*
87  * report_error text constants
88  */
89 const char* const ACCEPT_ERROR_MSG    = "error accepting connection for %s: %s";
90 const char* const BIND_ERROR_MSG      = "bind error for %s: %s";
91 const char* const CONNECT_ERROR_MSG   = "connect to host %s failed: %s";
92 const char* const CONNLIMIT_ERROR_MSG = "connect limit exceeded for %s: %s";
93 const char* const LISTEN_ERROR_MSG    = "listen error for %s: %s";
94 const char* const NONB_ERROR_MSG      = "error setting non-blocking for %s: %s";
95 const char* const PEERNAME_ERROR_MSG  = "getpeername failed for %s: %s";
96 const char* const POLL_ERROR_MSG      = "poll error for %s: %s";
97 const char* const REUSEADDR_ERROR_MSG = "error setting SO_REUSEADDR for %s: %s";
98 const char* const SELECT_ERROR_MSG    = "select error for %s: %s";
99 const char* const SETBUFS_ERROR_MSG   = "error setting buffer size for %s: %s";
100 const char* const SOCKET_ERROR_MSG    = "error creating socket for %s: %s";
101
102 #ifdef VIRTUAL_HOST
103 struct sockaddr_in vserv;
104 #endif
105
106 #ifdef GODMODE
107 #ifndef NODNS
108 #define NODNS
109 #endif
110 #ifndef NOFLOODCONTROL
111 #define NOFLOODCONTROL
112 #endif
113 #endif
114
115 #if !defined(USE_POLL)
116 #if FD_SETSIZE < (MAXCONNECTIONS + 4)
117 /*
118  * Sanity check
119  *
120  * All operating systems work when MAXCONNECTIONS <= 252.
121  * Most operating systems work when MAXCONNECTIONS <= 1020 and FD_SETSIZE is
122  *   updated correctly in the system headers (on BSD systems our sys.h has
123  *   defined FD_SETSIZE to MAXCONNECTIONS+4 before including the system's headers 
124  *   but sys/types.h might have abruptly redefined it so the check is still 
125  *   done), you might already need to recompile your kernel.
126  * For larger FD_SETSIZE your milage may vary (kernel patches may be needed).
127  * The check is _NOT_ done if we will not use FD_SETS at all (USE_POLL)
128  */
129 #error "FD_SETSIZE is too small or MAXCONNECTIONS too large."
130 #endif
131 #endif
132
133
134 /*
135  * Cannot use perror() within daemon. stderr is closed in
136  * ircd and cannot be used. And, worse yet, it might have
137  * been reassigned to a normal connection...
138  */
139
140 /*
141  * report_error
142  *
143  * This a replacement for perror(). Record error to log and
144  * also send a copy to all *LOCAL* opers online.
145  *
146  * text    is a *format* string for outputting error. It must
147  *         contain only two '%s', the first will be replaced
148  *         by the sockhost from the cptr, and the latter will
149  *         be taken from sys_errlist[errno].
150  *
151  * cptr    if not NULL, is the *LOCAL* client associated with
152  *         the error.
153  */
154 void report_error(const char* text, const char* who, int err)
155 {
156   static time_t last_notice = 0;
157   int           errtmp = errno;   /* debug may change 'errno' */
158   const char*   errmsg = (err) ? strerror(err) : "";
159   if (!errmsg)
160     errmsg = "Unknown error"; 
161
162   if (!who)
163     who = "unknown";
164
165   if (last_notice + 20 < CurrentTime) {
166     /*
167      * pace error messages so opers don't get flooded by transients
168      */
169     sendto_ops(text, who, errmsg);
170     last_notice = CurrentTime;
171   }
172
173   ircd_log(L_ERROR, text, who, errmsg);
174
175   if (!running_in_background) {
176     fprintf(stderr, text, who, errmsg);
177     fprintf(stderr, "\n");
178   }
179   errno = errtmp;
180 }
181
182
183 /*
184  * connect_dns_callback - called when resolver query finishes
185  * if the query resulted in a successful search, reply will contain
186  * a non-null pointer, otherwise reply will be null.
187  * if successful start the connection, otherwise notify opers
188  */
189 static void connect_dns_callback(void* vptr, struct DNSReply* reply)
190 {
191   struct ConfItem* aconf = (struct ConfItem*) vptr;
192   aconf->dns_pending = 0;
193   if (reply) {
194     memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
195     connect_server(aconf, 0, reply);
196   }
197   else
198     sendto_ops("Connect to %s failed: host lookup", aconf->name);
199 }
200
201
202 /*
203  * connect_inet - set up address and port and make a connection
204  */
205 static int connect_inet(struct ConfItem* aconf, struct Client* cptr)
206 {
207   static struct sockaddr_in sin;
208   assert(0 != aconf);
209   assert(0 != cptr);
210   /*
211    * Might as well get sockhost from here, the connection is attempted
212    * with it so if it fails its useless.
213    */
214   cptr->fd = socket(AF_INET, SOCK_STREAM, 0);
215   if (-1 == cptr->fd) {
216     cptr->error = errno;
217     report_error(SOCKET_ERROR_MSG, cptr->name, errno);
218     return 0;
219   }
220   if (cptr->fd >= MAXCLIENTS) {
221     report_error(CONNLIMIT_ERROR_MSG, cptr->name, 0);
222     close(cptr->fd);
223     cptr->fd = -1;
224     return 0;
225   }
226   /*
227    * Bind to a local IP# (with unknown port - let unix decide) so
228    * we have some chance of knowing the IP# that gets used for a host
229    * with more than one IP#.
230    *
231    * No we don't bind it, not all OS's can handle connecting with
232    * an already bound socket, different ip# might occur anyway
233    * leading to a freezing select() on this side for some time.
234    * I had this on my Linux 1.1.88 --Run
235    */
236 #ifdef VIRTUAL_HOST
237   /*
238    * No, we do bind it if we have virtual host support. If we don't
239    * explicitly bind it, it will default to IN_ADDR_ANY and we lose
240    * due to the other server not allowing our base IP --smg
241    */
242   if (bind(cptr->fd, (struct sockaddr*) &virtualHost, sizeof(virtualHost))) {
243     report_error(BIND_ERROR_MSG, cptr->name, errno);
244     return 0;
245   }
246 #endif
247
248   memset(&sin, 0, sizeof(sin));
249   sin.sin_family      = AF_INET;
250   sin.sin_addr.s_addr = aconf->ipnum.s_addr;
251   sin.sin_port        = htons(aconf->port);
252   /*
253    * save connection info in client
254    */
255   cptr->ip.s_addr = aconf->ipnum.s_addr;
256   cptr->port      = aconf->port;
257   ircd_ntoa_r(cptr->sock_ip, (const char*) &cptr->ip);
258   /*
259    * we want a big buffer for server connections
260    */
261   if (!os_set_sockbufs(cptr->fd, SERVER_TCP_WINDOW)) {
262     cptr->error = errno;
263     report_error(SETBUFS_ERROR_MSG, cptr->name, errno);
264     return 0;
265   }
266   /*
267    * ALWAYS set sockets non-blocking
268    */
269   if (!os_set_nonblocking(cptr->fd)) {
270     cptr->error = errno;
271     report_error(NONB_ERROR_MSG, cptr->name, errno);
272     return 0;
273   }
274   if (!os_connect_nonb(cptr->fd, &sin)) {
275     cptr->error = errno;
276     report_error(CONNECT_ERROR_MSG, cptr->name, errno);
277     return 0;
278   }
279   return 1;
280 }
281
282 /*
283  * deliver_it
284  *   Attempt to send a sequence of bytes to the connection.
285  *   Returns
286  *
287  *   < 0     Some fatal error occurred, (but not EWOULDBLOCK).
288  *           This return is a request to close the socket and
289  *           clean up the link.
290  *
291  *   >= 0    No real error occurred, returns the number of
292  *           bytes actually transferred. EWOULDBLOCK and other
293  *           possibly similar conditions should be mapped to
294  *           zero return. Upper level routine will have to
295  *           decide what to do with those unwritten bytes...
296  *
297  *   *NOTE*  alarm calls have been preserved, so this should
298  *           work equally well whether blocking or non-blocking
299  *           mode is used...
300  *
301  *   We don't use blocking anymore, that is impossible with the
302  *      net.loads today anyway. Commented out the alarms to save cpu.
303  *      --Run
304  */
305 unsigned int deliver_it(struct Client *cptr, const char *str, unsigned int len)
306 {
307   unsigned int bytes_written = 0;
308   assert(0 != cptr);
309
310   switch (os_send_nonb(cptr->fd, str, len, &bytes_written)) {
311   case IO_SUCCESS:
312     cptr->flags &= ~FLAGS_BLOCKED;
313
314     cptr->sendB += bytes_written;
315     me.sendB    += bytes_written;
316     if (cptr->sendB > 1023) {
317       cptr->sendK += (cptr->sendB >> 10);
318       cptr->sendB &= 0x03ff;    /* 2^10 = 1024, 3ff = 1023 */
319     }
320     if (me.sendB > 1023) {
321       me.sendK += (me.sendB >> 10);
322       me.sendB &= 0x03ff;
323     }
324     if (bytes_written < len)
325       cptr->flags |= FLAGS_BLOCKED;
326     break;
327   case IO_BLOCKED:
328     cptr->flags |= FLAGS_BLOCKED;
329     break;
330   case IO_FAILURE:
331     cptr->error = errno;
332     cptr->flags |= FLAGS_DEADSOCKET;
333     break;
334   }
335   return bytes_written;
336 }
337
338 /*
339  * init_sys
340  */
341 void init_sys(void)
342 {
343   int fd;
344   int limit = os_set_fdlimit(MAXCONNECTIONS);
345   if (limit < 0) {
346     fprintf(stderr, "error setting max fd's to %d\n", limit);
347     exit(2);
348   }
349   else if (limit > 0) {
350     fprintf(stderr, "ircd fd table too big\nHard Limit: %d IRC max: %d\n",
351             limit, MAXCONNECTIONS);
352     fprintf(stderr, "set MAXCONNECTIONS to a smaller value");
353     exit(2);
354   }
355
356   for (fd = 3; fd < MAXCONNECTIONS; ++fd)
357   {
358     close(fd);
359     LocalClientArray[fd] = NULL;
360   }
361   LocalClientArray[2] = 0;
362   LocalClientArray[1] = 0;
363   LocalClientArray[0] = 0;
364   close(1);
365   close(0);
366
367   if (bootopt & BOOT_TTY) {
368     /* debugging is going to a tty */
369     init_resolver();
370     return;
371   }
372   if (!(bootopt & BOOT_DEBUG))
373     close(2);
374
375   if (fork())
376     exit(0);
377   running_in_background = 1;
378 #ifdef TIOCNOTTY
379   if ((fd = open("/dev/tty", O_RDWR)) > -1) {
380     ioctl(fd, TIOCNOTTY, 0);
381     close(fd);
382   }
383 #endif
384   setsid();
385   init_resolver();
386 }
387
388 void release_dns_reply(struct Client* cptr)
389 {
390   assert(0 != cptr);
391   assert(MyConnect(cptr));
392
393   if (cptr->dns_reply) {
394     assert(0 < cptr->dns_reply->ref_count);
395     --cptr->dns_reply->ref_count;
396     cptr->dns_reply = 0;
397   }
398 }
399
400 /*
401  * completed_connection
402  *
403  * Complete non-blocking connect()-sequence. Check access and
404  * terminate connection, if trouble detected.
405  *
406  * Return  TRUE, if successfully completed
407  *        FALSE, if failed and ClientExit
408  */
409 static int completed_connection(struct Client* cptr)
410 {
411   struct ConfItem *aconf;
412   time_t newts;
413   struct Client *acptr;
414   int i;
415
416   assert(0 != cptr);
417
418   /*
419    * get the socket status from the fd first to check if
420    * connection actually succeeded
421    */
422   if ((cptr->error = os_get_sockerr(cptr->fd))) {
423     const char* msg = strerror(cptr->error);
424     if (!msg)
425       msg = "Unknown error";
426     sendto_ops("Connection failed to %s: %s", cptr->name, msg);
427     return 0;
428   }
429   if (!(aconf = find_conf_byname(cptr->confs, cptr->name, CONF_SERVER))) {
430     sendto_ops("Lost Server Line for %s", cptr->name);
431     return 0;
432   }
433   if (!EmptyString(aconf->passwd))
434     sendto_one(cptr, "PASS :%s", aconf->passwd);
435
436 #if 0 
437   /* dead code, already done in connect_server */
438   make_server(cptr);
439 #endif
440   /*
441    * Create a unique timestamp
442    */
443   newts = TStime();
444   for (i = HighestFd; i > -1; --i) {
445     if ((acptr = LocalClientArray[i]) && 
446         (IsServer(acptr) || IsHandshake(acptr))) {
447       if (acptr->serv->timestamp >= newts)
448         newts = acptr->serv->timestamp + 1;
449     }
450   }
451   assert(0 != cptr->serv);
452
453   cptr->serv->timestamp = newts;
454   SetHandshake(cptr);
455   /*
456    * Make us timeout after twice the timeout for DNS look ups
457    */
458   cptr->lasttime = CurrentTime;
459   cptr->flags |= FLAGS_PINGSENT;
460   sendto_one(cptr, "SERVER %s 1 " TIME_T_FMT " " TIME_T_FMT " J%s %s%s :%s",
461              me.name, me.serv->timestamp, newts, MAJOR_PROTOCOL, 
462              NumServCap(&me), me.info);
463
464   return (IsDead(cptr)) ? 0 : 1;
465 }
466
467 /*
468  * close_connection
469  *
470  * Close the physical connection. This function must make
471  * MyConnect(cptr) == FALSE, and set cptr->from == NULL.
472  */
473 void close_connection(struct Client *cptr)
474 {
475   struct ConfItem* aconf;
476
477   if (IsServer(cptr)) {
478     ServerStats->is_sv++;
479     ServerStats->is_sbs += cptr->sendB;
480     ServerStats->is_sbr += cptr->receiveB;
481     ServerStats->is_sks += cptr->sendK;
482     ServerStats->is_skr += cptr->receiveK;
483     ServerStats->is_sti += CurrentTime - cptr->firsttime;
484     if (ServerStats->is_sbs > 1023) {
485       ServerStats->is_sks += (ServerStats->is_sbs >> 10);
486       ServerStats->is_sbs &= 0x3ff;
487     }
488     if (ServerStats->is_sbr > 1023) {
489       ServerStats->is_skr += (ServerStats->is_sbr >> 10);
490       ServerStats->is_sbr &= 0x3ff;
491     }
492     /*
493      * If the connection has been up for a long amount of time, schedule
494      * a 'quick' reconnect, else reset the next-connect cycle.
495      */
496     if ((aconf = find_conf_exact(cptr->name, 0, cptr->sockhost, CONF_SERVER))) {
497       /*
498        * Reschedule a faster reconnect, if this was a automaticly
499        * connected configuration entry. (Note that if we have had
500        * a rehash in between, the status has been changed to
501        * CONF_ILLEGAL). But only do this if it was a "good" link.
502        */
503       aconf->hold = CurrentTime;
504       aconf->hold += (aconf->hold - cptr->since > HANGONGOODLINK) ?
505                      HANGONRETRYDELAY : ConfConFreq(aconf);
506       if (nextconnect > aconf->hold)
507         nextconnect = aconf->hold;
508     }
509   }
510   else if (IsUser(cptr)) {
511     ServerStats->is_cl++;
512     ServerStats->is_cbs += cptr->sendB;
513     ServerStats->is_cbr += cptr->receiveB;
514     ServerStats->is_cks += cptr->sendK;
515     ServerStats->is_ckr += cptr->receiveK;
516     ServerStats->is_cti += CurrentTime - cptr->firsttime;
517     if (ServerStats->is_cbs > 1023) {
518       ServerStats->is_cks += (ServerStats->is_cbs >> 10);
519       ServerStats->is_cbs &= 0x3ff;
520     }
521     if (ServerStats->is_cbr > 1023) {
522       ServerStats->is_ckr += (ServerStats->is_cbr >> 10);
523       ServerStats->is_cbr &= 0x3ff;
524     }
525   }
526   else
527     ServerStats->is_ni++;
528
529   if (-1 < cptr->fd) {
530     flush_connections(cptr);
531     LocalClientArray[cptr->fd] = 0;
532     close(cptr->fd);
533     cptr->fd = -1;
534   }
535   cptr->flags |= FLAGS_DEADSOCKET;
536
537   DBufClear(&cptr->sendQ);
538   DBufClear(&cptr->recvQ);
539   memset(cptr->passwd, 0, sizeof(cptr->passwd));
540   set_snomask(cptr, 0, SNO_SET);
541
542   det_confs_butmask(cptr, 0);
543
544   if (cptr->listener) {
545     release_listener(cptr->listener);
546     cptr->listener = 0;
547   }
548
549   for ( ; HighestFd > 0; --HighestFd) {
550     if (LocalClientArray[HighestFd])
551       break;
552   }
553 }
554
555 int net_close_unregistered_connections(struct Client* source)
556 {
557   int            i;
558   struct Client* cptr;
559   int            count = 0;
560   assert(0 != source);
561
562   for (i = HighestFd; i > 0; --i) {
563     if ((cptr = LocalClientArray[i]) && !IsRegistered(cptr)) {
564       sendto_one(source, rpl_str(RPL_CLOSING), me.name, source->name,
565                  get_client_name(source, HIDE_IP), cptr->status);
566       exit_client(source, cptr, &me, "Oper Closing");
567       ++count;
568     }
569   }
570   return count;
571 }
572
573 /*
574  * Creates a client which has just connected to us on the given fd.
575  * The sockhost field is initialized with the ip# of the host.
576  * The client is not added to the linked list of clients, it is
577  * passed off to the auth handler for dns and ident queries.
578  */
579 void add_connection(struct Listener* listener, int fd)
580 {
581   struct sockaddr_in addr;
582   struct Client*     new_client;
583   time_t             next_target = 0;
584   const char* const throttle_message =
585          "ERROR: Your host is trying to (re)connect too fast -- throttled\r\n";
586        /* 12345678901234567890123456789012345679012345678901234567890123456 */
587   
588   assert(0 != listener);
589   /*
590    * Removed preliminary access check. Full check is performed in
591    * m_server and m_user instead. Also connection time out help to
592    * get rid of unwanted connections.
593    */
594   if (!os_get_peername(fd, &addr) || !os_set_nonblocking(fd)) {
595     ++ServerStats->is_ref;
596     close(fd);
597     return;
598   }
599   /*
600    * XXX - do we really want to do this? is it needed?
601    */
602   os_disable_options(fd);
603
604   /*
605    * Add this local client to the IPcheck registry.
606    * If it is a connection to a user port and if the site has been throttled,
607    * reject the user.
608    */
609   if (!IPcheck_local_connect(addr.sin_addr, &next_target) && !listener->server) {
610     ++ServerStats->is_ref;
611     /*
612      * strlen(throttle_message) == 66
613      */
614     send(fd, throttle_message, 66, 0);
615     close(fd);
616     return;
617   }
618
619   new_client = make_client(NULL,
620       (listener->server) ? STAT_UNKNOWN_SERVER : STAT_UNKNOWN_USER);
621   /*
622    * Copy ascii address to 'sockhost' just in case. Then we
623    * have something valid to put into error messages...
624    */
625   ircd_ntoa_r(new_client->sock_ip, (const char*) &addr.sin_addr);   
626   strcpy(new_client->sockhost, new_client->sock_ip);
627   new_client->ip.s_addr = addr.sin_addr.s_addr;
628   new_client->port      = ntohs(addr.sin_port);
629
630   if (next_target)
631     new_client->nexttarget = next_target;
632
633   new_client->fd = fd;
634
635   if (!listener->server)
636     SetIPChecked(new_client);
637   new_client->listener = listener;
638   ++listener->ref_count;
639
640   Count_newunknown(UserStats);
641   /*
642    * if we've made it this far we can put the client on the auth query pile
643    */
644   start_auth(new_client);
645 }
646
647 /*
648  * read_packet
649  *
650  * Read a 'packet' of data from a connection and process it.  Read in 8k
651  * chunks to give a better performance rating (for server connections).
652  * Do some tricky stuff for client connections to make sure they don't do
653  * any flooding >:-) -avalon
654  */
655 static int read_packet(struct Client *cptr, int socket_ready)
656 {
657   unsigned int dolen = 0;
658   unsigned int length = 0;
659
660   if (socket_ready && !(IsUser(cptr) && DBufLength(&cptr->recvQ) > CLIENT_FLOOD)) {
661     switch (os_recv_nonb(cptr->fd, readbuf, sizeof(readbuf), &length)) {
662     case IO_SUCCESS:
663       if (length) {
664         cptr->lasttime = CurrentTime;
665         if (cptr->lasttime > cptr->since)
666           cptr->since = cptr->lasttime;
667         cptr->flags &= ~(FLAGS_PINGSENT | FLAGS_NONL);
668       }
669       break;
670     case IO_BLOCKED:
671       break;
672     case IO_FAILURE:
673       cptr->error = errno;
674       /* cptr->flags |= FLAGS_DEADSOCKET; */
675       return 0;
676     }
677   }
678
679   /*
680    * For server connections, we process as many as we can without
681    * worrying about the time of day or anything :)
682    */
683   if (length > 0 && IsServer(cptr)) {
684     return server_dopacket(cptr, readbuf, length);
685   }
686   else {
687     /*
688      * Before we even think of parsing what we just read, stick
689      * it on the end of the receive queue and do it when its
690      * turn comes around.
691      */
692     if (length > 0 && 0 == dbuf_put(&cptr->recvQ, readbuf, length)) {
693       return exit_client(cptr, cptr, &me, "dbuf_put fail");
694     }
695 #ifndef NOFLOODCONTROL
696     /*
697      * XXX - cptr will always be a user or unregistered
698      */
699     if (IsUser(cptr) && DBufLength(&cptr->recvQ) > CLIENT_FLOOD)
700       return exit_client(cptr, cptr, &me, "Excess Flood");
701
702     while (DBufLength(&cptr->recvQ) && !NoNewLine(cptr) && 
703            (IsTrusted(cptr) || cptr->since - CurrentTime < 10))
704 #else
705     while (DBufLength(&cptr->recvQ) && !NoNewLine(cptr))
706 #endif
707     {
708       /*
709        * If it has become registered as a Server
710        * then skip the per-message parsing below.
711        */
712       if (IsServer(cptr)) {
713         dolen = dbuf_get(&cptr->recvQ, readbuf, sizeof(readbuf));
714         return (dolen) ? server_dopacket(cptr, readbuf, dolen) : 1;
715       }
716       dolen = dbuf_getmsg(&cptr->recvQ, cptr->buffer, BUFSIZE);
717       /*
718        * Devious looking...whats it do ? well..if a client
719        * sends a *long* message without any CR or LF, then
720        * dbuf_getmsg fails and we pull it out using this
721        * loop which just gets the next 512 bytes and then
722        * deletes the rest of the buffer contents.
723        * -avalon
724        */
725       if (0 == dolen) {
726         if (DBufLength(&cptr->recvQ) < 510)
727           cptr->flags |= FLAGS_NONL;
728         else
729           DBufClear(&cptr->recvQ);
730       }
731       else if (CPTR_KILLED == client_dopacket(cptr, dolen))
732         return CPTR_KILLED;
733     }
734   }
735   return 1;
736 }
737
738 static int on_write_unblocked(struct Client* cptr)
739 {
740   /*
741    *  ...room for writing, empty some queue then...
742    */
743   cptr->flags &= ~FLAGS_BLOCKED;
744   if (IsConnecting(cptr)) {
745     if (!completed_connection(cptr))
746       return 0;
747   }
748   else if (cptr->listing && DBufLength(&cptr->sendQ) < 2048)
749     list_next_channels(cptr, 64);
750   send_queued(cptr);
751   return 1;
752 }
753
754 /*
755  * Select / Poll Read Algorithm for ircd
756  *
757  * We need to check the file descriptors for all the different types
758  * of things that use them, so check for reads on everything but connects
759  * and writes on connects and descriptors that are blocked
760  *
761  * for each (client in local) {
762  *   if (not connecting)
763  *     check for read;
764  *   if (connecting or blocked)
765  *     check for write;
766  * }
767  * wait for activity;
768  *
769  * for each (client in local) {
770  *   if (there are descriptors to check) {
771  *     if (write activity)
772  *       send data;
773  *     if (read activity)
774  *       read data;
775  *   }
776  *   process data read;
777  * }
778  * Note we must always process data read whether or not there has been
779  * read activity or file descriptors set, since data is buffered by the client.
780  */
781
782
783 #ifdef USE_POLL
784
785 /*
786  * poll macros
787  */
788 #if defined(POLLMSG) && defined(POLLIN) && defined(POLLRDNORM)
789 #  define POLLREADFLAGS (POLLMSG|POLLIN|POLLRDNORM)
790 #else
791 #  if defined(POLLIN) && defined(POLLRDNORM)
792 #    define POLLREADFLAGS (POLLIN|POLLRDNORM)
793 #  else
794 #    if defined(POLLIN)
795 #      define POLLREADFLAGS POLLIN
796 #    else
797 #      if defined(POLLRDNORM)
798 #        define POLLREADFLAGS POLLRDNORM
799 #      endif
800 #    endif
801 #  endif
802 #endif
803
804 #if defined(POLLOUT) && defined(POLLWRNORM)
805 #define POLLWRITEFLAGS (POLLOUT|POLLWRNORM)
806 #else
807 #  if defined(POLLOUT)
808 #    define POLLWRITEFLAGS POLLOUT
809 #  else
810 #    if defined(POLLWRNORM)
811 #      define POLLWRITEFLAGS POLLWRNORM
812 #    endif
813 #  endif
814 #endif
815
816 #ifdef POLLHUP
817 #define POLLERRORS (POLLHUP|POLLERR)
818 #else
819 #define POLLERRORS POLLERR
820 #endif
821
822 /*
823  * NOTE: pfd and pfd_count are local variable names in read_message
824  */
825 #define PFD_SETR(xfd) \
826   do { CHECK_ADD_PFD(xfd) pfd->events |= POLLREADFLAGS; } while(0)
827 #define PFD_SETW(xfd) \
828   do { CHECK_ADD_PFD(xfd) pfd->events |= POLLWRITEFLAGS; } while(0)
829
830 #define CHECK_ADD_PFD(xfd) \
831   if (pfd->fd != xfd) { \
832     pfd = &poll_fds[pfd_count++]; \
833     poll_fds[pfd_count].fd = -1; \
834     pfd->fd = xfd; \
835     pfd->events = 0; \
836   }
837
838 /*
839  * Check all connections for new connections and input data that is to be
840  * processed. Also check for connections with data queued and whether we can
841  * write it out.
842  *
843  * Don't ever use ZERO for `delay', unless you mean to poll and then
844  * you have to have sleep/wait somewhere else in the code.--msa
845  */
846 int read_message(time_t delay)
847 {
848   struct pollfd poll_fds[MAXCONNECTIONS + 1];
849   struct Client*    cptr;
850   struct Listener*    listener  = 0;
851   struct AuthRequest* auth      = 0;
852   struct AuthRequest* auth_next = 0;
853   int nfds;
854   time_t delay2 = delay;
855   int length;
856   int i;
857   int res = 0;
858   int pfd_count = 0;
859   struct pollfd* pfd = 0;
860   struct pollfd* res_pfd = 0;
861   int read_ready;
862   int write_ready;
863
864   unsigned int timeout;
865
866   for ( ; ; ) {
867     pfd_count = 0;
868     pfd = poll_fds;
869     res_pfd = 0;
870     pfd->fd = -1;
871
872     if (-1 < ResolverFileDescriptor) {
873       PFD_SETR(ResolverFileDescriptor);
874       res_pfd = pfd;
875     }
876     /*
877      * add auth file descriptors
878      */
879     for (auth = AuthPollList; auth; auth = auth->next) {
880       assert(-1 < auth->fd);
881       auth->index = pfd_count;
882       if (IsAuthConnect(auth))
883         PFD_SETW(auth->fd);
884       else
885         PFD_SETR(auth->fd);
886     }
887     /*
888      * add listener file descriptors
889      */    
890     for (listener = ListenerPollList; listener; listener = listener->next) {
891       assert(-1 < listener->fd);
892       /*
893        * pfd_count is incremented by PFD_SETR so we need to save the 
894        * index first 
895        */
896       listener->index = pfd_count;
897       PFD_SETR(listener->fd);
898     }
899
900     for (i = HighestFd; -1 < i; --i) {
901       if ((cptr = LocalClientArray[i])) {
902
903         if (DBufLength(&cptr->recvQ))
904           delay2 = 1;
905         if (DBufLength(&cptr->recvQ) < 4088 || IsServer(cptr)) {
906           PFD_SETR(i);
907         }
908         if (DBufLength(&cptr->sendQ) || IsConnecting(cptr) ||
909             (cptr->listing && DBufLength(&cptr->sendQ) < 2048)) {
910           PFD_SETW(i);
911         }
912       }
913     }
914
915     Debug((DEBUG_INFO, "poll: %d %d", delay, delay2));
916
917     timeout = (IRCD_MIN(delay2, delay)) * 1000;
918
919     nfds = poll(poll_fds, pfd_count, timeout);
920
921     CurrentTime = time(0);
922     if (-1 < nfds)
923       break;
924
925     if (EINTR == errno)
926       return -1;
927     report_error(POLL_ERROR_MSG, me.name, errno);
928     ++res;
929     if (res > 5)
930       server_restart("too many poll errors");
931     sleep(1);
932     CurrentTime = time(0);
933   }
934
935   if (res_pfd && (res_pfd->revents & (POLLREADFLAGS | POLLERRORS))) {
936     resolver_read();
937     --nfds;
938   }
939   /*
940    * check auth queries
941    */
942   for (auth = AuthPollList; auth; auth = auth_next) {
943     auth_next = auth->next;
944     i = auth->index;
945     /*
946      * check for any event, we only ask for one at a time
947      */
948     if (poll_fds[i].revents) {
949       if (IsAuthConnect(auth))
950         send_auth_query(auth);
951       else
952         read_auth_reply(auth);
953       if (0 == --nfds)
954         break;
955     }
956   }
957   /*
958    * check listeners
959    */
960   for (listener = ListenerPollList; listener; listener = listener->next) {
961     i = listener->index;
962     if (poll_fds[i].revents) {
963       accept_connection(listener);
964       if (0 == --nfds)
965         break;
966     }
967   }
968   /*
969    * i contains the next non-auth/non-listener index, since we put the
970    * resolver, auth and listener, file descriptors in poll_fds first,
971    * the very next one should be the start of the clients
972    */
973   pfd = &poll_fds[++i];
974
975   for ( ; (i < pfd_count); ++i, ++pfd) {
976     if (!(cptr = LocalClientArray[pfd->fd]))
977       continue;
978     read_ready = write_ready = 0;
979
980     if (0 < nfds && pfd->revents) {
981       --nfds;
982     
983       read_ready  = pfd->revents & POLLREADFLAGS;
984       write_ready = pfd->revents & POLLWRITEFLAGS;
985
986       if (pfd->revents & POLLERRORS) {
987         if (pfd->events & POLLREADFLAGS)
988           ++read_ready;
989         if (pfd->events & POLLWRITEFLAGS)
990           ++write_ready;
991       }
992     }
993     if (write_ready) {
994       if (!on_write_unblocked(cptr) || IsDead(cptr)) {
995         const char* msg = (cptr->error) ? strerror(cptr->error) : LastDeadComment(cptr);
996         if (!msg)
997           msg = "Unknown error";
998         exit_client(cptr, cptr, &me, (char*) msg);
999         continue;
1000       }
1001     }
1002     length = 1;                 /* for fall through case */
1003     if ((!NoNewLine(cptr) || read_ready) && !IsDead(cptr)) {
1004       if (CPTR_KILLED == (length = read_packet(cptr, read_ready)))
1005         continue;
1006     }
1007 #if 0
1008     /* Bullshit, why would we want to flush sockets while using non-blocking?
1009      * This uses > 4% cpu! --Run */
1010     if (length > 0)
1011       flush_connections(poll_cptr[i]);
1012 #endif
1013     if (IsDead(cptr)) {
1014       const char* msg = (cptr->error) ? strerror(cptr->error) : LastDeadComment(cptr);
1015       if (!msg)
1016         msg = "Unknown error";
1017       exit_client(cptr, cptr, &me, (char*) msg);
1018       continue;
1019     }
1020     if (length > 0)
1021       continue;
1022     cptr->flags |= FLAGS_DEADSOCKET;
1023     /*
1024      * ...hmm, with non-blocking sockets we might get
1025      * here from quite valid reasons, although.. why
1026      * would select report "data available" when there
1027      * wasn't... So, this must be an error anyway...  --msa
1028      * actually, EOF occurs when read() returns 0 and
1029      * in due course, select() returns that fd as ready
1030      * for reading even though it ends up being an EOF. -avalon
1031      */
1032     Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d", pfd->fd, errno, length));
1033
1034     if ((IsServer(cptr) || IsHandshake(cptr)) && cptr->error == 0 && length == 0)
1035       exit_client_msg(cptr, cptr, &me, "Server %s closed the connection (%s)",
1036                       cptr->name, cptr->serv->last_error_msg);
1037     else {
1038       const char* msg = (cptr->error) ? strerror(cptr->error) : "EOF from client";
1039       if (!msg)
1040         msg = "Unknown error";
1041       exit_client_msg(cptr, cptr, &me, "Read error to %s: %s",
1042                       get_client_name(cptr, HIDE_IP), msg);
1043     }
1044   }
1045   return 0;
1046 }
1047 #else /* USE_SELECT */
1048
1049 /*
1050  * Check all connections for new connections and input data that is to be
1051  * processed. Also check for connections with data queued and whether we can
1052  * write it out.
1053  *
1054  * Don't ever use ZERO for `delay', unless you mean to poll and then
1055  * you have to have sleep/wait somewhere else in the code.--msa
1056  */
1057 int read_message(time_t delay)
1058 {
1059   struct Client*   cptr;
1060   struct Listener* listener;
1061   int              nfds;
1062   struct timeval   wait;
1063   time_t           delay2 = delay;
1064   unsigned int     usec = 0;
1065   int              res = 0;
1066   int              length;
1067   int              i;
1068   struct AuthRequest* auth = 0;
1069   struct AuthRequest* auth_next = 0;
1070   int              read_ready;
1071   fd_set           read_set;
1072   fd_set           write_set;
1073
1074   for ( ; ; )
1075   {
1076     FD_ZERO(&read_set);
1077     FD_ZERO(&write_set);
1078
1079     if (-1 < ResolverFileDescriptor)
1080       FD_SET(ResolverFileDescriptor, &read_set);
1081     /*
1082      * set auth file descriptors
1083      */
1084     for (auth = AuthPollList; auth; auth = auth->next) {
1085       assert(-1 < auth->fd);
1086       if (IsAuthConnect(auth))
1087         FD_SET(auth->fd, &write_set);
1088       else /* if (IsAuthPending(auth)) */
1089         FD_SET(auth->fd, &read_set);
1090     }
1091     /*
1092      * set listener file descriptors
1093      */
1094     for (listener = ListenerPollList; listener; listener = listener->next) {
1095       assert(-1 < listener->fd);
1096       FD_SET(listener->fd, &read_set);
1097     }
1098
1099     for (i = HighestFd; i > -1; --i) {
1100       if ((cptr = LocalClientArray[i])) {
1101         if (DBufLength(&cptr->recvQ))
1102           delay2 = 1;
1103         if (DBufLength(&cptr->recvQ) < 4088 || IsServer(cptr))
1104           FD_SET(i, &read_set);
1105         if (DBufLength(&cptr->sendQ) || IsConnecting(cptr) ||
1106             (cptr->listing && DBufLength(&cptr->sendQ) < 2048))
1107           FD_SET(i, &write_set);
1108       }
1109     }
1110
1111     wait.tv_sec = IRCD_MIN(delay2, delay);
1112     wait.tv_usec = usec;
1113
1114     Debug((DEBUG_INFO, "select: %d %d", delay, delay2));
1115
1116     nfds = select(FD_SETSIZE, &read_set, &write_set, 0, &wait);
1117
1118     CurrentTime = time(0);
1119
1120     if (-1 < nfds)
1121       break;
1122
1123     if (errno == EINTR)
1124       return -1;
1125     report_error(SELECT_ERROR_MSG, me.name, errno);
1126     if (++res > 5)
1127       server_restart("too many select errors");
1128     sleep(1);
1129     CurrentTime = time(0);
1130   }
1131
1132   if (-1 < ResolverFileDescriptor && 
1133       FD_ISSET(ResolverFileDescriptor, &read_set)) {
1134     resolver_read();
1135     --nfds;
1136   }
1137   /*
1138    * Check fd sets for the auth fd's (if set and valid!) first
1139    * because these can not be processed using the normal loops below.
1140    * -avalon
1141    */
1142   for (auth = AuthPollList; auth; auth = auth_next) {
1143     auth_next = auth->next;
1144     assert(-1 < auth->fd);
1145     if (IsAuthConnect(auth) && FD_ISSET(auth->fd, &write_set)) {
1146       send_auth_query(auth);
1147       if (0 == --nfds)
1148         break;
1149     }
1150     else if (FD_ISSET(auth->fd, &read_set)) {
1151       read_auth_reply(auth);
1152       if (0 == --nfds)
1153         break;
1154     }
1155   }
1156   /*
1157    * next accept connections from active listeners
1158    */
1159   for (listener = ListenerPollList; listener; listener = listener->next) {
1160     assert(-1 < listener->fd);
1161     if (0 < nfds && FD_ISSET(listener->fd, &read_set))
1162       accept_connection(listener);
1163   } 
1164
1165   for (i = HighestFd; -1 < i; --i) {
1166     if (!(cptr = LocalClientArray[i]))
1167       continue;
1168     read_ready = 0;
1169     if (0 < nfds) {
1170       if (FD_ISSET(i, &write_set)) {
1171         --nfds;
1172         if (!on_write_unblocked(cptr) || IsDead(cptr)) {
1173           const char* msg = (cptr->error) ? strerror(cptr->error) : LastDeadComment(cptr);
1174           if (!msg)
1175             msg = "Unknown error";
1176           if (FD_ISSET(i, &read_set))
1177             --nfds;
1178           exit_client(cptr, cptr, &me, msg);
1179           continue;
1180         }
1181       }
1182       if ((read_ready = FD_ISSET(i, &read_set)))
1183         --nfds;
1184     }
1185     length = 1;                 /* for fall through case */
1186     if ((!NoNewLine(cptr) || read_ready) && !IsDead(cptr)) {
1187       if (CPTR_KILLED == (length = read_packet(cptr, read_ready)))
1188         continue;
1189     }
1190 #if 0
1191     /* Bullshit, why would we want to flush sockets while using non-blocking?
1192      * This uses > 4% cpu! --Run */
1193     if (length > 0)
1194       flush_connections(LocalClientArray[i]);
1195 #endif
1196     if (IsDead(cptr)) {
1197       const char* msg = (cptr->error) ? strerror(cptr->error) : LastDeadComment(cptr);
1198       if (!msg)
1199         msg = "Unknown error";
1200       exit_client(cptr, cptr, &me, msg);
1201       continue;
1202     }
1203     if (length > 0)
1204       continue;
1205
1206     /*
1207      * ...hmm, with non-blocking sockets we might get
1208      * here from quite valid reasons, although.. why
1209      * would select report "data available" when there
1210      * wasn't... So, this must be an error anyway...  --msa
1211      * actually, EOF occurs when read() returns 0 and
1212      * in due course, select() returns that fd as ready
1213      * for reading even though it ends up being an EOF. -avalon
1214      */
1215     Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d %d", i, cptr->error, length));
1216
1217     if ((IsServer(cptr) || IsHandshake(cptr)) && cptr->error == 0 && length == 0)
1218       exit_client_msg(cptr, cptr, &me, "Server %s closed the connection (%s)",
1219                       cptr->name, cptr->serv->last_error_msg);
1220     else {
1221       const char* msg = (cptr->error) ? strerror(cptr->error) : "EOF from client";
1222       if (!msg)
1223         msg = "Unknown error";
1224       exit_client_msg(cptr, cptr, &me, "Read error to %s: %s",
1225                       get_client_name(cptr, HIDE_IP), msg);
1226     }
1227   }
1228   return 0;
1229 }
1230
1231 #endif /* USE_SELECT */
1232
1233 /*
1234  * connect_server - start or complete a connection to another server
1235  * returns true (1) if successful, false (0) otherwise
1236  *
1237  * aconf must point to a valid C:line
1238  * m_connect            calls this with a valid by client and a null reply
1239  * try_connections      calls this with a null by client, and a null reply
1240  * connect_dns_callback call this with a null by client, and a valid reply
1241  *
1242  * XXX - if this comes from an m_connect message and a dns query needs to
1243  * be done, we loose the information about who started the connection and
1244  * it's considered an auto connect.
1245  */
1246 int connect_server(struct ConfItem* aconf, struct Client* by,
1247                    struct DNSReply* reply)
1248 {
1249   struct Client*   cptr = 0;
1250   assert(0 != aconf);
1251
1252   if (aconf->dns_pending) {
1253     sendto_ops("Server %s connect DNS pending");
1254     return 0;
1255   }
1256   Debug((DEBUG_NOTICE, "Connect to %s[@%s]", aconf->name,
1257          ircd_ntoa((const char*) &aconf->ipnum)));
1258
1259   if ((cptr = FindClient(aconf->name))) {
1260     if (IsServer(cptr) || IsMe(cptr)) {
1261       sendto_ops("Server %s already present from %s", 
1262                  aconf->name, cptr->from->name);
1263       if (by && IsUser(by) && !MyUser(by)) {
1264         sendto_one(by, "%s NOTICE %s%s :Server %s already present from %s",
1265                      NumServ(&me), NumNick(by), aconf->name, cptr->from->name);
1266       }
1267       return 0;
1268     }
1269     else if (IsHandshake(cptr) || IsConnecting(cptr)) {
1270       if (by && IsUser(by)) {
1271         if (MyUser(by))
1272           sendto_one(by, ":%s NOTICE %s :Connection to %s already in progress",
1273                      me.name, by->name, cptr->name);
1274         else
1275           sendto_one(by, "%s NOTICE %s%s :Connection to %s already in progress",
1276                      NumServ(&me), NumNick(by), cptr->name);
1277       }
1278       return 0;
1279     }
1280   }
1281   /*
1282    * If we dont know the IP# for this host and itis a hostname and
1283    * not a ip# string, then try and find the appropriate host record.
1284    */
1285   if (INADDR_NONE == aconf->ipnum.s_addr) {
1286     char buf[HOSTLEN + 1];
1287     assert(0 == reply);
1288     if (INADDR_NONE == (aconf->ipnum.s_addr = inet_addr(aconf->host))) {
1289       struct DNSQuery  query;
1290
1291       query.vptr     = aconf;
1292       query.callback = connect_dns_callback;
1293       host_from_uh(buf, aconf->host, HOSTLEN);
1294       buf[HOSTLEN] = '\0';
1295
1296       reply = gethost_byname(buf, &query);
1297
1298       if (!reply) {
1299         aconf->dns_pending = 1;
1300         return 0;
1301       }
1302       memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
1303     }
1304   }
1305   cptr = make_client(NULL, STAT_UNKNOWN_SERVER);
1306   if (reply)
1307     ++reply->ref_count;
1308   cptr->dns_reply = reply;
1309
1310   /*
1311    * Copy these in so we have something for error detection.
1312    */
1313   ircd_strncpy(cptr->name, aconf->name, HOSTLEN);
1314   ircd_strncpy(cptr->sockhost, aconf->host, HOSTLEN);
1315
1316   /*
1317    * Attach config entries to client here rather than in
1318    * completed_connection. This to avoid null pointer references
1319    */
1320   attach_confs_byhost(cptr, aconf->host, CONF_SERVER);
1321
1322   if (!find_conf_byhost(cptr->confs, aconf->host, CONF_SERVER)) {
1323     sendto_ops("Host %s is not enabled for connecting: no C-line", aconf->name);
1324     if (by && IsUser(by) && !MyUser(by)) {
1325       sendto_one(by, "%s NOTICE %s%s :Connect to host %s failed: no C-line",
1326                  NumServ(&me), NumNick(by), aconf->name);
1327     }
1328     det_confs_butmask(cptr, 0);
1329     free_client(cptr);
1330     return 0;
1331   }
1332   /*
1333    * attempt to connect to the server in the conf line
1334    */
1335   if (!connect_inet(aconf, cptr)) {
1336     if (by && IsUser(by) && !MyUser(by)) {
1337       sendto_one(by, "%s NOTICE %s%s :Couldn't connect to %s",
1338                  NumServ(&me), NumNick(by), cptr->name);
1339     }
1340     det_confs_butmask(cptr, 0);
1341     free_client(cptr);
1342     return 0;
1343   }
1344   /*
1345    * NOTE: if we're here we have a valid C:Line and the client should
1346    * have started the connection and stored the remote address/port and
1347    * ip address name in itself
1348    *
1349    * The socket has been connected or connect is in progress.
1350    */
1351   make_server(cptr);
1352   if (by && IsUser(by)) {
1353     sprintf_irc(cptr->serv->by, "%s%s", NumNick(by));
1354     assert(0 == cptr->serv->user);
1355     cptr->serv->user = by->user;
1356     by->user->refcnt++;
1357   }
1358   else {
1359     *cptr->serv->by = '\0';
1360     /* strcpy(cptr->serv->by, "Auto"); */
1361   }
1362   cptr->serv->up = &me;
1363   SetConnecting(cptr);
1364
1365   if (cptr->fd > HighestFd)
1366     HighestFd = cptr->fd;
1367   LocalClientArray[cptr->fd] = cptr;
1368
1369   Count_newunknown(UserStats);
1370   add_client_to_list(cptr);
1371   hAddClient(cptr);
1372   nextping = CurrentTime;
1373
1374   return 1;
1375 }
1376
1377 /*
1378  * Setup local socket structure to use for binding to.
1379  */
1380 void init_virtual_host(const struct ConfItem* conf)
1381 {
1382   assert(0 != conf);
1383
1384   memset(&virtualHost, 0, sizeof(virtualHost));
1385   virtualHost.sin_family = AF_INET;
1386   virtualHost.sin_addr.s_addr = INADDR_ANY;
1387
1388   if (EmptyString(conf->passwd) || 0 == strcmp(conf->passwd, "*"))
1389     return;
1390   virtualHost.sin_addr.s_addr = inet_addr(conf->passwd);
1391
1392   if (INADDR_NONE == virtualHost.sin_addr.s_addr)
1393     virtualHost.sin_addr.s_addr = INADDR_ANY;
1394 }  
1395
1396 /*
1397  * Find the real hostname for the host running the server (or one which
1398  * matches the server's name) and its primary IP#.  Hostname is stored
1399  * in the client structure passed as a pointer.
1400  */
1401 int init_server_identity()
1402 {
1403   struct ConfItem* conf = find_me();
1404
1405   if (!conf || EmptyString(conf->host))
1406     return 0;
1407
1408   ircd_strncpy(me.name, conf->host, HOSTLEN);
1409
1410   init_virtual_host(conf);
1411   return 1;
1412 }
1413
1414