File: match.c

package info (click to toggle)
plotnetcfg 0.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 336 kB
  • sloc: ansic: 2,952; makefile: 56
file content (92 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download | duplicates (3)
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
/*
 * This file is a part of plotnetcfg, a tool to visualize network config.
 * Copyright (C) 2014-2015 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 */

#include <stdlib.h>
#include "netns.h"
#include "if.h"
#include "match.h"

int match_if_heur(struct if_entry **found,
		  struct netns_entry *root, int all_ns,
		  struct if_entry *self,
		  int (*callback)(struct if_entry *, void *),
		  void *arg)
{
	struct netns_entry *ns;
	struct if_entry *entry;
	int prio = 0, count = 0, res;

	for (ns = root; ns; ns = ns->next) {
		for (entry = ns->ifaces; entry; entry = entry->next) {
			if (entry == self)
				continue;
			res = callback(entry, arg);
			if (res < 0)
				return -res;
			if (res > prio) {
				*found = entry;
				prio = res;
				count = 1;
			} else if (res == prio)
				count++;
		}
		if (!all_ns)
			break;
	}
	if (!prio) {
		*found = NULL;
		return 0;
	}
	if (count > 1)
		return -1;
	return 0;
}

struct if_entry *match_if_netnsid(unsigned int ifindex, int netnsid,
				  struct netns_entry *current)
{
	struct netns_id *ptr;
	struct if_entry *entry;

	for (ptr = current->ids; ptr; ptr = ptr->next) {
		if (ptr->id == netnsid) {
			for (entry = ptr->ns->ifaces; entry; entry = entry->next) {
				if (entry->if_index == ifindex)
					return entry;
			}
			break;
		}
	}
	return NULL;
}

void match_all_netnsid(struct netns_entry *root)
{
	struct netns_entry *ns;
	struct if_entry *entry;

	for (ns = root; ns; ns = ns->next) {
		for (entry = ns->ifaces; entry; entry = entry->next) {
			if (entry->link_netnsid >= 0)
				entry->link = match_if_netnsid(entry->link_index,
							       entry->link_netnsid,
							       ns);
			if (entry->peer_netnsid >= 0)
				entry->peer = match_if_netnsid(entry->peer_index,
							       entry->peer_netnsid,
							       ns);
		}
	}
}