ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
(Generate patch)

Comparing:
ircd-hybrid-7.2/src/s_bsd.c (file contents), Revision 896 by michael, Sat Nov 3 08:54:09 2007 UTC vs.
ircd-hybrid/trunk/src/s_bsd.c (file contents), Revision 2228 by michael, Thu Jun 13 19:46:30 2013 UTC

# Line 23 | Line 23
23   */
24  
25   #include "stdinc.h"
26 #ifndef _WIN32
26   #include <netinet/in_systm.h>
27   #include <netinet/ip.h>
28   #include <netinet/tcp.h>
29 < #endif
29 > #include "list.h"
30   #include "fdlist.h"
31   #include "s_bsd.h"
32   #include "client.h"
34 #include "common.h"
33   #include "dbuf.h"
34   #include "event.h"
35   #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
36   #include "ircd.h"
41 #include "list.h"
37   #include "listener.h"
38   #include "numeric.h"
39   #include "packet.h"
40   #include "irc_res.h"
46 #include "inet_misc.h"
41   #include "restart.h"
42   #include "s_auth.h"
43 < #include "s_conf.h"
44 < #include "s_log.h"
43 > #include "conf.h"
44 > #include "log.h"
45   #include "s_serv.h"
46   #include "send.h"
47   #include "memory.h"
# Line 60 | Line 54 | static const char *comm_err_str[] = { "C
54  
55   struct Callback *setup_socket_cb = NULL;
56  
57 < static void comm_connect_callback(fde_t *fd, int status);
57 > static void comm_connect_callback(fde_t *, int);
58   static PF comm_connect_timeout;
59 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
59 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
60   static PF comm_connect_tryconnect;
61  
68 extern void init_netio(void);
62  
63   /* check_can_use_v6()
64   *  Check if the system can open AF_INET6 sockets
# Line 81 | Line 74 | check_can_use_v6(void)
74    else
75    {
76      ServerInfo.can_use_v6 = 1;
84 #ifdef _WIN32
85    closesocket(v6);
86 #else
77      close(v6);
88 #endif
78    }
79   #else
80    ServerInfo.can_use_v6 = 0;
# Line 101 | Line 90 | check_can_use_v6(void)
90   int
91   get_sockerr(int fd)
92   {
104 #ifndef _WIN32
93    int errtmp = errno;
106 #else
107  int errtmp = WSAGetLastError();
108 #endif
94   #ifdef SO_ERROR
95    int err = 0;
96    socklen_t len = sizeof(err);
97  
98 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
98 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
99    {
100      if (err)
101        errtmp = err;
# Line 144 | Line 129 | report_error(int level, const char* text
129   {
130    who = (who) ? who : "";
131  
132 <  sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
133 <  log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
134 <  ilog(L_ERROR, text, who, strerror(error));
132 >  sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
133 >                       text, who, strerror(error));
134 >  ilog(LOG_TYPE_IRCD, text, who, strerror(error));
135   }
136  
137   /*
# Line 160 | Line 145 | setup_socket(va_list args)
145    int fd = va_arg(args, int);
146    int opt = 1;
147  
148 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
148 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
149  
150   #ifdef IPTOS_LOWDELAY
151    opt = IPTOS_LOWDELAY;
152 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
152 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
153   #endif
154  
170 #ifndef _WIN32
155    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
172 #endif
156  
157    return NULL;
158   }
# Line 194 | Line 177 | init_comm(void)
177   void
178   close_connection(struct Client *client_p)
179   {
180 <  struct ConfItem *conf;
198 <  struct AccessItem *aconf;
199 <  struct ClassItem *aclass;
180 >  dlink_node *ptr = NULL;
181  
182 <  assert(NULL != client_p);
182 >  assert(client_p);
183  
184    if (!IsDead(client_p))
185    {
# Line 211 | Line 192 | close_connection(struct Client *client_p
192      send_queued_write(client_p);
193    }
194  
195 <  if (IsServer(client_p))
195 >  if (IsClient(client_p))
196 >  {
197 >    ++ServerStats.is_cl;
198 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
199 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
200 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
201 >  }
202 >  else if (IsServer(client_p))
203    {
204      ++ServerStats.is_sv;
205      ServerStats.is_sbs += client_p->localClient->send.bytes;
206      ServerStats.is_sbr += client_p->localClient->recv.bytes;
207 <    ServerStats.is_sti += CurrentTime - client_p->firsttime;
207 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
208  
209 <    /* XXX Does this even make any sense at all anymore?
222 <     * scheduling a 'quick' reconnect could cause a pile of
223 <     * nick collides under TSora protocol... -db
224 <     */
225 <    /*
226 <     * If the connection has been up for a long amount of time, schedule
227 <     * a 'quick' reconnect, else reset the next-connect cycle.
228 <     */
229 <    if ((conf = find_conf_exact(SERVER_TYPE,
230 <                                  client_p->name, client_p->username,
231 <                                  client_p->host)))
209 >    DLINK_FOREACH(ptr, server_items.head)
210      {
211 +      struct MaskItem *conf = ptr->data;
212 +
213 +      if (irccmp(conf->name, client_p->name))
214 +        continue;
215 +
216        /*
217 <       * Reschedule a faster reconnect, if this was a automatically
218 <       * connected configuration entry. (Note that if we have had
236 <       * a rehash in between, the status has been changed to
237 <       * CONF_ILLEGAL). But only do this if it was a "good" link.
217 >       * Reset next-connect cycle of all connect{} blocks that match
218 >       * this servername.
219         */
220 <      aconf = (struct AccessItem *)map_to_conf(conf);
240 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
241 <      aconf->hold = time(NULL);
242 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
243 <        HANGONRETRYDELAY : ConFreq(aclass);
244 <      if (nextconnect > aconf->hold)
245 <        nextconnect = aconf->hold;
220 >      conf->until = CurrentTime + conf->class->con_freq;
221      }
222    }
248  else if (IsClient(client_p))
249  {
250    ++ServerStats.is_cl;
251    ServerStats.is_cbs += client_p->localClient->send.bytes;
252    ServerStats.is_cbr += client_p->localClient->recv.bytes;
253    ServerStats.is_cti += CurrentTime - client_p->firsttime;
254  }
223    else
224      ++ServerStats.is_ni;
225  
# Line 267 | Line 235 | close_connection(struct Client *client_p
235    if (client_p->localClient->fd.flags.open)
236      fd_close(&client_p->localClient->fd);
237  
270  if (HasServlink(client_p))
271  {
272    if (client_p->localClient->ctrlfd.flags.open)
273      fd_close(&client_p->localClient->ctrlfd);
274  }
275
238    dbuf_clear(&client_p->localClient->buf_sendq);
239    dbuf_clear(&client_p->localClient->buf_recvq);
240    
241    MyFree(client_p->localClient->passwd);
242 <  detach_conf(client_p, CONF_TYPE);
242 >  detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
243    client_p->from = NULL; /* ...this should catch them! >:) --msa */
244   }
245  
246 + /*  Base16 encoding is:
247 + *  Copyright (c) 2001-2004, Roger Dingledine
248 + *  Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson
249 + *
250 + *  Redistribution and use in source and binary forms, with or without
251 + *  modification, are permitted provided that the following conditions are
252 + *  met:
253 + *
254 + *  Redistributions of source code must retain the above copyright
255 + *  notice, this list of conditions and the following disclaimer.
256 +
257 + * Redistributions in binary form must reproduce the above copyright notice,
258 + * this list of conditions and the following disclaimer
259 + * in the documentation and/or other materials provided with the distribution.
260 + *
261 + * Neither the names of the copyright owners nor the names of its
262 + * contributors may be used to endorse or promote products derived from
263 + * this software without specific prior written permission.
264 +
265 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
266 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
267 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
268 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
269 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
270 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
271 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
273 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
275 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276 + */
277 + static void
278 + base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
279 + {
280 +  const char *end;
281 +  char *cp;
282 +
283 +  assert(destlen >= srclen * 2 + 1);
284 +
285 +  cp = dest;
286 +  end = src + srclen;
287 +
288 +  while (src < end)
289 +  {
290 +    *cp++ = "0123456789ABCDEF"[(*(const uint8_t *)src) >>  4];
291 +    *cp++ = "0123456789ABCDEF"[(*(const uint8_t *)src) & 0xf];
292 +    ++src;
293 +  }
294 +
295 +  *cp = '\0';
296 + }
297 +
298   #ifdef HAVE_LIBCRYPTO
299   /*
300   * ssl_handshake - let OpenSSL initialize the protocol. Register for
# Line 289 | Line 303 | close_connection(struct Client *client_p
303   static void
304   ssl_handshake(int fd, struct Client *client_p)
305   {
306 +  X509 *cert = NULL;
307    int ret = SSL_accept(client_p->localClient->fd.ssl);
308 +  int err = SSL_get_error(client_p->localClient->fd.ssl, ret);
309 +
310 +  ilog(LOG_TYPE_IRCD, "SSL Error %d %s", err, ERR_error_string(err, NULL));
311 +
312 +  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
313 +  {
314 +    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
315 +
316 +    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
317 +        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
318 +        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
319 +      base16_encode(client_p->certfp, sizeof(client_p->certfp),
320 +                    (const char *)cert->sha1_hash, sizeof(cert->sha1_hash));
321 +    else
322 +      ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
323 +           client_p->name, client_p->username, client_p->host, res);
324 +    X509_free(cert);
325 +  }
326  
327    if (ret <= 0)
328 +  {
329      switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
330      {
331        case SSL_ERROR_WANT_WRITE:
# Line 308 | Line 342 | ssl_handshake(int fd, struct Client *cli
342          exit_client(client_p, client_p, "Error during SSL handshake");
343          return;
344      }
345 +  }
346  
347 <  execute_callback(auth_cb, client_p);
347 >  start_auth(client_p);
348   }
349   #endif
350  
# Line 323 | Line 358 | ssl_handshake(int fd, struct Client *cli
358   void
359   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
360   {
361 <  struct Client *new_client;
327 <
328 <  assert(NULL != listener);
329 <
330 <  new_client = make_client(NULL);
361 >  struct Client *new_client = make_client(NULL);
362  
363    fd_open(&new_client->localClient->fd, fd, 1,
364            (listener->flags & LISTENER_SSL) ?
365            "Incoming SSL connection" : "Incoming connection");
366  
367 <  /*
367 >  /*
368     * copy address to 'sockhost' as a string, copy it to host too
369     * so we have something valid to put into error messages...
370     */
371    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
372  
373 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
374 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
375 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
373 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
374 >              new_client->localClient->ip.ss_len, new_client->sockhost,
375 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
376    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
346 #ifdef IPV6
347  if (new_client->sockhost[0] == ':')
348    strlcat(new_client->host, "0", HOSTLEN+1);
377  
378 <  if (new_client->localClient->aftype == AF_INET6 &&
379 <      ConfigFileEntry.dot_in_ip6_addr == 1)
378 > #ifdef HAVE_LIBGEOIP
379 >  /* XXX IPV6 SUPPORT XXX */
380 >  if (irn->ss.ss_family == AF_INET && geoip_ctx)
381    {
382 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
383 <    strlcat(new_client->host, ".", HOSTLEN+1);
382 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)&new_client->localClient->ip;
383 >    new_client->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
384    }
356  else
385   #endif
358    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
386  
387 <  new_client->connect_id = ++connect_id;
387 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
388 >  {
389 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
390 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
391 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
392 >    new_client->sockhost[0] = '0';
393 >  }
394 >  else
395 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
396 >
397    new_client->localClient->listener = listener;
398    ++listener->ref_count;
399  
400   #ifdef HAVE_LIBCRYPTO
401    if (listener->flags & LISTENER_SSL)
402    {
403 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
403 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
404      {
405 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
405 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
406             ERR_error_string(ERR_get_error(), NULL));
407  
408        SetDead(new_client);
# Line 379 | Line 415 | add_connection(struct Listener *listener
415    }
416    else
417   #endif
418 <    execute_callback(auth_cb, new_client);
418 >    start_auth(new_client);
419   }
420  
421   /*
# Line 509 | Line 545 | comm_connect_tcp(fde_t *fd, const char *
545                   void *data, int aftype, int timeout)
546   {
547    struct addrinfo hints, *res;
548 <  char portname[PORTNAMELEN+1];
548 >  char portname[PORTNAMELEN + 1];
549  
550    assert(callback);
551    fd->connect.callback = callback;
# Line 541 | Line 577 | comm_connect_tcp(fde_t *fd, const char *
577    hints.ai_socktype = SOCK_STREAM;
578    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
579  
580 <  snprintf(portname, PORTNAMELEN, "%d", port);
580 >  snprintf(portname, sizeof(portname), "%d", port);
581  
582 <  if (irc_getaddrinfo(host, portname, &hints, &res))
582 >  if (getaddrinfo(host, portname, &hints, &res))
583    {
584      /* Send the DNS request, for the next level */
549    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
550    fd->dns_query->ptr = fd;
551    fd->dns_query->callback = comm_connect_dns_callback;
585      if (aftype == AF_INET6)
586 <      gethost_byname_type(host, fd->dns_query, T_AAAA);
586 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
587      else
588 <      gethost_byname_type(host, fd->dns_query, T_A);
588 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
589    }
590    else
591    {
# Line 562 | Line 595 | comm_connect_tcp(fde_t *fd, const char *
595      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
596      fd->connect.hostaddr.ss_len = res->ai_addrlen;
597      fd->connect.hostaddr.ss.ss_family = res->ai_family;
598 <    irc_freeaddrinfo(res);
598 >    freeaddrinfo(res);
599      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
600      comm_connect_tryconnect(fd, NULL);
601    }
# Line 610 | Line 643 | comm_connect_timeout(fde_t *fd, void *no
643   * otherwise we initiate the connect()
644   */
645   static void
646 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
646 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
647   {
648    fde_t *F = vptr;
649  
650 <  if (reply == NULL)
650 >  if (name == NULL)
651    {
619    MyFree(F->dns_query);
620    F->dns_query = NULL;
652      comm_connect_callback(F, COMM_ERR_DNS);
653      return;
654    }
# Line 631 | Line 662 | comm_connect_dns_callback(void *vptr, st
662     * the DNS record around, and the DNS cache is gone anyway..
663     *     -- adrian
664     */
665 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
665 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
666    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
667    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
668      F->connect.hostaddr.ss_port;
669 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
669 >  F->connect.hostaddr.ss_len = addr->ss_len;
670  
671    /* Now, call the tryconnect() routine to try a connect() */
641  MyFree(F->dns_query);
642  F->dns_query = NULL;
672    comm_connect_tryconnect(F, NULL);
673   }
674  
# Line 667 | Line 696 | comm_connect_tryconnect(fde_t *fd, void
696    /* Error? */
697    if (retval < 0)
698    {
670 #ifdef _WIN32
671    errno = WSAGetLastError();
672 #endif
699      /*
700       * If we get EISCONN, then we've already connect()ed the socket,
701       * which is a good thing.
# Line 728 | Line 754 | comm_open(fde_t *F, int family, int sock
754     */
755    fd = socket(family, sock_type, proto);
756    if (fd < 0)
731  {
732 #ifdef _WIN32
733    errno = WSAGetLastError();
734 #endif
757      return -1; /* errno will be passed through, yay.. */
736  }
758  
759    execute_callback(setup_socket_cb, fd);
760  
# Line 766 | Line 787 | comm_accept(struct Listener *lptr, struc
787     * reserved fd limit, but we can deal with that when comm_open()
788     * also does it. XXX -- adrian
789     */
790 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
790 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
791    if (newfd < 0)
771  {
772 #ifdef _WIN32
773    errno = WSAGetLastError();
774 #endif
792      return -1;
776  }
793  
794   #ifdef IPV6
795    remove_ipv6_mapping(pn);
# Line 789 | Line 805 | comm_accept(struct Listener *lptr, struc
805  
806   /*
807   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
808 < * This function should really inspect the struct itself rather than relying
793 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
808 > * OSes with IPv6 mapping listening on both
809   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
810   *
811   */
# Line 800 | Line 815 | remove_ipv6_mapping(struct irc_ssaddr *a
815   {
816    if (addr->ss.ss_family == AF_INET6)
817    {
818 <    struct sockaddr_in6 *v6;
804 <
805 <    v6 = (struct sockaddr_in6*)addr;
806 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
818 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
819      {
820 <      char v4ip[HOSTIPLEN];
821 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
822 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
823 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
820 >      struct sockaddr_in6 v6;
821 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
822 >
823 >      memcpy(&v6, addr, sizeof(v6));
824 >      memset(v4, 0, sizeof(struct sockaddr_in));
825 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
826 >
827        addr->ss.ss_family = AF_INET;
828        addr->ss_len = sizeof(struct sockaddr_in);
829      }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)