]> jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/ports.c
Only count throttle entries that cause rejection in /stats t.
[irc/rqf/shadowircd.git] / libratbox / src / ports.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * ports.c: Solaris ports compatible network routines.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
8 * Copyright (C) 2002-2004 ircd-ratbox development team
9 * Copyright (C) 2005 Edward Brocklesby.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 * USA
25 *
26 * $Id: ports.c 26092 2008-09-19 15:13:52Z androsyn $
27 */
28
29 #include <libratbox_config.h>
30 #include <ratbox_lib.h>
31 #include <commio-int.h>
32
33 #if defined(HAVE_PORT_H) && (HAVE_PORT_CREATE)
34
35 #include <port.h>
36
37
38 #define PE_LENGTH 128
39
40 static void pe_update_events(rb_fde_t *, short, PF *);
41 static int pe;
42 static struct timespec zero_timespec;
43
44 static port_event_t *pelst; /* port buffer */
45 static int pemax; /* max structs to buffer */
46
47 int
48 rb_setup_fd_ports(int fd)
49 {
50 return 0;
51 }
52
53
54 static void
55 pe_update_events(rb_fde_t *F, short filter, PF * handler)
56 {
57 PF *cur_handler = NULL;
58
59 if(filter == POLLRDNORM)
60 cur_handler = F->read_handler;
61 else if(filter == POLLWRNORM)
62 cur_handler = F->write_handler;
63
64 if(!cur_handler && handler)
65 port_associate(pe, PORT_SOURCE_FD, F->fd, filter, F);
66 else if(cur_handler && !handler)
67 port_dissociate(pe, PORT_SOURCE_FD, F->fd);
68 }
69
70 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
71 /* Public functions */
72
73
74 /*
75 * rb_init_netio
76 *
77 * This is a needed exported function which will be called to initialise
78 * the network loop code.
79 */
80 int
81 rb_init_netio_ports(void)
82 {
83 if((pe = port_create()) < 0)
84 {
85 return errno;
86 }
87 pemax = getdtablesize();
88 pelst = rb_malloc(sizeof(port_event_t) * pemax);
89 zero_timespec.tv_sec = 0;
90 zero_timespec.tv_nsec = 0;
91 }
92
93 /*
94 * rb_setselect
95 *
96 * This is a needed exported function which will be called to register
97 * and deregister interest in a pending IO state for a given FD.
98 */
99 void
100 rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
101 {
102 lrb_assert(IsFDOpen(F));
103
104 /* Update the list, even though we're not using it .. */
105 if(type & RB_SELECT_READ)
106 {
107 pe_update_events(F, POLLRDNORM, handler);
108 F->read_handler = handler;
109 F->read_data = client_data;
110 }
111 if(type & RB_SELECT_WRITE)
112 {
113 pe_update_events(F, POLLWRNORM, handler);
114 F->write_handler = handler;
115 F->write_data = client_data;
116 }
117 }
118
119 /*
120 * rb_select
121 *
122 * Called to do the new-style IO, courtesy of squid (like most of this
123 * new IO code). This routine handles the stuff we've hidden in
124 * rb_setselect and fd_table[] and calls callbacks for IO ready
125 * events.
126 */
127
128 int
129 rb_select_ports(long delay)
130 {
131 int i, fd;
132 uint nget = 1;
133 struct timespec poll_time;
134 struct timer_data *tdata;
135
136 poll_time.tv_sec = delay / 1000;
137 poll_time.tv_nsec = (delay % 1000) * 1000000;
138
139 i = port_getn(pe, pelst, pemax, &nget, &poll_time);
140 rb_set_time();
141
142 if(i == -1)
143 return RB_OK;
144
145 for(i = 0; i < nget; i++)
146 {
147 switch (pelst[i].portev_source)
148 {
149 case PORT_SOURCE_FD:
150 fd = pelst[i].portev_object;
151 PF *hdl = NULL;
152 rb_fde_t *F = rb_find_fd(fd);
153
154 if((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler))
155 {
156 F->read_handler = NULL;
157 hdl(F, F->read_data);
158 }
159 if((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler))
160 {
161 F->write_handler = NULL;
162 hdl(F, F->write_data);
163 }
164 break;
165 }
166 }
167 return RB_OK;
168 }
169
170 #else /* ports not supported */
171 int
172 rb_init_netio_ports(void)
173 {
174 return ENOSYS;
175 }
176
177 void
178 rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
179 {
180 errno = ENOSYS;
181 return;
182 }
183
184 int
185 rb_select_ports(long delay)
186 {
187 errno = ENOSYS;
188 return -1;
189 }
190
191 int
192 rb_setup_fd_ports(rb_fde_t *F)
193 {
194 errno = ENOSYS;
195 return -1;
196 }
197
198
199 #endif