]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/src/ports.c
Readd DroneBL to the list of default blacklist. Most of the trustworthiness
[irc/rqf/shadowircd.git] / libratbox / src / ports.c
CommitLineData
b57f37fb
WP
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>
f030cae8 8 * Copyright (C) 2002-2004,2008 ircd-ratbox development team
b57f37fb
WP
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 *
f030cae8 26 * $Id: ports.c 26286 2008-12-10 23:28:53Z androsyn $
b57f37fb
WP
27 */
28
29#include <libratbox_config.h>
30#include <ratbox_lib.h>
31#include <commio-int.h>
f030cae8 32#include <event-int.h>
b57f37fb
WP
33#if defined(HAVE_PORT_H) && (HAVE_PORT_CREATE)
34
35#include <port.h>
36
b57f37fb
WP
37#define PE_LENGTH 128
38
b57f37fb
WP
39static int pe;
40static struct timespec zero_timespec;
41
42static port_event_t *pelst; /* port buffer */
43static int pemax; /* max structs to buffer */
44
94b4fbf9 45int
f030cae8 46rb_setup_fd_ports(rb_fde_t *F)
b57f37fb 47{
94b4fbf9 48 return 0;
b57f37fb 49}
94b4fbf9 50
b57f37fb
WP
51/*
52 * rb_init_netio
53 *
54 * This is a needed exported function which will be called to initialise
55 * the network loop code.
56 */
f030cae8 57
b57f37fb
WP
58int
59rb_init_netio_ports(void)
60{
94b4fbf9
VY
61 if((pe = port_create()) < 0)
62 {
b57f37fb
WP
63 return errno;
64 }
65 pemax = getdtablesize();
66 pelst = rb_malloc(sizeof(port_event_t) * pemax);
67 zero_timespec.tv_sec = 0;
68 zero_timespec.tv_nsec = 0;
f030cae8
VY
69 rb_set_time();
70 return 0;
b57f37fb
WP
71}
72
73/*
74 * rb_setselect
75 *
76 * This is a needed exported function which will be called to register
77 * and deregister interest in a pending IO state for a given FD.
78 */
79void
94b4fbf9 80rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
b57f37fb
WP
81{
82 lrb_assert(IsFDOpen(F));
f030cae8 83 int old_flags = F->pflags;
b57f37fb 84
94b4fbf9
VY
85 if(type & RB_SELECT_READ)
86 {
b57f37fb
WP
87 F->read_handler = handler;
88 F->read_data = client_data;
89 }
94b4fbf9
VY
90 if(type & RB_SELECT_WRITE)
91 {
b57f37fb
WP
92 F->write_handler = handler;
93 F->write_data = client_data;
94 }
f030cae8
VY
95 F->pflags = 0;
96
97 if(F->read_handler != NULL)
98 F->pflags = POLLIN;
99 if(F->write_handler != NULL)
100 F->pflags |= POLLOUT;
101
102 if(old_flags == 0 && F->pflags == 0)
103 return;
104 else if(F->pflags <= 0)
105 {
106 port_dissociate(pe, PORT_SOURCE_FD, F->fd);
107 return;
108 }
109
110 port_associate(pe, PORT_SOURCE_FD, F->fd, F->pflags, F);
111
b57f37fb
WP
112}
113
114/*
115 * rb_select
116 *
117 * Called to do the new-style IO, courtesy of squid (like most of this
118 * new IO code). This routine handles the stuff we've hidden in
119 * rb_setselect and fd_table[] and calls callbacks for IO ready
120 * events.
121 */
122
123int
124rb_select_ports(long delay)
125{
94b4fbf9 126 int i, fd;
f030cae8 127 int nget = 1;
94b4fbf9 128 struct timespec poll_time;
f030cae8
VY
129 struct timespec *p = NULL;
130 struct ev_entry *ev;
b57f37fb 131
f030cae8
VY
132 if(delay >= 0)
133 {
134 poll_time.tv_sec = delay / 1000;
135 poll_time.tv_nsec = (delay % 1000) * 1000000;
136 p = &poll_time;
137 }
b57f37fb 138
f030cae8
VY
139
140 i = port_getn(pe, pelst, pemax, &nget, p);
b57f37fb
WP
141 rb_set_time();
142
94b4fbf9 143 if(i == -1)
b57f37fb
WP
144 return RB_OK;
145
94b4fbf9
VY
146 for(i = 0; i < nget; i++)
147 {
f030cae8 148 if(pelst[i].portev_source == PORT_SOURCE_FD)
94b4fbf9 149 {
b57f37fb
WP
150 fd = pelst[i].portev_object;
151 PF *hdl = NULL;
f030cae8
VY
152 rb_fde_t *F = pelst[i].portev_user;
153 if((pelst[i].portev_events & (POLLIN | POLLHUP | POLLERR)) && (hdl = F->read_handler))
94b4fbf9 154 {
b57f37fb
WP
155 F->read_handler = NULL;
156 hdl(F, F->read_data);
157 }
f030cae8 158 if((pelst[i].portev_events & (POLLOUT | POLLHUP | POLLERR)) && (hdl = F->write_handler))
94b4fbf9 159 {
b57f37fb
WP
160 F->write_handler = NULL;
161 hdl(F, F->write_data);
162 }
f030cae8
VY
163 } else if(pelst[i].portev_source == PORT_SOURCE_TIMER)
164 {
165 ev = (struct ev_entry *)pelst[i].portev_user;
166 rb_run_event(ev);
b57f37fb
WP
167 }
168 }
169 return RB_OK;
170}
171
f030cae8
VY
172int
173rb_ports_supports_event(void)
174{
175 return 1;
176};
177
178void
179rb_ports_init_event(void)
180{
181 return;
182}
183
184int
185rb_ports_sched_event(struct ev_entry *event, int when)
186{
187 timer_t *id;
188 struct sigevent ev;
189 port_notify_t not;
190 struct itimerspec ts;
191
192 event->comm_ptr = rb_malloc(sizeof(timer_t));
193 id = event->comm_ptr;
194
195 memset(&ev, 0, sizeof(ev));
196 ev.sigev_notify = SIGEV_PORT;
197 ev.sigev_value.sival_ptr = &not;
198
199 memset(&not, 0, sizeof(not));
200 not.portnfy_port = pe;
201 not.portnfy_user = event;
202
203 if(timer_create(CLOCK_REALTIME, &ev, id) < 0)
204 {
205 rb_lib_log("timer_create: %s\n", strerror(errno));
206 return 0;
207 }
208
209 memset(&ts, 0, sizeof(ts));
210 ts.it_value.tv_sec = when;
211 ts.it_value.tv_nsec = 0;
212 if(event->frequency != 0)
213 ts.it_interval = ts.it_value;
214
215 if(timer_settime(*id, 0, &ts, NULL) < 0)
216 {
217 rb_lib_log("timer_settime: %s\n", strerror(errno));
218 return 0;
219 }
220 return 1;
221}
222
223void
224rb_ports_unsched_event(struct ev_entry *event)
225{
226 timer_delete(*((timer_t *) event->comm_ptr));
227 rb_free(event->comm_ptr);
228 event->comm_ptr = NULL;
229}
b57f37fb 230#else /* ports not supported */
f030cae8
VY
231
232int
233rb_ports_supports_event(void)
234{
235 errno = ENOSYS;
236 return 0;
237}
238
239void
240rb_ports_init_event(void)
241{
242 return;
243}
244
245int
246rb_ports_sched_event(struct ev_entry *event, int when)
247{
248 errno = ENOSYS;
249 return -1;
250}
251
252void
253rb_ports_unsched_event(struct ev_entry *event)
254{
255 return;
256}
257
94b4fbf9 258int
b57f37fb
WP
259rb_init_netio_ports(void)
260{
261 return ENOSYS;
262}
263
264void
265rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
266{
94b4fbf9
VY
267 errno = ENOSYS;
268 return;
b57f37fb 269}
94b4fbf9 270
b57f37fb
WP
271int
272rb_select_ports(long delay)
273{
94b4fbf9
VY
274 errno = ENOSYS;
275 return -1;
b57f37fb 276}
94b4fbf9 277
b57f37fb
WP
278int
279rb_setup_fd_ports(rb_fde_t *F)
280{
94b4fbf9
VY
281 errno = ENOSYS;
282 return -1;
b57f37fb
WP
283}
284
94b4fbf9 285
b57f37fb 286#endif