forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.cpp
70 lines (58 loc) · 1.67 KB
/
net.cpp
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
/*
* Copyright (c) 2021, Gunnar Beutner <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
const in6_addr in6addr_any = IN6ADDR_ANY_INIT;
const in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/if_nametoindex.html
unsigned int if_nametoindex([[maybe_unused]] char const* ifname)
{
int dummy_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (dummy_socket < 0) {
errno = -dummy_socket;
return 0;
}
struct ifreq ifr;
memcpy(ifr.ifr_name, ifname, IF_NAMESIZE);
int rc = ioctl(dummy_socket, SIOCGIFINDEX, &ifr);
if (rc) {
errno = -rc;
return 0;
}
return ifr.ifr_index;
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/if_indextoname.html#tag_16_236
char* if_indextoname([[maybe_unused]] unsigned int ifindex, [[maybe_unused]] char* ifname)
{
int dummy_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (dummy_socket < 0) {
errno = -dummy_socket;
return 0;
}
struct ifreq ifr;
ifr.ifr_index = ifindex;
int rc = ioctl(dummy_socket, SIOCGIFNAME, &ifr);
if (rc) {
errno = -rc;
return nullptr;
}
memcpy(ifname, ifr.ifr_name, IF_NAMESIZE);
return ifname;
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/if_nameindex.html
struct if_nameindex* if_nameindex()
{
errno = ENOSYS;
return nullptr;
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/if_freenameindex.html
void if_freenameindex(struct if_nameindex*)
{
}