C 向功能发送多个数据
我试图将多个数据发送到一个函数,该函数进一步接收数据并将其发送到另一个函数以插入数据库。我尝试使用指针引用数据,但由于某些原因,它没有收到我想要的数据。要发送的数据如下所示:C 向功能发送多个数据,c,C,我试图将多个数据发送到一个函数,该函数进一步接收数据并将其发送到另一个函数以插入数据库。我尝试使用指针引用数据,但由于某些原因,它没有收到我想要的数据。要发送的数据如下所示: 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 *
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 = ð->dhost;
*src_mac = ð->shost;
handledata(Buffer + header_size , Size - header_size, &dest_mac, &src_mac); //This is the function called
}
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?
}
此处,ether\u标题
定义为:
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 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 = ð->dhost;
*src_mac = ð->shost;
handledata(Buffer + header_size , Size - header_size, &dest_mac, &src_mac); //This is the function called
}
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->dhost
和eth->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\u in_db()
函数,并在那里进行处理?在您使用的代码中提前感谢
struct ether_header *eth = (struct ether_header *)Buffer;
这只是指针转换(最初缓冲区是常量u_char
),而不是数据处理
如果您的函数接收字节数组,则需要转换为struct-ether\u头
。
因此,如果<代码>缓冲区< /代码>中的字节顺序对应于<代码> dButh<代码>(和/或<代码> SUST),请考虑使用 在代码中使用“”复制数据。
struct ether_header *eth = (struct ether_header *)Buffer;
这只是指针转换(最初缓冲区是常量u_char
),而不是数据处理
如果您的函数接收字节数组,则需要转换为struct-ether\u头
。
因此,如果<代码>缓冲区< /代码>中的字节顺序对应于<代码> dButh<代码>(和/或<代码> SUST),请考虑用 < P>复制数据。您定义了<代码> DestyMac < /C> >和<代码> SRCMAC 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
函数时,您可以只传递指针,而不传递指针的地址。我认为您应该阅读有关指针的内容。您将dest_mac
和src_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
函数时,您可以只传递指针,而不传递指针的地址。我认为您应该阅读有关指针的内容。与指针相关的错误有很多。总体建议是提高对指针工作原理的理解,并学习使用调试器
错误1:在函数handledata
中,参数src_mac
的类型应该是指针,而不是指向指针的指针
错误2:此代码:*dest\u mac=ð->dhost代码>将仅分配给dest\u mac
的第一个元素。此外,它将分配eth->dhost
的地址,而您希望分配值
这将正确工作,并且更简单,例如:
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;
dest_mac = eth->dhost;
src_mac = eth->shost;
handledata(Buffer + header_size , Size - header_size, dest_mac, src_mac);
}
有许多与指针相关的错误。总体建议是提高对指针工作原理的理解,并学习使用调试器
错误1:在函数handledata
中,参数src_mac
的类型应该是指针,而不是指向指针的指针
错误2:此代码:*dest\u mac=ð->dhost代码>将仅分配给dest\u mac
的第一个元素。此外,它将分配eth->dhost
的地址,而您希望分配值
这将正确工作,并且更简单,例如:
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;
dest_mac = eth->dhost;
src_mac = eth->shost;
handledata(Buffer + header_size , Size - header_size, dest_mac, src_mac);
}
unsigned char*dest\u mac[6]
-这是一个由6个char*
组成的数组,而您可能想要一个由6个字符组成的数组。@kfx my bad。修复了有关数组类型的错误,但问题仍然存在于读取无符号字符*dest_mac[6]
-这是一个由6个字符组成的数组*
,而您可能希望改为使用6个字符的数组。@kfx my bad。修复了有关数组类型的错误,但问题仍然是READ Yes,但这是因为以太标头
生成缓冲区中以字节为单位的数据结构。除非不这样做,否则获取dhost
和shost
的正确结构不会发生。@DevanshuMisra您可以使用struct ether\u header
作为函数参数的一种类型。。。这有什么问题?因为我必须在两个地方打印数据。缓冲区也有其他数据,这里不显示。所以memcpy会将整个缓冲区复制到这个结构中,这在我看来是不可能的。@NickS哇,我不这么认为。我想我可以试试。完成后将更新。谢谢但是这些值仍然要发送到insert_in_db()
函数。是的,但这是因为ether_头
在缓冲区中生成以字节为单位的数据结构。除非不这样做,否则获取dhost
和shost
的正确结构不会发生。@DevanshuMisra您可以使用struct ether\u header
作为函数参数的一种类型。。。这有什么问题?因为我必须在两个地方打印数据。缓冲区也有其他数据,这里不显示。所以memcpy会将整个缓冲区复制到t