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 34 by lusky, Sun Oct 2 21:05:51 2005 UTC vs.
ircd-hybrid/trunk/src/s_bsd.c (file contents), Revision 1649 by michael, Sat Nov 10 19:27:13 2012 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"
52 #include "s_stats.h"
46   #include "send.h"
47   #include "memory.h"
48   #include "s_user.h"
# Line 61 | 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  
69 extern void init_netio(void);
62  
63   /* check_can_use_v6()
64   *  Check if the system can open AF_INET6 sockets
# Line 82 | Line 74 | check_can_use_v6(void)
74    else
75    {
76      ServerInfo.can_use_v6 = 1;
85 #ifdef _WIN32
86    closesocket(v6);
87 #else
77      close(v6);
89 #endif
78    }
79   #else
80    ServerInfo.can_use_v6 = 0;
# Line 102 | Line 90 | check_can_use_v6(void)
90   int
91   get_sockerr(int fd)
92   {
105 #ifndef _WIN32
93    int errtmp = errno;
107 #else
108  int errtmp = WSAGetLastError();
109 #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 145 | 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 161 | 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  
171 #ifndef _WIN32
155    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
156  
157    return NULL;
158   }
# Line 195 | Line 177 | init_comm(void)
177   void
178   close_connection(struct Client *client_p)
179   {
180 <  struct ConfItem *conf;
181 <  struct AccessItem *aconf;
182 <  struct ClassItem *aclass;
201 <
202 <  assert(NULL != client_p);
203 <
204 <  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;
210 <
211 <    /* XXX Does this even make any sense at all anymore?
212 <     * scheduling a 'quick' reconnect could cause a pile of
213 <     * nick collides under TSora protocol... -db
214 <     */
215 <    /*
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,
221 <                                  client_p->host)))
222 <    {
223 <      /*
224 <       * Reschedule a faster reconnect, if this was a automatically
225 <       * connected configuration entry. (Note that if we have had
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);
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;
236 <    }
237 <  }
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 <  }
245 <  else
246 <    ServerStats->is_ni++;
180 >  dlink_node *ptr = NULL;
181 >
182 >  assert(client_p);
183  
184    if (!IsDead(client_p))
185    {
# Line 256 | Line 192 | close_connection(struct Client *client_p
192      send_queued_write(client_p);
193    }
194  
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->localClient->firsttime;
208 +
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 +       * Reset next-connect cycle of all connect{} blocks that match
218 +       * this servername.
219 +       */
220 +      conf->until = CurrentTime + conf->class->con_freq;
221 +    }
222 +  }
223 +  else
224 +    ++ServerStats.is_ni;
225 +
226   #ifdef HAVE_LIBCRYPTO
227    if (client_p->localClient->fd.ssl)
228 <    SSL_shutdown(client_p->localClient->fd.ssl);
228 >  {
229 >    SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
230 >
231 >    if (!SSL_shutdown(client_p->localClient->fd.ssl))
232 >      SSL_shutdown(client_p->localClient->fd.ssl);
233 >  }
234   #endif
235    if (client_p->localClient->fd.flags.open)
236      fd_close(&client_p->localClient->fd);
237  
266  if (HasServlink(client_p))
267  {
268    if (client_p->localClient->ctrlfd.flags.open)
269      fd_close(&client_p->localClient->ctrlfd);
270  }
271
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  
# Line 317 | Line 283 | ssl_handshake(int fd, struct Client *cli
283   * any client list yet.
284   */
285   void
286 < add_connection(struct Listener* listener, int fd)
286 > add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
287   {
288 <  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 <   */
288 >  struct Client *new_client = make_client(NULL);
289  
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  }
348
349 #ifdef IPV6
350  remove_ipv6_mapping(&irn);
351 #else
352  irn.ss_len = len;
353 #endif
354  new_client = make_client(NULL);
290    fd_open(&new_client->localClient->fd, fd, 1,
291            (listener->flags & LISTENER_SSL) ?
292            "Incoming SSL connection" : "Incoming connection");
358  memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
293  
294 <  /*
294 >  /*
295     * copy address to 'sockhost' as a string, copy it to host too
296     * so we have something valid to put into error messages...
297     */
298 <  new_client->localClient->port = ntohs(irn.ss_port);
365 <  memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr));
298 >  memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
299  
300 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
301 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
302 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
300 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
301 >              new_client->localClient->ip.ss_len, new_client->sockhost,
302 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
303    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
304  
305 <  *new_client->host = '\0';
373 < #ifdef IPV6
374 <  if (*new_client->sockhost == ':')
375 <    strlcat(new_client->host, "0", HOSTLEN+1);
376 <
377 <  if (new_client->localClient->aftype == AF_INET6 &&
378 <      ConfigFileEntry.dot_in_ip6_addr == 1)
305 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
306    {
307 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
308 <    strlcat(new_client->host, ".", HOSTLEN+1);
309 <  } else
310 < #endif
311 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
307 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
308 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
309 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
310 >    new_client->sockhost[0] = '0';
311 >  }
312 >  else
313 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
314  
315    new_client->localClient->listener = listener;
316    ++listener->ref_count;
317  
389  connect_id++;
390  new_client->connect_id = connect_id;
391
318   #ifdef HAVE_LIBCRYPTO
319 <  if ((listener->flags & LISTENER_SSL))
319 >  if (listener->flags & LISTENER_SSL)
320    {
321 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
321 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
322      {
323 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
323 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
324             ERR_error_string(ERR_get_error(), NULL));
325  
326        SetDead(new_client);
# Line 537 | Line 463 | comm_connect_tcp(fde_t *fd, const char *
463                   void *data, int aftype, int timeout)
464   {
465    struct addrinfo hints, *res;
466 <  char portname[PORTNAMELEN+1];
466 >  char portname[PORTNAMELEN + 1];
467  
468    assert(callback);
469    fd->connect.callback = callback;
# Line 569 | Line 495 | comm_connect_tcp(fde_t *fd, const char *
495    hints.ai_socktype = SOCK_STREAM;
496    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
497  
498 <  snprintf(portname, PORTNAMELEN, "%d", port);
498 >  snprintf(portname, sizeof(portname), "%d", port);
499  
500 <  if (irc_getaddrinfo(host, portname, &hints, &res))
500 >  if (getaddrinfo(host, portname, &hints, &res))
501    {
502      /* Send the DNS request, for the next level */
503 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
504 <    fd->dns_query->ptr = fd;
505 <    fd->dns_query->callback = comm_connect_dns_callback;
506 <    gethost_byname(host, fd->dns_query);
503 >    if (aftype == AF_INET6)
504 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
505 >    else
506 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
507    }
508    else
509    {
# Line 587 | Line 513 | comm_connect_tcp(fde_t *fd, const char *
513      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
514      fd->connect.hostaddr.ss_len = res->ai_addrlen;
515      fd->connect.hostaddr.ss.ss_family = res->ai_family;
516 <    irc_freeaddrinfo(res);
516 >    freeaddrinfo(res);
517      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
518      comm_connect_tryconnect(fd, NULL);
519    }
# Line 635 | Line 561 | comm_connect_timeout(fde_t *fd, void *no
561   * otherwise we initiate the connect()
562   */
563   static void
564 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
564 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
565   {
566    fde_t *F = vptr;
567  
568 <  if (reply == NULL)
568 >  if (name == NULL)
569    {
644    MyFree(F->dns_query);
645    F->dns_query = NULL;
570      comm_connect_callback(F, COMM_ERR_DNS);
571      return;
572    }
# Line 656 | Line 580 | comm_connect_dns_callback(void *vptr, st
580     * the DNS record around, and the DNS cache is gone anyway..
581     *     -- adrian
582     */
583 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
583 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
584    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
585    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
586      F->connect.hostaddr.ss_port;
587 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
587 >  F->connect.hostaddr.ss_len = addr->ss_len;
588  
589    /* Now, call the tryconnect() routine to try a connect() */
666  MyFree(F->dns_query);
667  F->dns_query = NULL;
590    comm_connect_tryconnect(F, NULL);
591   }
592  
# Line 692 | Line 614 | comm_connect_tryconnect(fde_t *fd, void
614    /* Error? */
615    if (retval < 0)
616    {
695 #ifdef _WIN32
696    errno = WSAGetLastError();
697 #endif
617      /*
618       * If we get EISCONN, then we've already connect()ed the socket,
619       * which is a good thing.
# Line 753 | Line 672 | comm_open(fde_t *F, int family, int sock
672     */
673    fd = socket(family, sock_type, proto);
674    if (fd < 0)
756  {
757 #ifdef _WIN32
758    errno = WSAGetLastError();
759 #endif
675      return -1; /* errno will be passed through, yay.. */
761  }
676  
677    execute_callback(setup_socket_cb, fd);
678  
# Line 791 | Line 705 | comm_accept(struct Listener *lptr, struc
705     * reserved fd limit, but we can deal with that when comm_open()
706     * also does it. XXX -- adrian
707     */
708 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
708 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
709    if (newfd < 0)
796  {
797 #ifdef _WIN32
798    errno = WSAGetLastError();
799 #endif
710      return -1;
801  }
711  
712   #ifdef IPV6
713    remove_ipv6_mapping(pn);
# Line 814 | Line 723 | comm_accept(struct Listener *lptr, struc
723  
724   /*
725   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
726 < * This function should really inspect the struct itself rather than relying
818 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
726 > * OSes with IPv6 mapping listening on both
727   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
728   *
729   */
# Line 825 | Line 733 | remove_ipv6_mapping(struct irc_ssaddr *a
733   {
734    if (addr->ss.ss_family == AF_INET6)
735    {
736 <    struct sockaddr_in6 *v6;
829 <
830 <    v6 = (struct sockaddr_in6*)addr;
831 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
736 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
737      {
738 <      char v4ip[HOSTIPLEN];
739 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
740 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
741 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
738 >      struct sockaddr_in6 v6;
739 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
740 >
741 >      memcpy(&v6, addr, sizeof(v6));
742 >      memset(v4, 0, sizeof(struct sockaddr_in));
743 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
744 >
745        addr->ss.ss_family = AF_INET;
746        addr->ss_len = sizeof(struct sockaddr_in);
747      }

Diff Legend

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