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/src/s_bsd.c (file contents), Revision 31 by knight, Sun Oct 2 20:34:05 2005 UTC vs.
ircd-hybrid-8/src/s_bsd.c (file contents), Revision 1377 by michael, Mon Apr 30 19:35:16 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 201 | Line 182 | close_connection(struct Client *client_p
182  
183    assert(NULL != client_p);
184  
185 <  if (IsServer(client_p))
185 >  if (!IsDead(client_p))
186 >  {
187 >    /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
188 >    /* there is still a chance that we might send data to this socket
189 >     * even if it is marked as blocked (COMM_SELECT_READ handler is called
190 >     * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
191 >     */
192 >    ClearSendqBlocked(client_p);
193 >    send_queued_write(client_p);
194 >  }
195 >
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;
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      /* XXX Does this even make any sense at all anymore?
211       * scheduling a 'quick' reconnect could cause a pile of
# Line 216 | Line 215 | close_connection(struct Client *client_p
215       * If the connection has been up for a long amount of time, schedule
216       * a 'quick' reconnect, else reset the next-connect cycle.
217       */
218 <    if ((conf = find_conf_exact(SERVER_TYPE,
219 <                                  client_p->name, client_p->username,
221 <                                  client_p->host)))
218 >    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
219 >                                client_p->username, client_p->host)))
220      {
221        /*
222         * Reschedule a faster reconnect, if this was a automatically
# Line 226 | Line 224 | close_connection(struct Client *client_p
224         * a rehash in between, the status has been changed to
225         * CONF_ILLEGAL). But only do this if it was a "good" link.
226         */
227 <      aconf = (struct AccessItem *)map_to_conf(conf);
228 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
227 >      aconf  = map_to_conf(conf);
228 >      aclass = map_to_conf(aconf->class_ptr);
229        aconf->hold = time(NULL);
230 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
231 <        HANGONRETRYDELAY : ConFreq(aclass);
234 <      if (nextconnect > aconf->hold)
235 <        nextconnect = aconf->hold;
230 >      aconf->hold += (aconf->hold - client_p->localClient->since > HANGONGOODLINK) ?
231 >        HANGONRETRYDELAY : aclass->con_freq;
232      }
233    }
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  }
234    else
235 <    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 <  }
235 >    ++ServerStats.is_ni;
236  
237   #ifdef HAVE_LIBCRYPTO
238    if (client_p->localClient->fd.ssl)
239 <    SSL_shutdown(client_p->localClient->fd.ssl);
239 >  {
240 >    SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
241 >
242 >    if (!SSL_shutdown(client_p->localClient->fd.ssl))
243 >      SSL_shutdown(client_p->localClient->fd.ssl);
244 >  }
245   #endif
246    if (client_p->localClient->fd.flags.open)
247      fd_close(&client_p->localClient->fd);
248  
266  if (HasServlink(client_p))
267  {
268    if (client_p->localClient->ctrlfd.flags.open)
269      fd_close(&client_p->localClient->ctrlfd);
270  }
271
249    dbuf_clear(&client_p->localClient->buf_sendq);
250    dbuf_clear(&client_p->localClient->buf_recvq);
251    
# Line 317 | Line 294 | ssl_handshake(int fd, struct Client *cli
294   * any client list yet.
295   */
296   void
297 < add_connection(struct Listener* listener, int fd)
297 > add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
298   {
299 <  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 <   */
299 >  struct Client *new_client = make_client(NULL);
300  
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);
301    fd_open(&new_client->localClient->fd, fd, 1,
302            (listener->flags & LISTENER_SSL) ?
303            "Incoming SSL connection" : "Incoming connection");
358  memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
304  
305 <  /*
305 >  /*
306     * copy address to 'sockhost' as a string, copy it to host too
307     * so we have something valid to put into error messages...
308     */
309 <  new_client->localClient->port = ntohs(irn.ss_port);
365 <  memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr));
309 >  memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
310  
311 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
312 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
313 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
311 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
312 >              new_client->localClient->ip.ss_len, new_client->sockhost,
313 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
314    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
315  
316 <  *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)
316 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
317    {
318 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
319 <    strlcat(new_client->host, ".", HOSTLEN+1);
320 <  } else
321 < #endif
322 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
318 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
319 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
320 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
321 >    new_client->sockhost[0] = '0';
322 >  }
323 >  else
324 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
325  
326    new_client->localClient->listener = listener;
327    ++listener->ref_count;
328  
389  connect_id++;
390  new_client->connect_id = connect_id;
391
329   #ifdef HAVE_LIBCRYPTO
330 <  if ((listener->flags & LISTENER_SSL))
330 >  if (listener->flags & LISTENER_SSL)
331    {
332 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
332 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
333      {
334 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
334 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
335             ERR_error_string(ERR_get_error(), NULL));
336  
337        SetDead(new_client);
# Line 537 | Line 474 | comm_connect_tcp(fde_t *fd, const char *
474                   void *data, int aftype, int timeout)
475   {
476    struct addrinfo hints, *res;
477 <  char portname[PORTNAMELEN+1];
477 >  char portname[PORTNAMELEN + 1];
478  
479    assert(callback);
480    fd->connect.callback = callback;
# Line 569 | Line 506 | comm_connect_tcp(fde_t *fd, const char *
506    hints.ai_socktype = SOCK_STREAM;
507    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
508  
509 <  snprintf(portname, PORTNAMELEN, "%d", port);
509 >  snprintf(portname, sizeof(portname), "%d", port);
510  
511 <  if (irc_getaddrinfo(host, portname, &hints, &res))
511 >  if (getaddrinfo(host, portname, &hints, &res))
512    {
513      /* Send the DNS request, for the next level */
514 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
515 <    fd->dns_query->ptr = fd;
516 <    fd->dns_query->callback = comm_connect_dns_callback;
517 <    gethost_byname(host, fd->dns_query);
514 >    if (aftype == AF_INET6)
515 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
516 >    else
517 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
518    }
519    else
520    {
# Line 587 | Line 524 | comm_connect_tcp(fde_t *fd, const char *
524      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
525      fd->connect.hostaddr.ss_len = res->ai_addrlen;
526      fd->connect.hostaddr.ss.ss_family = res->ai_family;
527 <    irc_freeaddrinfo(res);
527 >    freeaddrinfo(res);
528      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
529      comm_connect_tryconnect(fd, NULL);
530    }
# Line 635 | Line 572 | comm_connect_timeout(fde_t *fd, void *no
572   * otherwise we initiate the connect()
573   */
574   static void
575 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
575 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
576   {
577    fde_t *F = vptr;
578  
579 <  if (reply == NULL)
579 >  if (name == NULL)
580    {
644    MyFree(F->dns_query);
645    F->dns_query = NULL;
581      comm_connect_callback(F, COMM_ERR_DNS);
582      return;
583    }
# Line 656 | Line 591 | comm_connect_dns_callback(void *vptr, st
591     * the DNS record around, and the DNS cache is gone anyway..
592     *     -- adrian
593     */
594 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
594 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
595    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
596    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
597      F->connect.hostaddr.ss_port;
598 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
598 >  F->connect.hostaddr.ss_len = addr->ss_len;
599  
600    /* Now, call the tryconnect() routine to try a connect() */
666  MyFree(F->dns_query);
667  F->dns_query = NULL;
601    comm_connect_tryconnect(F, NULL);
602   }
603  
# Line 692 | Line 625 | comm_connect_tryconnect(fde_t *fd, void
625    /* Error? */
626    if (retval < 0)
627    {
695 #ifdef _WIN32
696    errno = WSAGetLastError();
697 #endif
628      /*
629       * If we get EISCONN, then we've already connect()ed the socket,
630       * which is a good thing.
# Line 753 | Line 683 | comm_open(fde_t *F, int family, int sock
683     */
684    fd = socket(family, sock_type, proto);
685    if (fd < 0)
756  {
757 #ifdef _WIN32
758    errno = WSAGetLastError();
759 #endif
686      return -1; /* errno will be passed through, yay.. */
761  }
687  
688    execute_callback(setup_socket_cb, fd);
689  
# Line 791 | Line 716 | comm_accept(struct Listener *lptr, struc
716     * reserved fd limit, but we can deal with that when comm_open()
717     * also does it. XXX -- adrian
718     */
719 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
719 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
720    if (newfd < 0)
796  {
797 #ifdef _WIN32
798    errno = WSAGetLastError();
799 #endif
721      return -1;
801  }
722  
723   #ifdef IPV6
724    remove_ipv6_mapping(pn);
# Line 814 | Line 734 | comm_accept(struct Listener *lptr, struc
734  
735   /*
736   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
737 < * 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
737 > * OSes with IPv6 mapping listening on both
738   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
739   *
740   */
# Line 825 | Line 744 | remove_ipv6_mapping(struct irc_ssaddr *a
744   {
745    if (addr->ss.ss_family == AF_INET6)
746    {
747 <    struct sockaddr_in6 *v6;
829 <
830 <    v6 = (struct sockaddr_in6*)addr;
831 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
747 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
748      {
749 <      char v4ip[HOSTIPLEN];
750 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
751 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
752 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
749 >      struct sockaddr_in6 v6;
750 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
751 >
752 >      memcpy(&v6, addr, sizeof(v6));
753 >      memset(v4, 0, sizeof(struct sockaddr_in));
754 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
755 >
756        addr->ss.ss_family = AF_INET;
757        addr->ss_len = sizeof(struct sockaddr_in);
758      }

Comparing:
ircd-hybrid/src/s_bsd.c (property svn:keywords), Revision 31 by knight, Sun Oct 2 20:34:05 2005 UTC vs.
ircd-hybrid-8/src/s_bsd.c (property svn:keywords), Revision 1377 by michael, Mon Apr 30 19:35:16 2012 UTC

# Line 1 | Line 1
1 < "Id Revision"
1 > Id Revision

Diff Legend

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