我遇到了一些无法正确编译的函数的问题。它们几乎是从一个示例程序中借来的,以便更好地了解网络编程的工作原理,并看看我是否可以使用 RAW 套接字建立一个可靠的 TCP 连接(3 次握手)。这些是功能:
void recieve(u_char *args, const struct pcap_pkthdr *pkthdr, const u_char *buffer)
{
const int one = 1;
u_char *ptr;
int LEN = args; /* FIRST WARNING POINTS HERE */
struct ipheader *ip;
struct tcpheader *tcp;
ip = (struct ipheader *)(buffer + LEN);
tcp = (struct tcpheader *)(buffer + LEN + sizeof (struct ipheader));
printf("%d\n", LEN);
printf("Packet recieved. ACK number: %d\n", ntohl (tcp->tcph_seqnum));
printf("Packet recieved. SEQ number: %d\n", nthol (tcp->tcph_acknum));
s_seq = nthol (tcp->tcph_seqnum);
send_syn_ack(s_seq, dip, sip, dport, sport);
sleep(100);
}
和
void capture()
{
pcap_t *pd;
bpf_u_int32 netmask;
bpf_u_int32 localnet;
char *filter = ("ip dest host %s", dstip); /* SECOND WARNING POINTS HERE */
char *dev = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program filterprog;
int dl = 0, dl_len = 0;
if ((pd = pcap_open_live(dev, 1514, 1, 500, errbuf)) == NULL)
{
fprintf(stderr, "cannot open device %s: %s\n", dev, errbuf);
exit(1);
}
pcap_lookupnet(dev, &localnet, &netmask, errbuf);
pcap_compile(pd, &filterprog, filter, 0, localnet);
if (pcap_setfilter(pd, &filterprog) == - 1)
{
fprintf(stderr, "cannot set pcap filter %s: %s\n", filter, errbuf);
exit(1);
}
pcap_freecode(&filterprog);
dl = pcap_datalink(pd);
switch(dl) {
case 1:
dl_len = 14;
break;
default:
dl_len = 14;
break;
}
if (pcap_loop(pd, -1, recieve, (u_char *)dl_len) < 0) { /* LAST WARNING HERE */
fprintf(stderr, "cannot get raw packet: %s\n", pcap_geterr(pd));
exit(1);
}
}
这些是错误消息:
In function ‘recieve’:
warning: initialization makes integer from pointer without a cast
In function ‘capture’:
warning: initialization makes pointer from integer without a cast
Warning: cast to pointer from integer of different size
即使在示例程序中,我也会收到第一个和最后一个警告。这两个功能有什么好处吗?是否有更简单、更清洁的方法来实现它们的目的?如果没有,我应该怎么做才能摆脱这些警告?谢谢 :)