下面是我试图通读的代码片段。
swap_linux_sll_header(const struct pcap_pkthdr *hdr, u_char *buf)
{
u_int caplen = hdr->caplen;
u_int length = hdr->len;
struct sll_header *shdr = (struct sll_header *)buf;
uint16_t protocol;
pcap_can_socketcan_hdr *chdr;
if (caplen < (u_int) sizeof(struct sll_header) ||
length < (u_int) sizeof(struct sll_header)) {
/* Not enough data to have the protocol field */
return;
}发布于 2020-06-13 10:55:08
这是一个简单的类型转换。buf最初作为指向u_char的指针传递给函数,但在函数内部,它需要作为指向sll_header结构的指针使用和检查/操作。
当缓冲区作为原始字节序列获得时,这是很常见的,可能它是从介质或网络中读取的,然后被传递给一个函数,该函数理解它所表示的底层结构(例如,IP数据包)并理解它。
如果没有类型大小写,您将收到编译器警告。
https://stackoverflow.com/questions/62354875
复制相似问题