diff options
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/npppd/common/rtev.h | 57 | ||||
-rw-r--r-- | usr.sbin/npppd/common/rtev_common.c | 517 | ||||
-rw-r--r-- | usr.sbin/npppd/common/rtev_libevent.c | 312 | ||||
-rw-r--r-- | usr.sbin/npppd/common/rtev_local.h | 57 |
4 files changed, 0 insertions, 943 deletions
diff --git a/usr.sbin/npppd/common/rtev.h b/usr.sbin/npppd/common/rtev.h deleted file mode 100644 index ecb989f7a41..00000000000 --- a/usr.sbin/npppd/common/rtev.h +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * Copyright (c) 2009 Internet Initiative Japan Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. - */ -#ifndef RTEV_H -#define RTEV_H - -#ifndef NO_RTEV_WRAPPER -#define getifaddrs(ifa) rtev_getifaddrs(ifa) -#define if_nametoindex(ifname) rtev_if_nametoindex(ifname); -#define freeifaddrs(ifa) ((void)0) -#endif - -#define RTEV_UPDATE_IFA_ON_DEMAND 0x0001 - -#ifdef __cplusplus -extern "C" { -#endif - -int rtev_libevent_init (int, int, int, int); -void rtev_fini (void); -int rtev_write (void *); -int rtev_getifaddrs (struct ifaddrs **); -int rtev_ifa_is_primary (const char *, struct sockaddr *); -inline int rtev_get_event_serial (void); -struct ifaddrs *rtev_getifaddrs_by_ifname (const char *); -struct ifaddrs *rtev_getifaddrs_by_sockaddr(struct sockaddr const *); -unsigned int rtev_if_nametoindex (const char *); -int rtev_has_write_pending(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/usr.sbin/npppd/common/rtev_common.c b/usr.sbin/npppd/common/rtev_common.c deleted file mode 100644 index 0c65175a0c2..00000000000 --- a/usr.sbin/npppd/common/rtev_common.c +++ /dev/null @@ -1,517 +0,0 @@ -/*- - * Copyright (c) 2009 Internet Initiative Japan Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. - */ -/* $Id: rtev_common.c,v 1.3 2010/07/02 21:20:57 yasuoka Exp $ */ -/* - * PF_ROUTE related utility functions. - * <p> - * When use with libevent, call rtev_libevent_init() to initialize this - * library.</p> - * usage: - * <pre> - * #include <sys/types.h> - * #include <time.h> - * #include <event.h> - * #include <ifaddrs.h> - * #include <rtev.h> - * - * int main() - * { - * event_init(); - * rtev_libevent_init(5, 100, 16); // init after event_init() - * - * event_loop(); - * - * rtev_fini(); // fini before exit() - * exit(0); - * } - * - * void hogehoge() - * { - * struct ifaddrs *ifa = NULL; - * - * getifaddrs(&ifa); // rtev.h replaces getifaddrs(3) - * : - * freeifaddrs(&ifa); - * } - * </pre> - */ -#include <sys/types.h> -#include <sys/param.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <net/route.h> -#include <net/if_dl.h> - -#include <ifaddrs.h> -#include <stdlib.h> -#include <syslog.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include <stddef.h> -#include <errno.h> - -#define NO_RTEV_WRAPPER 1 -#include "bytebuf.h" -#include "rtev.h" -#include "rtev_local.h" - -#ifdef RTEV_DEBUG -#include "debugutil.h" -#endif - -#ifndef RTEV_BUFSIZ -#define RTEV_BUFSIZ 131072 /* 128K */ -#endif - -static int rtev_base_on_rtevent(rtev_impl *); -static int rtev_base_on_write(rtev_impl *, int, int); - -static struct ifaddrs *rtev_cached_ifa = NULL; -static int rtev_event_serial = 0; -static int rtev_send_serial = 0; -static int rtev_ifa_cache_serial = 0; -static bytebuffer *rtev_sndbuf = NULL; -static u_char rtev_buffer_space[RTEV_BUFSIZ]; -static rtev_impl *singleton_impl = NULL; - -static inline int rtev_update_ifa_cache(rtev_impl *); -static int rtev_update_ifa_cache_do(rtev_impl *); -static void ifa_rbentry_init (void); -static void ifa_rbentry_fini (void); -static inline int ifa_rb_ifacenam_insert (struct ifaddrs *); -static inline int ifa_rb_sockaddr_insert (struct ifaddrs *); -static inline struct ifaddrs *ifa_rb_ifacenam_find (const char *); -static inline struct ifaddrs *ifa_rb_sockaddr_find (struct sockaddr const *); - -/** - * Write a routing message. - * @return not zero indicates error. See errno. - */ -int -rtev_write(void *rtm_msg) -{ - int rval; - struct rt_msghdr *rtm; - - rtm = rtm_msg; - rtm->rtm_seq = rtev_send_serial++; - if (bytebuffer_put(rtev_sndbuf, rtm, rtm->rtm_msglen) == NULL) - rval = -1; - else - rval = rtm->rtm_msglen; - - if (singleton_impl->impl_on_write != NULL) - singleton_impl->impl_on_write(singleton_impl); - - return rval; -} - -/** - * same as getifaddrs(3) but returned obeject is cached. - * The cached object may be freed by the event handler of this library, - * so you cannot use it after the event handler returns the event loop. - */ -int -rtev_getifaddrs(struct ifaddrs **ifa) -{ - if (rtev_update_ifa_cache(singleton_impl) != 0) - return 1; - - *ifa = rtev_cached_ifa; - - return 0; -} - -/** - * checks whether given address is the primary address of the interface. - * @return not zero if the address is the primary. - */ -int -rtev_ifa_is_primary(const char *ifname, struct sockaddr *sa) -{ - int count; - struct ifaddrs *ifa; - - for (count = 0, ifa = rtev_getifaddrs_by_ifname(ifname); ifa != NULL; - ifa = ifa->ifa_next) { - - if (strcmp(ifa->ifa_name, ifname) != 0) - break; - if (ifa->ifa_addr->sa_family != sa->sa_family) - continue; - switch (sa->sa_family) { - case AF_INET: - if (((struct sockaddr_in *)sa)->sin_addr.s_addr == - ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr - .s_addr) { - if (count == 0) - return 1; - return 0; - } - count++; - break; - case AF_INET6: - if (IN6_ARE_ADDR_EQUAL( - &((struct sockaddr_in6 *)sa)->sin6_addr, - &((struct sockaddr_in6 *)ifa->ifa_addr) - ->sin6_addr)){ - if (count == 0) - return 1; - return 0; - } - count++; - break; - } - } - return 0; -} - -/** returns the routing event serial number */ -int -rtev_get_event_serial() -{ - return rtev_event_serial; -} - -/** same as ifaddrs(3), but returned object is the first entry of 'ifname' */ -struct ifaddrs * -rtev_getifaddrs_by_ifname(const char *ifname) -{ - if (rtev_update_ifa_cache(singleton_impl) != 0) - return NULL; - - return ifa_rb_ifacenam_find(ifname); -} - -struct ifaddrs * -rtev_getifaddrs_by_sockaddr(struct sockaddr const *sa) -{ - if (rtev_update_ifa_cache(singleton_impl) != 0) - return NULL; - - return ifa_rb_sockaddr_find(sa); -} - -/** same as if_nametoindex(3), but fast and no memory allocation. */ -unsigned int -rtev_if_nametoindex(const char *ifname) -{ - unsigned int ni; - struct ifaddrs *ifa; - - /* adapted from lib/libc/net/if_nametoindex.c */ - ni = 0; - - for (ifa = rtev_getifaddrs_by_ifname(ifname); ifa != NULL; - ifa = ifa->ifa_next) { - if (ifa->ifa_addr && - ifa->ifa_addr->sa_family == AF_LINK) { - if (strcmp(ifa->ifa_name, ifname) == 0) { - ni = ((struct sockaddr_dl*)ifa->ifa_addr) - ->sdl_index; - break; - } else - break; - } - } - - if (!ni) - errno = ENXIO; - - return ni; -} - -/** API have write pending packet internaly */ -int -rtev_has_write_pending(void) -{ - if (bytebuffer_position(rtev_sndbuf) > 0) - return 1; - return 0; -} - -/** finalize this library. */ -void -rtev_fini(void) -{ - if (singleton_impl != NULL) - singleton_impl->impl_fini(singleton_impl->impl); - singleton_impl = NULL; - - if (rtev_cached_ifa != NULL) - freeifaddrs(rtev_cached_ifa); - rtev_cached_ifa = NULL; - ifa_rbentry_fini(); - - if (rtev_sndbuf != NULL) { - bytebuffer_unwrap(rtev_sndbuf); - bytebuffer_destroy(rtev_sndbuf); - } - rtev_sndbuf = NULL; -} - -/* protected virtual */ -int -rtev_base_init(rtev_impl *impl, int flags) -{ - ifa_rbentry_init(); - - if (rtev_sndbuf == NULL) { - if ((rtev_sndbuf = bytebuffer_wrap(rtev_buffer_space, - sizeof(rtev_buffer_space))) == NULL) - goto fail; - bytebuffer_clear(rtev_sndbuf); - } - impl->base_on_rtevent = rtev_base_on_rtevent; - impl->base_on_write = rtev_base_on_write; - impl->base_flags = flags; - singleton_impl = impl; - - return 0; -fail: - rtev_fini(); - - return 1; -} - -static int -rtev_base_on_rtevent(rtev_impl *impl) -{ - - RTEV_DBG((LOG_DEBUG, "%s", __func__)); - - rtev_event_serial++; - - if ((impl->base_flags & RTEV_UPDATE_IFA_ON_DEMAND) == 0) - return rtev_update_ifa_cache_do(impl); - - return 0; -} - -static inline int -rtev_update_ifa_cache(rtev_impl *impl) -{ - if (rtev_event_serial == rtev_ifa_cache_serial && - rtev_cached_ifa != NULL) - return 0; - - return rtev_update_ifa_cache_do(impl); -} - -static int -rtev_update_ifa_cache_do(rtev_impl *impl) -{ - const char *ifname; - struct ifaddrs *ifa; - struct ifaddrs *ifa0; - - RTEV_DBG((LOG_DEBUG, "%s", __func__)); - - ifa0 = NULL; - rtev_ifa_cache_serial = rtev_event_serial; - if (getifaddrs(&ifa0) != 0) - return 1; - if (rtev_cached_ifa != NULL) { - ifa_rbentry_fini(); - freeifaddrs(rtev_cached_ifa); - rtev_cached_ifa = NULL; - } - - for (ifa = ifa0, ifname = NULL; ifa != NULL; ifa = ifa->ifa_next) { - if (ifname == NULL || strcmp(ifa->ifa_name, ifname)) { - ifname = ifa->ifa_name; - if (ifa_rb_ifacenam_find(ifname) == NULL) { - if (ifa_rb_ifacenam_insert(ifa) != 0) - goto on_error; - } - } - if (ifa->ifa_addr != NULL && - ifa_rb_sockaddr_find(ifa->ifa_addr) == NULL) { - if (ifa_rb_sockaddr_insert(ifa) != 0) - goto on_error; - } - } - rtev_cached_ifa = ifa0; - - return 0; - -on_error: - if (ifa0) - freeifaddrs(ifa0); - rtev_cached_ifa = NULL; - ifa_rbentry_fini(); - - return 1; -} - -static int -rtev_base_on_write(rtev_impl *impl, int rtsock, int npackets) -{ - int i, rval; - struct rt_msghdr *rtm; - - rval = 0; - bytebuffer_flip(rtev_sndbuf); - for (i = 0; i < npackets && bytebuffer_remaining(rtev_sndbuf) > 0; i++){ - rtm = bytebuffer_pointer(rtev_sndbuf); - if (send(rtsock, rtm, rtm->rtm_msglen, 0) <= 0 && - !(rtm->rtm_type == RTM_DELETE && errno == ESRCH) && - !(rtm->rtm_type == RTM_ADD && errno == EEXIST)) { - rval = 1; - } - bytebuffer_get(rtev_sndbuf, BYTEBUFFER_GET_DIRECT, - rtm->rtm_msglen); - } - bytebuffer_compact(rtev_sndbuf); - - return rval; -} - -/* - * Red-black trees for interface name and interface address lookups. - */ -#include <sys/tree.h> /* BSD sys/tree.h */ - -struct ifa_rbentry { - struct ifaddrs *ifa; - RB_ENTRY(ifa_rbentry) rbe; -}; - -static inline int ifacenam_compar(struct ifa_rbentry *, struct ifa_rbentry *); -static RB_HEAD(ifa_rb_ifacenam, ifa_rbentry) ifa_rb_ifacenam; -RB_PROTOTYPE(ifa_rb_ifacenam, ifa_rbentry, rbe, ifacenam_compar); -RB_GENERATE(ifa_rb_ifacenam, ifa_rbentry, rbe, ifacenam_compar); - -static inline int sockaddr_compar(struct ifa_rbentry *, struct ifa_rbentry *); -static RB_HEAD(ifa_rb_sockaddr, ifa_rbentry) ifa_rb_sockaddr; -RB_PROTOTYPE(ifa_rb_sockaddr, ifa_rbentry, rbe, sockaddr_compar); -RB_GENERATE(ifa_rb_sockaddr, ifa_rbentry, rbe, sockaddr_compar); - -static void -ifa_rbentry_init(void) -{ - RB_INIT(&ifa_rb_ifacenam); - RB_INIT(&ifa_rb_sockaddr); -} - -static void -ifa_rbentry_fini(void) -{ - struct ifa_rbentry *e, *n; - - for (e = RB_MIN(ifa_rb_ifacenam, &ifa_rb_ifacenam); e; e = n) { - n = RB_NEXT(ifa_rb_ifacenam, &ifa_rb_ifacenam, e); - RB_REMOVE(ifa_rb_ifacenam, &ifa_rb_ifacenam, e); - free(e); - } - for (e = RB_MIN(ifa_rb_sockaddr, &ifa_rb_sockaddr); e; e = n) { - n = RB_NEXT(ifa_rb_sockaddr, &ifa_rb_sockaddr, e); - RB_REMOVE(ifa_rb_sockaddr, &ifa_rb_sockaddr, e); - free(e); - } -} - -static inline int -ifa_rb_ifacenam_insert(struct ifaddrs *ifa) -{ - struct ifa_rbentry *e; - - if ((e = malloc(sizeof(struct ifa_rbentry))) == NULL) - return -1; - - e->ifa = ifa; - RB_INSERT(ifa_rb_ifacenam, &ifa_rb_ifacenam, e); - - return 0; -} - -static inline int -ifa_rb_sockaddr_insert(struct ifaddrs *ifa) -{ - struct ifa_rbentry *e; - - if ((e = malloc(sizeof(struct ifa_rbentry))) == NULL) - return -1; - - e->ifa = ifa; - RB_INSERT(ifa_rb_sockaddr, &ifa_rb_sockaddr, e); - - return 0; -} - -static inline struct ifaddrs * -ifa_rb_ifacenam_find(const char *ifname) -{ - struct ifa_rbentry *e, e0; - struct ifaddrs ifa; - - e = &e0; - e->ifa = &ifa; - e->ifa->ifa_name = (char *)ifname; - - e = RB_FIND(ifa_rb_ifacenam, &ifa_rb_ifacenam, e); - if (e == NULL) - return NULL; - - return e->ifa; -} - -static inline struct ifaddrs * -ifa_rb_sockaddr_find(struct sockaddr const *sa) -{ - struct ifa_rbentry *e, e0; - struct ifaddrs ifa; - - e = &e0; - e->ifa = &ifa; - e->ifa->ifa_addr = (struct sockaddr *)sa; - - e = RB_FIND(ifa_rb_sockaddr, &ifa_rb_sockaddr, e); - if (e == NULL) - return NULL; - - return e->ifa; -} - -static inline int -ifacenam_compar(struct ifa_rbentry *a, struct ifa_rbentry *b) -{ - return strcmp(a->ifa->ifa_name, b->ifa->ifa_name); -} - -static inline int -sockaddr_compar(struct ifa_rbentry *a, struct ifa_rbentry *b) -{ - int cmp; - - cmp = b->ifa->ifa_addr->sa_family - a->ifa->ifa_addr->sa_family; - if (cmp != 0) - return cmp; - return memcmp(a->ifa->ifa_addr->sa_data, b->ifa->ifa_addr->sa_data, - MIN(a->ifa->ifa_addr->sa_len, b->ifa->ifa_addr->sa_len) - - offsetof(struct sockaddr, sa_data)); -} diff --git a/usr.sbin/npppd/common/rtev_libevent.c b/usr.sbin/npppd/common/rtev_libevent.c deleted file mode 100644 index bd062300ba6..00000000000 --- a/usr.sbin/npppd/common/rtev_libevent.c +++ /dev/null @@ -1,312 +0,0 @@ -/*- - * Copyright (c) 2009 Internet Initiative Japan Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. - */ -/* $Id: rtev_libevent.c,v 1.2 2010/07/01 03:38:17 yasuoka Exp $ */ -#include <sys/types.h> -#include <sys/socket.h> -#include <net/route.h> -#include <ifaddrs.h> -#include <time.h> -#include <event.h> -#include <errno.h> -#include <fcntl.h> -#include <string.h> -#include <unistd.h> -#include <syslog.h> -#include <stdio.h> -#include <stdlib.h> - -#define NO_RTEV_WRAPPER 1 -#include "rtev_local.h" -#include "rtev.h" - -typedef struct _rtev_libevent rtev_libevent; - -static int rtev_libevent_init0 (rtev_libevent *, int, int, int, int); -static void rtev_libevent_fini (rtev_impl *); -static inline time_t get_monosec(void); - -static rtev_libevent rtev_libevent_this; - -/** - * Initialize 'rtev' with libevent. - * - * @param rt_delay_sec wait given time before we work after routing - * event. - * @param send_delay_millisec wait given time each sending. - * @param send_npackets send give number of packets at once. - */ -int -rtev_libevent_init(int rt_delay_sec, int send_delay_millisec, int send_npackets, - int flags) -{ - RTEV_DBG((LOG_DEBUG, "%s(%d,%d,%d)", - __func__, rt_delay_sec, send_delay_millisec, send_npackets)); - return rtev_libevent_init0(&rtev_libevent_this, rt_delay_sec, - send_delay_millisec, send_npackets, flags); -} - -/*********************************************************************** - * private functions - ***********************************************************************/ -static void rtev_libevent_reset_event (rtev_libevent *); -static void rtev_libevent_timer_event (int, short, void *); -static void rtev_libevent_io_event (int, short, void *); - -struct _rtev_libevent { - rtev_impl impl; - int sock; - int nsend; - int p_delay; - int w_delay; /* milli sec */ - int write_ready:1, write_wait:1; - struct event ev, ev_timer; - time_t last_rtupdate; -}; - -static void -rtev_libevent_reset_event(rtev_libevent *_this) -{ - int evmask; - struct timeval tv; - - if (event_initialized(&_this->ev_timer)) - event_del(&_this->ev_timer); - if (event_initialized(&_this->ev)) - event_del(&_this->ev); - - /* - * I/O Event - */ - evmask = EV_READ; - if (_this->write_ready == 0 && rtev_has_write_pending()) - evmask |= EV_WRITE; - event_set(&_this->ev, _this->sock, evmask, rtev_libevent_io_event, - _this); - event_add(&_this->ev, NULL); - RTEV_DBG((DEBUG_LEVEL_2, "%s I/O [%s%s] Wait", __func__, - ((evmask & EV_READ) != 0)? "R" : "", - ((evmask & EV_WRITE) != 0)? "W" : "")); - - /* - * Timer event - */ - if (_this->write_wait != 0 && _this->w_delay > 0) { - RTEV_DBG((DEBUG_LEVEL_2, "%s Timer %f sec", __func__, - _this->w_delay / 1000.0)); - tv.tv_sec = _this->w_delay / 1000; - tv.tv_usec = (_this->w_delay % 1000) * 1000L; - RTEV_ASSERT(tv.tv_usec < 1000000L); - RTEV_ASSERT(tv.tv_usec >= 0L); - evtimer_set(&_this->ev_timer, rtev_libevent_timer_event, _this); - event_add(&_this->ev_timer, &tv); - } else if (_this->last_rtupdate != 0 && _this->p_delay > 0) { - time_t currtime; - - currtime = get_monosec(); - - tv.tv_sec = _this->last_rtupdate + _this->p_delay - currtime; - tv.tv_usec = 0; - if (tv.tv_sec < 0) - tv.tv_sec = 0; - RTEV_DBG((DEBUG_LEVEL_2, "%s Timer %ld sec", __func__, - (long)tv.tv_sec)); - - evtimer_set(&_this->ev_timer, rtev_libevent_timer_event, _this); - event_add(&_this->ev_timer, &tv); - } -} - -static void -rtev_libevent_timer_event(int sock, short evmask, void *ctx) -{ - time_t currtime; - rtev_libevent *_this; - - _this = ctx; - RTEV_DBG((DEBUG_LEVEL_2, "%s", __func__)); - - currtime = get_monosec(); - if (_this->last_rtupdate + _this->p_delay <= currtime) { - _this->impl.base_on_rtevent(&_this->impl); - _this->last_rtupdate = 0; - } - - if (_this->write_wait != 0) { - _this->write_wait = 0; - if (rtev_has_write_pending() && _this->write_ready != 0) { - RTEV_DBG((DEBUG_LEVEL_1, "rt_send() by timer")); - if (_this->impl.base_on_write(&_this->impl, - _this->sock, _this->nsend) != 0) { - log_printf(LOG_INFO, - "sending message to routing socket failed" - ": %m"); - } - _this->write_ready = 0; - _this->write_wait = 1; /* wait again */ - } - } - - rtev_libevent_reset_event(_this); -} - -static void -rtev_libevent_io_event(int sock, short evmask, void *ctx) -{ - char buf[8192]; - struct rt_msghdr *rt_msg; - int sz, rt_updated; - rtev_libevent *_this; - - _this = ctx; - - RTEV_DBG((DEBUG_LEVEL_2, "%s I/O [%s%s] Ready", __func__, - ((evmask & EV_READ) != 0)? "R" : "", - ((evmask & EV_WRITE) != 0)? "W" : "")); - - if ((evmask & EV_WRITE) != 0) { - if (_this->write_wait != 0 && _this->w_delay > 0) { - _this->write_ready = 1; - } else { - RTEV_DBG((DEBUG_LEVEL_1, "rt_send() by event")); - if (_this->impl.base_on_write(&_this->impl, sock, - _this->nsend) != 0) { - log_printf(LOG_INFO, - "sending message to routing socket failed" - ": %m"); - } - _this->write_ready = 0; - _this->write_wait = 1; - } - } - if ((evmask & EV_READ) != 0) { - rt_updated = 0; - while ((sz = recv(sock, buf, sizeof(buf), 0)) > 0) { - rt_msg = (struct rt_msghdr *)buf; - if (rt_msg->rtm_version != RTM_VERSION) - continue; - switch (rt_msg->rtm_type) { - case RTM_ADD: - case RTM_CHANGE: - case RTM_NEWADDR: - case RTM_DELADDR: - case RTM_DELETE: - case RTM_IFINFO: - rt_updated++; - break; - } - } - if (sz < 0) - RTEV_ASSERT(errno == EAGAIN); - if (rt_updated) { - if (_this->p_delay <= 0) - rtev_libevent_timer_event(sock, evmask, ctx); - else if (_this->last_rtupdate == 0) - _this->last_rtupdate = get_monosec(); - } - } - rtev_libevent_reset_event(_this); -} - -static void -rtev_libevent_on_write(rtev_impl *impl) -{ - rtev_libevent *_this = impl->impl; - - rtev_libevent_reset_event(_this); -} - -static int -rtev_libevent_init0(rtev_libevent *_this, int rt_delay, int send_delay, - int nsend, int flags) -{ - int sock, fflags, dummy; - - sock = -1; - memset(_this, 0, sizeof(rtev_libevent)); - - _this->impl.impl = _this; - _this->impl.impl_fini = rtev_libevent_fini; - _this->impl.impl_on_write = rtev_libevent_on_write; - - if (rtev_base_init(&_this->impl, flags) != 0) - goto fail; - - if ((sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) - goto fail; - - dummy = 0; - if ((fflags = fcntl(sock, F_GETFL, dummy)) < 0) - goto fail; - - if (fcntl(sock, F_SETFL, fflags | O_NONBLOCK) < 0) - goto fail; - - _this->sock = sock; - - _this->w_delay = send_delay; - _this->nsend = nsend; - _this->p_delay = rt_delay; - if (rt_delay <= 0) - rtev_libevent_timer_event(0, EV_TIMEOUT, _this); - rtev_libevent_reset_event(_this); - - return 0; -fail: - if (sock >= 0) - close(sock); - - if (event_initialized(&_this->ev)) - event_del(&_this->ev); - - _this->impl.impl = NULL; - - return -1; -} - -static void -rtev_libevent_fini(rtev_impl *impl) -{ - rtev_libevent *_this = impl->impl; - - if (_this->sock >= 0) - close(_this->sock); - - if (event_initialized(&_this->ev)) - event_del(&_this->ev); - if (event_initialized(&_this->ev_timer)) - event_del(&_this->ev_timer); - _this->sock = -1; -} - -static inline time_t -get_monosec(void) -{ - struct timespec ts; - - if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) - abort(); - return ts.tv_sec; -} diff --git a/usr.sbin/npppd/common/rtev_local.h b/usr.sbin/npppd/common/rtev_local.h deleted file mode 100644 index dc437933b31..00000000000 --- a/usr.sbin/npppd/common/rtev_local.h +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * Copyright (c) 2009 Internet Initiative Japan Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. - */ -#ifndef RTEV_LOCAL_H -#define RTEV_LOCAL_H - -#ifdef RTEV_DEBUG -#include "debugutil.h" -#define RTEV_DBG(x) log_printf x -#define RTEV_ASSERT(cond) \ - if (!(cond)) { \ - fprintf(stderr, \ - "\nASSERT(" #cond ") failed on %s() at %s:%d.\n"\ - , __func__, __FILE__, __LINE__); \ - abort(); \ - } -#else -#define RTEV_ASSERT(cond) -#define RTEV_DBG(x) -#define log_printf syslog -#endif - -typedef struct _rtev_impl rtev_impl; -struct _rtev_impl { - void *impl; - void (*impl_fini)(rtev_impl *); - void (*impl_on_write)(rtev_impl *); - int (*base_on_rtevent)(rtev_impl *); - int (*base_on_write)(rtev_impl *, int, int); - int base_flags; -}; - -int rtev_base_init (rtev_impl *, int); - -#endif |