From 131920eca1d2780da1275b50ab89024f3e01928b Mon Sep 17 00:00:00 2001 From: laforge Date: Fri, 23 Jul 2004 04:11:27 +0000 Subject: move libulog specific code to ulogd_inppkt_ULOG.c move ulogd_LOCAL.c to ulogd_raw2packet_LOCAL.c --- extensions/ulogd_LOCAL.c | 100 ---------------------- filter/raw2packet/ulogd_raw2packet_LOCAL.c | 100 ++++++++++++++++++++++ input/packet/ulogd_inppkt_ULOG.c | 128 +++++++++++++++++++++++++++++ ulogd.c | 54 ------------ 4 files changed, 228 insertions(+), 154 deletions(-) delete mode 100644 extensions/ulogd_LOCAL.c create mode 100644 filter/raw2packet/ulogd_raw2packet_LOCAL.c create mode 100644 input/packet/ulogd_inppkt_ULOG.c diff --git a/extensions/ulogd_LOCAL.c b/extensions/ulogd_LOCAL.c deleted file mode 100644 index 41ebe5b..0000000 --- a/extensions/ulogd_LOCAL.c +++ /dev/null @@ -1,100 +0,0 @@ -/* ulogd_LOCAL.c, Version 0.3 - * - * ulogd interpreter plugin for: - local time of packet - * - hostname of localhost - * - * (C) 2001-2002 by Florent AIDE - * with the help of Moez MKADMI - * shamelessly ripped from Harald Welte - * - * 2002 extended by Martin Kaehmer - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation - * - * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * - */ - -#include -#include -#include -#include -#include - -#ifdef DEBUG_LOCAL -#define DEBUGP(x) ulogd_log(ULOGD_DEBUG, x) -#else -#define DEBUGP(format, args...) -#endif - - -static char hostname[255]; - - -static ulog_iret_t *_interp_local(ulog_interpreter_t *ip, - ulog_packet_msg_t *pkt) -{ - struct timeval tv; - ulog_iret_t *ret = ip->result; - - /* Get date */ - gettimeofday(&tv, NULL); - - /* put date */ - ret[0].value.ui32 = (unsigned long) tv.tv_sec; - ret[0].flags |= ULOGD_RETF_VALID; - - ret[1].value.ptr = hostname; - ret[1].flags |= ULOGD_RETF_VALID; - - return ret; -} - -static ulog_iret_t local_rets[] = { - { NULL, NULL, 0, ULOGD_RET_UINT32, ULOGD_RETF_NONE, "local.time", - { ui32: 0 } }, - { NULL, NULL, 0, ULOGD_RET_STRING, ULOGD_RETF_NONE, "local.hostname", - { ptr: NULL } }, -}; - -static ulog_interpreter_t local_ip[] = { - - { NULL, "local", 0, &_interp_local, 2, local_rets }, - { NULL, "", 0, NULL, 0, NULL }, -}; - -void _local_reg_ip(void) -{ - ulog_interpreter_t *ip = local_ip; - ulog_interpreter_t *p; - - for (p = ip; p->interp; p++) - register_interpreter(p); - -} - -void _init(void) -{ - /* get hostname */ - char *tmp; - if (gethostname(hostname, sizeof(hostname)) < 0) { - ulogd_log(ULOGD_FATAL, "can't gethostname(): %s\n", - strerror(errno)); - exit(2); - } - /* strip off everything after first '.' */ - if ((tmp = strchr(hostname, '.'))) - *tmp = '\0'; - - _local_reg_ip(); -} diff --git a/filter/raw2packet/ulogd_raw2packet_LOCAL.c b/filter/raw2packet/ulogd_raw2packet_LOCAL.c new file mode 100644 index 0000000..41ebe5b --- /dev/null +++ b/filter/raw2packet/ulogd_raw2packet_LOCAL.c @@ -0,0 +1,100 @@ +/* ulogd_LOCAL.c, Version 0.3 + * + * ulogd interpreter plugin for: - local time of packet + * - hostname of localhost + * + * (C) 2001-2002 by Florent AIDE + * with the help of Moez MKADMI + * shamelessly ripped from Harald Welte + * + * 2002 extended by Martin Kaehmer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * + */ + +#include +#include +#include +#include +#include + +#ifdef DEBUG_LOCAL +#define DEBUGP(x) ulogd_log(ULOGD_DEBUG, x) +#else +#define DEBUGP(format, args...) +#endif + + +static char hostname[255]; + + +static ulog_iret_t *_interp_local(ulog_interpreter_t *ip, + ulog_packet_msg_t *pkt) +{ + struct timeval tv; + ulog_iret_t *ret = ip->result; + + /* Get date */ + gettimeofday(&tv, NULL); + + /* put date */ + ret[0].value.ui32 = (unsigned long) tv.tv_sec; + ret[0].flags |= ULOGD_RETF_VALID; + + ret[1].value.ptr = hostname; + ret[1].flags |= ULOGD_RETF_VALID; + + return ret; +} + +static ulog_iret_t local_rets[] = { + { NULL, NULL, 0, ULOGD_RET_UINT32, ULOGD_RETF_NONE, "local.time", + { ui32: 0 } }, + { NULL, NULL, 0, ULOGD_RET_STRING, ULOGD_RETF_NONE, "local.hostname", + { ptr: NULL } }, +}; + +static ulog_interpreter_t local_ip[] = { + + { NULL, "local", 0, &_interp_local, 2, local_rets }, + { NULL, "", 0, NULL, 0, NULL }, +}; + +void _local_reg_ip(void) +{ + ulog_interpreter_t *ip = local_ip; + ulog_interpreter_t *p; + + for (p = ip; p->interp; p++) + register_interpreter(p); + +} + +void _init(void) +{ + /* get hostname */ + char *tmp; + if (gethostname(hostname, sizeof(hostname)) < 0) { + ulogd_log(ULOGD_FATAL, "can't gethostname(): %s\n", + strerror(errno)); + exit(2); + } + /* strip off everything after first '.' */ + if ((tmp = strchr(hostname, '.'))) + *tmp = '\0'; + + _local_reg_ip(); +} diff --git a/input/packet/ulogd_inppkt_ULOG.c b/input/packet/ulogd_inppkt_ULOG.c new file mode 100644 index 0000000..5cc3a58 --- /dev/null +++ b/input/packet/ulogd_inppkt_ULOG.c @@ -0,0 +1,128 @@ +/* ulogd_inppkt_ULOG.c - stackable input plugin for ULOG packets -> ulogd2 + * (C) 2004 by Harald Welte + */ + +#include +#include + +#include + +static config_entry_t bufsiz_ce = { NULL, "bufsize", CONFIG_TYPE_INT, + CONFIG_OPT_NONE, 0, + { value: ULOGD_BUFSIZE_DEFAULT } }; + +static config_entry_t nlgroup_ce = { &bufsiz_ce, "nlgroup", CONFIG_TYPE_INT, + CONFIG_OPT_NONE, 0, + { value: ULOGD_NLGROUP_DEFAULT } }; + +static config_entry_t rmem_ce = { &nlgroup_ce, "rmem", CONFIG_TYPE_INT, + CONFIG_OPT_NONE, 0, + { value: ULOGD_RMEM_DEFAULT } }; + +struct ulog_input { + struct ipulog_handle *libulog_h; + static unsigned char *libulog_buf; + static struct ulogd_fd ulog_fd; +}; + +/* call all registered interpreters and hand the results over to + * propagate_results */ +static void handle_packet(ulog_packet_msg_t *pkt) +{ + ulog_iret_t *ret; + ulog_iret_t *allret = NULL; + ulog_interpreter_t *ip; + + unsigned int i,j; + + /* If there are no interpreters registered yet, + * ignore this packet */ + if (!ulogd_interh_ids) { + ulogd_log(ULOGD_NOTICE, + "packet received, but no interpreters found\n"); + return; + } + + for (i = 1; i <= ulogd_interh_ids; i++) { + ip = ulogd_interh[i]; + /* call interpreter */ + if ((ret = ((ip)->interp)(ip, pkt))) { + /* create references for result linked-list */ + for (j = 0; j < ip->key_num; j++) { + if (IS_VALID(ip->result[j])) { + ip->result[j].cur_next = allret; + allret = &ip->result[j]; + } + } + } + } + propagate_results(allret); + clean_results(ulogd_interpreters->result); +} + +static struct ulog_read_cb(int fd, void *param) +{ + struct ulog_input *u = (struct ulog_input *)param; + int len; + + while (len = ipulog_read(u->libulog_h, u->libulog_buf, + bufsiz_ce.u.value, 1)) { + if (len <= 0) { + /* this is not supposed to happen */ + ulogd_log(ULOGD_ERROR, "ipulog_read = %d! " + "ipulog_errno = %d, errno = %d\n", + len, ipulog_errno, errno); + break; + } + while ((upkt = ipulog_get_paccket(u->libulog_h, + u->libulog_buf, len))) { + DEBUGP("==> ulog packet received\n"); + handle_packet(upkt); + } + } + return 0; +} + +static struct ulog_input *new_instance() +{ + struct ulog_input *ui = malloc(sizeof(*ui)); + if (!ui) + return NULL; + + ui->libulog_buf = malloc(bufsiz_ce.u.value); + if (!ui->libulog_buf) + return NULL; + + ui->libulog_h = ipulog_create_handle( + ipulog_group2gmask(nlgroup_ce.u.value), + rmem_ce.u.value); + if (!libulog_h) + return NULL; + + ui->ulog_fd.fd = ui->libulog_h->fd; + ui->ulog_fd.cb = &ulog_read_cb; + ui->ulog_fd.data = ui; + + return ui; +} + +static struct ulogd_pluginstance *init() +{ + struct ulog_input *ui = new_instance(); + + ulogd_register_fd(&ui->ulog_fd); +} + +struct ulogd_plugin libulog_plugin = { + .name = "ULOG", + .input_type = ULOGD_DTYPE_NULL, + .output_type = ULOGD_DTYPE_RAW, + .constructor = &init, + .input_fn = &input, + .configs = &rmem_ce, +}; + +void _init(void) +{ + ulogd_register_plugin(&libulog_plugin); +} diff --git a/ulogd.c b/ulogd.c index 6dc37cd..3abf251 100644 --- a/ulogd.c +++ b/ulogd.c @@ -428,41 +428,6 @@ static void clean_results(ulog_iret_t *ret) } } -/* call all registered interpreters and hand the results over to - * propagate_results */ -static void handle_packet(ulog_packet_msg_t *pkt) -{ - ulog_iret_t *ret; - ulog_iret_t *allret = NULL; - ulog_interpreter_t *ip; - - unsigned int i,j; - - /* If there are no interpreters registered yet, - * ignore this packet */ - if (!ulogd_interh_ids) { - ulogd_log(ULOGD_NOTICE, - "packet received, but no interpreters found\n"); - return; - } - - for (i = 1; i <= ulogd_interh_ids; i++) { - ip = ulogd_interh[i]; - /* call interpreter */ - if ((ret = ((ip)->interp)(ip, pkt))) { - /* create references for result linked-list */ - for (j = 0; j < ip->key_num; j++) { - if (IS_VALID(ip->result[j])) { - ip->result[j].cur_next = allret; - allret = &ip->result[j]; - } - } - } - } - propagate_results(allret); - clean_results(ulogd_interpreters->result); -} - /* plugin loader to dlopen() a plugins */ static int load_plugin(char *file) { @@ -766,25 +731,6 @@ int main(int argc, char* argv[]) ulogd_log(ULOGD_NOTICE, "initialization finished, entering main loop\n"); - /* endless loop receiving packets and handling them over to - * handle_packet */ - while ((len = ipulog_read(libulog_h, libulog_buf, - bufsiz_ce.u.value, 1))) { - - if (len <= 0) { - /* this is not supposed to happen */ - ulogd_log(ULOGD_ERROR, "ipulog_read == %d! " - "ipulog_errno == %d, errno = %d\n", - len, ipulog_errno, errno); - } else { - while ((upkt = ipulog_get_packet(libulog_h, - libulog_buf, len))) { - DEBUGP("==> packet received\n"); - handle_packet(upkt); - } - } - } - /* hackish, but result is the same */ sigterm_handler(SIGTERM); return(0); -- cgit v1.2.3