RCFProto
 All Classes Functions Typedefs
GetLocalIps.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_UTIL_GETLOCALIPS_HPP
20 #define INCLUDE_UTIL_GETLOCALIPS_HPP
21 
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <RCF/Exception.hpp>
27 #include <RCF/util/Platform/OS/BsdSockets.hpp>
28 
29 namespace RCF {
30 
31  std::pair<std::string, std::vector<std::string> > getLocalIps()
32  {
33  std::vector<char> hostname(80);
34  int ret = gethostname(&hostname[0], hostname.size());
35  int err = Platform::OS::BsdSockets::GetLastError();
36  RCF_VERIFY(
37  ret != SOCKET_ERROR,
38  RCF::Exception(0, err, RCF::RcfSubsystem_Os))(ret)(err);
39  hostent *phe = gethostbyname(&hostname[0]);
40  err = Platform::OS::BsdSockets::GetLastError();
41  RCF_VERIFY(
42  phe,
43  RCF::Exception(0, err, RCF::RcfSubsystem_Os))(err);
44  std::vector<std::string> ips;
45  for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
46  struct in_addr addr;
47  memcpy(&addr, phe->h_addr_list[i], sizeof( in_addr));
48  ips.push_back(inet_ntoa(addr));
49  }
50  return std::make_pair( std::string(&hostname[0]), ips);
51  }
52 
53 } // namespace RCF
54 
55 #endif