-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlsocket.h
72 lines (62 loc) · 1.86 KB
/
nlsocket.h
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
// Copyright 2014-2024 Nicholas J. Kain <njkain at gmail dot com>
// SPDX-License-Identifier: MIT
#ifndef NDHS_NLSOCKET_H_
#define NDHS_NLSOCKET_H_
#include <stdint.h>
#include <ipaddr.h>
#include <net/if.h>
#include "nl.h"
#define MAX_NL_INTERFACES 50
struct netif_info
{
char name[IFNAMSIZ];
char macaddr[6];
char macbc[6];
struct in6_addr v4_address;
struct in6_addr v6_address_global;
struct in6_addr v6_address_link;
int index;
int link_type;
unsigned int flags;
unsigned int change_mask;
unsigned int mtu;
unsigned short device_type;
unsigned char v6_prefixlen_global;
unsigned char family;
bool is_active:1;
bool has_v4_address:1;
bool has_v6_address_global:1;
bool has_v6_address_link:1;
};
struct NLSocket
{
struct netif_info interfaces[MAX_NL_INTERFACES];
int query_ifindex;
int fd;
uint32_t nlseq;
bool got_newlink:1;
};
void NLSocket_init(struct NLSocket *self);
bool NLSocket_get_interface_addresses(struct NLSocket *self, int ifindex);
void NLSocket_process_input(struct NLSocket *self);
static inline int NLSocket_get_ifindex(const struct NLSocket *self, const char *name) {
for (int i = 0; i < MAX_NL_INTERFACES; ++i) {
if (!strcmp(name, self->interfaces[i].name)) return i;
}
return -1;
}
// The pointer that is returned is stable because the function is only
// called after NLSocket is constructed.
static inline struct netif_info *NLSocket_get_ifinfo(struct NLSocket *self, int ifindex)
{
if (ifindex < 0 || ifindex >= MAX_NL_INTERFACES) return NULL;
return &self->interfaces[ifindex];
}
static inline struct netif_info *NLSocket_get_ifinfo_by_name(struct NLSocket *self, const char *name)
{
for (size_t i = 0; i < MAX_NL_INTERFACES; ++i) {
if (!strcmp(name, self->interfaces[i].name)) return &self->interfaces[i];
}
return NULL;
}
#endif