我正在尝试从一个函数返回无符号字符到另一个函数,但是由于我是C语言的新手,它的数据结构对我来说仍然是个谜。这里是调用该函数的函数...
void print_ethernet_data(const u_char * Buffer, int Size)
{
unsigned char destination, src;
unsigned short qtype;
get_ethernet_header(Buffer , Size, &destination, &src, &qtype); //The function that is supposed to return the values
printf("|-Dest : %u \n", destination);
printf("|-Protocol : %u \n",qtype);
}
这是函数
get_internet_header
的描述:它用于获取互联网报头。
void get_ethernet_header(const u_char *Buffer, int Size, unsigned char* destination, unsigned char* src, unsigned short* qtype)
{
struct ether_header *eth = (struct ether_header *)Buffer;
printf( "\n");
printf( "Ethernet Header\n");
printf( " |-Destination Address : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X \n", eth->dhost[0] , eth->dhost[1] , eth->dhost[2] , eth->dhost[3] , eth->dhost[4] , eth->dhost[5] );
printf( " |-Source Address : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X \n", eth->shost[0] , eth->shost[1] , eth->shost[2] , eth->shost[3] , eth->shost[4] , eth->shost[5] );
printf( " |-Protocol : %u \n",(unsigned short)eth->type);
*destination = eth->dhost;
*src = eth->shost;
*qtype = (unsigned short)eth->type;
}
但是出于我不知道的某种原因,我无法像这样做,因为它对我打印的内容是这样的。
Ethernet Header
|-Destination Address : 58:49:3B:38:B5:11
|-Source Address : E4:FC:82:FD:32:C1
|-Protocol : 8
|-Dest : 134 //I suppose that this is the address the pointer points to
|-Protocol : 8 //This matches the Protocol printed above
这是因为我返回的方式不正确还是打印数据的格式不正确导致的?请注意,协议以正确的方式打印出来了,这正是我想要的。但是目标mac地址没有打印出来。
我在StackOverflow上阅读了各种答案,但无法使其正常工作。如果有人能帮忙解决这个问题,我将非常感激。提前致谢。
P.S:我认为不需要说明“struct ether_header”的描述,但如果需要,我可以稍后进行编辑。
编辑-1:
struct ether_header {
unsigned char dhost[ETHER_ADDR_LEN]; // Destination host address
unsigned char shost[ETHER_ADDR_LEN]; // Source host address
unsigned short type; // IP? ARP? RARP? etc
};
"%s"
?对于dhost,它会给出"����2�XI;8�"
。而对于destination,则会导致分段错误。 - Devanshu Misra