Annotation of embedaddon/libnet/sample/arp.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.2   misho       2:  *  $Id: arp.c,v 1.7 2004/11/09 07:05:07 mike Exp $
1.1       misho       3:  *
                      4:  *  libnet 1.1
                      5:  *  Build an ARP packet
                      6:  *
                      7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      8:  *  All rights reserved.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  *
                     31:  */
                     32: 
                     33: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #include "./libnet_test.h"
                     37: 
                     38: int
                     39: main(int argc, char *argv[])
                     40: {
                     41:     int c;
1.1.1.2   misho      42:     uint32_t i;
1.1       misho      43:     libnet_t *l;
                     44:     libnet_ptag_t t;
                     45:     char *device = NULL;
1.1.1.2   misho      46:     uint8_t *packet;
                     47:     uint32_t packet_s;
1.1       misho      48:     char errbuf[LIBNET_ERRBUF_SIZE];
                     49: 
                     50:     printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n"); 
                     51: 
                     52:     if (argc > 1)
                     53:     {
                     54:          device = argv[1];
                     55:     }
                     56: 
                     57:     l = libnet_init(
                     58:             LIBNET_LINK_ADV,                        /* injection type */
                     59:             device,                                 /* network interface */
                     60:             errbuf);                                /* errbuf */
                     61: 
                     62:     if (l == NULL)
                     63:     {
                     64:         fprintf(stderr, "%s", errbuf);
                     65:         exit(EXIT_FAILURE);
                     66:     }
                     67:        else
                     68: 
                     69:     i = libnet_get_ipaddr4(l);
                     70:   
                     71:     t = libnet_build_arp(
                     72:             ARPHRD_ETHER,                           /* hardware addr */
                     73:             ETHERTYPE_IP,                           /* protocol addr */
                     74:             6,                                      /* hardware addr size */
                     75:             4,                                      /* protocol addr size */
                     76:             ARPOP_REPLY,                            /* operation type */
                     77:             enet_src,                               /* sender hardware addr */
1.1.1.2   misho      78:             (uint8_t *)&i,                         /* sender protocol addr */
1.1       misho      79:             enet_dst,                               /* target hardware addr */
1.1.1.2   misho      80:             (uint8_t *)&i,                         /* target protocol addr */
1.1       misho      81:             NULL,                                   /* payload */
                     82:             0,                                      /* payload size */
                     83:             l,                                      /* libnet context */
                     84:             0);                                     /* libnet id */
                     85:     if (t == -1)
                     86:     {
                     87:         fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
                     88:         goto bad;
                     89:     }
                     90: 
                     91:     t = libnet_autobuild_ethernet(
                     92:             enet_dst,                               /* ethernet destination */
                     93:             ETHERTYPE_ARP,                          /* protocol type */
                     94:             l);                                     /* libnet handle */
                     95:     if (t == -1)
                     96:     {
                     97:         fprintf(stderr, "Can't build ethernet header: %s\n",
                     98:                 libnet_geterror(l));
                     99:         goto bad;
                    100:     }
                    101: 
                    102: 
                    103:     if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
                    104:     {
                    105:         fprintf(stderr, "%s", libnet_geterror(l));
                    106:     }
                    107:     else
                    108:     {
                    109:         fprintf(stderr, "packet size: %d\n", packet_s);
                    110:         libnet_adv_free_packet(l, packet);
                    111:     }
                    112: 
                    113:     c = libnet_write(l);
                    114: 
                    115:     if (c == -1)
                    116:     {
                    117:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    118:         goto bad;
                    119:     }
                    120:     else
                    121:     {
                    122:         fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; "
                    123:                 "check the wire.\n", c, libnet_cq_getlabel(l));
                    124:     }
                    125:     libnet_destroy(l);
                    126:     return (EXIT_SUCCESS);
                    127: bad:
                    128:     libnet_destroy(l);
                    129:     return (EXIT_FAILURE);
                    130: }
                    131: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>