blob: 28ebed279a3e4cc400f41bed908b702a1c0622fa [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include <arpa/inet.h>
2#include <ifaddrs.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05303#include <net/if.h>
4#include <netinet/in.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <sys/socket.h>
6#include <sys/types.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05307
8#define MAX_IFADDRS 5
9
10int debugging = false;
11
12/* Data for mocking getifaddrs */
Gunnar Mills57d9c502018-09-14 14:42:34 -050013struct ifaddr_storage
14{
Ratan Gupta8ab17922017-05-25 13:07:05 +053015 struct ifaddrs ifaddr;
16 struct sockaddr_storage addr;
17 struct sockaddr_storage mask;
18 struct sockaddr_storage bcast;
19} mock_ifaddr_storage[MAX_IFADDRS];
20
Gunnar Mills57d9c502018-09-14 14:42:34 -050021struct ifaddrs* mock_ifaddrs = nullptr;
Ratan Gupta8ab17922017-05-25 13:07:05 +053022
23int ifaddr_count = 0;
24
25/* Stub library functions */
Gunnar Mills57d9c502018-09-14 14:42:34 -050026void freeifaddrs(ifaddrs* ifp)
Ratan Gupta8ab17922017-05-25 13:07:05 +053027{
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 return;
Ratan Gupta8ab17922017-05-25 13:07:05 +053029}
30
31void mock_addIP(const char* name, const char* addr, const char* mask,
32 unsigned int flags)
33{
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 struct ifaddrs* ifaddr = &mock_ifaddr_storage[ifaddr_count].ifaddr;
Ratan Gupta8ab17922017-05-25 13:07:05 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 struct sockaddr_in* in =
37 reinterpret_cast<sockaddr_in*>(&mock_ifaddr_storage[ifaddr_count].addr);
38 struct sockaddr_in* mask_in =
39 reinterpret_cast<sockaddr_in*>(&mock_ifaddr_storage[ifaddr_count].mask);
Ratan Gupta8ab17922017-05-25 13:07:05 +053040
41 in->sin_family = AF_INET;
42 in->sin_port = 0;
43 in->sin_addr.s_addr = inet_addr(addr);
44
45 mask_in->sin_family = AF_INET;
46 mask_in->sin_port = 0;
47 mask_in->sin_addr.s_addr = inet_addr(mask);
48
49 ifaddr->ifa_next = nullptr;
50 ifaddr->ifa_name = const_cast<char*>(name);
51 ifaddr->ifa_flags = flags;
52 ifaddr->ifa_addr = reinterpret_cast<struct sockaddr*>(in);
53 ifaddr->ifa_netmask = reinterpret_cast<struct sockaddr*>(mask_in);
54 ifaddr->ifa_data = nullptr;
55
56 if (ifaddr_count > 0)
57 mock_ifaddr_storage[ifaddr_count - 1].ifaddr.ifa_next = ifaddr;
58 ifaddr_count++;
59 mock_ifaddrs = &mock_ifaddr_storage[0].ifaddr;
Ratan Gupta8ab17922017-05-25 13:07:05 +053060}
61
Gunnar Mills57d9c502018-09-14 14:42:34 -050062int getifaddrs(ifaddrs** ifap)
Ratan Gupta8ab17922017-05-25 13:07:05 +053063{
64 *ifap = mock_ifaddrs;
65 if (mock_ifaddrs == nullptr)
66 return -1;
67 return (0);
68}