payload_s:负载长度,或为 0(这里通常写 0 )
l:libnet 句柄,libnet_init() 返回的 libnet * 指针
ptag:协议标记,第一次组新的发送包时,这里写 0,同一个应用程序,下一次再组包时,这个位置的值写此函数的返回值 。
返回值:
成功:协议标记libnet_ptag_t libnet_build_ethernet(
失败:-1
u_int8_t*dst, u_int8_t *src,功能:
u_int16_ttype, u_int8_t*payload,
u_int32_tpayload_s, libnet_t*l,
libnet_ptag_t ptag );
构造一个以太网数据包参数:
dst:目的 mac返回值:
src:源 mac
type:上层协议类型
payload:负载,即附带的数据,可设置为 NULL(这里通常写 NULL)
payload_s:负载长度,或为 0(这里通常写 0 )
l:libnet 句柄,libnet_init() 返回的 libnet * 指针
ptag:协议标记,第一次组新的发送包时,这里写 0,同一个应用程序,下一次再组包时,这个位置的值写此函数的返回值 。
成功:协议标记int libnet_write(libnet_t * l);
失败:-1
功能:
发送数据包参数:
l:libnet 句柄,libnet_init() 返回的 libnet * 指针返回值:
成功:发送数据包的长度使用实例
失败:返回 -1
#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,0x0c,0x29,0x97,0xc7,0xc1};//发送者网卡地址00:0c:29:97:c7:c1unsigned char dst_mac[6] = {0x74,0x27,0xea,0xb5,0xff,0xd8};//接收者网卡地址74-27-EA-B5-FF-D8char *src_ip_str = "192.168.31.163"; //源主机IP地址char *dst_ip_str = "192.168.31.248"; //目的主机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_IPNULL,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;编译代码时,需要加上 -lnet:
文章插图
推荐阅读
- 掌握Linux文件权限,看这篇就够了
- Linux 文件查找与编辑命令集合
- 机器学习之恶意流量检测的特征工程
- 月球背面之谜 人类为什么看不见月球的背面
- 俄罗斯谢尔盖穿越事件 前苏联谢尔盖穿越之谜
- 经典传奇成吉思汗陵墓之谜 成吉思汗陵墓之谜
- 养生|养生指南事业定位——有心之人皆因用心而有所成
- 埃及人面狮身像的鼻子是被谁炸毁的 埃及狮身人面像之谜
- 霍山黄芽之名人笔下,霍山黄芽品赏先容
- 行走茶山之景谷黄草坝,六大茶山
