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 683 by michael, Thu Jun 15 14:28:59 2006 UTC vs.
ircd-hybrid-8/src/s_bsd.c (file contents), Revision 1391 by michael, Tue May 1 15:34:29 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 146 | Line 130 | report_error(int level, const char* text
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));
150 <  ilog(L_ERROR, text, who, strerror(error));
133 >  ilog(LOG_TYPE_IRCD, text, who, strerror(error));
134   }
135  
136   /*
# Line 161 | Line 144 | setup_socket(va_list args)
144    int fd = va_arg(args, int);
145    int opt = 1;
146  
147 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
147 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
148  
149   #ifdef IPTOS_LOWDELAY
150    opt = IPTOS_LOWDELAY;
151 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
151 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
152   #endif
153  
171 #ifndef _WIN32
154    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
155  
156    return NULL;
157   }
# Line 195 | Line 176 | init_comm(void)
176   void
177   close_connection(struct Client *client_p)
178   {
198  struct ConfItem *conf;
179    struct AccessItem *aconf;
180    struct ClassItem *aclass;
181 +  dlink_node *ptr = NULL;
182  
183 <  assert(NULL != client_p);
183 >  assert(client_p);
184  
185    if (!IsDead(client_p))
186    {
# Line 212 | Line 193 | close_connection(struct Client *client_p
193      send_queued_write(client_p);
194    }
195  
196 <  if (IsServer(client_p))
196 >  if (IsClient(client_p))
197    {
198 <    ServerStats->is_sv++;
199 <    ServerStats->is_sbs += client_p->localClient->send.bytes;
200 <    ServerStats->is_sbr += client_p->localClient->recv.bytes;
201 <    ServerStats->is_sti += CurrentTime - client_p->firsttime;
202 <
203 <    /* XXX Does this even make any sense at all anymore?
204 <     * scheduling a 'quick' reconnect could cause a pile of
205 <     * nick collides under TSora protocol... -db
206 <     */
207 <    /*
208 <     * If the connection has been up for a long amount of time, schedule
209 <     * a 'quick' reconnect, else reset the next-connect cycle.
210 <     */
230 <    if ((conf = find_conf_exact(SERVER_TYPE,
231 <                                  client_p->name, client_p->username,
232 <                                  client_p->host)))
198 >    ++ServerStats.is_cl;
199 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
200 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
201 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
202 >  }
203 >  else if (IsServer(client_p))
204 >  {
205 >    ++ServerStats.is_sv;
206 >    ServerStats.is_sbs += client_p->localClient->send.bytes;
207 >    ServerStats.is_sbr += client_p->localClient->recv.bytes;
208 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
209 >
210 >    DLINK_FOREACH(ptr, server_items.head)
211      {
212 +      struct ConfItem *conf = ptr->data;
213 +
214 +      if (irccmp(conf->name, client_p->name))
215 +        continue;
216 +
217        /*
218 <       * Reschedule a faster reconnect, if this was a automatically
219 <       * connected configuration entry. (Note that if we have had
237 <       * a rehash in between, the status has been changed to
238 <       * CONF_ILLEGAL). But only do this if it was a "good" link.
218 >       * Reset next-connect cycle of all connect{} blocks that match
219 >       * this servername.
220         */
221 <      aconf = (struct AccessItem *)map_to_conf(conf);
222 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
223 <      aconf->hold = time(NULL);
243 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
244 <        HANGONRETRYDELAY : ConFreq(aclass);
245 <      if (nextconnect > aconf->hold)
246 <        nextconnect = aconf->hold;
221 >      aconf  = map_to_conf(conf);
222 >      aclass = map_to_conf(aconf->class_ptr);
223 >      aconf->hold = CurrentTime + aclass->con_freq;
224      }
225    }
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  }
226    else
227 <    ServerStats->is_ni++;
227 >    ++ServerStats.is_ni;
228  
229   #ifdef HAVE_LIBCRYPTO
230    if (client_p->localClient->fd.ssl)
# Line 268 | Line 238 | close_connection(struct Client *client_p
238    if (client_p->localClient->fd.flags.open)
239      fd_close(&client_p->localClient->fd);
240  
271  if (HasServlink(client_p))
272  {
273    if (client_p->localClient->ctrlfd.flags.open)
274      fd_close(&client_p->localClient->ctrlfd);
275  }
276
241    dbuf_clear(&client_p->localClient->buf_sendq);
242    dbuf_clear(&client_p->localClient->buf_recvq);
243    
# Line 324 | Line 288 | ssl_handshake(int fd, struct Client *cli
288   void
289   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
290   {
291 <  struct Client *new_client;
328 <
329 <  assert(NULL != listener);
330 <
331 <  new_client = make_client(NULL);
291 >  struct Client *new_client = make_client(NULL);
292  
293    fd_open(&new_client->localClient->fd, fd, 1,
294            (listener->flags & LISTENER_SSL) ?
295            "Incoming SSL connection" : "Incoming connection");
296  
297 <  /*
297 >  /*
298     * copy address to 'sockhost' as a string, copy it to host too
299     * so we have something valid to put into error messages...
300     */
301    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
302  
303 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
304 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
305 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
303 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
304 >              new_client->localClient->ip.ss_len, new_client->sockhost,
305 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
306    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);
307  
308 <  if (new_client->localClient->aftype == AF_INET6 &&
352 <      ConfigFileEntry.dot_in_ip6_addr == 1)
308 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
309    {
310 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
311 <    strlcat(new_client->host, ".", HOSTLEN+1);
310 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
311 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
312 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
313 >    new_client->sockhost[0] = '0';
314    }
315    else
316 < #endif
359 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
316 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
317  
361  new_client->connect_id = ++connect_id;
318    new_client->localClient->listener = listener;
319    ++listener->ref_count;
320  
321   #ifdef HAVE_LIBCRYPTO
322    if (listener->flags & LISTENER_SSL)
323    {
324 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
324 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
325      {
326 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
326 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
327             ERR_error_string(ERR_get_error(), NULL));
328  
329        SetDead(new_client);
# Line 510 | Line 466 | comm_connect_tcp(fde_t *fd, const char *
466                   void *data, int aftype, int timeout)
467   {
468    struct addrinfo hints, *res;
469 <  char portname[PORTNAMELEN+1];
469 >  char portname[PORTNAMELEN + 1];
470  
471    assert(callback);
472    fd->connect.callback = callback;
# Line 542 | Line 498 | comm_connect_tcp(fde_t *fd, const char *
498    hints.ai_socktype = SOCK_STREAM;
499    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
500  
501 <  snprintf(portname, PORTNAMELEN, "%d", port);
501 >  snprintf(portname, sizeof(portname), "%d", port);
502  
503 <  if (irc_getaddrinfo(host, portname, &hints, &res))
503 >  if (getaddrinfo(host, portname, &hints, &res))
504    {
505      /* Send the DNS request, for the next level */
506 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
507 <    fd->dns_query->ptr = fd;
508 <    fd->dns_query->callback = comm_connect_dns_callback;
509 <    gethost_byname(host, fd->dns_query);
506 >    if (aftype == AF_INET6)
507 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
508 >    else
509 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
510    }
511    else
512    {
# Line 560 | Line 516 | comm_connect_tcp(fde_t *fd, const char *
516      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
517      fd->connect.hostaddr.ss_len = res->ai_addrlen;
518      fd->connect.hostaddr.ss.ss_family = res->ai_family;
519 <    irc_freeaddrinfo(res);
519 >    freeaddrinfo(res);
520      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
521      comm_connect_tryconnect(fd, NULL);
522    }
# Line 608 | Line 564 | comm_connect_timeout(fde_t *fd, void *no
564   * otherwise we initiate the connect()
565   */
566   static void
567 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
567 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
568   {
569    fde_t *F = vptr;
570  
571 <  if (reply == NULL)
571 >  if (name == NULL)
572    {
617    MyFree(F->dns_query);
618    F->dns_query = NULL;
573      comm_connect_callback(F, COMM_ERR_DNS);
574      return;
575    }
# Line 629 | Line 583 | comm_connect_dns_callback(void *vptr, st
583     * the DNS record around, and the DNS cache is gone anyway..
584     *     -- adrian
585     */
586 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
586 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
587    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
588    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
589      F->connect.hostaddr.ss_port;
590 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
590 >  F->connect.hostaddr.ss_len = addr->ss_len;
591  
592    /* Now, call the tryconnect() routine to try a connect() */
639  MyFree(F->dns_query);
640  F->dns_query = NULL;
593    comm_connect_tryconnect(F, NULL);
594   }
595  
# Line 665 | Line 617 | comm_connect_tryconnect(fde_t *fd, void
617    /* Error? */
618    if (retval < 0)
619    {
668 #ifdef _WIN32
669    errno = WSAGetLastError();
670 #endif
620      /*
621       * If we get EISCONN, then we've already connect()ed the socket,
622       * which is a good thing.
# Line 726 | Line 675 | comm_open(fde_t *F, int family, int sock
675     */
676    fd = socket(family, sock_type, proto);
677    if (fd < 0)
729  {
730 #ifdef _WIN32
731    errno = WSAGetLastError();
732 #endif
678      return -1; /* errno will be passed through, yay.. */
734  }
679  
680    execute_callback(setup_socket_cb, fd);
681  
# Line 764 | Line 708 | comm_accept(struct Listener *lptr, struc
708     * reserved fd limit, but we can deal with that when comm_open()
709     * also does it. XXX -- adrian
710     */
711 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
711 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
712    if (newfd < 0)
769  {
770 #ifdef _WIN32
771    errno = WSAGetLastError();
772 #endif
713      return -1;
774  }
714  
715   #ifdef IPV6
716    remove_ipv6_mapping(pn);
# Line 787 | Line 726 | comm_accept(struct Listener *lptr, struc
726  
727   /*
728   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
729 < * This function should really inspect the struct itself rather than relying
791 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
729 > * OSes with IPv6 mapping listening on both
730   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
731   *
732   */
# Line 798 | Line 736 | remove_ipv6_mapping(struct irc_ssaddr *a
736   {
737    if (addr->ss.ss_family == AF_INET6)
738    {
739 <    struct sockaddr_in6 *v6;
802 <
803 <    v6 = (struct sockaddr_in6*)addr;
804 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
739 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
740      {
741 <      char v4ip[HOSTIPLEN];
742 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
743 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
744 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
741 >      struct sockaddr_in6 v6;
742 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
743 >
744 >      memcpy(&v6, addr, sizeof(v6));
745 >      memset(v4, 0, sizeof(struct sockaddr_in));
746 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
747 >
748        addr->ss.ss_family = AF_INET;
749        addr->ss_len = sizeof(struct sockaddr_in);
750      }

Diff Legend

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