/[svn]/ircd-hybrid/trunk/src/s_bsd_kqueue.c
ViewVC logotype

Annotation of /ircd-hybrid/trunk/src/s_bsd_kqueue.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2725 - (hide annotations)
Sun Dec 29 13:01:00 2013 UTC (8 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 5143 byte(s)
- Fixed bug where ircd didn't timeout SSL connections that haven't
  finished the SSL handshake. Reported by Adam.

1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * s_bsd_kqueue.c: FreeBSD kqueue compatible network routines.
4     *
5     * Originally by Adrian Chadd <adrian@creative.net.au>
6     * Copyright (C) 2005 Hybrid Development Team
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21     * USA
22     *
23 knight 31 * $Id$
24 adx 30 */
25    
26     #include "stdinc.h"
27 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE
28 adx 30 #include <sys/event.h>
29     #include "fdlist.h"
30     #include "ircd.h"
31     #include "memory.h"
32     #include "s_bsd.h"
33 michael 1309 #include "log.h"
34 adx 30
35     #define KE_LENGTH 128
36    
37     /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
38    
39     #ifndef EV_SET
40     #define EV_SET(kevp, a, b, c, d, e, f) do { \
41     (kevp)->ident = (a); \
42     (kevp)->filter = (b); \
43     (kevp)->flags = (c); \
44     (kevp)->fflags = (d); \
45     (kevp)->data = (e); \
46     (kevp)->udata = (f); \
47     } while(0)
48     #endif
49    
50     static fde_t kqfd;
51     static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */
52     static int kqoff; /* offset into the buffer */
53    
54     /*
55     * init_netio
56     *
57     * This is a needed exported function which will be called to initialise
58     * the network loop code.
59     */
60     void
61     init_netio(void)
62     {
63     int fd;
64    
65     if ((fd = kqueue()) < 0)
66     {
67 michael 1247 ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open kqueue fd!");
68 adx 30 exit(115); /* Whee! */
69     }
70    
71     fd_open(&kqfd, fd, 0, "kqueue() file descriptor");
72     }
73    
74     /*
75     * Write a single update to the kqueue list.
76     */
77     static void
78     kq_update_events(int fd, int filter, int what)
79     {
80     static struct timespec zero_timespec = {0, 0};
81     struct kevent *kep = kq_fdlist + kqoff;
82    
83     EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL);
84    
85 adx 842 if (++kqoff == KE_LENGTH)
86 adx 30 {
87 michael 2589 int i;
88    
89     for (i = 0; i < kqoff; ++i)
90 michael 2613 kevent(kqfd.fd, &kq_fdlist[i], 1, NULL, 0, &zero_timespec);
91 adx 30 kqoff = 0;
92     }
93     }
94    
95     /*
96     * comm_setselect
97     *
98     * This is a needed exported function which will be called to register
99     * and deregister interest in a pending IO state for a given FD.
100     */
101     void
102     comm_setselect(fde_t *F, unsigned int type, PF *handler,
103     void *client_data, time_t timeout)
104     {
105     int new_events, diff;
106    
107     if ((type & COMM_SELECT_READ))
108     {
109     F->read_handler = handler;
110     F->read_data = client_data;
111     }
112    
113     if ((type & COMM_SELECT_WRITE))
114     {
115     F->write_handler = handler;
116     F->write_data = client_data;
117     }
118    
119     new_events = (F->read_handler ? COMM_SELECT_READ : 0) |
120 michael 2650 (F->write_handler ? COMM_SELECT_WRITE : 0);
121 adx 30
122     if (timeout != 0)
123 michael 2725 {
124 adx 30 F->timeout = CurrentTime + (timeout / 1000);
125 michael 2725 F->timeout_handler = handler;
126     F->timeout_data = client_data;
127     }
128 adx 30
129     diff = new_events ^ F->evcache;
130    
131     if ((diff & COMM_SELECT_READ))
132     kq_update_events(F->fd, EVFILT_READ,
133     (new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE);
134     if ((diff & COMM_SELECT_WRITE))
135     kq_update_events(F->fd, EVFILT_WRITE,
136     (new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE);
137    
138     F->evcache = new_events;
139     }
140    
141     /*
142     * comm_select
143     *
144     * Called to do the new-style IO, courtesy of squid (like most of this
145     * new IO code). This routine handles the stuff we've hidden in
146     * comm_setselect and fd_table[] and calls callbacks for IO ready
147     * events.
148     */
149     void
150     comm_select(void)
151     {
152     int num, i;
153     static struct kevent ke[KE_LENGTH];
154     struct timespec poll_time;
155     PF *hdl;
156     fde_t *F;
157    
158     /*
159     * remember we are doing NANOseconds here, not micro/milli. God knows
160     * why jlemon used a timespec, but hey, he wrote the interface, not I
161     * -- Adrian
162     */
163     poll_time.tv_sec = 0;
164     poll_time.tv_nsec = SELECT_DELAY * 1000000;
165     num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time);
166     kqoff = 0;
167    
168     set_time();
169    
170     if (num < 0)
171     {
172     #ifdef HAVE_USLEEP
173     usleep(50000); /* avoid 99% CPU in comm_select */
174     #endif
175     return;
176     }
177    
178     for (i = 0; i < num; i++)
179     {
180     F = lookup_fd(ke[i].ident);
181     if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR))
182     continue;
183    
184     if (ke[i].filter == EVFILT_READ)
185 michael 2650 {
186 adx 30 if ((hdl = F->read_handler) != NULL)
187     {
188     F->read_handler = NULL;
189     hdl(F, F->read_data);
190 michael 2614 if (!F->flags.open)
191     continue;
192 adx 30 }
193 michael 2650 }
194 adx 30
195     if (ke[i].filter == EVFILT_WRITE)
196 michael 2650 {
197 adx 30 if ((hdl = F->write_handler) != NULL)
198     {
199     F->write_handler = NULL;
200     hdl(F, F->write_data);
201 michael 2614 if (!F->flags.open)
202     continue;
203 adx 30 }
204 michael 2650 }
205 adx 30
206     comm_setselect(F, 0, NULL, NULL, 0);
207     }
208     }
209 stu 908 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28