参考文章:
一、libnet库下载
二、libnet库交叉编译安装
-
配置交叉编译环境
普通用户和root用户下都需要配置 -
从 GitHub下载最新版本 libnet-master.tar.gz 或 libnet-1.2.tar.gz,解压缩到当前目录:
tar -xzvf ./libnet-master.tar.gz -C ./
-
使用 ./autogen.sh 生成 configure 脚本
需要的工具套件:autoconf (>=2.69)、automake (>=1.14)、libtool (>=2.4.2)
(1) 安装工具:sudo apt install autoconf automake libtool
(2) 进入libnet-master目录,生成 configure 脚本
cd libnet-master/
./autogen.sh
说明:如果下载的是 Releases版本(如:libnet-1.2.tar.gz),不需要此步骤。
-
配置安装目录和交叉编译环境
./configure --prefix=xxx/xxx/install/ --host=arm-linux-gnueabihf
配置结果:
-
编译
make
-
安装
sudo make install
安装结果:
在 install/lib/ 目录下生成如下文件:
-
错误说明
如果报如下错误,是因为root用户下未配置交叉编译环境,配置后即可
../libtool: line 1719: arm-linux-gnueabihf-ranlib: command not found
-
cd 进入 install 安装目录,打包lib目录下动态库文件(libnet.so libnet.so.9 libnet.so.9.0.0)
tar -zcvf libnet-1.2-install.tar.gz -C ./lib libnet.so libnet.so.9 libnet.so.9.0.0
-
将 libnet-1.2-install.tar.gz 压缩包拷贝到开发板上,解压
新建文件夹:/usr/local/lib/libnet , 然后解压到该文件夹中sudo mkdir /usr/local/lib/libnet
sudo tar -zxvf libnet-1.2-install.tar.gz -C /usr/local/lib/libnet
-
开发板上添加库文件搜索路径
打开ld.so.conf文件sudo vi /etc/ld.so.conf.d/libc.conf
在 /etc/ld.so.conf 文件中添加库的搜索路径
/usr/local/lib/libnet //根据自己的库路径添加
然后
ldconfig
生成/etc/ld.so.cache,ldconfig -v
查看ldconfig
三、应用程序交叉编译
交叉编译应用程序:需要加 -lnet 选项,并指定头文件及动态库路径
arm-linux-gnueabihf-gcc ./libnet_test.c -o ./libnet_test -lnet -I/xxx/include/ -L/xxx/lib/
-
查看头文件及动态库路径
Libnet 安装为一个库和一组包含文件。在您的程序中使用的主要包含文件是:#include <libnet.h>
要获得头文件和库文件的正确搜索路径,请使用标准pkg-config工具:
pkg-config --libs --static --cflags libnet
结果:
-I/usr/local/include -L/usr/local/lib -lnet
/usr/local/此处显示的路径为默认值。configure时,可以使用 --prefix 选项指定不同的路径。
-
编译需要添加 -lnet 选项
gcc test.c -o test -lnet
-
基于 GNU autotools 的项目,请在以下内容中使用
configure.ac
:# Check for required libraries PKG_CHECK_MODULES([libnet], [libnet >= 1.2])
并在您的
Makefile.am
:proggy_CFLAGS = $(libnet_CFLAGS) proggy_LDADD = $(libnet_LIBS)
四、Ubuntu系统安装 libnet(非交叉编译)
-
libnet 的安装
sudo apt-get install libnet1-dev
-
应用程序编译
gcc libnet_test.c -o libnet_test -lnet
五、libnet使用
参考:
六、开发板上测试
使用libnet库发送udp包测试程序(libnet_test.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libnet.h>
int main(int argc, char *argv[])
{
char send_msg[1000] = "";
char err_buf[100] = "";
libnet_t *lib_net = NULL;
int lens = 0;
libnet_ptag_t lib_t = 0;
unsigned char src_mac[6] = {0x00,0x0a,0x35,0x00,0x10,0x01}; //发送者网卡地址
unsigned char dst_mac[6] = {0xa4,0xbb,0x6d,0xc3,0x1d,0xce}; //接收者网卡地址
char *src_ip_str = "192.168.10.10"; //源主机IP地址
char *dst_ip_str = "192.168.10.201"; //目的主机IP地址
unsigned long src_ip,dst_ip = 0;
lens = sprintf(send_msg, "%s", "this is for the udp test");
lib_net = libnet_init(LIBNET_LINK_ADV, "eth0", err_buf); //初始化
if(NULL == lib_net)
{
perror("libnet_init");
exit(-1);
}
src_ip = libnet_name2addr4(lib_net,src_ip_str,LIBNET_RESOLVE); //将字符串类型的ip转换为顺序网络字节流
dst_ip = libnet_name2addr4(lib_net,dst_ip_str,LIBNET_RESOLVE);
lib_t = libnet_build_udp( //构造udp数据包
8080,
8080,
8+lens,
0,
send_msg,
lens,
lib_net,
0
);
lib_t = libnet_build_ipv4( //构造ip数据包
20+8+lens,
0,
500,
0,
10,
17,
0,
src_ip,
dst_ip,
NULL,
0,
lib_net,
0
);
lib_t = libnet_build_ethernet( //构造以太网数据包
(u_int8_t *)dst_mac,
(u_int8_t *)src_mac,
0x800,// 或者,ETHERTYPE_IP
NULL,
0,
lib_net,
0
);
int res = 0;
res = libnet_write(lib_net); //发送数据包
if(-1 == res)
{
perror("libnet_write");
exit(-1);
}
libnet_destroy(lib_net); //销毁资源
printf("----ok-----\n");
return 0;
}
交叉编译:
arm-linux-gnueabihf-gcc ./libnet_test.c -o ./libnet_test -lnet -I/home/osrc/Projects/tools/libnet/install/include/ -L/home/osrc/Projects/tools/libnet/install/lib
在开发板上运行,需要root账户权限:
sudo ./libnet_test
在PC机上使用网络调试助手接收udp数据,结果如下: