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 169 by michael, Fri Oct 21 09:37:16 2005 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 201 | Line 184 | close_connection(struct Client *client_p
184  
185    assert(NULL != client_p);
186  
187 <  if (IsServer(client_p))
187 >  if (!IsDead(client_p))
188 >  {
189 >    /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
190 >    /* there is still a chance that we might send data to this socket
191 >     * even if it is marked as blocked (COMM_SELECT_READ handler is called
192 >     * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
193 >     */
194 >    ClearSendqBlocked(client_p);
195 >    send_queued_write(client_p);
196 >  }
197 >
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 216 | 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,
221 <                                  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 226 | 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);
234      if (nextconnect > aconf->hold)
235        nextconnect = aconf->hold;
234      }
235    }
238  else if (IsClient(client_p))
239  {
240    ServerStats->is_cl++;
241    ServerStats->is_cbs += client_p->localClient->send.bytes;
242    ServerStats->is_cbr += client_p->localClient->recv.bytes;
243    ServerStats->is_cti += CurrentTime - client_p->firsttime;
244  }
236    else
237 <    ServerStats->is_ni++;
247 <
248 <  if (!IsDead(client_p))
249 <  {
250 <    /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
251 <    /* there is still a chance that we might send data to this socket
252 <     * even if it is marked as blocked (COMM_SELECT_READ handler is called
253 <     * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
254 <     */
255 <    ClearSendqBlocked(client_p);
256 <    send_queued_write(client_p);
257 <  }
237 >    ++ServerStats.is_ni;
238  
239   #ifdef HAVE_LIBCRYPTO
240    if (client_p->localClient->fd.ssl)
241 <    SSL_shutdown(client_p->localClient->fd.ssl);
241 >  {
242 >    SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
243 >
244 >    if (!SSL_shutdown(client_p->localClient->fd.ssl))
245 >      SSL_shutdown(client_p->localClient->fd.ssl);
246 >  }
247   #endif
248    if (client_p->localClient->fd.flags.open)
249      fd_close(&client_p->localClient->fd);
# Line 317 | Line 302 | ssl_handshake(int fd, struct Client *cli
302   * any client list yet.
303   */
304   void
305 < add_connection(struct Listener* listener, int fd)
305 > add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
306   {
307 <  struct Client *new_client;
323 <  socklen_t len = sizeof(struct irc_ssaddr);
324 <  struct irc_ssaddr irn;
325 <  assert(NULL != listener);
326 <
327 <  /*
328 <   * get the client socket name from the socket
329 <   * the client has already been checked out in accept_connection
330 <   */
331 <
332 <  memset(&irn, 0, sizeof(irn));
333 <  if (getpeername(fd, (struct sockaddr *)&irn, (socklen_t *)&len))
334 <  {
335 < #ifdef _WIN32
336 <    errno = WSAGetLastError();
337 < #endif
338 <    report_error(L_ALL, "Failed in adding new connection %s :%s",
339 <            get_listener_name(listener), errno);
340 <    ServerStats->is_ref++;
341 < #ifdef _WIN32
342 <    closesocket(fd);
343 < #else
344 <    close(fd);
345 < #endif
346 <    return;
347 <  }
307 >  struct Client *new_client = make_client(NULL);
308  
349 #ifdef IPV6
350  remove_ipv6_mapping(&irn);
351 #else
352  irn.ss_len = len;
353 #endif
354  new_client = make_client(NULL);
309    fd_open(&new_client->localClient->fd, fd, 1,
310            (listener->flags & LISTENER_SSL) ?
311            "Incoming SSL connection" : "Incoming connection");
358  memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
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));
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;
323  
324 <  *new_client->host = '\0';
372 < #ifdef IPV6
373 <  if (*new_client->sockhost == ':')
374 <    strlcat(new_client->host, "0", HOSTLEN+1);
375 <
376 <  if (new_client->localClient->aftype == AF_INET6 &&
377 <      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);
328 <  } else
329 < #endif
330 <    strlcat(new_client->host, new_client->sockhost,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 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
333  
334    new_client->localClient->listener = listener;
335    ++listener->ref_count;
336  
388  connect_id++;
389  new_client->connect_id = connect_id;
390
337   #ifdef HAVE_LIBCRYPTO
338 <  if ((listener->flags & LISTENER_SSL))
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 536 | 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 568 | 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 */
522 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
523 <    fd->dns_query->ptr = fd;
524 <    fd->dns_query->callback = comm_connect_dns_callback;
525 <    gethost_byname(host, fd->dns_query);
522 >    if (aftype == AF_INET6)
523 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
524 >    else
525 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
526    }
527    else
528    {
# Line 586 | 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 634 | 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    {
643    MyFree(F->dns_query);
644    F->dns_query = NULL;
589      comm_connect_callback(F, COMM_ERR_DNS);
590      return;
591    }
# Line 655 | 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() */
665  MyFree(F->dns_query);
666  F->dns_query = NULL;
609    comm_connect_tryconnect(F, NULL);
610   }
611  
# Line 691 | Line 633 | comm_connect_tryconnect(fde_t *fd, void
633    /* Error? */
634    if (retval < 0)
635    {
694 #ifdef _WIN32
695    errno = WSAGetLastError();
696 #endif
636      /*
637       * If we get EISCONN, then we've already connect()ed the socket,
638       * which is a good thing.
# Line 752 | Line 691 | comm_open(fde_t *F, int family, int sock
691     */
692    fd = socket(family, sock_type, proto);
693    if (fd < 0)
755  {
756 #ifdef _WIN32
757    errno = WSAGetLastError();
758 #endif
694      return -1; /* errno will be passed through, yay.. */
760  }
695  
696    execute_callback(setup_socket_cb, fd);
697  
# Line 790 | 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)
795  {
796 #ifdef _WIN32
797    errno = WSAGetLastError();
798 #endif
729      return -1;
800  }
730  
731   #ifdef IPV6
732    remove_ipv6_mapping(pn);
# Line 813 | 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
817 < * 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 824 | Line 752 | remove_ipv6_mapping(struct irc_ssaddr *a
752   {
753    if (addr->ss.ss_family == AF_INET6)
754    {
755 <    struct sockaddr_in6 *v6;
828 <
829 <    v6 = (struct sockaddr_in6*)addr;
830 <    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)