- 论坛徽章:
- 0
|
请教一个关于在网络层通信的问题。
需要在网络层对数据包进行数据加密和解密处理,我了解到raw socket 可以在传输层以下工作,但还是有几个疑问,希望得到帮助,谢谢。
1. raw socket 是否支持ipv6,在网络层 发送和接收数据包?(IPv4的可以实现,但是IPv6编程不熟悉,没有尝试。)
2. libpcap 关于libpcap又很多疑问,以前我一直以为pcap就是packet capture,发送数据包是通过libnet之类的库,但是我简单查看了thc-ipv6-tool的代码,它是基于libpcap实现的,其中也有这样的代码:- int thc_send_pkt(char *interface, unsigned char *pkt, int *pkt_len) {
- struct sockaddr sa;
- thc_ipv6_hdr *hdr = (thc_ipv6_hdr *) pkt;
- if (pkt == NULL || hdr->pkt == NULL || hdr->pkt_len < 1 || hdr->pkt_len > 65535)
- return -2;
- if (interface == NULL)
- interface = default_interface;
- strcpy(sa.sa_data, interface);
- if (thc_socket < 0)
- thc_socket = thc_open_ipv6();
- if (thc_socket < 0 && geteuid() != 0) {
- fprintf(stderr, "Error: Program must be run as root.\n");
- exit(-1);
- }
- if (debug)
- thc_dump_data(hdr->pkt, hdr->pkt_len, "Sent Packet");
- if ((_thc_ipv6_rawmode > 0 && hdr->pkt_len > thc_get_mtu(interface)) || (_thc_ipv6_rawmode == 0 && hdr->pkt_len > thc_get_mtu(interface) + 14)) {
- if (_thc_ipv6_showerrors)
- fprintf(stderr, "Warning: packet size is larger than MTU of interface (%d > %d)!\n", hdr->pkt_len, thc_get_mtu(interface));
- if (thc_get_mtu(interface) == -1) {
- if (_thc_ipv6_showerrors)
- fprintf(stderr, "Error: interface invalid\n");
- exit( -1);
- }
- }
- return sendto(thc_socket, hdr->pkt, hdr->pkt_len, 0, &sa, sizeof(sa));
- }
复制代码 通过调用了sendto()这样的函数达到发送的目的,这个函数应该是socket中的函数,而thc_socket是通过thc_open_ipv6()建立的,这个函数内容很多,但是又部分如下代码:- if (_thc_ipv6_rawmode)
- return socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)); // XXX BUG TODO FIXME : no this is not working.
- else
- return socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP));
- }
复制代码 非常非常奇怪,因为我刚刚查到了libpcap中也有发送数据包的函数:- pcap_inject,调用p->inject_op,pcap_inject_linux,send发送数据;
复制代码 为什么作者发送数据包的时候使用的是socket,而不使用libpcap呢?这样libpcap和socket混着使用是为什么?是不是libpcap中的pcap_inject函数不支持IPv6?(没有查)
另外,我对socket和pcap都一知半解,二者是什么关系?希望大家帮助,非常感谢。 |
|