首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libnet在C中创建arp数据包

使用libnet在C中创建arp数据包
EN

Stack Overflow用户
提问于 2012-12-12 20:03:39
回答 1查看 1.6K关注 0票数 1

我想知道如何发送帧一旦我创建了它。

我有下一条:

int arp_send(libnet_t *l,int op,u_char *sha,in_addr_t spa,u_char *tha,in_addr_t tpa) {

代码语言:javascript
复制
 libnet\_ptag\_t t;    
代码语言:javascript
复制
if (sha == NULL &&
    (sha = (u_char *)libnet_get_hwaddr(l)) == NULL) {
    return (-1);
}
if (spa == 0) {
    if ((spa = libnet_get_ipaddr4(l)) == -1)
        return (-1);        
}
if (tha == NULL)
    tha = (u_char *)"\xff\xff\xff\xff\xff\xff";

libnet_clear_packet(l);

/*
 *  Build the packet, remmebering that order IS important.  We must
 *  build the packet from lowest protocol type on up as it would
 *  appear on the wire.  So for our ARP packet:
 *
 *  -------------------------------------------
 *  |  Ethernet   |           ARP             |
 *  -------------------------------------------
 *         ^                     ^
 *         |------------------   |
 *  libnet_build_ethernet()--|   |
 *                               |
 *  libnet_build_arp()-----------|
 */

t = libnet_build_arp(
        ARPHRD_ETHER,                           /* hardware addr */
        ETHERTYPE_IP,                           /* protocol addr */
        6,                                      /* hardware addr size */
        4,                                      /* protocol addr size */
        op,                                     /* operation type */
        sha,                                    /* sender hardware addr */
        (u_int8_t *)&spa,                       /* sender protocol addr */
        tha,                                    /* target hardware addr */
        (u_int8_t *)&tpa,                       /* target protocol addr */
        NULL,                                   /* payload */
        0,                                      /* payload size */
        l,                                      /* libnet context */
        0);                                     /* libnet id */

if (t == -1)
{
    fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
    return -1;
}

t = libnet_autobuild_ethernet(
        tha,                                    /* ethernet destination */
        ETHERTYPE_ARP,                          /* protocol type */
        l);                                     /* libnet handle */

if (t == -1)
{
    fprintf(stderr, "Can't build ethernet header: %s\n",
            libnet_geterror(l));
    return -1;
}    
return libnet_write(l);         

}

我在网上找到了这段代码,它从无到有创建了一个arp数据包。我确实理解除了返回之外的所有东西,因为我找不到"send“命令。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-07-11 21:31:24

libnet_write()可以做到这一点。

代码语言:javascript
复制
Writes a prebuilt packet to the network. The function assumes that l was
previously initialized (via a call to libnet_init()) and that a
previously constructed packet has been built inside this context (via one or
more calls to the libnet_build* family of functions) and is ready to go.
Depending on how libnet was initialized, the function will write the packet
to the wire either via the raw or link layer interface. The function will
also bump up the internal libnet stat counters which are retrievable via
libnet_stats().
  @param l pointer to a libnet context
  @return the number of bytes written, -1 on error

根据libnet/libnet-functions.h中的注释

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13839373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档