/****************************************************************************
@File Name: hostinfo.h
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Sun 05 Mar 2017 08:20:39 AM CST
****************************************************************************/
#ifndef HOST_INFO_H
#define HOST_INFO_H
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string>
#include <vector>
namespace hostinfo
{
static const int MAX_INTERFACE = 16;
using namespace std;
class HostInfo
{
public:
HostInfo()
{
m_hptr = 0;
}
/*
* @brief init by host domain
* first find in DNS then find in /etc/hosts
* */
inline bool InitByDomain(const string &domain)
{
if(!(m_hptr = gethostbyname(domain.c_str()))) return false;
return true;
}
/*
* @brief init by host ip addr
* */
inline bool InitByIpAddr(const string &ipaddr)
{
bool success = true;
struct in_addr *hipaddr = (struct in_addr *)malloc(sizeof(struct in_addr));
if(!hipaddr) return false;
if(!inet_aton(ipaddr.c_str(), hipaddr))
{
success = false;
}
else
if(!(m_hptr = gethostbyaddr(hipaddr, 4, AF_INET)))
{
success = false;
}
free(hipaddr);
return success;
}
/*
* @brief get the official host name
* */
inline void GetHostName_official(string &hostname)
{
if(m_hptr) hostname = m_hptr->h_name;
}
/*
* @brief get the alias host names
* */
void GetHostName_alias(vector<string>&hostnames);
/*
* @brief get the ip addr
* */
bool GetIpaddrs(vector<string>&ipaddrs);
public:
/*
* @brief the ip and mac address from NIC
* */
static bool GetNICIp(vector<string>&ipaddrs, vector<string>&macaddrs);
private:
struct hostent *m_hptr;
};
}
#endif
/****************************************************************************
@File Name: hostinfo.cpp
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Sun 05 Mar 2017 08:20:39 AM CST
****************************************************************************/
#include "hostinfo.h"
namespace hostinfo
{
/*
* @brief get the alias host names
* */
void HostInfo::GetHostName_alias(vector<string>&hostnames)
{
char **pptr;
hostnames.clear();
if(!m_hptr) return;
for(pptr = m_hptr->h_aliases;*pptr;pptr++)
{
hostnames.push_back(*pptr);
}
}
/*
* @brief get the ip addr
* */
bool HostInfo::GetIpaddrs(vector<string>&ipaddrs)
{
char **pptr;
char buf[64] = {0};
ipaddrs.clear();
if(!m_hptr) return false;
switch(m_hptr->h_addrtype)
{
case AF_INET:
case AF_INET6:
for(pptr = m_hptr->h_addr_list;*pptr;pptr++)
{
inet_ntop(m_hptr->h_addrtype, *pptr, buf, sizeof(buf));
ipaddrs.push_back(buf);
}
return true;
default:
return false;
}
}
/*
* @brief the ip and mac address from NIC
* */
bool HostInfo::GetNICIp(vector<string>&ipaddrs, vector<string>&macaddrs)
{
int fd;
int if_num; // NIC num
char tmp[64];
struct ifreq buf[MAX_INTERFACE];
struct ifconf ifc;
ipaddrs.clear();
macaddrs.clear();
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return false;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if(ioctl(fd, SIOCGIFCONF, (char *)&ifc)) return false;
if_num = ifc.ifc_len / sizeof(struct ifreq);
while(if_num-- > 0)
{
if(ioctl(fd, SIOCGIFADDR, (char *)&buf[if_num])) continue;
const char *ptr = inet_ntoa(((struct sockaddr_in *)(&buf[if_num].ifr_addr))->sin_addr);
ipaddrs.push_back(ptr);
if(ioctl(fd, SIOCGIFHWADDR, (char*)&buf[if_num])) continue;
snprintf(tmp, sizeof tmp, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[0],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[1],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[2],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[3],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[4],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[5]);
macaddrs.push_back(tmp);
}
close(fd);
return true;
}
}
/****************************************************************************
@File Name: test.cpp
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Sun 05 Mar 2017 08:46:02 AM CST
****************************************************************************/
#include "hostinfo.h"
#include <iostream>
using namespace hostinfo;
int main()
{
string domain;
string ipaddr;
string str;
vector<string>strs;
vector<string>str1s;
vector<string>::iterator it;
cout << "domain = ";
cin >> domain;
cout << "ip addr = ";
cin >> ipaddr;
HostInfo hostinfo;
cout << "init by domain===============================================" << endl;
// init by domain
if(hostinfo.InitByDomain(domain))
{
hostinfo.GetHostName_official(str);
cout << "offical host name = " << str << endl;
hostinfo.GetHostName_alias(strs);
for(it = strs.begin();it != strs.end();++it)
{
cout << "alias host name = " << *it << endl;
}
hostinfo.GetIpaddrs(strs);
for(it = strs.begin();it != strs.end();++it)
{
cout << "ip addr = " << *it << endl;
}
}
cout << "init by ip addr===============================================" << endl;
// init by ip addr
if(hostinfo.InitByDomain(ipaddr))
{
hostinfo.GetHostName_official(str);
cout << "offical host name = " << str << endl;
hostinfo.GetHostName_alias(strs);
for(it = strs.begin();it != strs.end();++it)
{
cout << "alias host name = " << *it << endl;
}
hostinfo.GetIpaddrs(strs);
for(it = strs.begin();it != strs.end();++it)
{
cout << "ip addr = " << *it << endl;
}
}
cout << "get NIC ip and mac===============================================" << endl;
// get NIC IP
if(HostInfo::GetNICIp(strs, str1s))
{
for(it = strs.begin();it != strs.end();++it)
{
cout << "NIC ip addr = " << *it << endl;
}
for(it = str1s.begin();it != str1s.end();++it)
{
cout << "NIC mac addr = " << *it << endl;
}
}
return 0;
}
CC=g++
all:
$(CC) -g -o hostinfoTest test.cpp hostinfo.cpp hostinfo.h
hostinfo
最新推荐文章于 2024-04-17 09:58:21 发布