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.
Revision 1028 by michael, Sun Nov 8 13:03:38 2009 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 38 | Line 37
37   #include "irc_getnameinfo.h"
38   #include "irc_getaddrinfo.h"
39   #include "ircd.h"
41 #include "list.h"
40   #include "listener.h"
41   #include "numeric.h"
42   #include "packet.h"
# Line 49 | Line 47
47   #include "s_conf.h"
48   #include "s_log.h"
49   #include "s_serv.h"
52 #include "s_stats.h"
50   #include "send.h"
51   #include "memory.h"
52   #include "s_user.h"
# Line 61 | Line 58 | static const char *comm_err_str[] = { "C
58  
59   struct Callback *setup_socket_cb = NULL;
60  
61 < static void comm_connect_callback(fde_t *fd, int status);
61 > static void comm_connect_callback(fde_t *, int);
62   static PF comm_connect_timeout;
63 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
63 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
64   static PF comm_connect_tryconnect;
65  
69 extern void init_netio(void);
66  
67   /* check_can_use_v6()
68   *  Check if the system can open AF_INET6 sockets
# Line 82 | Line 78 | check_can_use_v6(void)
78    else
79    {
80      ServerInfo.can_use_v6 = 1;
85 #ifdef _WIN32
86    closesocket(v6);
87 #else
81      close(v6);
89 #endif
82    }
83   #else
84    ServerInfo.can_use_v6 = 0;
# Line 102 | Line 94 | check_can_use_v6(void)
94   int
95   get_sockerr(int fd)
96   {
105 #ifndef _WIN32
97    int errtmp = errno;
107 #else
108  int errtmp = WSAGetLastError();
109 #endif
98   #ifdef SO_ERROR
99    int err = 0;
100    socklen_t len = sizeof(err);
101  
102 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
102 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
103    {
104      if (err)
105        errtmp = err;
# Line 161 | Line 149 | setup_socket(va_list args)
149    int fd = va_arg(args, int);
150    int opt = 1;
151  
152 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
152 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
153  
154   #ifdef IPTOS_LOWDELAY
155    opt = IPTOS_LOWDELAY;
156 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
156 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
157   #endif
158  
171 #ifndef _WIN32
159    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
160  
161    return NULL;
162   }
# Line 201 | Line 187 | close_connection(struct Client *client_p
187  
188    assert(NULL != client_p);
189  
190 +  if (!IsDead(client_p))
191 +  {
192 +    /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
193 +    /* there is still a chance that we might send data to this socket
194 +     * even if it is marked as blocked (COMM_SELECT_READ handler is called
195 +     * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
196 +     */
197 +    ClearSendqBlocked(client_p);
198 +    send_queued_write(client_p);
199 +  }
200 +
201    if (IsServer(client_p))
202    {
203 <    ServerStats->is_sv++;
204 <    ServerStats->is_sbs += client_p->localClient->send.bytes;
205 <    ServerStats->is_sbr += client_p->localClient->recv.bytes;
206 <    ServerStats->is_sti += CurrentTime - client_p->firsttime;
203 >    ++ServerStats.is_sv;
204 >    ServerStats.is_sbs += client_p->localClient->send.bytes;
205 >    ServerStats.is_sbr += client_p->localClient->recv.bytes;
206 >    ServerStats.is_sti += CurrentTime - client_p->firsttime;
207  
208      /* XXX Does this even make any sense at all anymore?
209       * scheduling a 'quick' reconnect could cause a pile of
# Line 216 | Line 213 | close_connection(struct Client *client_p
213       * If the connection has been up for a long amount of time, schedule
214       * a 'quick' reconnect, else reset the next-connect cycle.
215       */
216 <    if ((conf = find_conf_exact(SERVER_TYPE,
217 <                                  client_p->name, client_p->username,
221 <                                  client_p->host)))
216 >    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
217 >                                client_p->username, client_p->host)))
218      {
219        /*
220         * Reschedule a faster reconnect, if this was a automatically
# Line 226 | Line 222 | close_connection(struct Client *client_p
222         * a rehash in between, the status has been changed to
223         * CONF_ILLEGAL). But only do this if it was a "good" link.
224         */
225 <      aconf = (struct AccessItem *)map_to_conf(conf);
226 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
225 >      aconf  = map_to_conf(conf);
226 >      aclass = map_to_conf(aconf->class_ptr);
227        aconf->hold = time(NULL);
228        aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
229          HANGONRETRYDELAY : ConFreq(aclass);
234      if (nextconnect > aconf->hold)
235        nextconnect = aconf->hold;
230      }
231    }
232    else if (IsClient(client_p))
233    {
234 <    ServerStats->is_cl++;
235 <    ServerStats->is_cbs += client_p->localClient->send.bytes;
236 <    ServerStats->is_cbr += client_p->localClient->recv.bytes;
237 <    ServerStats->is_cti += CurrentTime - client_p->firsttime;
234 >    ++ServerStats.is_cl;
235 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
236 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
237 >    ServerStats.is_cti += CurrentTime - client_p->firsttime;
238    }
239    else
240 <    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 <  }
240 >    ++ServerStats.is_ni;
241  
242   #ifdef HAVE_LIBCRYPTO
243    if (client_p->localClient->fd.ssl)
244 <    SSL_shutdown(client_p->localClient->fd.ssl);
244 >  {
245 >    SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
246 >
247 >    if (!SSL_shutdown(client_p->localClient->fd.ssl))
248 >      SSL_shutdown(client_p->localClient->fd.ssl);
249 >  }
250   #endif
251    if (client_p->localClient->fd.flags.open)
252      fd_close(&client_p->localClient->fd);
# Line 317 | Line 305 | ssl_handshake(int fd, struct Client *cli
305   * any client list yet.
306   */
307   void
308 < add_connection(struct Listener* listener, int fd)
308 > add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
309   {
310    struct Client *new_client;
323  socklen_t len = sizeof(struct irc_ssaddr);
324  struct irc_ssaddr irn;
325  assert(NULL != listener);
311  
312 <  /*
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 <  }
312 >  assert(NULL != listener);
313  
349 #ifdef IPV6
350  remove_ipv6_mapping(&irn);
351 #else
352  irn.ss_len = len;
353 #endif
314    new_client = make_client(NULL);
315 +
316    fd_open(&new_client->localClient->fd, fd, 1,
317            (listener->flags & LISTENER_SSL) ?
318            "Incoming SSL connection" : "Incoming connection");
358  memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
319  
320    /*
321     * copy address to 'sockhost' as a string, copy it to host too
322     * so we have something valid to put into error messages...
323     */
324 <  new_client->localClient->port = ntohs(irn.ss_port);
365 <  memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr));
324 >  memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
325  
326    irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
327 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
328 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
327 >        new_client->localClient->ip.ss_len, new_client->sockhost,
328 >        sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
329    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
371
372  *new_client->host = '\0';
330   #ifdef IPV6
331 <  if (*new_client->sockhost == ':')
331 >  if (new_client->sockhost[0] == ':')
332      strlcat(new_client->host, "0", HOSTLEN+1);
333  
334    if (new_client->localClient->aftype == AF_INET6 &&
# Line 379 | Line 336 | add_connection(struct Listener* listener
336    {
337      strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
338      strlcat(new_client->host, ".", HOSTLEN+1);
339 <  } else
339 >  }
340 >  else
341   #endif
342      strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
343  
344    new_client->localClient->listener = listener;
345    ++listener->ref_count;
346  
389  connect_id++;
390  new_client->connect_id = connect_id;
391
347   #ifdef HAVE_LIBCRYPTO
348 <  if ((listener->flags & LISTENER_SSL))
348 >  if (listener->flags & LISTENER_SSL)
349    {
350 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
350 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
351      {
352        ilog(L_CRIT, "SSL_new() ERROR! -- %s",
353             ERR_error_string(ERR_get_error(), NULL));
# Line 537 | Line 492 | comm_connect_tcp(fde_t *fd, const char *
492                   void *data, int aftype, int timeout)
493   {
494    struct addrinfo hints, *res;
495 <  char portname[PORTNAMELEN+1];
495 >  char portname[PORTNAMELEN + 1];
496  
497    assert(callback);
498    fd->connect.callback = callback;
# Line 569 | Line 524 | comm_connect_tcp(fde_t *fd, const char *
524    hints.ai_socktype = SOCK_STREAM;
525    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
526  
527 <  snprintf(portname, PORTNAMELEN, "%d", port);
527 >  snprintf(portname, sizeof(portname), "%d", port);
528  
529    if (irc_getaddrinfo(host, portname, &hints, &res))
530    {
531      /* Send the DNS request, for the next level */
532 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
533 <    fd->dns_query->ptr = fd;
534 <    fd->dns_query->callback = comm_connect_dns_callback;
535 <    gethost_byname(host, fd->dns_query);
532 >    if (aftype == AF_INET6)
533 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
534 >    else
535 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
536    }
537    else
538    {
# Line 635 | Line 590 | comm_connect_timeout(fde_t *fd, void *no
590   * otherwise we initiate the connect()
591   */
592   static void
593 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
593 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
594   {
595    fde_t *F = vptr;
596  
597 <  if (reply == NULL)
597 >  if (name == NULL)
598    {
644    MyFree(F->dns_query);
645    F->dns_query = NULL;
599      comm_connect_callback(F, COMM_ERR_DNS);
600      return;
601    }
# Line 656 | Line 609 | comm_connect_dns_callback(void *vptr, st
609     * the DNS record around, and the DNS cache is gone anyway..
610     *     -- adrian
611     */
612 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
612 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
613    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
614    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
615      F->connect.hostaddr.ss_port;
616 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
616 >  F->connect.hostaddr.ss_len = addr->ss_len;
617  
618    /* Now, call the tryconnect() routine to try a connect() */
666  MyFree(F->dns_query);
667  F->dns_query = NULL;
619    comm_connect_tryconnect(F, NULL);
620   }
621  
# Line 692 | Line 643 | comm_connect_tryconnect(fde_t *fd, void
643    /* Error? */
644    if (retval < 0)
645    {
695 #ifdef _WIN32
696    errno = WSAGetLastError();
697 #endif
646      /*
647       * If we get EISCONN, then we've already connect()ed the socket,
648       * which is a good thing.
# Line 753 | Line 701 | comm_open(fde_t *F, int family, int sock
701     */
702    fd = socket(family, sock_type, proto);
703    if (fd < 0)
756  {
757 #ifdef _WIN32
758    errno = WSAGetLastError();
759 #endif
704      return -1; /* errno will be passed through, yay.. */
761  }
705  
706    execute_callback(setup_socket_cb, fd);
707  
# Line 791 | Line 734 | comm_accept(struct Listener *lptr, struc
734     * reserved fd limit, but we can deal with that when comm_open()
735     * also does it. XXX -- adrian
736     */
737 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
737 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
738    if (newfd < 0)
796  {
797 #ifdef _WIN32
798    errno = WSAGetLastError();
799 #endif
739      return -1;
801  }
740  
741   #ifdef IPV6
742    remove_ipv6_mapping(pn);

Comparing ircd-hybrid/src/s_bsd.c (property svn:keywords):
Revision 31 by knight, Sun Oct 2 20:34:05 2005 UTC vs.
Revision 1028 by michael, Sun Nov 8 13:03:38 2009 UTC

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

Diff Legend

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