using libnet to send igmp join packet.

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "libnet.h"

#include <assert.h>
int
main(int argc, char *argv[])
{
    libnet_t *l;
    int r;
    char *device = "eth0";
    char errbuf[LIBNET_ERRBUF_SIZE];
    libnet_ptag_t ip_ptag = 0;
    libnet_ptag_t eth_ptag = 0;
    int pkt1_payload = 10;
    uint8_t* pkt1 = NULL;
    uint32_t pkt1_sz = 0;
    struct libnet_ipv4_hdr* h1;
    int pkt2_payload = 2;
    uint8_t* pkt2 = NULL;
    uint32_t pkt2_sz = 0;
    struct libnet_ipv4_hdr* h2;

    l = libnet_init( LIBNET_RAW4, device, errbuf);


    assert(l);

    printf("Packet 1:\n");

    u_long dst_ip;
    char* dst = "224.1.1.1";
    if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == -1)
    {
        fprintf(stderr, "Bad destination IP address: %s\n", dst);
        exit(EXIT_FAILURE);
    }
        u_long src_ip;
    char* src = "192.168.100.10";
    if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == -1)
    {
        fprintf(stderr, "Bad destination IP address: %s\n", dst);
        exit(EXIT_FAILURE);
    }

    int ret = libnet_build_igmp(IGMP_V2_MEMBERSHIP_REPORT, 0, 0, dst_ip, NULL, 0, l, 0);
    if(ret == -1)
    {
        printf("build failed\n");
    }

    /*
    *  Write it to the wire.
    */


        r = libnet_build_ipv4(
            LIBNET_IPV4_H + LIBNET_IGMP_H , /* length */
            0,                                    /* TOS */
            0x42,                                 /* IP ID */
            0,                                    /* IP Frag */
            64,                                   /* TTL */
            IPPROTO_IGMP,                         /* protocol */
            0,                                    /* checksum */
            src_ip,                               /* source IP */
            dst_ip,                               /* destination IP */
            NULL,                              /* payload */
            0,                            /* payload size */
            l,                                    /* libnet handle */
            0);
        if (r == -1)
        {
            fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
            goto bad;
        }


    printf("begin write\n");
    r = libnet_write(l);
    if (r == -1)
    {
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        goto bad;
    }
    else
    {
        fprintf(stderr, "Wrote %d byte IP packet; check the wire.\n", r);
    }

    libnet_destroy(l);
    return (EXIT_SUCCESS);
    bad:
        libnet_destroy(l);
    return (EXIT_FAILURE);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值