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 871 by db, Fri Sep 14 03:33:44 2007 UTC vs.
ircd-hybrid-8/src/s_bsd.c (file contents), Revision 1156 by michael, Tue Aug 9 20:29:20 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"
# Line 35 | Line 34
34   #include "dbuf.h"
35   #include "event.h"
36   #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
37   #include "ircd.h"
41 #include "list.h"
38   #include "listener.h"
39   #include "numeric.h"
40   #include "packet.h"
41   #include "irc_res.h"
46 #include "inet_misc.h"
42   #include "restart.h"
43   #include "s_auth.h"
44   #include "s_conf.h"
45   #include "s_log.h"
46   #include "s_serv.h"
52 #include "s_stats.h"
47   #include "send.h"
48   #include "memory.h"
49   #include "s_user.h"
# Line 61 | Line 55 | static const char *comm_err_str[] = { "C
55  
56   struct Callback *setup_socket_cb = NULL;
57  
58 < static void comm_connect_callback(fde_t *fd, int status);
58 > static void comm_connect_callback(fde_t *, int);
59   static PF comm_connect_timeout;
60 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
60 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
61   static PF comm_connect_tryconnect;
62  
69 extern void init_netio(void);
63  
64   /* check_can_use_v6()
65   *  Check if the system can open AF_INET6 sockets
# Line 82 | Line 75 | check_can_use_v6(void)
75    else
76    {
77      ServerInfo.can_use_v6 = 1;
85 #ifdef _WIN32
86    closesocket(v6);
87 #else
78      close(v6);
89 #endif
79    }
80   #else
81    ServerInfo.can_use_v6 = 0;
# Line 102 | Line 91 | check_can_use_v6(void)
91   int
92   get_sockerr(int fd)
93   {
105 #ifndef _WIN32
94    int errtmp = errno;
107 #else
108  int errtmp = WSAGetLastError();
109 #endif
95   #ifdef SO_ERROR
96    int err = 0;
97    socklen_t len = sizeof(err);
98  
99 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
99 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
100    {
101      if (err)
102        errtmp = err;
# Line 161 | Line 146 | setup_socket(va_list args)
146    int fd = va_arg(args, int);
147    int opt = 1;
148  
149 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
149 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
150  
151   #ifdef IPTOS_LOWDELAY
152    opt = IPTOS_LOWDELAY;
153 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
153 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
154   #endif
155  
171 #ifndef _WIN32
156    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
157  
158    return NULL;
159   }
# Line 212 | Line 195 | close_connection(struct Client *client_p
195      send_queued_write(client_p);
196    }
197  
198 <  if (IsServer(client_p))
198 >  if (IsClient(client_p))
199    {
200 <    ServerStats->is_sv++;
201 <    ServerStats->is_sbs += client_p->localClient->send.bytes;
202 <    ServerStats->is_sbr += client_p->localClient->recv.bytes;
203 <    ServerStats->is_sti += CurrentTime - client_p->firsttime;
200 >    ++ServerStats.is_cl;
201 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
202 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
203 >    ServerStats.is_cti += CurrentTime - client_p->firsttime;
204 >  }
205 >  else if (IsServer(client_p))
206 >  {
207 >    ++ServerStats.is_sv;
208 >    ServerStats.is_sbs += client_p->localClient->send.bytes;
209 >    ServerStats.is_sbr += client_p->localClient->recv.bytes;
210 >    ServerStats.is_sti += CurrentTime - client_p->firsttime;
211  
212      /* XXX Does this even make any sense at all anymore?
213       * scheduling a 'quick' reconnect could cause a pile of
# Line 227 | Line 217 | close_connection(struct Client *client_p
217       * If the connection has been up for a long amount of time, schedule
218       * a 'quick' reconnect, else reset the next-connect cycle.
219       */
220 <    if ((conf = find_conf_exact(SERVER_TYPE,
221 <                                  client_p->name, client_p->username,
232 <                                  client_p->host)))
220 >    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
221 >                                client_p->username, client_p->host)))
222      {
223        /*
224         * Reschedule a faster reconnect, if this was a automatically
# Line 237 | Line 226 | close_connection(struct Client *client_p
226         * a rehash in between, the status has been changed to
227         * CONF_ILLEGAL). But only do this if it was a "good" link.
228         */
229 <      aconf = (struct AccessItem *)map_to_conf(conf);
230 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
229 >      aconf  = map_to_conf(conf);
230 >      aclass = map_to_conf(aconf->class_ptr);
231        aconf->hold = time(NULL);
232        aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
233          HANGONRETRYDELAY : ConFreq(aclass);
245      if (nextconnect > aconf->hold)
246        nextconnect = aconf->hold;
234      }
235    }
249  else if (IsClient(client_p))
250  {
251    ServerStats->is_cl++;
252    ServerStats->is_cbs += client_p->localClient->send.bytes;
253    ServerStats->is_cbr += client_p->localClient->recv.bytes;
254    ServerStats->is_cti += CurrentTime - client_p->firsttime;
255  }
236    else
237 <    ServerStats->is_ni++;
237 >    ++ServerStats.is_ni;
238  
239   #ifdef HAVE_LIBCRYPTO
240    if (client_p->localClient->fd.ssl)
# Line 324 | Line 304 | ssl_handshake(int fd, struct Client *cli
304   void
305   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
306   {
307 <  struct Client *new_client;
328 <
329 <  assert(NULL != listener);
330 <
331 <  new_client = make_client(NULL);
307 >  struct Client *new_client = make_client(NULL);
308  
309    fd_open(&new_client->localClient->fd, fd, 1,
310            (listener->flags & LISTENER_SSL) ?
311            "Incoming SSL connection" : "Incoming connection");
312  
313 <  /*
313 >  /*
314     * copy address to 'sockhost' as a string, copy it to host too
315     * so we have something valid to put into error messages...
316     */
317    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
318  
319 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
320 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
321 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
319 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
320 >              new_client->localClient->ip.ss_len, new_client->sockhost,
321 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
322    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
347 #ifdef IPV6
348  if (new_client->sockhost[0] == ':')
349    strlcat(new_client->host, "0", HOSTLEN+1);
323  
324 <  if (new_client->localClient->aftype == AF_INET6 &&
352 <      ConfigFileEntry.dot_in_ip6_addr == 1)
324 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
325    {
326 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
327 <    strlcat(new_client->host, ".", HOSTLEN+1);
326 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
327 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
328 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
329 >    new_client->sockhost[0] = '0';
330    }
331    else
332 < #endif
359 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
332 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
333  
361  new_client->connect_id = ++connect_id;
334    new_client->localClient->listener = listener;
335    ++listener->ref_count;
336  
337   #ifdef HAVE_LIBCRYPTO
338    if (listener->flags & LISTENER_SSL)
339    {
340 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
340 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
341      {
342        ilog(L_CRIT, "SSL_new() ERROR! -- %s",
343             ERR_error_string(ERR_get_error(), NULL));
# Line 510 | Line 482 | comm_connect_tcp(fde_t *fd, const char *
482                   void *data, int aftype, int timeout)
483   {
484    struct addrinfo hints, *res;
485 <  char portname[PORTNAMELEN+1];
485 >  char portname[PORTNAMELEN + 1];
486  
487    assert(callback);
488    fd->connect.callback = callback;
# Line 542 | Line 514 | comm_connect_tcp(fde_t *fd, const char *
514    hints.ai_socktype = SOCK_STREAM;
515    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
516  
517 <  snprintf(portname, PORTNAMELEN, "%d", port);
517 >  snprintf(portname, sizeof(portname), "%d", port);
518  
519 <  if (irc_getaddrinfo(host, portname, &hints, &res))
519 >  if (getaddrinfo(host, portname, &hints, &res))
520    {
521      /* Send the DNS request, for the next level */
550    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
551    fd->dns_query->ptr = fd;
552    fd->dns_query->callback = comm_connect_dns_callback;
522      if (aftype == AF_INET6)
523 <      gethost_byname_type(host, fd->dns_query, T_AAAA);
523 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
524      else
525 <      gethost_byname_type(host, fd->dns_query, T_A);
525 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
526    }
527    else
528    {
# Line 563 | Line 532 | comm_connect_tcp(fde_t *fd, const char *
532      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
533      fd->connect.hostaddr.ss_len = res->ai_addrlen;
534      fd->connect.hostaddr.ss.ss_family = res->ai_family;
535 <    irc_freeaddrinfo(res);
535 >    freeaddrinfo(res);
536      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
537      comm_connect_tryconnect(fd, NULL);
538    }
# Line 611 | Line 580 | comm_connect_timeout(fde_t *fd, void *no
580   * otherwise we initiate the connect()
581   */
582   static void
583 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
583 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
584   {
585    fde_t *F = vptr;
586  
587 <  if (reply == NULL)
587 >  if (name == NULL)
588    {
620    MyFree(F->dns_query);
621    F->dns_query = NULL;
589      comm_connect_callback(F, COMM_ERR_DNS);
590      return;
591    }
# Line 632 | Line 599 | comm_connect_dns_callback(void *vptr, st
599     * the DNS record around, and the DNS cache is gone anyway..
600     *     -- adrian
601     */
602 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
602 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
603    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
604    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
605      F->connect.hostaddr.ss_port;
606 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
606 >  F->connect.hostaddr.ss_len = addr->ss_len;
607  
608    /* Now, call the tryconnect() routine to try a connect() */
642  MyFree(F->dns_query);
643  F->dns_query = NULL;
609    comm_connect_tryconnect(F, NULL);
610   }
611  
# Line 668 | Line 633 | comm_connect_tryconnect(fde_t *fd, void
633    /* Error? */
634    if (retval < 0)
635    {
671 #ifdef _WIN32
672    errno = WSAGetLastError();
673 #endif
636      /*
637       * If we get EISCONN, then we've already connect()ed the socket,
638       * which is a good thing.
# Line 729 | Line 691 | comm_open(fde_t *F, int family, int sock
691     */
692    fd = socket(family, sock_type, proto);
693    if (fd < 0)
732  {
733 #ifdef _WIN32
734    errno = WSAGetLastError();
735 #endif
694      return -1; /* errno will be passed through, yay.. */
737  }
695  
696    execute_callback(setup_socket_cb, fd);
697  
# Line 767 | Line 724 | comm_accept(struct Listener *lptr, struc
724     * reserved fd limit, but we can deal with that when comm_open()
725     * also does it. XXX -- adrian
726     */
727 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
727 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
728    if (newfd < 0)
772  {
773 #ifdef _WIN32
774    errno = WSAGetLastError();
775 #endif
729      return -1;
777  }
730  
731   #ifdef IPV6
732    remove_ipv6_mapping(pn);
# Line 790 | Line 742 | comm_accept(struct Listener *lptr, struc
742  
743   /*
744   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
745 < * This function should really inspect the struct itself rather than relying
794 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
745 > * OSes with IPv6 mapping listening on both
746   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
747   *
748   */
# Line 801 | Line 752 | remove_ipv6_mapping(struct irc_ssaddr *a
752   {
753    if (addr->ss.ss_family == AF_INET6)
754    {
755 <    struct sockaddr_in6 *v6;
805 <
806 <    v6 = (struct sockaddr_in6*)addr;
807 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
755 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
756      {
757 <      char v4ip[HOSTIPLEN];
758 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
759 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
760 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
757 >      struct sockaddr_in6 v6;
758 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
759 >
760 >      memcpy(&v6, addr, sizeof(v6));
761 >      memset(v4, 0, sizeof(struct sockaddr_in));
762 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
763 >
764        addr->ss.ss_family = AF_INET;
765        addr->ss_len = sizeof(struct sockaddr_in);
766      }

Diff Legend

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