]>
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> | |
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 25038 2008-01-23 16:03:08Z 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 | |
72cedbce | 48 | rb_setup_fd_ports(rb_fde_t *F) |
db137867 AC |
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 | return errno; | |
85 | } | |
86 | pemax = getdtablesize(); | |
87 | pelst = rb_malloc(sizeof(port_event_t) * pemax); | |
88 | zero_timespec.tv_sec = 0; | |
89 | zero_timespec.tv_nsec = 0; | |
90 | } | |
91 | ||
92 | /* | |
93 | * rb_setselect | |
94 | * | |
95 | * This is a needed exported function which will be called to register | |
96 | * and deregister interest in a pending IO state for a given FD. | |
97 | */ | |
98 | void | |
99 | rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, | |
100 | 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 | pe_update_events(F, POLLRDNORM, handler); | |
107 | F->read_handler = handler; | |
108 | F->read_data = client_data; | |
109 | } | |
110 | if(type & RB_SELECT_WRITE) { | |
111 | pe_update_events(F, POLLWRNORM, handler); | |
112 | F->write_handler = handler; | |
113 | F->write_data = client_data; | |
114 | } | |
115 | } | |
116 | ||
117 | /* | |
118 | * rb_select | |
119 | * | |
120 | * Called to do the new-style IO, courtesy of squid (like most of this | |
121 | * new IO code). This routine handles the stuff we've hidden in | |
122 | * rb_setselect and fd_table[] and calls callbacks for IO ready | |
123 | * events. | |
124 | */ | |
125 | ||
126 | int | |
127 | rb_select_ports(long delay) | |
128 | { | |
129 | int i, fd; | |
130 | uint nget = 1; | |
131 | struct timespec poll_time; | |
132 | struct timer_data *tdata; | |
133 | ||
134 | poll_time.tv_sec = delay / 1000; | |
135 | poll_time.tv_nsec = (delay % 1000) * 1000000; | |
136 | ||
137 | i = port_getn(pe, pelst, pemax, &nget, &poll_time); | |
138 | rb_set_time(); | |
139 | ||
140 | if (i == -1) | |
141 | return RB_OK; | |
142 | ||
143 | for (i = 0; i < nget; i++) { | |
144 | switch(pelst[i].portev_source) { | |
145 | case PORT_SOURCE_FD: | |
146 | fd = pelst[i].portev_object; | |
147 | PF *hdl = NULL; | |
148 | rb_fde_t *F = rb_find_fd(fd); | |
149 | ||
150 | if ((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler)) { | |
151 | F->read_handler = NULL; | |
152 | hdl(F, F->read_data); | |
153 | } | |
154 | if ((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler)) { | |
155 | F->write_handler = NULL; | |
156 | hdl(F, F->write_data); | |
157 | } | |
158 | break; | |
159 | } | |
160 | } | |
161 | return RB_OK; | |
162 | } | |
163 | ||
164 | #else /* ports not supported */ | |
165 | int | |
166 | rb_init_netio_ports(void) | |
167 | { | |
168 | return ENOSYS; | |
169 | } | |
170 | ||
171 | void | |
172 | rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data) | |
173 | { | |
174 | errno = ENOSYS; | |
175 | return; | |
176 | } | |
177 | ||
178 | int | |
179 | rb_select_ports(long delay) | |
180 | { | |
181 | errno = ENOSYS; | |
182 | return -1; | |
183 | } | |
184 | ||
185 | int | |
186 | rb_setup_fd_ports(rb_fde_t *F) | |
187 | { | |
188 | errno = ENOSYS; | |
189 | return -1; | |
190 | } | |
191 | ||
192 | ||
193 | #endif |