forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.c
137 lines (113 loc) · 2.93 KB
/
addr.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2016 Red Hat, Inc. -- Jiri Benc <[email protected]>
* Ondrej Hlavaty <[email protected]>
*
* 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 "addr.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include "netlink.h"
int addr_init(struct addr *dest, int family, int prefixlen, const void *raw)
{
char buf[64];
unsigned int len = family == AF_INET ? 4 : 16;
dest->family = family;
dest->prefixlen = prefixlen;
dest->raw = malloc(len);
if (!dest->raw)
goto err;
memcpy(dest->raw, raw, len);
if (!inet_ntop(family, raw, buf, sizeof(buf)))
return errno;
len = strlen(buf);
if (prefixlen >= 0)
snprintf(buf + len, sizeof(buf) - len, "/%d", prefixlen);
dest->formatted = strdup(buf);
if (!dest->formatted)
goto err_raw;
return 0;
err_raw:
free(dest->raw);
err:
return ENOMEM;
}
int addr_init_netlink(struct addr *dest, const struct ifaddrmsg *ifa,
const struct nlattr *nla)
{
return addr_init(dest, ifa->ifa_family, ifa->ifa_prefixlen, nla_read(nla));
}
int addr_parse_raw(void *dest, const char *src)
{
int af;
af = strchr(src, ':') ? AF_INET6 : AF_INET;
if (inet_pton(af, src, dest) <= 0)
return -1;
return af;
}
int addr_is_zero(struct addr *addr)
{
static const char zero [16];
unsigned int len = addr->family == AF_INET ? 4 : 16;
return !memcmp(zero, addr->raw, len);
}
void addr_destruct(struct addr *addr)
{
if (addr->raw) {
free(addr->raw);
free(addr->formatted);
addr->raw = addr->formatted = NULL;
}
}
int mac_addr_init(struct mac_addr *addr)
{
addr->len = 0;
addr->raw = NULL;
addr->formatted = NULL;
return 0;
}
int mac_addr_fill_netlink(struct mac_addr *addr, const struct nlattr *nla)
{
const unsigned char *data = nla_read(nla);
int len = nla_len(nla);
int i;
addr->len = len;
addr->raw = malloc(len);
if (!addr->raw)
return ENOMEM;
addr->formatted = malloc(len * 3);
if (!addr->raw)
goto err_raw;
memcpy(addr->raw, data, len);
for (i = 0; i < len; i++)
if (3 != snprintf(addr->formatted + i * 3, (i+1 == len) ? 3 : 4 , "%02x:", data[i]))
goto err_formatted;
return 0;
err_formatted:
free(addr->formatted);
addr->formatted = NULL;
err_raw:
free(addr->raw);
addr->raw = NULL;
addr->len = 0;
return errno;
}
void mac_addr_destruct(struct mac_addr *addr)
{
if (addr->raw)
free(addr->raw);
if (addr->formatted)
free(addr->formatted);
}