<commit_before>/**
 * @file   routingtable.c
 * @brief  routingtable probe
 * @author "<NAME> <<EMAIL>>"
 *
 */

/*
 * Copyright 2009-2011 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include "probe-api.h"
#include "probe/entcmp.h"
#include "alloc.h"
#include "util.h"

#if (HAVE_LIBNL)
#include <netinet/in.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/list.h>
#include <netlink/cache.h>
#include <netlink/route/nexthop.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <netlink/route/link.h>
#include <net/route.h>

typedef struct {
	probe_ctx *ctx;
	SEXP_t    *in_dst;
        struct nl_cache *links;
} rt_cb_helper;

static void collect_item(struct nl_object *object, void *arg)
{
	rt_cb_helper *h = (rt_cb_helper *)arg;
	struct rtnl_route *route = nl_object_priv(object);

	SEXP_t *item, *out_dst;
	char   *flag_array[32];
	size_t  flag_count = 0;

	struct nl_addr *dst, *gw;

	uint32_t flags;
	char dst_str[INET6_ADDRSTRLEN + 1], *gw_str, gw_strmem[INET6_ADDRSTRLEN + 1];
        char if_name[128];
        int  if_index;

	dI("processing object: %p\n", object);

	/* get destination addr */
	dst = rtnl_route_get_dst(route);

	if (dst != NULL)
		nl_addr2str(dst, dst_str, sizeof dst_str);
	else {
		dE("rtnl_route_get_dst(%p) returned NULL!\n", route);
		return;
	}

	out_dst = SEXP_string_new(dst_str, strlen(dst_str));

	/* get gateway addr */
	gw = rtnl_route_get_gateway(route);

	if (gw != NULL)
		gw_str = nl_addr2str(gw, gw_strmem, sizeof gw_strmem);
	else
		gw_str = NULL;

	dI("dst: %s\n"
	   " gw: %s\n"
	   "  a: %p\n"
	   "  b: %p\n", dst_str, gw_str, h->in_dst, out_dst);

	if (probe_entobj_cmp(h->in_dst, out_dst) != OVAL_RESULT_TRUE) {
		SEXP_free(out_dst);
		return;
	}

        dI("match\n");

	/* construct the flag array */
	flags = rtnl_route_get_flags(route);

#define RTF_COND_ADD(flg, str) if (flags & (flg)) flag_array[flag_count++] = str

	RTF_COND_ADD(RTF_UP,        "UP");
	RTF_COND_ADD(RTF_REINSTATE, "REINSTATE");
	RTF_COND_ADD(RTF_MTU,       "MTU");
	RTF_COND_ADD(RTF_WINDOW,    "WINDOW");
	RTF_COND_ADD(RTF_REJECT,    "REJECT");
	RTF_COND_ADD(RTF_STATIC,    "STATIC");
	RTF_COND_ADD(RTF_ALLONLINK, "ALLONLINK");
	RTF_COND_ADD(RTF_ADDRCONF,  "ADDRCONF");
	RTF_COND_ADD(RTF_NONEXTHOP, "NONEXTHOP");
	RTF_COND_ADD(RTF_FLOW,      "FLOW");
	RTF_COND_ADD(RTF_POLICY,    "POLICY");

	flag_array[flag_count] = NULL;

        dI("flags      = %x\n", flags);
        dI("flag_count = %zu\n", flag_count);
        dI("flag_array[0] = %s\n", flag_array[0]);

        if_index = rtnl_route_get_oif(route);
        rtnl_link_i2name(h->links, if_index, if_name, sizeof if_name);

	/* create the item */
	item = probe_item_create(OVAL_UNIX_ROUTINGTABLE, NULL,
				 "destination",    OVAL_DATATYPE_IPV4ADDR, dst_str,
				 "gateway",        OVAL_DATATYPE_IPV4ADDR, gw_str,
				 "flags",          OVAL_DATATYPE_STRING_M, flag_array,
				 "interface_name", OVAL_DATATYPE_STRING,   if_name,
				 NULL);

	probe_item_collect(h->ctx, item);
	SEXP_free(out_dst);
}
#endif /* HAVE_LIBNL */

int probe_main (probe_ctx *ctx, void *arg)
{
        SEXP_t *probe_in, *dst_ent;
	struct nl_handle *sk;
	struct nl_cache  *routes, *links;
	rt_cb_helper h;
<commit_msg>[probes] routingtable: added missing #ifdefs<commit_after>/**
 * @file   routingtable.c
 * @brief  routingtable probe
 * @author "<NAME> <<EMAIL>>"
 *
 */

/*
 * Copyright 2009-2011 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include "probe-api.h"
#include "probe/entcmp.h"
#include "alloc.h"
#include "util.h"

#if defined(HAVE_LIBNL)
#include <netinet/in.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/list.h>
#include <netlink/cache.h>
#include <netlink/route/nexthop.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <netlink/route/link.h>
#include <net/route.h>

typedef struct {
	probe_ctx *ctx;
	SEXP_t    *in_dst;
        struct nl_cache *links;
} rt_cb_helper;

static void collect_item(struct nl_object *object, void *arg)
{
	rt_cb_helper *h = (rt_cb_helper *)arg;
	struct rtnl_route *route = nl_object_priv(object);

	SEXP_t *item, *out_dst;
	char   *flag_array[32];
	size_t  flag_count = 0;

	struct nl_addr *dst, *gw;

	uint32_t flags;
	char dst_str[INET6_ADDRSTRLEN + 1], *gw_str, gw_strmem[INET6_ADDRSTRLEN + 1];
        char if_name[128];
        int  if_index;

	dI("processing object: %p\n", object);

	/* get destination addr */
	dst = rtnl_route_get_dst(route);

	if (dst != NULL)
		nl_addr2str(dst, dst_str, sizeof dst_str);
	else {
		dE("rtnl_route_get_dst(%p) returned NULL!\n", route);
		return;
	}

	out_dst = SEXP_string_new(dst_str, strlen(dst_str));

	/* get gateway addr */
	gw = rtnl_route_get_gateway(route);

	if (gw != NULL)
		gw_str = nl_addr2str(gw, gw_strmem, sizeof gw_strmem);
	else
		gw_str = NULL;

	dI("dst: %s\n"
	   " gw: %s\n"
	   "  a: %p\n"
	   "  b: %p\n", dst_str, gw_str, h->in_dst, out_dst);

	if (probe_entobj_cmp(h->in_dst, out_dst) != OVAL_RESULT_TRUE) {
		SEXP_free(out_dst);
		return;
	}

        dI("match\n");

	/* construct the flag array */
	flags = rtnl_route_get_flags(route);

#define RTF_COND_ADD(flg, str) if (flags & (flg)) flag_array[flag_count++] = str

	RTF_COND_ADD(RTF_UP,        "UP");
	RTF_COND_ADD(RTF_REINSTATE, "REINSTATE");
	RTF_COND_ADD(RTF_MTU,       "MTU");
	RTF_COND_ADD(RTF_WINDOW,    "WINDOW");
	RTF_COND_ADD(RTF_REJECT,    "REJECT");
	RTF_COND_ADD(RTF_STATIC,    "STATIC");
	RTF_COND_ADD(RTF_ALLONLINK, "ALLONLINK");
	RTF_COND_ADD(RTF_ADDRCONF,  "ADDRCONF");
	RTF_COND_ADD(RTF_NONEXTHOP, "NONEXTHOP");
	RTF_COND_ADD(RTF_FLOW,      "FLOW");
	RTF_COND_ADD(RTF_POLICY,    "POLICY");

	flag_array[flag_count] = NULL;

        dI("flags      = %x\n", flags);
        dI("flag_count = %zu\n", flag_count);
        dI("flag_array[0] = %s\n", flag_array[0]);

        if_index = rtnl_route_get_oif(route);
        rtnl_link_i2name(h->links, if_index, if_name, sizeof if_name);

	/* create the item */
	item = probe_item_create(OVAL_UNIX_ROUTINGTABLE, NULL,
				 "destination",    OVAL_DATATYPE_IPV4ADDR, dst_str,
				 "gateway",        OVAL_DATATYPE_IPV4ADDR, gw_str,
				 "flags",          OVAL_DATATYPE_STRING_M, flag_array,
				 "interface_name", OVAL_DATATYPE_STRING,   if_name,
				 NULL);

	probe_item_collect(h->ctx, item);
	SEXP_free(out_dst);
}
#endif /* HAVE_LIBNL */

int probe_main (probe_ctx *ctx, void *arg)
{
        SEXP_t *probe_in, *dst_ent;
#if defined(HAVE_LIBNL)
	struct nl_handle *sk;
	struct nl_cache  *routes, *links;
	rt_cb_helper h;
#endif /* HAVE_LIBNL */
{'fraction_non_alphanumeric': 0.13276535321263755, 'fraction_numerical': 0.010176310495799314, 'mean_word_length': 3.977620730270907, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4134187', 'n_tokens_mistral': 3503, 'n_tokens_neox': 3353, 'n_words': 786}