Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include <arpa/inet.h> |
| 2 | #include <ifaddrs.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 3 | #include <net/if.h> |
| 4 | #include <netinet/in.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <sys/socket.h> |
| 6 | #include <sys/types.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 7 | |
| 8 | #define MAX_IFADDRS 5 |
| 9 | |
| 10 | int debugging = false; |
| 11 | |
| 12 | /* Data for mocking getifaddrs */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | struct ifaddr_storage |
| 14 | { |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 15 | 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 21 | struct ifaddrs* mock_ifaddrs = nullptr; |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 22 | |
| 23 | int ifaddr_count = 0; |
| 24 | |
| 25 | /* Stub library functions */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 26 | void freeifaddrs(ifaddrs* ifp) |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 27 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | return; |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void mock_addIP(const char* name, const char* addr, const char* mask, |
| 32 | unsigned int flags) |
| 33 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | struct ifaddrs* ifaddr = &mock_ifaddr_storage[ifaddr_count].ifaddr; |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 36 | 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 Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 40 | |
| 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 Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 60 | } |
| 61 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | int getifaddrs(ifaddrs** ifap) |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 63 | { |
| 64 | *ifap = mock_ifaddrs; |
| 65 | if (mock_ifaddrs == nullptr) |
| 66 | return -1; |
| 67 | return (0); |
| 68 | } |