runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
NetworkHelper.cpp
Go to the documentation of this file.
1 #include <NetworkHelper.h>
2 
3 #ifdef HAVE_WINDOWS
4 #include <winsock2.h>
5 #include <ws2tcpip.h>
6 #include <windows.h>
7 #include <locale>
8 #include <codecvt>
9 #endif
10 
11 #ifdef HAVE_LINUX
12 #include <ifaddrs.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #endif
16 
17 #include <iostream>
18 #include <cstring>
19 using namespace std;
20 
22 #ifdef HAVE_WINDOWS
23  WORD wVersionRequested;
24  WSADATA wsaData;
25 
26  wVersionRequested = MAKEWORD(2,2);
27  if(WSAStartup(wVersionRequested, &wsaData) != 0) {
28  cerr << "COULD NOT LOAD WINSOCK LIBRARY!" << endl;
29  }
30 #endif
31 }
32 
34 #ifdef HAVE_WINDOWS
35  WSACleanup();
36 #endif
37 }
38 
39 std::list<std::string> NetworkHelper::getIpAddresses(bool allowIPv6) {
40  list<string> out;
41 #ifdef HAVE_WINDOWS
42  char ac[80];
43  if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
44  cerr << "Error " << WSAGetLastError() <<
45  " when getting local host name." << endl;
46  return out;
47  }
48  cout << "Host name is " << ac << "." << endl;
49 
50  PADDRINFOA addrInfo;
51 
52  if (0 != getaddrinfo(ac, nullptr, nullptr, &addrInfo)) {
53  cerr << "Yow! Bad host lookup." << endl;
54  return out;
55  }
56 
57  while(addrInfo != nullptr) {
58  switch(addrInfo->ai_family) {
59  case AF_INET6:
60  if(!allowIPv6) {
61  break;
62  }
63  /* fallthrough */
64  case AF_INET:
65  {
66  WCHAR str[64];
67  DWORD strlen = (DWORD)(sizeof(str) / sizeof(WCHAR));
68  WSAAddressToStringW(addrInfo->ai_addr,
69  (DWORD)addrInfo->ai_addrlen,
70  NULL,
71  str,
72  &strlen);
73  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
74  out.emplace_back(converter.to_bytes(str));
75  }
76  }
77  addrInfo = addrInfo->ai_next;
78  }
79 #endif
80 #ifdef HAVE_LINUX
81  struct ifaddrs * ifAddrStruct=nullptr;
82  struct ifaddrs * ifa=nullptr;
83  void * tmpAddrPtr=nullptr;
84 
85  getifaddrs(&ifAddrStruct);
86 
87  for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
88  if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
89  // is a valid IP4 Address
90  tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
91  char addressBuffer[INET_ADDRSTRLEN];
92  inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
93  if(strcmp(addressBuffer, "127.0.0.1") == 0)
94  continue;
95  out.emplace_back(addressBuffer);
96  } else if (ifa->ifa_addr->sa_family==AF_INET6 && allowIPv6) { // check it is IP6
97  // is a valid IP6 Address
98  tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
99  char addressBuffer[INET6_ADDRSTRLEN];
100  inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
101  if(strcmp(addressBuffer, "::1") == 0)
102  continue;
103  out.emplace_back(addressBuffer);
104  }
105  }
106  if (ifAddrStruct!=nullptr) freeifaddrs(ifAddrStruct);
107 #endif
108  return out;
109 }
STL namespace.
std::list< std::string > getIpAddresses(bool allowIPv6=false)