为什么原始套接字有时在h_source和h_dest中返回零?

问题描述

我正在尝试使用Linux原始套接字,并编写了一个程序,该程序仅侦听认接口并读取以太网源和目标MAC地址以及其上方的协议号。代码运行正常,我猜没有问题(我的意思是没有错误),但是有时(并非总是)它只是在源地址和目标地址中都返回零。否则,将正确打印3个字段。因此数据包如何将这两个字段设为零。发生这种情况时收到的数据缓冲区是(我不知道为什么有时十六进制显示为超过1个十六进制字符):


    Created raw socket
            Data received length is :85
    The source Mac address is 00-00-00-00-00-00
    The destination Mac address is 00-00-00-00-00-00
    The above protocol number is 0x0800
    Data is: 00 
    00 00 00 00 00 00 00 00 00 00 
    00 08 00 45 00 00 47 ffffff8d 44 40 
    00 40 11 ffffffaf 2b 7f 00 00 01 7f 
    00 00 35 ffffff8a 0e 00 35 00 33 fffffffe 
    7a ffffffe2 53 01 00 00 01 00 00 00 
    00 00 01 03 77 77 77 06 67 6f 
    6f 67 6c 65 03 63 6f 6d 00 00 
    01 00 01 00 00 29 04 ffffffb0 00 00 

整个代码是:

#include <linux/if_packet.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/if_ether.h>
#include <memory.h>
#include <netinet/in.h>
#include <libnet.h>

void error_print(char* error_msg){
    fprintf(stderr,"%s",error_msg);
    fprintf(stderr,"\nExiting...");
    exit(1);
}

void print_mac(char* buffer,int bytes_recieved){

    struct ethhdr* mac = (struct ethhdr* ) buffer;

    printf("The source Mac address is %.2x-%.2x-%.2x-%.2x-%.2x-%.2x\n",mac->h_source[0],mac->h_source[1],mac->h_source[2],mac->h_source[3],mac->h_source[4],mac->h_source[5]);

    printf("The destination Mac address is %.2x-%.2x-%.2x-%.2x-%.2x-%.2x\n",mac->h_dest[0],mac->h_dest[1],mac->h_dest[2],mac->h_dest[3],mac->h_dest[4],mac->h_dest[5]);

    printf("The above protocol number is 0x%.4x\n",ntohs(mac->h_proto));

            /*....... format recieved buffer for debugging ...........*/
    printf("Data is: ");
    for (int j=0; j < bytes_recieved; j++){
        printf("%.*x ",2,buffer[j]);
        if ((j % 10) == 0 && j != 0)
            printf("\n");
    }
    printf("\n");
}

int main(int argc,char* argv[]){
        if(argc < 1)
            error_print("Incorrect Number of arguments");

        int sockfd;
        sockfd = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
        if(sockfd == -1)
            error_print("Unable to open row socket...are you root...");
        printf("Created raw socket\n");

        struct sockaddr sockaddr_details;
        u_int sock_len = sizeof(struct sockaddr);

        /* Now recv data */
#define BUFFER_SIZE 1024
        char *buffer = malloc(BUFFER_SIZE);
        memset(buffer,BUFFER_SIZE);     // set memory to zero since recvfrom doesnt null terminate
        int recv_size;
        recv_size = recvfrom(sockfd,buffer,BUFFER_SIZE,&sockaddr_details,&sock_len);
        if(recv_size == -1 || recv_size == 0)
            error_print("Error recieving data");
        printf("\tData received length is :%d\n",recv_size);

        /* ********************************************** */
    struct ifreq ifreq_i;
    memset(&ifreq_i,sizeof(ifreq_i));
    strncpy(ifreq_i.ifr_name,"wlp2s0",IFNAMSIZ-1); //giving name of Interface

    if((ioctl(sockfd,SIocgIFINDEX,&ifreq_i))<0)
        printf("error in index ioctl reading\n");//getting Index Name
    else
        printf("index=%d\n",ifreq_i.ifr_ifindex);

        /* Now print ether frame header */
        print_mac(buffer,recv_size);

        close(sockfd);
        free(buffer);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)