2012-02-24 9 views
1

これは私の最初の投稿です。ifconfigとlibpcapからのインターフェイスipが一致しません

私はlibpcap(1.2.1)でパケットアナライザを実行しています。私はいくつかのサンプルを実行して管理しています。私は既にアナライザの一部を書いています。

私のifconfigコマンドは、これを言う:

wlan0  Link encap:Ethernet HWaddr 90:00:4e:96:80:b1 
      inet addr:192.168.100.100 Bcast:192.168.100.255 Mask:255.255.255.0 
      inet6 addr: fe80::9200:4eff:fe96:80b1/64 Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:304853 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:279099 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1000 
      RX bytes:403507845 (403.5 MB) TX bytes:28330813 (28.3 MB) 

あなたはIPアドレスがある192.168.100.100

を見ることができるように、私はこの例を使用し、DEV名、ネットマスクとIPを表示するには、再度の例をコンパイル私は

考える正しくない

DEV: wlan0 
NET: 192.168.100.0 
MASK: 255.255.255.0 

:libpcapをし、私はこれを得ましたその例の3210

コードは次のとおりです。

#include <stdio.h> 
#include <stdlib.h> 
#include <pcap.h> 
#include <errno.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 

int main(int argc, char **argv) 
{ 
    char *dev; /* name of the device to use */ 
    char *net; /* dot notation of the network address */ 
    char *mask;/* dot notation of the network mask */ 
    int ret; /* return code */ 
    char errbuf[PCAP_ERRBUF_SIZE]; 
    bpf_u_int32 netp; /* ip   */ 
    bpf_u_int32 maskp;/* subnet mask */ 
    struct in_addr addr; 

    /* ask pcap to find a valid device for use to sniff on */ 
    dev = pcap_lookupdev(errbuf); 

    /* error checking */ 
    if(dev == NULL) 
    { 
     printf("%s\n",errbuf); 
     exit(1); 
    } 

    /* print out device name */ 
    printf("DEV: %s\n",dev); 

    /* ask pcap for the network address and mask of the device */ 
    ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); 

    if(ret == -1) 
    { 
     printf("%s\n",errbuf); 
     exit(1); 
    } 

    /* get the network address in a human readable form */ 
    addr.s_addr = netp; 
    net = inet_ntoa(addr); 

    if(net == NULL)/* thanks Scott :-P */ 
    { 
     perror("inet_ntoa"); 
     exit(1); 
    } 

    printf("NET: %s\n",net); 

    /* do the same as above for the device's mask */ 
    addr.s_addr = maskp; 
    mask = inet_ntoa(addr); 

    if(mask == NULL) 
    { 
     perror("inet_ntoa"); 
     exit(1); 
    } 

    printf("MASK: %s\n",mask); 

    return 0; 
} 

何が悪いのでしょうか?事前

+0

[pcap_lookupnetが間違ったIPアドレスを返す](http://stackoverflow.com/questions/9213828/pcap-lookupnet-returns-incorrect-ip-address) –

+0

これは本当ですが、それはまったく同じ問題です私は が重複して私に謝罪している – Jan

答えて

0

おかげでコードが言うように、192.168.100.0ネットワークアドレスではなく、あなたのIPアドレスです。ネットワークアドレスは、IPアドレスとネットマスクをビット単位で論理積することによって生成されます。

関連する問題