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

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: #if ((_WIN32) && !(__CYGWIN__)) 
                     35: #include "../include/win32/config.h"
                     36: #else
                     37: #include "../include/config.h"
                     38: #endif
                     39: #endif
                     40: #include "./libnet_test.h"
                     41: 
                     42: int
                     43: main(int argc, char *argv[])
                     44: {
                     45:     int c;
1.1.1.2 ! misho      46:     uint32_t i;
1.1       misho      47:     libnet_t *l;
                     48:     libnet_ptag_t t;
                     49:     char *device = NULL;
1.1.1.2 ! misho      50:     uint8_t *packet;
        !            51:     uint32_t packet_s;
1.1       misho      52:     char errbuf[LIBNET_ERRBUF_SIZE];
                     53: 
                     54:     printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n"); 
                     55: 
                     56:     if (argc > 1)
                     57:     {
                     58:          device = argv[1];
                     59:     }
                     60: 
                     61:     l = libnet_init(
                     62:             LIBNET_LINK_ADV,                        /* injection type */
                     63:             device,                                 /* network interface */
                     64:             errbuf);                                /* errbuf */
                     65: 
                     66:     if (l == NULL)
                     67:     {
                     68:         fprintf(stderr, "%s", errbuf);
                     69:         exit(EXIT_FAILURE);
                     70:     }
                     71:        else
                     72: 
                     73:     i = libnet_get_ipaddr4(l);
                     74:   
                     75:     t = libnet_build_arp(
                     76:             ARPHRD_ETHER,                           /* hardware addr */
                     77:             ETHERTYPE_IP,                           /* protocol addr */
                     78:             6,                                      /* hardware addr size */
                     79:             4,                                      /* protocol addr size */
                     80:             ARPOP_REPLY,                            /* operation type */
                     81:             enet_src,                               /* sender hardware addr */
1.1.1.2 ! misho      82:             (uint8_t *)&i,                         /* sender protocol addr */
1.1       misho      83:             enet_dst,                               /* target hardware addr */
1.1.1.2 ! misho      84:             (uint8_t *)&i,                         /* target protocol addr */
1.1       misho      85:             NULL,                                   /* payload */
                     86:             0,                                      /* payload size */
                     87:             l,                                      /* libnet context */
                     88:             0);                                     /* libnet id */
                     89:     if (t == -1)
                     90:     {
                     91:         fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
                     92:         goto bad;
                     93:     }
                     94: 
                     95:     t = libnet_autobuild_ethernet(
                     96:             enet_dst,                               /* ethernet destination */
                     97:             ETHERTYPE_ARP,                          /* protocol type */
                     98:             l);                                     /* libnet handle */
                     99:     if (t == -1)
                    100:     {
                    101:         fprintf(stderr, "Can't build ethernet header: %s\n",
                    102:                 libnet_geterror(l));
                    103:         goto bad;
                    104:     }
                    105: 
                    106: 
                    107:     if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
                    108:     {
                    109:         fprintf(stderr, "%s", libnet_geterror(l));
                    110:     }
                    111:     else
                    112:     {
                    113:         fprintf(stderr, "packet size: %d\n", packet_s);
                    114:         libnet_adv_free_packet(l, packet);
                    115:     }
                    116: 
                    117:     c = libnet_write(l);
                    118: 
                    119:     if (c == -1)
                    120:     {
                    121:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    122:         goto bad;
                    123:     }
                    124:     else
                    125:     {
                    126:         fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; "
                    127:                 "check the wire.\n", c, libnet_cq_getlabel(l));
                    128:     }
                    129:     libnet_destroy(l);
                    130:     return (EXIT_SUCCESS);
                    131: bad:
                    132:     libnet_destroy(l);
                    133:     return (EXIT_FAILURE);
                    134: }
                    135: 
                    136: /* EOF */

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