]>
Commit | Line | Data |
---|---|---|
db137867 AC |
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> | |
030272f3 | 8 | * Copyright (C) 2002-2004,2008 ircd-ratbox development team |
db137867 AC |
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 | * | |
030272f3 | 26 | * $Id: ports.c 26286 2008-12-10 23:28:53Z androsyn $ |
db137867 AC |
27 | */ |
28 | ||
fe037171 EM |
29 | #include <librb_config.h> |
30 | #include <rb_lib.h> | |
db137867 | 31 | #include <commio-int.h> |
030272f3 | 32 | #include <event-int.h> |
db137867 AC |
33 | #if defined(HAVE_PORT_H) && (HAVE_PORT_CREATE) |
34 | ||
35 | #include <port.h> | |
36 | ||
db137867 AC |
37 | #define PE_LENGTH 128 |
38 | ||
db137867 AC |
39 | static int pe; |
40 | static struct timespec zero_timespec; | |
41 | ||
42 | static port_event_t *pelst; /* port buffer */ | |
43 | static int pemax; /* max structs to buffer */ | |
44 | ||
3202e249 | 45 | int |
030272f3 | 46 | rb_setup_fd_ports(rb_fde_t *F) |
db137867 | 47 | { |
3202e249 | 48 | return 0; |
db137867 | 49 | } |
3202e249 | 50 | |
db137867 AC |
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 | */ | |
030272f3 | 57 | |
db137867 AC |
58 | int |
59 | rb_init_netio_ports(void) | |
60 | { | |
3202e249 VY |
61 | if((pe = port_create()) < 0) |
62 | { | |
db137867 AC |
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; | |
030272f3 VY |
69 | rb_set_time(); |
70 | return 0; | |
db137867 AC |
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 | */ | |
79 | void | |
3202e249 | 80 | rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) |
db137867 AC |
81 | { |
82 | lrb_assert(IsFDOpen(F)); | |
030272f3 | 83 | int old_flags = F->pflags; |
db137867 | 84 | |
3202e249 VY |
85 | if(type & RB_SELECT_READ) |
86 | { | |
db137867 AC |
87 | F->read_handler = handler; |
88 | F->read_data = client_data; | |
89 | } | |
3202e249 VY |
90 | if(type & RB_SELECT_WRITE) |
91 | { | |
db137867 AC |
92 | F->write_handler = handler; |
93 | F->write_data = client_data; | |
94 | } | |
030272f3 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 | } | |
55abcbb2 | 109 | |
030272f3 | 110 | port_associate(pe, PORT_SOURCE_FD, F->fd, F->pflags, F); |
55abcbb2 | 111 | |
db137867 AC |
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 | ||
123 | int | |
124 | rb_select_ports(long delay) | |
125 | { | |
3a1f645b | 126 | int i; |
29c92cf9 | 127 | unsigned int nget = 1; |
3202e249 | 128 | struct timespec poll_time; |
030272f3 VY |
129 | struct timespec *p = NULL; |
130 | struct ev_entry *ev; | |
db137867 | 131 | |
030272f3 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 | } | |
db137867 | 138 | |
030272f3 VY |
139 | |
140 | i = port_getn(pe, pelst, pemax, &nget, p); | |
db137867 AC |
141 | rb_set_time(); |
142 | ||
3202e249 | 143 | if(i == -1) |
db137867 AC |
144 | return RB_OK; |
145 | ||
29c92cf9 | 146 | for(i = 0; (unsigned)i < nget; i++) |
3202e249 | 147 | { |
030272f3 | 148 | if(pelst[i].portev_source == PORT_SOURCE_FD) |
3202e249 | 149 | { |
db137867 AC |
150 | fd = pelst[i].portev_object; |
151 | PF *hdl = NULL; | |
030272f3 VY |
152 | rb_fde_t *F = pelst[i].portev_user; |
153 | if((pelst[i].portev_events & (POLLIN | POLLHUP | POLLERR)) && (hdl = F->read_handler)) | |
3202e249 | 154 | { |
db137867 AC |
155 | F->read_handler = NULL; |
156 | hdl(F, F->read_data); | |
157 | } | |
030272f3 | 158 | if((pelst[i].portev_events & (POLLOUT | POLLHUP | POLLERR)) && (hdl = F->write_handler)) |
3202e249 | 159 | { |
db137867 AC |
160 | F->write_handler = NULL; |
161 | hdl(F, F->write_data); | |
162 | } | |
030272f3 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); | |
db137867 AC |
167 | } |
168 | } | |
169 | return RB_OK; | |
170 | } | |
171 | ||
030272f3 VY |
172 | int |
173 | rb_ports_supports_event(void) | |
174 | { | |
175 | return 1; | |
176 | }; | |
177 | ||
178 | void | |
179 | rb_ports_init_event(void) | |
180 | { | |
181 | return; | |
182 | } | |
183 | ||
184 | int | |
185 | rb_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; | |
55abcbb2 | 194 | |
030272f3 VY |
195 | memset(&ev, 0, sizeof(ev)); |
196 | ev.sigev_notify = SIGEV_PORT; | |
197 | ev.sigev_value.sival_ptr = ¬ | |
55abcbb2 KB |
198 | |
199 | memset(¬, 0, sizeof(not)); | |
030272f3 VY |
200 | not.portnfy_port = pe; |
201 | not.portnfy_user = event; | |
55abcbb2 | 202 | |
030272f3 VY |
203 | if(timer_create(CLOCK_REALTIME, &ev, id) < 0) |
204 | { | |
205 | rb_lib_log("timer_create: %s\n", strerror(errno)); | |
206 | return 0; | |
207 | } | |
55abcbb2 | 208 | |
030272f3 VY |
209 | memset(&ts, 0, sizeof(ts)); |
210 | ts.it_value.tv_sec = when; | |
55abcbb2 | 211 | ts.it_value.tv_nsec = 0; |
030272f3 VY |
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 | } | |
55abcbb2 | 220 | return 1; |
030272f3 VY |
221 | } |
222 | ||
223 | void | |
224 | rb_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 | } | |
db137867 | 230 | #else /* ports not supported */ |
030272f3 VY |
231 | |
232 | int | |
233 | rb_ports_supports_event(void) | |
234 | { | |
235 | errno = ENOSYS; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | void | |
240 | rb_ports_init_event(void) | |
241 | { | |
242 | return; | |
243 | } | |
55abcbb2 | 244 | |
030272f3 VY |
245 | int |
246 | rb_ports_sched_event(struct ev_entry *event, int when) | |
247 | { | |
248 | errno = ENOSYS; | |
249 | return -1; | |
250 | } | |
55abcbb2 | 251 | |
030272f3 VY |
252 | void |
253 | rb_ports_unsched_event(struct ev_entry *event) | |
254 | { | |
255 | return; | |
256 | } | |
257 | ||
3202e249 | 258 | int |
db137867 AC |
259 | rb_init_netio_ports(void) |
260 | { | |
261 | return ENOSYS; | |
262 | } | |
263 | ||
264 | void | |
265 | rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) | |
266 | { | |
3202e249 VY |
267 | errno = ENOSYS; |
268 | return; | |
db137867 | 269 | } |
3202e249 | 270 | |
db137867 AC |
271 | int |
272 | rb_select_ports(long delay) | |
273 | { | |
3202e249 VY |
274 | errno = ENOSYS; |
275 | return -1; | |
db137867 | 276 | } |
3202e249 | 277 | |
db137867 AC |
278 | int |
279 | rb_setup_fd_ports(rb_fde_t *F) | |
280 | { | |
3202e249 VY |
281 | errno = ENOSYS; |
282 | return -1; | |
db137867 AC |
283 | } |
284 | ||
3202e249 | 285 | |
db137867 | 286 | #endif |