Annotation of embedaddon/libnet/sample/hsrp.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *
        !             3:  *  libnet 1.1.2
        !             4:  *  Build a HSRP packet
        !             5:  *
        !             6:  *  Copyright (c) 2004 David Barroso Berrueta <tomac@wasahero.org>
        !             7:  *  All rights reserved.
        !             8:  *
        !             9:  * Redistribution and use in source and binary forms, with or without
        !            10:  * modification, are permitted provided that the following conditions
        !            11:  * are met:
        !            12:  * 1. Redistributions of source code must retain the above copyright
        !            13:  *    notice, this list of conditions and the following disclaimer.
        !            14:  * 2. Redistributions in binary form must reproduce the above copyright
        !            15:  *    notice, this list of conditions and the following disclaimer in the
        !            16:  *    documentation and/or other materials provided with the distribution.
        !            17:  *
        !            18:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            19:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            20:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            21:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            22:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            23:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            24:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            25:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            26:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            27:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            28:  * SUCH DAMAGE.
        !            29:  *
        !            30:  */
        !            31: 
        !            32: #if (HAVE_CONFIG_H)
        !            33: #if ((_WIN32) && !(__CYGWIN__)) 
        !            34: #include "../include/win32/config.h"
        !            35: #else
        !            36: #include "../include/config.h"
        !            37: #endif
        !            38: #endif
        !            39: #include "./libnet_test.h"
        !            40: 
        !            41: 
        !            42: int
        !            43: main(int argc, char *argv[])
        !            44: {
        !            45:     uint8_t version = LIBNET_HSRP_VERSION;
        !            46:     uint8_t opcode = LIBNET_HSRP_TYPE_HELLO;
        !            47:     uint8_t state = LIBNET_HSRP_STATE_ACTIVE;
        !            48:     uint8_t hello_time = 3;
        !            49:     uint8_t hold_time = 10;
        !            50:     uint8_t priority = 1;
        !            51:     uint8_t group = 1;
        !            52:     uint8_t reserved = 0;
        !            53:     uint8_t authdata[8];
        !            54: 
        !            55:     libnet_t *l;
        !            56:     char *device = NULL;
        !            57:     char src[] = "1.1.1.1";
        !            58:     char *eth_dst = "DE:AD:00:00:BE:EF";
        !            59:     char dst[] = "224.0.0.2";
        !            60:     int port = 1985;
        !            61:     int c;
        !            62:     u_long src_ip, dst_ip;
        !            63:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            64:     libnet_ptag_t ptag = 0;
        !            65: 
        !            66:     printf("libnet 1.1.2 packet shaping: HSRP[link]\n"); 
        !            67: 
        !            68:     /*
        !            69:      *  Initialize the library.  Root priviledges are required.
        !            70:      */
        !            71:     l = libnet_init(
        !            72:            LIBNET_LINK_ADV,                        /* injection type */
        !            73:            device,                                 /* network interface */
        !            74:             errbuf);                                /* error buffer */
        !            75: 
        !            76:     if (l == NULL)
        !            77:     {
        !            78:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !            79:         exit(EXIT_FAILURE); 
        !            80:     }
        !            81: 
        !            82:     printf("Using device %s\n", l->device);
        !            83: 
        !            84:     if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == (u_long)-1)
        !            85:     {
        !            86:        fprintf(stderr, "Bad destination IP address: %s\n", dst);
        !            87:        exit(EXIT_FAILURE);
        !            88:     }
        !            89:     
        !            90:     if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == (u_long)-1)
        !            91:     {
        !            92:        fprintf(stderr, "Bad source IP address: %s\n", src);
        !            93:        exit(EXIT_FAILURE);
        !            94:     }
        !            95: 
        !            96:     memset(authdata, 0, 8);
        !            97:     strncpy(authdata, "cisco", 5);
        !            98: 
        !            99: 
        !           100:     ptag = libnet_build_hsrp(
        !           101:        version,
        !           102:        opcode,
        !           103:        state,
        !           104:        hello_time,                        /* Recommended hello time */
        !           105:        hold_time,                       /* Recommended hold time */
        !           106:        priority,                        /* Priority */
        !           107:        group,                        /* Group */
        !           108:        reserved,                        /* Reserved */
        !           109:        authdata,                  /* Password */
        !           110:         src_ip,                   /* Virtual IP */
        !           111:         NULL,
        !           112:         0,
        !           113:        l,
        !           114:        0
        !           115:        );
        !           116: 
        !           117:     if (ptag == -1)
        !           118:     {
        !           119:        fprintf(stderr, "Can't build HSRP header: %s\n", libnet_geterror(l));
        !           120:        goto bad;
        !           121:     }
        !           122: 
        !           123:     ptag = libnet_build_udp(
        !           124:        port,                                      /* source port */
        !           125:        port,                                      /* destination port */
        !           126:        LIBNET_UDP_H + LIBNET_HSRP_H ,             /* packet length */
        !           127:        0,                                         /* checksum */
        !           128:        NULL,                                      /* payload */
        !           129:        0,                                         /* payload size */
        !           130:        l,                                         /* libnet handle */
        !           131:        0);                                        /* libnet id */
        !           132: 
        !           133:     if (ptag == -1)
        !           134:     {
        !           135:        fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
        !           136:        goto bad;
        !           137:     }
        !           138: 
        !           139:     ptag = libnet_build_ipv4(
        !           140:        LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_HSRP_H,/* length */
        !           141:        0,                                          /* TOS */
        !           142:        666,                                        /* IP ID */
        !           143:        0,                                          /* IP Frag */
        !           144:        1 ,                                         /* TTL */
        !           145:        IPPROTO_UDP,                                /* protocol */
        !           146:        0,                                          /* checksum */
        !           147:        src_ip,                                     /* source IP */
        !           148:        dst_ip,                                     /* destination IP */
        !           149:        NULL,                                       /* payload */
        !           150:        0,                                          /* payload size */
        !           151:        l,                                          /* libnet handle */
        !           152:        0);                                         /* libnet id */
        !           153:     
        !           154:     if (ptag == -1)
        !           155:     {
        !           156:        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
        !           157:        exit(EXIT_FAILURE);
        !           158:     }
        !           159: 
        !           160:     
        !           161:     eth_dst = libnet_hex_aton(eth_dst, &c);
        !           162:     ptag = libnet_autobuild_ethernet(
        !           163:        eth_dst,                                /* ethernet destination */
        !           164:        ETHERTYPE_IP,                           /* protocol type */
        !           165:        l);                                     /* libnet handle */
        !           166: 
        !           167:     free(eth_dst);
        !           168:     if (ptag == -1)
        !           169:     {
        !           170:         fprintf(stderr, "Can't build ethernet header: %s\n",
        !           171:                 libnet_geterror(l));
        !           172:         goto bad;
        !           173:     }
        !           174: 
        !           175: 
        !           176:     /*
        !           177:      * write to the wire
        !           178:      */
        !           179:     c = libnet_write(l);
        !           180:     if (c == -1)
        !           181:     {
        !           182:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           183:         goto bad;
        !           184:     }
        !           185:     else
        !           186:     {
        !           187:         fprintf(stderr, "Wrote %d byte HSRP packet; check the wire.\n", c);
        !           188:     }
        !           189:     libnet_destroy(l);
        !           190:     return (EXIT_SUCCESS);
        !           191:   bad:
        !           192:     libnet_destroy(l);
        !           193:     return (EXIT_FAILURE);
        !           194: 
        !           195:     return 0;
        !           196: }

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