百度空间 | 百度首页 
 
查看文章
 
pcap ipaddr
2009-11-03 14:35

//start

// yjyfir.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#define HAVE_REMOTE
#include "pcap.h"

#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif

#include <pcap.h>

#pragma comment(lib, "Packet.lib")
#pragma comment(lib, "wpcap.lib")
void ifprint(pcap_if_t * );
int main()
{
pcap_if_t * alldevs;
pcap_if_t * d;

char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)<0){
   printf("%s\n",errbuf);
   exit(1);
}
for (d=alldevs;d!=NULL;d=d->next){
   printf("%s\n",d->description);
   ifprint(d);
}
if (alldevs==NULL){
   printf("NO netcard found!\n");
}
pcap_freealldevs(alldevs);
return 0;
}
void ifprint(pcap_if_t * nd){
   pcap_addr_t * addr;
   printf("name: %s\n",nd->name);
   printf("description: %s\n",nd->description);
   printf("addr:\n");
   for (addr=nd->addresses;addr!=NULL;addr=addr->next){
    if (addr->addr){
     //ip addr
     u_char* p=(u_char*)&((struct sockaddr_in *)addr->addr)->sin_addr.s_addr;
     if (p!=NULL)
      printf("%d . %d . %d . %d\n",p[0],p[1],p[2],p[3]);
    }
   }
}

//end


类别:net 协议 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu