]> jfr.im git - irc/rqf/shadowircd.git/blob - libcharybdis/ports.c
[svn] - the new plan:
[irc/rqf/shadowircd.git] / libcharybdis / ports.c
1 /*
2 * charybdis: 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 * Copyright (C) 2005 William Pitcock and Jilles Tjoelker
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * USA
26 *
27 * $Id$
28 */
29
30 #include "stdinc.h"
31 #include <port.h>
32 #include <time.h>
33
34 #include "libcharybdis.h"
35
36 #define PE_LENGTH 128
37
38 static void pe_update_events(fde_t *, short, PF *);
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
45 int
46 ircd_setup_fd(int fd)
47 {
48 return 0;
49 }
50
51
52 void
53 pe_update_events(fde_t * F, short filter, PF * handler)
54 {
55 PF *cur_handler = NULL;
56
57 if (filter == POLLRDNORM)
58 cur_handler = F->read_handler;
59 else if (filter == POLLWRNORM)
60 cur_handler = F->write_handler;
61
62 if (!cur_handler && handler)
63 port_associate(pe, PORT_SOURCE_FD, F->fd, filter, F);
64 else if (cur_handler && !handler)
65 port_dissociate(pe, PORT_SOURCE_FD, F->fd);
66 }
67
68 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
69 /* Public functions */
70
71
72 /*
73 * init_netio
74 *
75 * This is a needed exported function which will be called to initialise
76 * the network loop code.
77 */
78 void
79 init_netio(void)
80 {
81 if((pe = port_create()) < 0) {
82 libcharybdis_log("init_netio: Couldn't open port fd!\n");
83 exit(115); /* Whee! */
84 }
85 pemax = getdtablesize();
86 pelst = MyMalloc(sizeof(port_event_t) * pemax);
87 zero_timespec.tv_sec = 0;
88 zero_timespec.tv_nsec = 0;
89 }
90
91 /*
92 * ircd_setselect
93 *
94 * This is a needed exported function which will be called to register
95 * and deregister interest in a pending IO state for a given FD.
96 */
97 void
98 ircd_setselect(int fd, fdlist_t list, unsigned int type, PF * handler,
99 void *client_data)
100 {
101 fde_t *F = &fd_table[fd];
102 s_assert(fd >= 0);
103 s_assert(F->flags.open);
104
105 /* Update the list, even though we're not using it .. */
106 F->list = list;
107
108 if(type & IRCD_SELECT_READ) {
109 pe_update_events(F, POLLRDNORM, handler);
110 F->read_handler = handler;
111 F->read_data = client_data;
112 }
113 if(type & IRCD_SELECT_WRITE) {
114 pe_update_events(F, POLLWRNORM, handler);
115 F->write_handler = handler;
116 F->write_data = client_data;
117 }
118 }
119
120 /*
121 * ircd_select
122 *
123 * Called to do the new-style IO, courtesy of squid (like most of this
124 * new IO code). This routine handles the stuff we've hidden in
125 * ircd_setselect and fd_table[] and calls callbacks for IO ready
126 * events.
127 */
128
129 int
130 ircd_select(unsigned long delay)
131 {
132 int i, fd;
133 uint nget = 1;
134 struct timespec poll_time;
135 struct timer_data *tdata;
136
137 poll_time.tv_sec = delay / 1000;
138 poll_time.tv_nsec = (delay % 1000) * 1000000;
139
140 i = port_getn(pe, pelst, pemax, &nget, &poll_time);
141 ircd_set_time();
142
143 if (i == -1)
144 return COMM_ERROR;
145
146 for (i = 0; i < nget; i++) {
147 switch(pelst[i].portev_source) {
148 case PORT_SOURCE_FD:
149 fd = pelst[i].portev_object;
150 PF *hdl = NULL;
151 fde_t *F = &fd_table[fd];
152
153 if ((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler)) {
154 F->read_handler = NULL;
155 hdl(fd, F->read_data);
156 }
157 if ((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler)) {
158 F->write_handler = NULL;
159 hdl(fd, F->write_data);
160 }
161 break;
162
163 case PORT_SOURCE_TIMER:
164 tdata = pelst[i].portev_user;
165 tdata->td_cb(tdata->td_udata);
166
167 if (!tdata->td_repeat)
168 free(tdata);
169
170 break;
171 }
172 }
173 return COMM_OK;
174 }