2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* ports.c: Solaris ports compatible network routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
|
2008-12-22 10:49:01 +01:00
|
|
|
* Copyright (C) 2002-2004,2008 ircd-ratbox development team
|
2008-04-01 18:52:26 +02:00
|
|
|
* Copyright (C) 2005 Edward Brocklesby.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <rb/rb.h>
|
|
|
|
#include <rb/commio_int.h>
|
|
|
|
#include <rb/event_int.h>
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#if defined(HAVE_PORT_H) && (HAVE_PORT_CREATE)
|
2008-04-01 18:52:26 +02:00
|
|
|
#include <port.h>
|
|
|
|
|
|
|
|
#define PE_LENGTH 128
|
|
|
|
|
|
|
|
static int pe;
|
|
|
|
static struct timespec zero_timespec;
|
|
|
|
|
|
|
|
static port_event_t *pelst; /* port buffer */
|
|
|
|
static int pemax; /* max structs to buffer */
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
int
|
2008-12-22 10:49:01 +01:00
|
|
|
rb_setup_fd_ports(rb_fde_t *F)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
return 0;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
/*
|
|
|
|
* rb_init_netio
|
|
|
|
*
|
|
|
|
* This is a needed exported function which will be called to initialise
|
|
|
|
* the network loop code.
|
|
|
|
*/
|
2008-12-22 10:49:01 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
int
|
|
|
|
rb_init_netio_ports(void)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
if((pe = port_create()) < 0)
|
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
pemax = getdtablesize();
|
|
|
|
pelst = rb_malloc(sizeof(port_event_t) * pemax);
|
|
|
|
zero_timespec.tv_sec = 0;
|
|
|
|
zero_timespec.tv_nsec = 0;
|
2008-12-22 10:49:01 +01:00
|
|
|
rb_set_time();
|
|
|
|
return 0;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_setselect
|
|
|
|
*
|
|
|
|
* This is a needed exported function which will be called to register
|
|
|
|
* and deregister interest in a pending IO state for a given FD.
|
|
|
|
*/
|
|
|
|
void
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
|
2008-04-01 18:52:26 +02:00
|
|
|
{
|
|
|
|
lrb_assert(IsFDOpen(F));
|
2008-12-22 10:49:01 +01:00
|
|
|
int old_flags = F->pflags;
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
if(type & RB_SELECT_READ)
|
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
F->read_handler = handler;
|
|
|
|
F->read_data = client_data;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
if(type & RB_SELECT_WRITE)
|
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
F->write_handler = handler;
|
|
|
|
F->write_data = client_data;
|
|
|
|
}
|
2008-12-22 10:49:01 +01:00
|
|
|
F->pflags = 0;
|
|
|
|
|
|
|
|
if(F->read_handler != NULL)
|
|
|
|
F->pflags = POLLIN;
|
|
|
|
if(F->write_handler != NULL)
|
|
|
|
F->pflags |= POLLOUT;
|
|
|
|
|
|
|
|
if(old_flags == 0 && F->pflags == 0)
|
|
|
|
return;
|
|
|
|
else if(F->pflags <= 0)
|
|
|
|
{
|
|
|
|
port_dissociate(pe, PORT_SOURCE_FD, F->fd);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
port_associate(pe, PORT_SOURCE_FD, F->fd, F->pflags, F);
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_select
|
|
|
|
*
|
|
|
|
* Called to do the new-style IO, courtesy of squid (like most of this
|
|
|
|
* new IO code). This routine handles the stuff we've hidden in
|
|
|
|
* rb_setselect and fd_table[] and calls callbacks for IO ready
|
|
|
|
* events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_select_ports(long delay)
|
|
|
|
{
|
2016-03-23 18:06:26 +01:00
|
|
|
int i, fd = -1;
|
2015-04-20 07:55:20 +02:00
|
|
|
unsigned int nget = 1;
|
2008-12-03 00:49:39 +01:00
|
|
|
struct timespec poll_time;
|
2008-12-22 10:49:01 +01:00
|
|
|
struct timespec *p = NULL;
|
|
|
|
struct ev_entry *ev;
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
if(delay >= 0)
|
|
|
|
{
|
|
|
|
poll_time.tv_sec = delay / 1000;
|
|
|
|
poll_time.tv_nsec = (delay % 1000) * 1000000;
|
|
|
|
p = &poll_time;
|
|
|
|
}
|
2008-04-01 18:52:26 +02:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
|
|
|
|
i = port_getn(pe, pelst, pemax, &nget, p);
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_set_time();
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
if(i == -1)
|
2008-04-01 18:52:26 +02:00
|
|
|
return RB_OK;
|
|
|
|
|
2015-04-20 07:55:20 +02:00
|
|
|
for(i = 0; (unsigned)i < nget; i++)
|
2008-12-03 00:49:39 +01:00
|
|
|
{
|
2008-12-22 10:49:01 +01:00
|
|
|
if(pelst[i].portev_source == PORT_SOURCE_FD)
|
2008-12-03 00:49:39 +01:00
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
fd = pelst[i].portev_object;
|
|
|
|
PF *hdl = NULL;
|
2008-12-22 10:49:01 +01:00
|
|
|
rb_fde_t *F = pelst[i].portev_user;
|
|
|
|
if((pelst[i].portev_events & (POLLIN | POLLHUP | POLLERR)) && (hdl = F->read_handler))
|
2008-12-03 00:49:39 +01:00
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
F->read_handler = NULL;
|
|
|
|
hdl(F, F->read_data);
|
|
|
|
}
|
2008-12-22 10:49:01 +01:00
|
|
|
if((pelst[i].portev_events & (POLLOUT | POLLHUP | POLLERR)) && (hdl = F->write_handler))
|
2008-12-03 00:49:39 +01:00
|
|
|
{
|
2008-04-01 18:52:26 +02:00
|
|
|
F->write_handler = NULL;
|
|
|
|
hdl(F, F->write_data);
|
|
|
|
}
|
2008-12-22 10:49:01 +01:00
|
|
|
} else if(pelst[i].portev_source == PORT_SOURCE_TIMER)
|
|
|
|
{
|
|
|
|
ev = (struct ev_entry *)pelst[i].portev_user;
|
2016-04-03 06:38:28 +02:00
|
|
|
rb_run_one_event(ev);
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return RB_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
int
|
|
|
|
rb_ports_supports_event(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ports_init_event(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_ports_sched_event(struct ev_entry *event, int when)
|
|
|
|
{
|
|
|
|
timer_t *id;
|
|
|
|
struct sigevent ev;
|
|
|
|
port_notify_t not;
|
|
|
|
struct itimerspec ts;
|
|
|
|
|
|
|
|
event->comm_ptr = rb_malloc(sizeof(timer_t));
|
|
|
|
id = event->comm_ptr;
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
memset(&ev, 0, sizeof(ev));
|
|
|
|
ev.sigev_notify = SIGEV_PORT;
|
|
|
|
ev.sigev_value.sival_ptr = ¬
|
2014-03-03 05:25:47 +01:00
|
|
|
|
|
|
|
memset(¬, 0, sizeof(not));
|
2008-12-22 10:49:01 +01:00
|
|
|
not.portnfy_port = pe;
|
|
|
|
not.portnfy_user = event;
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
if(timer_create(CLOCK_REALTIME, &ev, id) < 0)
|
|
|
|
{
|
|
|
|
rb_lib_log("timer_create: %s\n", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
memset(&ts, 0, sizeof(ts));
|
|
|
|
ts.it_value.tv_sec = when;
|
2014-03-03 05:25:47 +01:00
|
|
|
ts.it_value.tv_nsec = 0;
|
2008-12-22 10:49:01 +01:00
|
|
|
if(event->frequency != 0)
|
|
|
|
ts.it_interval = ts.it_value;
|
|
|
|
|
|
|
|
if(timer_settime(*id, 0, &ts, NULL) < 0)
|
|
|
|
{
|
|
|
|
rb_lib_log("timer_settime: %s\n", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
return 1;
|
2008-12-22 10:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ports_unsched_event(struct ev_entry *event)
|
|
|
|
{
|
|
|
|
timer_delete(*((timer_t *) event->comm_ptr));
|
|
|
|
rb_free(event->comm_ptr);
|
|
|
|
event->comm_ptr = NULL;
|
|
|
|
}
|
2008-04-01 18:52:26 +02:00
|
|
|
#else /* ports not supported */
|
2008-12-22 10:49:01 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
rb_ports_supports_event(void)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ports_init_event(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
int
|
|
|
|
rb_ports_sched_event(struct ev_entry *event, int when)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-22 10:49:01 +01:00
|
|
|
void
|
|
|
|
rb_ports_unsched_event(struct ev_entry *event)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
int
|
2008-04-01 18:52:26 +02:00
|
|
|
rb_init_netio_ports(void)
|
|
|
|
{
|
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_setselect_ports(rb_fde_t *F, unsigned int type, PF * handler, void *client_data)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
errno = ENOSYS;
|
|
|
|
return;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
int
|
|
|
|
rb_select_ports(long delay)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
int
|
|
|
|
rb_setup_fd_ports(rb_fde_t *F)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2008-04-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-01 18:52:26 +02:00
|
|
|
#endif
|