-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.c
82 lines (68 loc) · 1.91 KB
/
utils.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2014 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 "utils.h"
#include <limits.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include "if.h"
#include "netns.h"
#include "route.h"
char *ifstr(struct if_entry *entry)
{
static char buf[IFNAMSIZ + NAME_MAX + 2];
if (!entry->ns->name)
/* root ns */
snprintf(buf, sizeof(buf), "/%s", entry->if_name);
else
snprintf(buf, sizeof(buf), "%s/%s", entry->ns->name, entry->if_name);
return buf;
}
#define INTERNAL_NS_MAX (NAME_MAX)
#define NETNS_MAX (NAME_MAX + 1)
#define IFID_MAX (INTERNAL_NS_MAX + NETNS_MAX + IFNAMSIZ + 1)
char *ifid(struct if_entry *entry)
{
static char buf[IFID_MAX + 1];
char *ins, *ns;
ins = entry->internal_ns ? : "";
ns = nsid(entry->ns);
snprintf(buf, sizeof(buf), "%s%s/%s", ns, ins, entry->if_name);
return buf;
}
char *ifdrv(struct if_entry *entry)
{
static char buf[256];
if (!entry->driver)
return "";
if (!entry->sub_driver)
return entry->driver;
snprintf(buf, sizeof(buf), "%s/%s", entry->driver, entry->sub_driver);
return buf;
}
char *nsid(struct netns_entry *entry)
{
static char buf[NETNS_MAX + 1];
if (!entry->name)
return "/";
snprintf(buf, sizeof(buf), "%s/", entry->name);
return buf;
}
char *rtid(struct rtable *rt)
{
static char buf [32];
snprintf(buf, sizeof(buf), "%u", rt->id);
return buf;
}