blob: 173b67fa441b4c30d8b363b51923f4d377a5f9b4 [file] [log] [blame]
Ratan Gupta8ab17922017-05-25 13:07:05 +05301#include <sys/types.h>
2#include <sys/socket.h>
3
4#include <net/if.h>
5#include <netinet/in.h>
6#include <arpa/inet.h>
7
8#include <ifaddrs.h>
9
10#define MAX_IFADDRS 5
11
12int debugging = false;
13
14/* Data for mocking getifaddrs */
15struct ifaddr_storage {
16 struct ifaddrs ifaddr;
17 struct sockaddr_storage addr;
18 struct sockaddr_storage mask;
19 struct sockaddr_storage bcast;
20} mock_ifaddr_storage[MAX_IFADDRS];
21
22struct ifaddrs *mock_ifaddrs = nullptr;
23
24int ifaddr_count = 0;
25
26/* Stub library functions */
27void freeifaddrs(ifaddrs *ifp)
28{
29 return ;
30}
31
32void mock_addIP(const char* name, const char* addr, const char* mask,
33 unsigned int flags)
34{
35 struct ifaddrs *ifaddr = &mock_ifaddr_storage[ifaddr_count].ifaddr;
36
37 struct sockaddr_in *in = reinterpret_cast<sockaddr_in*>
38 (&mock_ifaddr_storage[ifaddr_count].addr);
39 struct sockaddr_in *mask_in = reinterpret_cast<sockaddr_in*>
40 (&mock_ifaddr_storage[ifaddr_count].mask);
41
42 in->sin_family = AF_INET;
43 in->sin_port = 0;
44 in->sin_addr.s_addr = inet_addr(addr);
45
46 mask_in->sin_family = AF_INET;
47 mask_in->sin_port = 0;
48 mask_in->sin_addr.s_addr = inet_addr(mask);
49
50 ifaddr->ifa_next = nullptr;
51 ifaddr->ifa_name = const_cast<char*>(name);
52 ifaddr->ifa_flags = flags;
53 ifaddr->ifa_addr = reinterpret_cast<struct sockaddr*>(in);
54 ifaddr->ifa_netmask = reinterpret_cast<struct sockaddr*>(mask_in);
55 ifaddr->ifa_data = nullptr;
56
57 if (ifaddr_count > 0)
58 mock_ifaddr_storage[ifaddr_count - 1].ifaddr.ifa_next = ifaddr;
59 ifaddr_count++;
60 mock_ifaddrs = &mock_ifaddr_storage[0].ifaddr;
61
62}
63
64int getifaddrs(ifaddrs **ifap)
65{
66 *ifap = mock_ifaddrs;
67 if (mock_ifaddrs == nullptr)
68 return -1;
69 return (0);
70}
71