博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用libnet 发送自定义的以太网帧
阅读量:3559 次
发布时间:2019-05-20

本文共 1406 字,大约阅读时间需要 4 分钟。

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

//libnet

#include <libnet.h>

static int sendLibnetLink()
{
    int packet_size = 0;
    libnet_t *l = NULL;
    char *device = "eth0";
    char *destination_ip_str = "192.168.8.1";
    char *source_ip_str = "192.168.8.83";
    libnet_ptag_t eth_ptag = 0;
        
    unsigned char src_mac[ETH_ALEN] = {0x00, 0xe0, 0x4c, 0x3d, 0x59, 0xdc};
    unsigned char dst_mac[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    char error_information[LIBNET_ERRBUF_SIZE];
    l = libnet_init(LIBNET_LINK, device, error_information);
    printf("Using device %s\n", l->device);
    if (l == NULL)
    {
        fprintf(stderr, "libnet_init() failed: %s", error_information);
        exit(EXIT_FAILURE); 
    }
    eth_ptag = libnet_build_ethernet(
        dst_mac,                                    /* ethernet destination */
        src_mac,                                    /* ethernet source */
        (0x8033),                                   /* protocol type */
        "hello", //senddata,                                       /* payload */
        strlen("hello"), //sizeof(senddata),                                          /* payload size */
        l,                                          /* libnet handle */
        0);                                         /* libnet id */
    if (eth_ptag == -1)
    {
        fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
        goto bad;
    }
    /*
     *  Write it to the wire.
     */
    int c = libnet_write(l);
    if (c == -1)
    {
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        goto bad;
    }
    else
    {
        fprintf(stderr, "Wrote %d byte IP packet; check the wire.\n", c);
    }
    libnet_destroy(l);
    return (EXIT_SUCCESS);
bad:
    libnet_destroy(l);
    return (EXIT_FAILURE);
}

//compile

#gcc link.c -o link -lnet

//run

#./link

转载地址:http://tsprj.baihongyu.com/

你可能感兴趣的文章
JavaEE Bean的两种常用作用域 singleton(单例)和prototype(原型)
查看>>
MySQL 数据库索引
查看>>
JavaEE Spring与MyBatis的整合之传统DAO方式整合(教材学习笔记)
查看>>
JavaEE MyBatis与Spring的整合——基于mapper接口方式开发(教材学习笔记)
查看>>
JavaWeb 使用Cookie实现——显示用户上次访问时间(教材学习笔记)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(五)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(六)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(七)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(八)
查看>>
中国大学MOOC—陆军工程大学数据结构MOOC习题集(2018秋)7-3 中位数
查看>>
Java发送邮件 注册成功发送邮件
查看>>
Mybatis的简单使用(增删改查),解决数据库字段名和实体类映射属性名不一致的问题
查看>>
Mybatis配置log4j文件 分页查询(limit,rowBounds)
查看>>
Mysql利用注解进行开发
查看>>
Mybatis一对多查询,多对一查询
查看>>
Spring配置bean.xml文件的头目录模板
查看>>
代理模式之------动态代理
查看>>
Spring实现AOP的三种方式
查看>>
Mybatis-Spring简单的配置和使用,配置事务
查看>>
SpringMVC和Mybatis整合使用的配置文件
查看>>