From d28e2bf3222ef4c58bebebd9c118818bfccedb8c Mon Sep 17 00:00:00 2001 From: laforge Date: Fri, 4 Nov 2005 13:50:25 +0000 Subject: implement ifindex to interface name filter plugin --- filter/ulogd_filter_IFINDEX.c | 82 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) (limited to 'filter/ulogd_filter_IFINDEX.c') diff --git a/filter/ulogd_filter_IFINDEX.c b/filter/ulogd_filter_IFINDEX.c index 7fc39de..fbe8ccf 100644 --- a/filter/ulogd_filter_IFINDEX.c +++ b/filter/ulogd_filter_IFINDEX.c @@ -1,7 +1,33 @@ +/* ulogd_filter_IFINDEX.c, Version $Revision: 1500 $ + * + * ulogd interpreter plugin for ifindex to ifname conversion + * + * (C) 2005 by Harald Welte + * + * 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 + * + * $Id: ulogd_filter_IFINDEX.c 1500 2005-10-03 16:54:02Z laforge $ + * + */ + #include #include #include +#include "rtnl.h" +#include "iftable.h" + static struct ulogd_key ifindex_keys[] = { { .type = ULOGD_RET_STRING, @@ -29,23 +55,75 @@ static struct ulogd_key ifindex_inp[] = { static int interp_ifindex(struct ulogd_pluginstance *pi) { struct ulogd_key *ret = pi->output; + struct ulogd_key *inp = pi->input; - ret[0].u.value.ptr = "eth_in_FIXME"; + ret[0].u.value.ptr = ifindex_2name(inp[0].u.source->u.value.ui32); ret[0].flags |= ULOGD_RETF_VALID; - ret[1].u.value.ptr = "eth_out_FIXME"; + ret[1].u.value.ptr = ifindex_2name(inp[1].u.source->u.value.ui32); ret[1].flags |= ULOGD_RETF_VALID; return 0; } +/* we only need one global static cache of ifindex to ifname mappings, + * so all state is global (as opposed to per-instance local state in almost + * all other plugins */ +static struct ulogd_fd rtnl_fd = { .fd = -1 }; +static int rtnl_users; + +static int rtnl_read_cb(int fd, unsigned int what, void *param) +{ + if (!(what & ULOGD_FD_READ)) + return 0; + + rtnl_receive(); +} static int ifindex_start(struct ulogd_pluginstance *upi) { + int rc; + + /* if we're already initialized, inc usage count and exit */ + if (rtnl_fd.fd >= 0) { + rtnl_users++; + return 0; + } + + /* if we reach here, we need to initialize */ + rtnl_fd.fd = rtnl_init(); + if (rtnl_fd.fd < 0) + return rtnl_fd.fd; + + rc = iftable_init(); + if (rc < 0) + goto out_rtnl; + + rtnl_fd.when = ULOGD_FD_READ; + rtnl_fd.cb = &rtnl_read_cb; + rc = ulogd_register_fd(&rtnl_fd); + if (rc < 0) + goto out_iftable; + + rtnl_users++; return 0; + +out_iftable: + iftable_fini(); +out_rtnl: + rtnl_fini(); + rtnl_fd.fd = -1; + return rc; } static int ifindex_fini(struct ulogd_pluginstance *upi) { + if (--rtnl_users == 0) { + ulogd_unregister_fd(&rtnl_fd); + iftable_fini(); + rtnl_fini(); + rtnl_fd.fd = -1; + } + return 0; } -- cgit v1.2.3