1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filter-netmask.h"
#include "gsocket.h"
#include "logmsg/logmsg.h"
#include <stdlib.h>
#include <string.h>
typedef struct _FilterNetmask
{
FilterExprNode super;
struct in_addr address;
struct in_addr netmask;
} FilterNetmask;
static gboolean
filter_netmask_eval(FilterExprNode *s, LogMessage **msgs, gint num_msg, LogTemplateEvalOptions *options)
{
FilterNetmask *self = (FilterNetmask *) s;
struct in_addr *addr, addr_storage;
LogMessage *msg = msgs[num_msg - 1];
gboolean res;
if (msg->saddr && g_sockaddr_inet_check(msg->saddr))
{
addr = &((struct sockaddr_in *) &msg->saddr->sa)->sin_addr;
}
else if (!msg->saddr || msg->saddr->sa.sa_family == AF_UNIX)
{
addr_storage.s_addr = htonl(INADDR_LOOPBACK);
addr = &addr_storage;
}
else
{
addr = NULL;
}
if (addr)
res = ((addr->s_addr & self->netmask.s_addr) == (self->address.s_addr));
else
res = FALSE;
msg_trace("netmask() evaluation started",
evt_tag_inaddr("msg_address", addr),
evt_tag_inaddr("address", &self->address),
evt_tag_inaddr("netmask", &self->netmask),
evt_tag_msg_reference(msg));
return res ^ s->comp;
}
FilterExprNode *
filter_netmask_new(const gchar *cidr)
{
FilterNetmask *self = g_new0(FilterNetmask, 1);
gchar buf[32];
gchar *slash;
filter_expr_node_init_instance(&self->super);
slash = strchr(cidr, '/');
if (strlen(cidr) >= sizeof(buf) || !slash)
{
g_inet_aton(cidr, &self->address);
self->netmask.s_addr = htonl(0xFFFFFFFF);
}
else
{
strncpy(buf, cidr, slash - cidr);
buf[slash - cidr] = 0;
g_inet_aton(buf, &self->address);
if (strchr(slash + 1, '.'))
{
g_inet_aton(slash + 1, &self->netmask);
}
else
{
gint prefix = strtol(slash + 1, NULL, 10);
if (prefix == 32)
self->netmask.s_addr = htonl(0xFFFFFFFF);
else
self->netmask.s_addr = htonl(((1 << prefix) - 1) << (32 - prefix));
}
}
self->address.s_addr &= self->netmask.s_addr;
self->super.eval = filter_netmask_eval;
return &self->super;
}
|