Linux网络编程:libnet 移植及使用

参考文章:

  1. libnet库下载、编译、示例、文档
  2. Linux 网络编程—— libnet 使用指南
  3. libnet 函数列表

一、libnet库下载

  1. https://github.com/libnet/libnet/releases

二、libnet库交叉编译安装

  1. 配置交叉编译环境
    普通用户和root用户下都需要配置

  2. 从 GitHub下载最新版本 libnet-master.tar.gz 或 libnet-1.2.tar.gz,解压缩到当前目录:

     tar -xzvf ./libnet-master.tar.gz -C ./ 
    
  3. 使用 ./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),不需要此步骤。

  4. 配置安装目录和交叉编译环境

    ./configure --prefix=xxx/xxx/install/ --host=arm-linux-gnueabihf
    

    配置结果:在这里插入图片描述

  5. 编译

    make
    
  6. 安装

    sudo make install
    

    安装结果:在这里插入图片描述
    在 install/lib/ 目录下生成如下文件:
    在这里插入图片描述

  7. 错误说明
    如果报如下错误,是因为root用户下未配置交叉编译环境,配置后即可
    ../libtool: line 1719: arm-linux-gnueabihf-ranlib: command not found

  8. 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
    
  9. 将 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
    
  10. 开发板上添加库文件搜索路径
    打开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/
  1. 查看头文件及动态库路径
    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 选项指定不同的路径。

  2. 编译需要添加 -lnet 选项

    gcc test.c -o test -lnet
    
  3. 基于 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(非交叉编译)

  1. libnet 的安装

    sudo apt-get install libnet1-dev
    
  2. 应用程序编译

    gcc libnet_test.c -o libnet_test -lnet
    

五、libnet使用

参考:

  1. Linux 网络编程—— libnet 使用指南
  2. 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数据,结果如下:
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值