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

1.1     ! misho       1: /*
        !             2:  *  $Id: cdp.c,v 1.2 2004/01/03 20:31:01 mike Exp $
        !             3:  *
        !             4:  *  libnet 1.1
        !             5:  *  Build an CDP 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, len;
        !            42:     libnet_t *l;
        !            43:     libnet_ptag_t t;
        !            44:     u_char *value;
        !            45:     u_char values[100];
        !            46:     u_short tmp;
        !            47:     u_long tmp2;
        !            48:     char *device = NULL;
        !            49:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            50: 
        !            51:     printf("libnet 1.1 packet shaping: CDP[link]\n"); 
        !            52:     /*
        !            53:      *  Initialize the library.  Root priviledges are required.
        !            54:      */
        !            55:     l = libnet_init(
        !            56:             LIBNET_LINK,                            /* injection type */
        !            57:             device,                                 /* network interface */
        !            58:             errbuf);                                /* errbuf */
        !            59: 
        !            60:     if (l == NULL)
        !            61:     {
        !            62:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !            63:         exit(EXIT_FAILURE);
        !            64:     }
        !            65: 
        !            66:     value   = "switch";
        !            67:     len     = strlen(value);
        !            68: 
        !            69:     t = libnet_build_cdp(
        !            70:             1,                                      /* version */
        !            71:             30,                                     /* time to live */
        !            72:             0,                                      /* checksum */
        !            73:             LIBNET_CDP_DEVID,                       /* type */
        !            74:             len,                                    /* length */
        !            75:             value,                                  /* value */
        !            76:             NULL,                                   /* payload */
        !            77:             0,                                      /* payload size */
        !            78:             l,                                      /* libnet handle */
        !            79:             0);                                     /* libnet id */
        !            80:     if (t == -1)
        !            81:     {
        !            82:         fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
        !            83:         goto bad;
        !            84:     }
        !            85: 
        !            86:     memset(values, 0, sizeof(values));
        !            87:     tmp = htons(LIBNET_CDP_PORTID);
        !            88:     memcpy(values, &tmp, 2);
        !            89:     tmp = htons(0x0014);
        !            90:     memcpy(values + 2, &tmp, 2);
        !            91:     memcpy(values + 4, (u_char *)"FastEthernet0/20", 16);
        !            92:     t = libnet_build_data(
        !            93:             values,
        !            94:             20,
        !            95:             l,
        !            96:             0);
        !            97:     if (t == -1)
        !            98:     {
        !            99:         fprintf(stderr, "Can't build CDP data: %s\n", libnet_geterror(l));
        !           100:         goto bad;
        !           101:     }
        !           102:     memset(values, 0, sizeof(values));
        !           103:     tmp = htons(LIBNET_CDP_CAPABIL);
        !           104:     memcpy(values, &tmp, 2);
        !           105:     tmp = htons(0x0008);
        !           106:     memcpy(values + 2, &tmp, 2);
        !           107:     tmp2 = htonl((LIBNET_CDP_CAP_L2S | LIBNET_CDP_CAP_L2B));
        !           108:     memcpy(values + 4, &tmp2, 4);
        !           109:     t = libnet_build_data(
        !           110:             values,
        !           111:             8,
        !           112:             l,
        !           113:             0);
        !           114:     if (t == -1)
        !           115:     {
        !           116:         fprintf(stderr, "Can't build CDP data: %s\n", libnet_geterror(l));
        !           117:         goto bad;
        !           118:     }
        !           119:     memset(values, 0, sizeof(values));
        !           120:     tmp = htons(LIBNET_CDP_VERSION);
        !           121:     memcpy(values, &tmp, 2);
        !           122:     tmp = htons(0x001f);
        !           123:     memcpy(values + 2, &tmp, 2);
        !           124:     memcpy(values + 4, (u_char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
        !           125:     t = libnet_build_data(
        !           126:             values,
        !           127:             31,
        !           128:             l,
        !           129:             0);
        !           130:     if (t == -1)
        !           131:     {
        !           132:         fprintf(stderr, "Can't build CDP data: %s\n", libnet_geterror(l));
        !           133:         goto bad;
        !           134:     }
        !           135:     memset(values, 0, sizeof(values));
        !           136:     tmp = htons(LIBNET_CDP_PLATFORM);
        !           137:     memcpy(values, &tmp, 2);
        !           138:     tmp = htons(0x0015);
        !           139:     memcpy(values + 2, &tmp, 2);
        !           140:     memcpy(values + 4, (u_char *)"cisco WS-C2924-XL", 17);
        !           141:     t = libnet_build_data(
        !           142:             values,
        !           143:             21,
        !           144:             l,
        !           145:             0);
        !           146:     if (t == -1)
        !           147:     {
        !           148:         fprintf(stderr, "Can't build CDP data: %s\n", libnet_geterror(l));
        !           149:         goto bad;
        !           150:     }
        !           151: 
        !           152:     t = libnet_build_ethernet(
        !           153:             enet_dst,                               /* ethernet destination */
        !           154:             enet_src,                               /* ethernet source */
        !           155:             0x2000,                                 /* protocol type */
        !           156:             NULL,                                   /* payload */
        !           157:             0,                                      /* payload size */
        !           158:             l,                                      /* libnet handle */
        !           159:             0);                                     /* libnet id */
        !           160:     if (t == -1)
        !           161:     {
        !           162:         fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
        !           163:         goto bad;
        !           164:     }
        !           165: 
        !           166:     /*
        !           167:      *  Write it to the wire.
        !           168:      */
        !           169:     c = libnet_write(l);
        !           170: 
        !           171:     if (c == -1)
        !           172:     {
        !           173:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           174:         goto bad;
        !           175:     }
        !           176:     else
        !           177:     {
        !           178:         fprintf(stderr, "Wrote %d byte CDP packet; check the wire.\n", c);
        !           179:     }
        !           180:     libnet_destroy(l);
        !           181:     return (EXIT_SUCCESS);
        !           182: bad:
        !           183:     libnet_destroy(l);
        !           184:     return (EXIT_FAILURE);
        !           185: }
        !           186: 
        !           187: /* EOF */

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