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