22 #include <arpa/inet.h> 23 #include <netinet/in.h> 26 #include <coil/Routing.h> 27 #include <coil/stringutil.h> 60 struct ::hostent *hostent;
61 struct ::sockaddr_in addr;
63 hostent = gethostbyname(dest_addr.c_str());
64 addr.sin_addr.s_addr = **(
unsigned int **)(hostent->h_addr_list);
65 dest_addr = inet_ntoa(addr.sin_addr);
67 #if defined(COIL_OS_FREEBSD) || defined(COIL_OS_DARWIN) || defined(COIL_OS_CYGWIN) || defined(COIL_OS_QNX) 68 std::string
cmd(
"PATH=/bin:/sbin:/usr/bin:/usr/sbin " 70 const char* match_str =
"interface";
71 const char* delimiter =
":";
74 cmd +=
" 2> /dev/null";
75 #endif // COIL_OS_IS_FREEBSD || COIL_OS_DARWIN || COIL_OS_CYGWIN || COIL_OS_QNX 76 #if defined(COIL_OS_LINUX) 77 std::string
cmd(
"PATH=/bin:/sbin:/usr/bin:/usr/sbin " 79 const char* match_str =
"dev ";
80 const char* delimiter =
" ";
83 cmd +=
" 2> /dev/null";
84 #endif // COIL_OS_IS_LINUX 87 if ((fp =
popen(cmd.c_str(),
"r")) == NULL)
96 std::string line(str);
98 if (std::string::npos == line.find(match_str)) {
continue; }
100 line.erase(line.end() - 1);
103 #if defined(COIL_OS_FREEBSD) || defined(COIL_OS_DARWIN) || defined(COIL_OS_CYGWIN) || defined(COIL_OS_QNX) 104 if (vs.size() > ifname_pos)
106 dest_if = vs[ifname_pos];
111 #endif // COIL_OS_FREEBSD || COIL_OS_DARWIN || COIL_OS_CYGWIN || COIL_OS_QNX 112 #if defined(COIL_OS_LINUX) 113 for (
int i(0); i < vs.size(); ++i)
122 #endif // COIL_OS_LINUX 138 std::string
cmd(
"ifconfig ");
140 cmd +=
" 2> /dev/null";
143 if ((fp =
popen(cmd.c_str(),
"r")) == NULL)
152 std::string line(str);
154 if (std::string::npos == line.find(
"inet ")) {
continue; }
156 line.erase(line.end() - 1);
bool ifname_to_ipaddr(std::string ifname, std::string &ipaddr)
Get IP address from a network interface name.
void eraseHeadBlank(std::string &str)
Erase the head blank characters of string.
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
bool find_dest_ifname(std::string dest_addr, std::string &dest_if)
Getting network interface name from destination address.
std::vector< std::string > vstring
FILE * popen(const char *cmd, const char *mode)
bool dest_to_endpoint(std::string dest_addr, std::string &endpoint)
Getting network interface name from destination address.
Common Object Interface Layer.