2007-01-23 13:46:18 +00:00
|
|
|
/*
|
|
|
|
* util/netevent.c - event notification
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
*
|
2007-01-25 12:39:51 +00:00
|
|
|
* This software is open source.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2007-01-23 13:46:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* This file contains event notification functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util/netevent.h"
|
|
|
|
#include "util/log.h"
|
|
|
|
#include <errno.h>
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
/* -------- Start of local definitions -------- */
|
2007-02-07 15:44:19 +00:00
|
|
|
/** The TCP reading or writing query timeout in seconds */
|
|
|
|
#define TCP_QUERY_TIMEOUT 120
|
2007-02-07 14:18:42 +00:00
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
/* We define libevent structures here to hide the libevent stuff. */
|
2007-01-23 16:10:23 +00:00
|
|
|
|
2007-01-23 13:46:18 +00:00
|
|
|
/* we use libevent */
|
|
|
|
#include <event.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal event structure for keeping libevent info for the event.
|
|
|
|
* Possibly other structures (list, tree) this is part of.
|
|
|
|
*/
|
|
|
|
struct internal_event {
|
|
|
|
/** libevent event type, alloced here */
|
|
|
|
struct event ev;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal base structure, so that every thread has its own events.
|
|
|
|
*/
|
|
|
|
struct internal_base {
|
|
|
|
/** libevent event_base type. */
|
|
|
|
struct event_base* base;
|
|
|
|
};
|
|
|
|
|
2007-02-01 15:06:38 +00:00
|
|
|
/**
|
|
|
|
* Internal timer structure, to store timer event in.
|
|
|
|
*/
|
|
|
|
struct internal_timer {
|
|
|
|
/** libevent event type, alloced here */
|
|
|
|
struct event ev;
|
2007-02-06 14:00:52 +00:00
|
|
|
/** is timer enabled */
|
2007-02-01 15:06:38 +00:00
|
|
|
uint8_t enabled;
|
|
|
|
};
|
|
|
|
|
2007-02-05 16:46:40 +00:00
|
|
|
/**
|
|
|
|
* Internal signal structure, to store signal event in.
|
|
|
|
*/
|
|
|
|
struct internal_signal {
|
|
|
|
/** libevent event type, alloced here */
|
|
|
|
struct event ev;
|
|
|
|
/** next in signal list */
|
|
|
|
struct internal_signal* next;
|
|
|
|
};
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
/**
|
|
|
|
* handle libevent callback for udp comm point.
|
|
|
|
* @param fd: file descriptor.
|
|
|
|
* @param event: event bits from libevent:
|
|
|
|
* EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
|
|
|
|
* @param arg: the comm_point structure.
|
|
|
|
*/
|
|
|
|
static void comm_point_udp_callback(int fd, short event, void* arg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handle libevent callback for tcp accept comm point
|
|
|
|
* @param fd: file descriptor.
|
|
|
|
* @param event: event bits from libevent:
|
|
|
|
* EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
|
|
|
|
* @param arg: the comm_point structure.
|
|
|
|
*/
|
|
|
|
static void comm_point_tcp_accept_callback(int fd, short event, void* arg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handle libevent callback for tcp data comm point
|
|
|
|
* @param fd: file descriptor.
|
|
|
|
* @param event: event bits from libevent:
|
|
|
|
* EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
|
|
|
|
* @param arg: the comm_point structure.
|
|
|
|
*/
|
|
|
|
static void comm_point_tcp_handle_callback(int fd, short event, void* arg);
|
|
|
|
|
2007-02-01 15:06:38 +00:00
|
|
|
/**
|
|
|
|
* handle libevent callback for timer comm.
|
|
|
|
* @param fd: file descriptor (always -1).
|
|
|
|
* @param event: event bits from libevent:
|
|
|
|
* EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
|
2007-02-05 16:46:40 +00:00
|
|
|
* @param arg: the comm_timer structure.
|
2007-02-01 15:06:38 +00:00
|
|
|
*/
|
|
|
|
static void comm_timer_callback(int fd, short event, void* arg);
|
|
|
|
|
2007-02-05 16:46:40 +00:00
|
|
|
/**
|
|
|
|
* handle libevent callback for signal comm.
|
2007-02-06 14:00:52 +00:00
|
|
|
* @param fd: file descriptor (used for the signal number).
|
2007-02-05 16:46:40 +00:00
|
|
|
* @param event: event bits from libevent:
|
|
|
|
* EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
|
|
|
|
* @param arg: the internal commsignal structure.
|
|
|
|
*/
|
|
|
|
static void comm_signal_callback(int fd, short event, void* arg);
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
/** create a tcp handler with a parent */
|
|
|
|
static struct comm_point* comm_point_create_tcp_handler(
|
|
|
|
struct comm_base *base, struct comm_point* parent, size_t bufsize,
|
|
|
|
comm_point_callback_t* callback, void* callback_arg);
|
|
|
|
|
|
|
|
/* -------- End of local definitions -------- */
|
|
|
|
|
|
|
|
struct comm_base*
|
|
|
|
comm_base_create()
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
|
|
|
struct comm_base* b = (struct comm_base*)calloc(1,
|
|
|
|
sizeof(struct comm_base));
|
|
|
|
if(!b)
|
|
|
|
return NULL;
|
|
|
|
b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
|
|
|
|
if(!b->eb) {
|
|
|
|
free(b);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
b->eb->base = event_init();
|
|
|
|
if(!b->eb->base) {
|
|
|
|
free(b->eb);
|
|
|
|
free(b);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-25 12:39:51 +00:00
|
|
|
verbose(VERB_ALGO, "libevent uses %s method.", event_get_method());
|
2007-01-23 13:46:18 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
void
|
|
|
|
comm_base_delete(struct comm_base* b)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
2007-01-25 12:39:51 +00:00
|
|
|
#ifdef HAVE_EVENT_BASE_FREE
|
|
|
|
/* only libevent 1.2+ has it */
|
|
|
|
event_base_free(b->eb->base);
|
|
|
|
#endif /* HAVE_EVENT_BASE_FREE */
|
2007-01-23 13:46:18 +00:00
|
|
|
b->eb->base = NULL;
|
|
|
|
free(b->eb);
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
void
|
|
|
|
comm_base_dispatch(struct comm_base* b)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
|
|
|
int retval;
|
2007-02-05 16:46:40 +00:00
|
|
|
retval = event_base_dispatch(b->eb->base);
|
|
|
|
if(retval != 0) {
|
|
|
|
fatal_exit("event_dispatch returned error %d, "
|
|
|
|
"errno is %s", retval, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void comm_base_exit(struct comm_base* b)
|
|
|
|
{
|
|
|
|
if(event_base_loopexit(b->eb->base, NULL) != 0) {
|
|
|
|
log_err("Could not loopexit");
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-31 09:32:30 +00:00
|
|
|
/** send a UDP reply */
|
2007-02-01 15:06:38 +00:00
|
|
|
int
|
|
|
|
comm_point_send_udp_msg(struct comm_point *c, ldns_buffer* packet,
|
|
|
|
struct sockaddr* addr, socklen_t addrlen)
|
|
|
|
{
|
2007-01-31 09:32:30 +00:00
|
|
|
ssize_t sent;
|
2007-02-01 15:06:38 +00:00
|
|
|
sent = sendto(c->fd, ldns_buffer_begin(packet),
|
|
|
|
ldns_buffer_remaining(packet), 0,
|
2007-01-31 09:32:30 +00:00
|
|
|
addr, addrlen);
|
|
|
|
if(sent == -1) {
|
|
|
|
log_err("sendto failed: %s", strerror(errno));
|
2007-02-01 15:06:38 +00:00
|
|
|
return 0;
|
|
|
|
} else if((size_t)sent != ldns_buffer_remaining(packet)) {
|
2007-01-31 09:32:30 +00:00
|
|
|
log_err("sent %d in place of %d bytes",
|
2007-02-14 10:10:43 +00:00
|
|
|
(int)sent, (int)ldns_buffer_remaining(packet));
|
2007-02-01 15:06:38 +00:00
|
|
|
return 0;
|
2007-01-31 09:32:30 +00:00
|
|
|
}
|
2007-02-01 15:06:38 +00:00
|
|
|
return 1;
|
2007-01-31 09:32:30 +00:00
|
|
|
}
|
|
|
|
|
2007-01-23 13:46:18 +00:00
|
|
|
static void
|
2007-01-31 09:32:30 +00:00
|
|
|
comm_point_udp_callback(int fd, short event, void* arg)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
2007-01-31 09:32:30 +00:00
|
|
|
struct comm_reply rep;
|
|
|
|
ssize_t recv;
|
|
|
|
|
|
|
|
rep.c = (struct comm_point*)arg;
|
2007-02-06 14:00:52 +00:00
|
|
|
log_assert(rep.c->type == comm_udp);
|
2007-01-31 09:32:30 +00:00
|
|
|
|
|
|
|
verbose(VERB_ALGO, "callback udp");
|
|
|
|
if(!(event&EV_READ))
|
|
|
|
return;
|
|
|
|
log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
|
|
|
|
ldns_buffer_clear(rep.c->buffer);
|
2007-02-07 10:08:55 +00:00
|
|
|
rep.addrlen = (socklen_t)sizeof(rep.addr);
|
2007-01-31 09:32:30 +00:00
|
|
|
recv = recvfrom(fd, ldns_buffer_begin(rep.c->buffer),
|
|
|
|
ldns_buffer_remaining(rep.c->buffer), 0,
|
|
|
|
(struct sockaddr*)&rep.addr, &rep.addrlen);
|
|
|
|
if(recv == -1) {
|
|
|
|
if(errno != EAGAIN && errno != EINTR) {
|
|
|
|
log_err("recvfrom failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ldns_buffer_skip(rep.c->buffer, recv);
|
|
|
|
ldns_buffer_flip(rep.c->buffer);
|
2007-02-05 14:25:07 +00:00
|
|
|
if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
|
2007-01-31 09:32:30 +00:00
|
|
|
/* send back immediate reply */
|
2007-02-01 15:06:38 +00:00
|
|
|
(void)comm_point_send_udp_msg(rep.c, rep.c->buffer,
|
|
|
|
(struct sockaddr*)&rep.addr, rep.addrlen);
|
2007-01-31 09:32:30 +00:00
|
|
|
}
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-07 14:18:42 +00:00
|
|
|
/** Use a new tcp handler for new query fd, set to read query. */
|
|
|
|
static void
|
|
|
|
setup_tcp_handler(struct comm_point* c, int fd)
|
|
|
|
{
|
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
log_assert(c->fd == -1);
|
|
|
|
ldns_buffer_clear(c->buffer);
|
|
|
|
c->tcp_is_reading = 1;
|
|
|
|
c->tcp_byte_count = 0;
|
2007-02-07 15:44:19 +00:00
|
|
|
comm_point_start_listening(c, fd, TCP_QUERY_TIMEOUT);
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
|
|
|
|
2007-01-23 13:46:18 +00:00
|
|
|
static void
|
2007-02-07 14:18:42 +00:00
|
|
|
comm_point_tcp_accept_callback(int fd, short event, void* arg)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
2007-02-07 14:18:42 +00:00
|
|
|
struct comm_point* c = (struct comm_point*)arg, *c_hdl;
|
|
|
|
struct comm_reply rep;
|
|
|
|
int new_fd;
|
2007-02-06 14:00:52 +00:00
|
|
|
log_assert(c->type == comm_tcp_accept);
|
2007-02-07 14:18:42 +00:00
|
|
|
if(!(event & EV_READ)) {
|
|
|
|
log_info("ignoring tcp accept event %d", (int)event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* accept incoming connection. */
|
|
|
|
rep.c = NULL;
|
|
|
|
rep.addrlen = (socklen_t)sizeof(rep.addr);
|
|
|
|
new_fd = accept(fd, (struct sockaddr*)&rep.addr, &rep.addrlen);
|
|
|
|
if(new_fd == -1) {
|
|
|
|
/* EINTR is signal interrupt. others are closed connection. */
|
2007-02-15 10:48:43 +00:00
|
|
|
if( errno != EINTR
|
|
|
|
&& errno != EWOULDBLOCK
|
|
|
|
&& errno != ECONNABORTED
|
|
|
|
#ifdef EPROTO
|
|
|
|
&& errno != EPROTO
|
|
|
|
#endif /* EPROTO */
|
|
|
|
)
|
2007-02-07 14:18:42 +00:00
|
|
|
log_err("accept failed: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* find free tcp handler. */
|
|
|
|
if(!c->tcp_free) {
|
|
|
|
log_err("accepted too many tcp, connections full, from:");
|
|
|
|
log_addr(&rep.addr, rep.addrlen);
|
|
|
|
close(new_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* grab it */
|
|
|
|
c_hdl = c->tcp_free;
|
|
|
|
c->tcp_free = c_hdl->tcp_free;
|
|
|
|
if(!c->tcp_free) {
|
|
|
|
/* stop accepting incoming queries for now. */
|
|
|
|
comm_point_stop_listening(c);
|
|
|
|
}
|
|
|
|
/* addr is dropped. Not needed for tcp reply. */
|
|
|
|
setup_tcp_handler(c_hdl, new_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make tcp handler free for next assignment. */
|
|
|
|
static void
|
|
|
|
reclaim_tcp_handler(struct comm_point* c)
|
|
|
|
{
|
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
comm_point_close(c);
|
|
|
|
c->tcp_free = c->tcp_parent->tcp_free;
|
|
|
|
c->tcp_parent->tcp_free = c;
|
|
|
|
if(!c->tcp_free) {
|
|
|
|
/* re-enable listening on accept socket */
|
|
|
|
comm_point_start_listening(c->tcp_parent, -1, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-07 15:44:19 +00:00
|
|
|
/** do the callback when writing is done */
|
|
|
|
static void
|
|
|
|
tcp_callback_writer(struct comm_point* c)
|
|
|
|
{
|
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
ldns_buffer_clear(c->buffer);
|
|
|
|
c->tcp_is_reading = 1;
|
|
|
|
c->tcp_byte_count = 0;
|
|
|
|
comm_point_stop_listening(c);
|
|
|
|
/* for listening socket */
|
|
|
|
reclaim_tcp_handler(c);
|
|
|
|
}
|
|
|
|
|
2007-02-07 14:18:42 +00:00
|
|
|
/** do the callback when reading is done */
|
|
|
|
static void
|
|
|
|
tcp_callback_reader(struct comm_point* c)
|
|
|
|
{
|
2007-02-07 15:44:19 +00:00
|
|
|
struct comm_reply rep;
|
2007-02-07 14:18:42 +00:00
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
ldns_buffer_flip(c->buffer);
|
|
|
|
c->tcp_is_reading = 0;
|
|
|
|
c->tcp_byte_count = 0;
|
|
|
|
comm_point_stop_listening(c);
|
2007-02-07 15:44:19 +00:00
|
|
|
rep.c = c;
|
|
|
|
rep.addrlen = 0;
|
|
|
|
if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &rep) ) {
|
|
|
|
comm_point_start_listening(c, -1, TCP_QUERY_TIMEOUT);
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-07 15:44:19 +00:00
|
|
|
/** Handle tcp reading callback.
|
|
|
|
* @param fd: file descriptor of socket.
|
|
|
|
* @param c: comm point to read from into buffer.
|
|
|
|
* @return: 0 on error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
comm_point_tcp_handle_read(int fd, struct comm_point* c)
|
2007-01-23 16:10:23 +00:00
|
|
|
{
|
2007-02-07 14:18:42 +00:00
|
|
|
ssize_t r;
|
2007-02-06 14:00:52 +00:00
|
|
|
log_assert(c->type == comm_tcp);
|
2007-02-07 15:44:19 +00:00
|
|
|
if(!c->tcp_is_reading)
|
|
|
|
return 0;
|
2007-02-07 14:18:42 +00:00
|
|
|
|
|
|
|
if(c->tcp_byte_count < sizeof(uint16_t)) {
|
|
|
|
/* read length bytes */
|
|
|
|
r = read(fd, ldns_buffer_at(c->buffer, c->tcp_byte_count),
|
|
|
|
sizeof(uint16_t)-c->tcp_byte_count);
|
2007-02-07 15:44:19 +00:00
|
|
|
if(r == 0)
|
|
|
|
return 0;
|
|
|
|
else if(r == -1) {
|
|
|
|
if(errno == EINTR || errno == EAGAIN)
|
|
|
|
return 1;
|
|
|
|
log_err("read (in tcp s): %s", strerror(errno));
|
|
|
|
return 0;
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
|
|
|
c->tcp_byte_count += r;
|
|
|
|
if(c->tcp_byte_count != sizeof(uint16_t))
|
2007-02-07 15:44:19 +00:00
|
|
|
return 1;
|
|
|
|
if(ldns_buffer_read_u16_at(c->buffer, 0) >
|
|
|
|
ldns_buffer_capacity(c->buffer)) {
|
|
|
|
verbose(VERB_DETAIL, "tcp: dropped larger than buffer");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-02-07 14:18:42 +00:00
|
|
|
ldns_buffer_set_limit(c->buffer,
|
|
|
|
ldns_buffer_read_u16_at(c->buffer, 0));
|
|
|
|
if(ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
|
|
|
|
verbose(VERB_DETAIL, "tcp: dropped bogus too short.");
|
2007-02-07 15:44:19 +00:00
|
|
|
return 0;
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
2007-02-15 15:50:22 +00:00
|
|
|
verbose(VERB_ALGO, "Reading tcp query of length %d",
|
2007-02-14 10:10:43 +00:00
|
|
|
(int)ldns_buffer_limit(c->buffer));
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = read(fd, ldns_buffer_current(c->buffer),
|
|
|
|
ldns_buffer_remaining(c->buffer));
|
|
|
|
if(r == 0) {
|
2007-02-07 15:44:19 +00:00
|
|
|
return 0;
|
2007-02-07 14:18:42 +00:00
|
|
|
} else if(r == -1) {
|
2007-02-07 15:44:19 +00:00
|
|
|
if(errno == EINTR || errno == EAGAIN)
|
|
|
|
return 1;
|
|
|
|
log_err("read (in tcp r): %s", strerror(errno));
|
|
|
|
return 0;
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
|
|
|
ldns_buffer_skip(c->buffer, r);
|
|
|
|
if(ldns_buffer_remaining(c->buffer) <= 0) {
|
|
|
|
tcp_callback_reader(c);
|
|
|
|
}
|
2007-02-07 15:44:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Handle tcp writing callback.
|
|
|
|
* @param fd: file descriptor of socket.
|
|
|
|
* @param c: comm point to write buffer out of.
|
|
|
|
* @return: 0 on error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
comm_point_tcp_handle_write(int fd, struct comm_point* c)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
if(c->tcp_is_reading)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(c->tcp_byte_count < sizeof(uint16_t)) {
|
|
|
|
uint16_t len = htons(ldns_buffer_limit(c->buffer));
|
|
|
|
r = write(fd, &len, sizeof(uint16_t)-c->tcp_byte_count);
|
|
|
|
if(r == -1) {
|
|
|
|
if(errno == EINTR || errno == EAGAIN)
|
|
|
|
return 1;
|
|
|
|
log_err("tcp write(s): %s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
c->tcp_byte_count += r;
|
|
|
|
if(c->tcp_byte_count != sizeof(uint16_t))
|
|
|
|
return 1;
|
|
|
|
ldns_buffer_set_position(c->buffer, 0);
|
|
|
|
}
|
|
|
|
r = write(fd, ldns_buffer_current(c->buffer),
|
|
|
|
ldns_buffer_remaining(c->buffer));
|
|
|
|
if(r == -1) {
|
|
|
|
if(errno == EINTR || errno == EAGAIN)
|
|
|
|
return 1;
|
|
|
|
log_err("tcp write(w): %s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ldns_buffer_skip(c->buffer, r);
|
|
|
|
|
|
|
|
if(ldns_buffer_remaining(c->buffer) == 0) {
|
|
|
|
tcp_callback_writer(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
comm_point_tcp_handle_callback(int fd, short event, void* arg)
|
|
|
|
{
|
|
|
|
struct comm_point* c = (struct comm_point*)arg;
|
|
|
|
log_assert(c->type == comm_tcp);
|
|
|
|
|
|
|
|
if(event&EV_READ) {
|
|
|
|
if(!comm_point_tcp_handle_read(fd, c)) {
|
|
|
|
reclaim_tcp_handler(c);
|
|
|
|
if(!c->tcp_do_close)
|
|
|
|
(void)(*c->callback)(c, c->cb_arg,
|
|
|
|
NETEVENT_CLOSED, NULL);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event&EV_WRITE) {
|
|
|
|
if(!comm_point_tcp_handle_write(fd, c)) {
|
|
|
|
reclaim_tcp_handler(c);
|
|
|
|
if(!c->tcp_do_close)
|
|
|
|
(void)(*c->callback)(c, c->cb_arg,
|
|
|
|
NETEVENT_CLOSED, NULL);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event&EV_TIMEOUT) {
|
|
|
|
verbose(VERB_DETAIL, "tcp took too long, dropped");
|
|
|
|
reclaim_tcp_handler(c);
|
|
|
|
if(!c->tcp_do_close)
|
|
|
|
(void)(*c->callback)(c, c->cb_arg,
|
|
|
|
NETEVENT_TIMEOUT, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_err("Ignored event %d for tcphdl.", event);
|
2007-01-23 16:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct comm_point*
|
2007-01-24 13:46:31 +00:00
|
|
|
comm_point_create_udp(struct comm_base *base, int fd, ldns_buffer* buffer,
|
2007-01-23 13:46:18 +00:00
|
|
|
comm_point_callback_t* callback, void* callback_arg)
|
|
|
|
{
|
|
|
|
struct comm_point* c = (struct comm_point*)calloc(1,
|
|
|
|
sizeof(struct comm_point));
|
|
|
|
short evbits;
|
|
|
|
if(!c)
|
|
|
|
return NULL;
|
|
|
|
c->ev = (struct internal_event*)calloc(1,
|
|
|
|
sizeof(struct internal_event));
|
|
|
|
if(!c->ev) {
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->fd = fd;
|
|
|
|
c->buffer = buffer;
|
|
|
|
c->timeout = NULL;
|
|
|
|
c->tcp_is_reading = 0;
|
|
|
|
c->tcp_byte_count = 0;
|
|
|
|
c->tcp_parent = NULL;
|
|
|
|
c->max_tcp_count = 0;
|
|
|
|
c->tcp_handlers = NULL;
|
|
|
|
c->tcp_free = NULL;
|
|
|
|
c->type = comm_udp;
|
|
|
|
c->tcp_do_close = 0;
|
|
|
|
c->tcp_do_toggle_rw = 0;
|
|
|
|
c->callback = callback;
|
|
|
|
c->cb_arg = callback_arg;
|
|
|
|
evbits = EV_READ | EV_PERSIST;
|
|
|
|
/* libevent stuff */
|
|
|
|
event_set(&c->ev->ev, c->fd, evbits, comm_point_udp_callback, c);
|
|
|
|
if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
|
|
|
|
event_add(&c->ev->ev, c->timeout) != 0 ) {
|
|
|
|
log_err("could not add udp event");
|
|
|
|
comm_point_delete(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
static struct comm_point*
|
2007-01-23 13:46:18 +00:00
|
|
|
comm_point_create_tcp_handler(struct comm_base *base,
|
|
|
|
struct comm_point* parent, size_t bufsize,
|
|
|
|
comm_point_callback_t* callback, void* callback_arg)
|
|
|
|
{
|
2007-01-23 16:10:23 +00:00
|
|
|
struct comm_point* c = (struct comm_point*)calloc(1,
|
|
|
|
sizeof(struct comm_point));
|
|
|
|
short evbits;
|
|
|
|
if(!c)
|
|
|
|
return NULL;
|
|
|
|
c->ev = (struct internal_event*)calloc(1,
|
|
|
|
sizeof(struct internal_event));
|
|
|
|
if(!c->ev) {
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->fd = -1;
|
2007-01-24 13:46:31 +00:00
|
|
|
c->buffer = ldns_buffer_new(bufsize);
|
2007-02-07 14:18:42 +00:00
|
|
|
if(!c->buffer) {
|
|
|
|
free(c->ev);
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
|
|
|
|
if(!c->timeout) {
|
|
|
|
ldns_buffer_free(c->buffer);
|
|
|
|
free(c->ev);
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-23 16:10:23 +00:00
|
|
|
c->tcp_is_reading = 0;
|
|
|
|
c->tcp_byte_count = 0;
|
|
|
|
c->tcp_parent = parent;
|
|
|
|
c->max_tcp_count = 0;
|
|
|
|
c->tcp_handlers = NULL;
|
|
|
|
c->tcp_free = NULL;
|
|
|
|
c->type = comm_tcp;
|
|
|
|
c->tcp_do_close = 0;
|
|
|
|
c->tcp_do_toggle_rw = 0;
|
|
|
|
c->callback = callback;
|
|
|
|
c->cb_arg = callback_arg;
|
|
|
|
/* add to parent free list */
|
|
|
|
c->tcp_free = parent->tcp_free;
|
|
|
|
parent->tcp_free = c;
|
|
|
|
/* libevent stuff */
|
|
|
|
evbits = EV_PERSIST | EV_READ;
|
|
|
|
event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_handle_callback, c);
|
|
|
|
if(event_base_set(base->eb->base, &c->ev->ev) != 0)
|
|
|
|
{
|
|
|
|
log_err("could not basetset tcphdl event");
|
|
|
|
parent->tcp_free = c->tcp_free;
|
|
|
|
free(c->ev);
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return c;
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct comm_point*
|
|
|
|
comm_point_create_tcp(struct comm_base *base, int fd, int num, size_t bufsize,
|
|
|
|
comm_point_callback_t* callback, void* callback_arg)
|
|
|
|
{
|
|
|
|
struct comm_point* c = (struct comm_point*)calloc(1,
|
|
|
|
sizeof(struct comm_point));
|
|
|
|
short evbits;
|
|
|
|
int i;
|
|
|
|
/* first allocate the TCP accept listener */
|
|
|
|
if(!c)
|
|
|
|
return NULL;
|
|
|
|
c->ev = (struct internal_event*)calloc(1,
|
|
|
|
sizeof(struct internal_event));
|
|
|
|
if(!c->ev) {
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->fd = fd;
|
|
|
|
c->buffer = NULL;
|
|
|
|
c->timeout = NULL;
|
|
|
|
c->tcp_is_reading = 0;
|
|
|
|
c->tcp_byte_count = 0;
|
|
|
|
c->tcp_parent = NULL;
|
|
|
|
c->max_tcp_count = num;
|
2007-01-25 09:48:37 +00:00
|
|
|
c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
|
2007-01-23 13:46:18 +00:00
|
|
|
sizeof(struct comm_point*));
|
2007-02-06 14:00:52 +00:00
|
|
|
if(!c->tcp_handlers) {
|
|
|
|
free(c->ev);
|
|
|
|
free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-23 13:46:18 +00:00
|
|
|
c->tcp_free = NULL;
|
|
|
|
c->type = comm_tcp_accept;
|
|
|
|
c->tcp_do_close = 0;
|
|
|
|
c->tcp_do_toggle_rw = 0;
|
|
|
|
c->callback = NULL;
|
|
|
|
c->cb_arg = NULL;
|
|
|
|
evbits = EV_READ | EV_PERSIST;
|
|
|
|
/* libevent stuff */
|
|
|
|
event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_accept_callback, c);
|
2007-02-06 14:00:52 +00:00
|
|
|
if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
|
2007-01-23 13:46:18 +00:00
|
|
|
event_add(&c->ev->ev, c->timeout) != 0 )
|
|
|
|
{
|
|
|
|
log_err("could not add tcpacc event");
|
2007-02-06 14:00:52 +00:00
|
|
|
comm_point_delete(c);
|
2007-01-23 13:46:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now prealloc the tcp handlers */
|
|
|
|
for(i=0; i<num; i++) {
|
|
|
|
c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
|
|
|
|
c, bufsize, callback, callback_arg);
|
2007-01-23 16:10:23 +00:00
|
|
|
if(!c->tcp_handlers[i]) {
|
|
|
|
comm_point_delete(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
void
|
|
|
|
comm_point_close(struct comm_point* c)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
2007-02-06 14:00:52 +00:00
|
|
|
if(!c)
|
|
|
|
return;
|
2007-01-23 13:46:18 +00:00
|
|
|
if(event_del(&c->ev->ev) != 0) {
|
|
|
|
log_err("could not event_del on close");
|
|
|
|
}
|
2007-02-06 14:00:52 +00:00
|
|
|
/* close fd after removing from event lists, or epoll.. is messed up */
|
2007-01-25 16:08:52 +00:00
|
|
|
if(c->fd != -1)
|
|
|
|
close(c->fd);
|
|
|
|
c->fd = -1;
|
2007-01-23 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2007-01-23 16:10:23 +00:00
|
|
|
void
|
|
|
|
comm_point_delete(struct comm_point* c)
|
2007-01-23 13:46:18 +00:00
|
|
|
{
|
2007-01-23 16:10:23 +00:00
|
|
|
if(!c)
|
|
|
|
return;
|
2007-01-23 13:46:18 +00:00
|
|
|
comm_point_close(c);
|
|
|
|
if(c->tcp_handlers) {
|
|
|
|
int i;
|
|
|
|
for(i=0; i<c->max_tcp_count; i++)
|
|
|
|
comm_point_delete(c->tcp_handlers[i]);
|
|
|
|
free(c->tcp_handlers);
|
|
|
|
}
|
2007-01-25 16:08:52 +00:00
|
|
|
if(c->type == comm_tcp)
|
|
|
|
ldns_buffer_free(c->buffer);
|
2007-01-23 13:46:18 +00:00
|
|
|
free(c->ev);
|
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
2007-01-25 16:08:52 +00:00
|
|
|
void
|
|
|
|
comm_point_set_cb_arg(struct comm_point* c, void *arg)
|
|
|
|
{
|
|
|
|
log_assert(c);
|
|
|
|
c->cb_arg = arg;
|
|
|
|
}
|
2007-01-31 09:32:30 +00:00
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
void
|
|
|
|
comm_point_send_reply(struct comm_reply *repinfo)
|
2007-01-31 09:32:30 +00:00
|
|
|
{
|
|
|
|
log_assert(repinfo && repinfo->c);
|
|
|
|
if(repinfo->c->type == comm_udp) {
|
2007-02-01 15:06:38 +00:00
|
|
|
comm_point_send_udp_msg(repinfo->c, repinfo->c->buffer,
|
2007-01-31 09:32:30 +00:00
|
|
|
(struct sockaddr*)&repinfo->addr, repinfo->addrlen);
|
|
|
|
} else {
|
2007-02-07 15:44:19 +00:00
|
|
|
comm_point_start_listening(repinfo->c, -1, TCP_QUERY_TIMEOUT);
|
2007-01-31 09:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-07 15:53:51 +00:00
|
|
|
void
|
|
|
|
comm_point_drop_reply(struct comm_reply* repinfo)
|
|
|
|
{
|
|
|
|
if(!repinfo)
|
|
|
|
return;
|
|
|
|
log_assert(repinfo && repinfo->c);
|
|
|
|
log_assert(repinfo->c->type != comm_tcp_accept);
|
|
|
|
if(repinfo->c->type == comm_udp)
|
|
|
|
return;
|
|
|
|
reclaim_tcp_handler(repinfo->c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
comm_point_stop_listening(struct comm_point* c)
|
2007-02-07 14:18:42 +00:00
|
|
|
{
|
2007-02-15 15:50:22 +00:00
|
|
|
verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
|
2007-02-07 14:18:42 +00:00
|
|
|
if(event_del(&c->ev->ev) != 0) {
|
|
|
|
log_err("event_del error to stoplisten");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-07 15:53:51 +00:00
|
|
|
void
|
|
|
|
comm_point_start_listening(struct comm_point* c, int newfd, int sec)
|
2007-02-07 14:18:42 +00:00
|
|
|
{
|
2007-02-15 15:50:22 +00:00
|
|
|
verbose(VERB_ALGO, "comm point start listening %d", c->fd);
|
2007-02-07 14:18:42 +00:00
|
|
|
if(c->type == comm_tcp_accept && !c->tcp_free) {
|
|
|
|
/* no use to start listening no free slots. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(sec != -1 && sec != 0) {
|
|
|
|
if(!c->timeout) {
|
|
|
|
c->timeout = (struct timeval*)malloc(sizeof(
|
|
|
|
struct timeval));
|
|
|
|
if(!c->timeout) {
|
|
|
|
log_err("cpsl: malloc failed. No net read.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-02-07 15:44:19 +00:00
|
|
|
#ifndef S_SPLINT_S /* splint fails on struct timeval. */
|
2007-02-07 14:18:42 +00:00
|
|
|
c->timeout->tv_sec = sec;
|
|
|
|
c->timeout->tv_usec = 0;
|
2007-02-07 15:44:19 +00:00
|
|
|
#endif /* S_SPLINT_S */
|
|
|
|
}
|
|
|
|
if(c->type == comm_tcp) {
|
|
|
|
c->ev->ev.ev_events &= ~(EV_READ|EV_WRITE);
|
|
|
|
if(c->tcp_is_reading)
|
|
|
|
c->ev->ev.ev_events |= EV_READ;
|
|
|
|
else c->ev->ev.ev_events |= EV_WRITE;
|
|
|
|
}
|
|
|
|
if(newfd != -1) {
|
|
|
|
if(c->fd != -1)
|
|
|
|
close(c->fd);
|
|
|
|
c->fd = newfd;
|
|
|
|
c->ev->ev.ev_fd = c->fd;
|
2007-02-07 14:18:42 +00:00
|
|
|
}
|
|
|
|
if(event_add(&c->ev->ev, sec==0?NULL:c->timeout) != 0) {
|
|
|
|
log_err("event_add failed. in cpsl.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
struct comm_timer*
|
|
|
|
comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
|
2007-02-01 15:06:38 +00:00
|
|
|
{
|
|
|
|
struct comm_timer *tm = (struct comm_timer*)calloc(1,
|
|
|
|
sizeof(struct comm_timer));
|
|
|
|
if(!tm)
|
|
|
|
return NULL;
|
|
|
|
tm->ev_timer = (struct internal_timer*)calloc(1,
|
|
|
|
sizeof(struct internal_timer));
|
|
|
|
if(!tm->ev_timer) {
|
|
|
|
log_err("malloc failed");
|
|
|
|
free(tm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tm->callback = cb;
|
|
|
|
tm->cb_arg = cb_arg;
|
2007-02-02 12:52:40 +00:00
|
|
|
event_set(&tm->ev_timer->ev, -1, EV_PERSIST|EV_TIMEOUT,
|
|
|
|
comm_timer_callback, tm);
|
2007-02-01 15:06:38 +00:00
|
|
|
if(event_base_set(base->eb->base, &tm->ev_timer->ev) != 0) {
|
|
|
|
log_err("timer_create: event_base_set failed.");
|
|
|
|
free(tm->ev_timer);
|
|
|
|
free(tm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
void
|
|
|
|
comm_timer_disable(struct comm_timer* timer)
|
2007-02-01 15:06:38 +00:00
|
|
|
{
|
|
|
|
if(!timer)
|
|
|
|
return;
|
|
|
|
evtimer_del(&timer->ev_timer->ev);
|
|
|
|
timer->ev_timer->enabled = 0;
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
void
|
|
|
|
comm_timer_set(struct comm_timer* timer, struct timeval* tv)
|
2007-02-01 15:06:38 +00:00
|
|
|
{
|
|
|
|
if(timer->ev_timer->enabled)
|
|
|
|
comm_timer_disable(timer);
|
2007-02-02 12:52:40 +00:00
|
|
|
evtimer_add(&timer->ev_timer->ev, tv);
|
2007-02-01 15:06:38 +00:00
|
|
|
timer->ev_timer->enabled = 1;
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
void
|
|
|
|
comm_timer_delete(struct comm_timer* timer)
|
2007-02-01 15:06:38 +00:00
|
|
|
{
|
|
|
|
if(!timer)
|
|
|
|
return;
|
|
|
|
comm_timer_disable(timer);
|
|
|
|
free(timer->ev_timer);
|
|
|
|
free(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
|
|
|
|
{
|
|
|
|
struct comm_timer* tm = (struct comm_timer*)arg;
|
|
|
|
if(!(event&EV_TIMEOUT))
|
|
|
|
return;
|
|
|
|
tm->ev_timer->enabled = 0;
|
|
|
|
(*tm->callback)(tm->cb_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
comm_timer_is_set(struct comm_timer* timer)
|
|
|
|
{
|
|
|
|
return (int)timer->ev_timer->enabled;
|
|
|
|
}
|
2007-02-05 16:46:40 +00:00
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
struct comm_signal*
|
|
|
|
comm_signal_create(struct comm_base* base,
|
2007-02-05 16:46:40 +00:00
|
|
|
void (*callback)(int, void*), void* cb_arg)
|
|
|
|
{
|
|
|
|
struct comm_signal* com = (struct comm_signal*)malloc(
|
|
|
|
sizeof(struct comm_signal));
|
|
|
|
if(!com) {
|
|
|
|
log_err("malloc failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
com->base = base;
|
|
|
|
com->callback = callback;
|
|
|
|
com->cb_arg = cb_arg;
|
|
|
|
com->ev_signal = NULL;
|
|
|
|
return com;
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
static void
|
|
|
|
comm_signal_callback(int sig, short event, void* arg)
|
2007-02-05 16:46:40 +00:00
|
|
|
{
|
2007-02-06 14:00:52 +00:00
|
|
|
struct comm_signal* comsig = (struct comm_signal*)arg;
|
2007-02-05 16:46:40 +00:00
|
|
|
if(!(event & EV_SIGNAL))
|
|
|
|
return;
|
2007-02-06 14:00:52 +00:00
|
|
|
(*comsig->callback)(sig, comsig->cb_arg);
|
2007-02-05 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
int
|
|
|
|
comm_signal_bind(struct comm_signal* comsig, int sig)
|
2007-02-05 16:46:40 +00:00
|
|
|
{
|
|
|
|
struct internal_signal* entry = (struct internal_signal*)calloc(1,
|
|
|
|
sizeof(struct internal_signal));
|
|
|
|
if(!entry) {
|
|
|
|
log_err("malloc failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
log_assert(comsig);
|
|
|
|
/* add signal event */
|
2007-02-06 14:00:52 +00:00
|
|
|
signal_set(&entry->ev, sig, comm_signal_callback, comsig);
|
2007-02-05 16:46:40 +00:00
|
|
|
if(event_base_set(comsig->base->eb->base, &entry->ev) != 0) {
|
|
|
|
log_err("Could not set signal base");
|
|
|
|
free(entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(signal_add(&entry->ev, NULL) != 0) {
|
|
|
|
log_err("Could not add signal handler");
|
|
|
|
free(entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* link into list */
|
|
|
|
entry->next = comsig->ev_signal;
|
|
|
|
comsig->ev_signal = entry;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-06 14:00:52 +00:00
|
|
|
void
|
|
|
|
comm_signal_delete(struct comm_signal* comsig)
|
2007-02-05 16:46:40 +00:00
|
|
|
{
|
|
|
|
struct internal_signal* p, *np;
|
|
|
|
if(!comsig)
|
|
|
|
return;
|
|
|
|
p=comsig->ev_signal;
|
|
|
|
while(p) {
|
|
|
|
np = p->next;
|
|
|
|
signal_del(&p->ev);
|
|
|
|
free(p);
|
|
|
|
p = np;
|
|
|
|
}
|
|
|
|
free(comsig);
|
|
|
|
}
|
2007-02-07 14:18:42 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
log_addr(struct sockaddr_storage* addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
uint16_t port;
|
|
|
|
const char* family = "unknown";
|
|
|
|
char dest[100];
|
|
|
|
int af = (int)((struct sockaddr_in*)addr)->sin_family;
|
|
|
|
void* sinaddr = &((struct sockaddr_in*)addr)->sin_addr;
|
|
|
|
switch(af) {
|
|
|
|
case AF_INET: family="ip4"; break;
|
|
|
|
case AF_INET6: family="ip6";
|
|
|
|
sinaddr = &((struct sockaddr_in6*)addr)->sin6_addr;
|
|
|
|
break;
|
|
|
|
case AF_UNIX: family="unix"; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if(inet_ntop(af, sinaddr, dest, (socklen_t)sizeof(dest)) == 0) {
|
|
|
|
strncpy(dest, "(inet_ntop error)", sizeof(dest));
|
|
|
|
}
|
|
|
|
port = ntohs(((struct sockaddr_in*)addr)->sin_port);
|
2007-02-15 15:50:22 +00:00
|
|
|
verbose(VERB_DETAIL, "addr fam=%s port=%d dest=%s len=%d",
|
2007-02-07 14:18:42 +00:00
|
|
|
family, (int)port, dest, (int)addrlen);
|
|
|
|
}
|