1

作成したフレームを送信する方法を知りたいです。

私は次のものを持っています:

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

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 パケットを作成するコードを見つけました。「送信」コマンドが見つからないため、リターン以外はすべて理解しています。

ありがとうございました。

4

1 に答える 1

0

libnet_write() がそれを行います。

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 のコメントに従って

于 2014-07-11T13:31:24.013 に答える