日期:2014-05-17  浏览次数:20952 次

高分求高手
是这样的,我住的这个房东的网速原来一直很好。但是近来网速慢的出奇!!有时候qq登上去都能自动掉下来。每次我就发现网线就掉了,然后在链接上的时候网速开始就能好一点点。房东给我说是他重启路由器了。要不然实在是没办法了。
我就想问问高手们,我咋才能知道是不是这里面有人在捣乱。因为原来网速一直很好呀! 还有我和房东挺熟的,他让我帮他弄,所以我现在可以等上去路由器。 记得原来上学的时候老师给我们教过原始套接字 还写过一点小程序,我用我的程序运行的结果是这样的:
~ 1 MAC: 00:50:56:C0:00:08==>FF:FF:FF:FF:FF:FF
  2 2 IP: 192.168.227.1 => 192.168.227.255
  3 3 Protocol: UDP,source port: 138,dest port: 138
  4 4 MAC: 00:50:56:C0:00:08==>FF:FF:FF:FF:FF:FF
  5 5 IP: 192.168.227.1 => 192.168.227.255
  6 6 Protocol: UDP,source port: 138,dest port: 138
  7 7 MAC: 00:50:56:E5:30:2B==>00:0C:29:76:22:D7
  8 8 IP: 192.168.227.254 => 192.168.227.128
  9 9 Protocol: UDP,source port: 67,dest port: 68
  10 10 MAC: 00:50:56:C0:00:08==>FF:FF:FF:FF:FF:FF
  11 11 IP: 192.168.227.1 => 192.168.227.255
  12 12 Protocol: UDP,source port: 138,dest port: 138

程序的源代码是这样
C/C++ code
/***************SimpelSniffer.c*************/

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#define BUFFER_MAX 2048


int main(int argc, char *argv[])
{
    
    int sock, n_read, proto;    
    char buffer[BUFFER_MAX];
    char *ethhead, *iphead, *tcphead, *udphead, *icmphead, *p;
    
    if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
    {
        fprintf(stdout, "create socket error\n");
        exit(0);
    }
    while(1) 
    {
        n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
        /*
        14   6(dest)+6(source)+2(type or length)
        +
        20   ip header 
        +
        8    icmp,tcp or udp header
        = 42
        */
        if(n_read < 42) 
        {
            fprintf(stdout, "Incomplete header, packet corrupt\n");
            continue;
        }
        
        ethhead = buffer;
        p = ethhead;
        int n = 0XFF;
        printf("MAC: %.2X:%02X:%02X:%02X:%02X:%02X==>"
            "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
            p[6]&n, p[7]&n, p[8]&n, p[9]&n, p[10]&n, p[11]&n,
            p[0]&n, p[1]&n, p[2]&n, p[3]&n, p[4]&n, p[5]&n);


        iphead = ethhead + 14;  
        p = iphead + 12;
        
        printf("IP: %d.%d.%d.%d => %d.%d.%d.%d\n",
            p[0]&0XFF, p[1]&0XFF, p[2]&0XFF, p[3]&0XFF,
            p[4]&0XFF, p[5]&0XFF, p[6]&0XFF, p[7]&0XFF);
        
        proto = *(iphead + 9);

        p = iphead + 20;
        printf("Protocol: ");
        switch(proto)
        {
        case IPPROTO_ICMP: printf("ICMP\n");break;
        case IPPROTO_IGMP: printf("IGMP\n");break;
        case IPPROTO_IPIP: printf("IPIP\n");break;
        case IPPROTO_TCP :
        case IPPROTO_UDP : printf("%s,", proto == IPPROTO_TCP ? "TCP": "UDP"); 
            printf("source port: %u,",(p[0]<<8)&0XFF00 |  p[1]&0XFF);
            printf("dest port: %u\n", (p[2]<<8)&0XFF00 | p[3]&0XFF);
            break;
        case IPPROTO_RAW : printf("RAW\n");break;
        default:printf("Unkown, please query in include/linux/in.h\n");
        }
    }
}



现在都忘了当时是啥意思了 但是大概还能看明白! 对了 ,这是linux下的程序! 望高手指点 我咋能看出是那个人在捣乱 还是我们的路由器出问题了

------解决方案--------------------

分开来说,就是抓取数据的mac、IP及协议信息,数据包太少,几乎都是arp广播、IP广播及138解析主机名用的
------解决方案--------------------