gpt4 book ai didi

c - 向函数发送多个数据

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:56 24 4
gpt4 key购买 nike

我正在尝试将多个数据发送到一个函数,该函数进一步接收数据并将其发送到另一个函数以进行数据库插入。我曾尝试使用指针引用数据,但由于某种原因它没有收到我想要的数据。需要发送的数据如下:

void print_ethernet_data(const u_char *Buffer , int Size){

unsigned char dest_mac[6]; unsigned char src_mac[6];
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);
*dest_mac = &eth->dhost;
*src_mac = &eth->shost;
handledata(Buffer + header_size , Size - header_size, &dest_mac, &src_mac); //This is the function called
}

此处ether_header定义为:

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
};

调用函数handledata如下:

void handledata(struct sniff_dns* dns, int size, unsigned char* dest_mac, unsigned char** src_mac)
{

//RECEIVEING DATA HERE-------------------------------
printf("DST_MAC = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",dest_mac[0], dest_mac[1], dest_mac[2], dest_mac[3], dest_mac[4], dest_mac[5]);
printf("SRC_MAC = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5]);
insert_in_db(&dst_mac, &src_mac); //How should I handle this function and its call?
}

我想要的输出与我期望的完全不同。我已经打印了两个输出,它们是不同的。我猜第二个是指向上面在 eth->dhosteth->shost

中定义的数组的地址

输出:

|-Destination Address : E4:FC:82:FD:32:C1 
|-Source Address : 58:49:3B:38:B5:11
|-DST_MAC = 86:64:98:46:2E:7F //They are different from the ones above
|-SRC_MAC = 4698648C:00:00:00:00:00 //This one too

如果有人能帮助我解决这个问题,我将不胜感激。另外,我应该如何将数据传递给 insert_in_db() 函数并在那里进行处理?提前致谢

最佳答案

您将 dest_macsrc_mac 定义为数组,然后试图将它们用作没有意义的指针。我猜您希望这些变量指向缓冲区中的数据。如果是这种情况,请使用指针而不是数组。

void print_ethernet_data(const u_char *Buffer , int Size){

unsigned char *dest_mac; unsigned char *src_mac;
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);
dest_mac = eth->dhost;
src_mac = eth->shost;
handledata(Buffer + header_size , Size - header_size, &dest_mac, &src_mac); //This is the function called
}

您的其他函数也需要修复 src_mac 变量,因为您将其定义为指向指针的指针,而不仅仅是指向 char 的普通指针。

void handledata(struct sniff_dns* dns, int size, unsigned char* dest_mac, unsigned char* src_mac)
{

//RECEIVEING DATA HERE-------------------------------
printf("DST_MAC = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",dest_mac[0], dest_mac[1], dest_mac[2], dest_mac[3], dest_mac[4], dest_mac[5]);
printf("SRC_MAC = %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5]);

insert_in_db(dst_mac, src_mac); //How should I handle this function and its call?
}

最后,在调用 insert_in_db 函数时,您可以只传递指针而不传递指针的地址。我认为您应该阅读有关指针的内容。

关于c - 向函数发送多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52929785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com