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-8/src/s_bsd.c (file contents), Revision 1243 by michael, Fri Sep 30 10:47:53 2011 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"
# 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 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 211 | Line 194 | close_connection(struct Client *client_p
194      send_queued_write(client_p);
195    }
196  
197 <  if (IsServer(client_p))
197 >  if (IsClient(client_p))
198 >  {
199 >    ++ServerStats.is_cl;
200 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
201 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
202 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
203 >  }
204 >  else if (IsServer(client_p))
205    {
206      ++ServerStats.is_sv;
207      ServerStats.is_sbs += client_p->localClient->send.bytes;
208      ServerStats.is_sbr += client_p->localClient->recv.bytes;
209 <    ServerStats.is_sti += CurrentTime - client_p->firsttime;
209 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
210  
211      /* XXX Does this even make any sense at all anymore?
212       * scheduling a 'quick' reconnect could cause a pile of
# Line 226 | Line 216 | close_connection(struct Client *client_p
216       * If the connection has been up for a long amount of time, schedule
217       * a 'quick' reconnect, else reset the next-connect cycle.
218       */
219 <    if ((conf = find_conf_exact(SERVER_TYPE,
220 <                                  client_p->name, client_p->username,
231 <                                  client_p->host)))
219 >    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
220 >                                client_p->username, client_p->host)))
221      {
222        /*
223         * Reschedule a faster reconnect, if this was a automatically
# Line 236 | Line 225 | close_connection(struct Client *client_p
225         * a rehash in between, the status has been changed to
226         * CONF_ILLEGAL). But only do this if it was a "good" link.
227         */
228 <      aconf = (struct AccessItem *)map_to_conf(conf);
229 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
228 >      aconf  = map_to_conf(conf);
229 >      aclass = map_to_conf(aconf->class_ptr);
230        aconf->hold = time(NULL);
231 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
231 >      aconf->hold += (aconf->hold - client_p->localClient->since > HANGONGOODLINK) ?
232          HANGONRETRYDELAY : ConFreq(aclass);
244      if (nextconnect > aconf->hold)
245        nextconnect = aconf->hold;
233      }
234    }
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  }
235    else
236      ++ServerStats.is_ni;
237  
# Line 323 | Line 303 | ssl_handshake(int fd, struct Client *cli
303   void
304   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
305   {
306 <  struct Client *new_client;
327 <
328 <  assert(NULL != listener);
329 <
330 <  new_client = make_client(NULL);
306 >  struct Client *new_client = make_client(NULL);
307  
308    fd_open(&new_client->localClient->fd, fd, 1,
309            (listener->flags & LISTENER_SSL) ?
310            "Incoming SSL connection" : "Incoming connection");
311  
312 <  /*
312 >  /*
313     * copy address to 'sockhost' as a string, copy it to host too
314     * so we have something valid to put into error messages...
315     */
316    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
317  
318 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
319 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
320 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
318 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
319 >              new_client->localClient->ip.ss_len, new_client->sockhost,
320 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
321    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);
322  
323 <  if (new_client->localClient->aftype == AF_INET6 &&
351 <      ConfigFileEntry.dot_in_ip6_addr == 1)
323 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
324    {
325 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
326 <    strlcat(new_client->host, ".", HOSTLEN+1);
325 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
326 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
327 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
328 >    new_client->sockhost[0] = '0';
329    }
330    else
331 < #endif
358 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
331 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
332  
360  new_client->connect_id = ++connect_id;
333    new_client->localClient->listener = listener;
334    ++listener->ref_count;
335  
336   #ifdef HAVE_LIBCRYPTO
337    if (listener->flags & LISTENER_SSL)
338    {
339 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
339 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
340      {
341        ilog(L_CRIT, "SSL_new() ERROR! -- %s",
342             ERR_error_string(ERR_get_error(), NULL));
# Line 509 | Line 481 | comm_connect_tcp(fde_t *fd, const char *
481                   void *data, int aftype, int timeout)
482   {
483    struct addrinfo hints, *res;
484 <  char portname[PORTNAMELEN+1];
484 >  char portname[PORTNAMELEN + 1];
485  
486    assert(callback);
487    fd->connect.callback = callback;
# Line 541 | Line 513 | comm_connect_tcp(fde_t *fd, const char *
513    hints.ai_socktype = SOCK_STREAM;
514    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
515  
516 <  snprintf(portname, PORTNAMELEN, "%d", port);
516 >  snprintf(portname, sizeof(portname), "%d", port);
517  
518 <  if (irc_getaddrinfo(host, portname, &hints, &res))
518 >  if (getaddrinfo(host, portname, &hints, &res))
519    {
520      /* 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;
521      if (aftype == AF_INET6)
522 <      gethost_byname_type(host, fd->dns_query, T_AAAA);
522 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
523      else
524 <      gethost_byname_type(host, fd->dns_query, T_A);
524 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
525    }
526    else
527    {
# Line 562 | Line 531 | comm_connect_tcp(fde_t *fd, const char *
531      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
532      fd->connect.hostaddr.ss_len = res->ai_addrlen;
533      fd->connect.hostaddr.ss.ss_family = res->ai_family;
534 <    irc_freeaddrinfo(res);
534 >    freeaddrinfo(res);
535      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
536      comm_connect_tryconnect(fd, NULL);
537    }
# Line 610 | Line 579 | comm_connect_timeout(fde_t *fd, void *no
579   * otherwise we initiate the connect()
580   */
581   static void
582 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
582 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
583   {
584    fde_t *F = vptr;
585  
586 <  if (reply == NULL)
586 >  if (name == NULL)
587    {
619    MyFree(F->dns_query);
620    F->dns_query = NULL;
588      comm_connect_callback(F, COMM_ERR_DNS);
589      return;
590    }
# Line 631 | Line 598 | comm_connect_dns_callback(void *vptr, st
598     * the DNS record around, and the DNS cache is gone anyway..
599     *     -- adrian
600     */
601 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
601 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
602    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
603    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
604      F->connect.hostaddr.ss_port;
605 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
605 >  F->connect.hostaddr.ss_len = addr->ss_len;
606  
607    /* Now, call the tryconnect() routine to try a connect() */
641  MyFree(F->dns_query);
642  F->dns_query = NULL;
608    comm_connect_tryconnect(F, NULL);
609   }
610  
# Line 667 | Line 632 | comm_connect_tryconnect(fde_t *fd, void
632    /* Error? */
633    if (retval < 0)
634    {
670 #ifdef _WIN32
671    errno = WSAGetLastError();
672 #endif
635      /*
636       * If we get EISCONN, then we've already connect()ed the socket,
637       * which is a good thing.
# Line 728 | Line 690 | comm_open(fde_t *F, int family, int sock
690     */
691    fd = socket(family, sock_type, proto);
692    if (fd < 0)
731  {
732 #ifdef _WIN32
733    errno = WSAGetLastError();
734 #endif
693      return -1; /* errno will be passed through, yay.. */
736  }
694  
695    execute_callback(setup_socket_cb, fd);
696  
# Line 766 | Line 723 | comm_accept(struct Listener *lptr, struc
723     * reserved fd limit, but we can deal with that when comm_open()
724     * also does it. XXX -- adrian
725     */
726 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
726 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
727    if (newfd < 0)
771  {
772 #ifdef _WIN32
773    errno = WSAGetLastError();
774 #endif
728      return -1;
776  }
729  
730   #ifdef IPV6
731    remove_ipv6_mapping(pn);
# Line 789 | Line 741 | comm_accept(struct Listener *lptr, struc
741  
742   /*
743   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
744 < * 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
744 > * OSes with IPv6 mapping listening on both
745   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
746   *
747   */
# Line 800 | Line 751 | remove_ipv6_mapping(struct irc_ssaddr *a
751   {
752    if (addr->ss.ss_family == AF_INET6)
753    {
754 <    struct sockaddr_in6 *v6;
804 <
805 <    v6 = (struct sockaddr_in6*)addr;
806 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
754 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
755      {
756 <      char v4ip[HOSTIPLEN];
757 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
758 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
759 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
756 >      struct sockaddr_in6 v6;
757 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
758 >
759 >      memcpy(&v6, addr, sizeof(v6));
760 >      memset(v4, 0, sizeof(struct sockaddr_in));
761 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
762 >
763        addr->ss.ss_family = AF_INET;
764        addr->ss_len = sizeof(struct sockaddr_in);
765      }

Diff Legend

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