Debian Codegraph @

test/mock-ifaddrs.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2023 Martin Belanger, Dell Technologies Inc.
5 */
6 #include <sys/types.h>
7 #include <ifaddrs.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include <netinet/in.h>
13 #include <net/if.h>
14 #include <arpa/inet.h>
15
16 struct ifaddrs_storage {
17 struct ifaddrs ifa;
18 union {
19 /* Reserve space for the biggest of the sockaddr types */
20 struct sockaddr_in s4;
21 struct sockaddr_in6 s6;
22 } addr, netmask, broadaddr;
23 char name[IF_NAMESIZE + 1];
24 };
25
26 static void init_entry(struct ifaddrs_storage *storage,
27 const char *ifname,
28 int family,
29 uint32_t addr1,
30 uint32_t addr2,
31 uint32_t addr3,
32 uint32_t addr4,
33 bool last)
34 {
35 struct ifaddrs *p;
36
37 p = &storage->ifa;
38 p->ifa_next = last ? NULL : &storage[1].ifa;
39 p->ifa_name = storage->name;
40 strcpy(p->ifa_name, ifname);
41 p->ifa_flags = 0;
42
43 if (family == AF_INET) {
44 struct sockaddr_in *ipv4;
45
46 ipv4 = &storage->addr.s4;
47 ipv4->sin_family = family;
48 ipv4->sin_port = 0;
49 ipv4->sin_addr.s_addr = htonl(addr1);
50 p->ifa_addr = (struct sockaddr *)ipv4;
51
52 ipv4 = &storage->netmask.s4;
53 ipv4->sin_family = family;
54 ipv4->sin_port = 0;
55 ipv4->sin_addr.s_addr = 0xffffff00;
56 p->ifa_netmask = (struct sockaddr *)ipv4;
57
58 ipv4 = &storage->broadaddr.s4;
59 ipv4->sin_family = family;
60 ipv4->sin_port = 0;
61 ipv4->sin_addr.s_addr = 0;
62 p->ifa_broadaddr = (struct sockaddr *)ipv4;;
63 } else {
64 struct sockaddr_in6 *ipv6;
65
66 ipv6 = &storage->addr.s6;
67 ipv6->sin6_family = family;
68 ipv6->sin6_port = 0;
69 ipv6->sin6_flowinfo = 0;
70 ipv6->sin6_addr.s6_addr32[0] = htonl(addr1);
71 ipv6->sin6_addr.s6_addr32[1] = htonl(addr2);
72 ipv6->sin6_addr.s6_addr32[2] = htonl(addr3);
73 ipv6->sin6_addr.s6_addr32[3] = htonl(addr4);
74 ipv6->sin6_scope_id = 0;
75 p->ifa_addr = (struct sockaddr *)ipv6;
76
77 ipv6 = &storage->netmask.s6;
78 ipv6->sin6_family = family;
79 ipv6->sin6_port = 0;
80 ipv6->sin6_flowinfo = 0;
81 ipv6->sin6_addr.s6_addr32[0] = 0xffffffff;
82 ipv6->sin6_addr.s6_addr32[1] = 0xffffffff;
83 ipv6->sin6_addr.s6_addr32[2] = 0xffffffff;
84 ipv6->sin6_addr.s6_addr32[3] = 0;
85 ipv6->sin6_scope_id = 0;
86 p->ifa_netmask = (struct sockaddr *)ipv6;
87
88 ipv6 = &storage->broadaddr.s6;
89 ipv6->sin6_family = family;
90 ipv6->sin6_port = 0;
91 ipv6->sin6_flowinfo = 0;
92 ipv6->sin6_addr.s6_addr32[0] = 0;
93 ipv6->sin6_addr.s6_addr32[1] = 0;
94 ipv6->sin6_addr.s6_addr32[2] = 0;
95 ipv6->sin6_addr.s6_addr32[3] = 0;
96 ipv6->sin6_scope_id = 0;
97 p->ifa_broadaddr = (struct sockaddr *)ipv6;
98 }
99
100 p->ifa_data = NULL;
101 }
102
103 int getifaddrs(struct ifaddrs **ifap) {
104 struct ifaddrs_storage *storage;
105
106 /* Allocate memory for 4 interfaces */
107 storage = (struct ifaddrs_storage *)calloc(4, sizeof(struct ifaddrs_storage));
108 *ifap = &storage[0].ifa;
109
110 init_entry(&storage[0], "eth0", AF_INET, 0xc0a80114, 0, 0, 0, false); /* 192.168.1.20 */
111 init_entry(&storage[1], "eth0", AF_INET6, 0xfe800000, 0, 0, 0xdeadbeef, false); /* fe80::dead:beef */
112
113 /* Loopback interface */
114 init_entry(&storage[2], "lo", AF_INET, 0x7f000001, 0, 0, 0, false); /* 127.0.0.1 */
115 init_entry(&storage[3], "lo", AF_INET6, 0, 0, 0, 1, true); /* ::1 */
116
117 return 0;
118 }
119
120 void freeifaddrs(struct ifaddrs *ifa) {
121 free(ifa);
122 }
123