近期工作接触到了些 linux 在 OSI 二层(数据链路层)使用 ethernet 协议收发数据包的内容,网上搜罗了一些有用的文档:
- https://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/
- https://www.binarytides.com/raw-sockets-c-code-linux/
- https://squidarth.com/networking/systems/rc/2018/05/28/using-raw-sockets.html
- https://stackoverflow.com/questions/14774668/what-is-raw-socket-in-socket-programming
- https://www.tenouk.com/Module43a.html
- 推荐: https://www.opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/
- 推荐: https://github.com/xgfone/snippet/blob/master/snippet/docs/linux/program/raw-socket.md
- 推荐: https://man7.org/linux/man-pages/man7/packet.7.html
- 推荐(上面链接的中文翻译): https://manpages.debian.org/unstable/manpages-zh/packet.7.zh_CN.html
另外有一些第三方库也可以做到处理链路层数据:
- libpcap 第三方 c 开发文档: https://www.devdungeon.com/content/using-libpcap-c
- libpcap 官方 c 开发文档: https://www.tcpdump.org/pcap.html
- libpcap: https://ruofeng.me/2018/04/23/use-libpcap-to-send-and-capture/
- libnet 库: https://github.com/libnet/libnet
- libnet 库: https://codedocs.xyz/libnet/libnet/globals.html
下面是我对 linux man 手册 packet(7) 的翻译,虽然上面已有相关中文翻译的链接,但我个人认为有部分相关术语使用得不太合适,另外我自己再翻译一边也可以加深我个人理解。
NAME
packet - 设备层的 packet 接口
SYNOPSIS 概要
1 | #include <sys/socket.h> |