获得主机的IP和主机名
2009-07-21 11:03
351 查看
/*
* g++ -o gethostip gethostip.cpp
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#define MAXINTERFACES 16
#define INT int
#define CHAR char
class CIfCfg
{
public:
enum IFSTATUS {UNKOWN,PROMISC,UP,RUNNING,DOWN};
struct Sifcfg
{
CHAR name[16];
CHAR address[16];
IFSTATUS status;
};
Sifcfg interfaces[MAXINTERFACES];
INT size;
CIfCfg()
{
size=0;
}
};
INT getLocalHostAddress(CIfCfg& ifcfg)
{
register int fd, intrface;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
{
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
if (ioctl (fd, SIOCGIFCONF, (char *) &ifc) < 0)
{
return -1;
}
intrface = ifc.ifc_len/sizeof(struct ifreq);
while (intrface-- > 0)
{
if ((ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])) < 0)
{
continue;
}
snprintf(ifcfg.interfaces[ifcfg.size].name,
sizeof(ifcfg.interfaces[ifcfg.size].name),
"%s",buf[intrface].ifr_name);
if (buf[intrface].ifr_flags & IFF_PROMISC)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::PROMISC;
}
else
{
if (buf[intrface].ifr_flags & IFF_UP)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::UP;
}
else
{
if (buf[intrface].ifr_flags & IFF_RUNNING)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::RUNNING;
}
}
}
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
snprintf(ifcfg.interfaces[ifcfg.size].address,
sizeof(ifcfg.interfaces[ifcfg.size].address),
"%s",
inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
}
else
continue;
ifcfg.size++;
}
close (fd);
return ifcfg.size;
}
int main(void)
{
CIfCfg ipconfig;
int num=getLocalHostAddress(ipconfig);
for (int i=0; i<num; i++)
printf("--------/nnum:%d/nname:%s/nstatus:%d/naddress:%s/n",
i,
ipconfig.interfaces[i].name,
ipconfig.interfaces[i].status,
ipconfig.interfaces[i].address);
return 1;
}
* g++ -o gethostip gethostip.cpp
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#define MAXINTERFACES 16
#define INT int
#define CHAR char
class CIfCfg
{
public:
enum IFSTATUS {UNKOWN,PROMISC,UP,RUNNING,DOWN};
struct Sifcfg
{
CHAR name[16];
CHAR address[16];
IFSTATUS status;
};
Sifcfg interfaces[MAXINTERFACES];
INT size;
CIfCfg()
{
size=0;
}
};
INT getLocalHostAddress(CIfCfg& ifcfg)
{
register int fd, intrface;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
{
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
if (ioctl (fd, SIOCGIFCONF, (char *) &ifc) < 0)
{
return -1;
}
intrface = ifc.ifc_len/sizeof(struct ifreq);
while (intrface-- > 0)
{
if ((ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])) < 0)
{
continue;
}
snprintf(ifcfg.interfaces[ifcfg.size].name,
sizeof(ifcfg.interfaces[ifcfg.size].name),
"%s",buf[intrface].ifr_name);
if (buf[intrface].ifr_flags & IFF_PROMISC)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::PROMISC;
}
else
{
if (buf[intrface].ifr_flags & IFF_UP)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::UP;
}
else
{
if (buf[intrface].ifr_flags & IFF_RUNNING)
{
ifcfg.interfaces[ifcfg.size].status=CIfCfg::RUNNING;
}
}
}
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
snprintf(ifcfg.interfaces[ifcfg.size].address,
sizeof(ifcfg.interfaces[ifcfg.size].address),
"%s",
inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
}
else
continue;
ifcfg.size++;
}
close (fd);
return ifcfg.size;
}
int main(void)
{
CIfCfg ipconfig;
int num=getLocalHostAddress(ipconfig);
for (int i=0; i<num; i++)
printf("--------/nnum:%d/nname:%s/nstatus:%d/naddress:%s/n",
i,
ipconfig.interfaces[i].name,
ipconfig.interfaces[i].status,
ipconfig.interfaces[i].address);
return 1;
}
相关文章推荐
- 通过域名获得主机ip
- 在Windows下通过IP获得主机名
- 通过ip获得远程主机的MAC
- IP扫描;SendARP() 查询本地网络在线电脑IP,MAC,以及获得主机名。
- 利用IP,用C#来获得远程主机的MAC地址
- C#获得客户端的IP,主机名 获取当前时间
- VC++ 获得主机名与IP
- python 使用socket 获得本机主机名和IP
- 利用IP,用C#来获得远程主机的MAC地址
- C#自带的.net类库获得主机名和IP
- 根据主机域名获得主机IP
- aws 主机如何在主机内获得本机邦定的ip
- 『Python』爬行搜索引擎结果获得指定主机二级域名及IP信息
- 如何用DOS获得目标主机的IP(媒介类似qq聊天等通讯工具)
- 根据主机名查ip,局域网内,通过主机名获得IP
- 获得服务器IP和主机名添加到hosts文件
- 获得给定主机名和IP 地址
- 根据计算机名获得IP,和根据IP获得主机名(转)
- 获得本地主机IP
- Python 获得主机IP