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

1.1     ! misho       1: /*
        !             2:  *  $Id: stp.c,v 1.3 2004/01/21 19:01:29 mike Exp $
        !             3:  *
        !             4:  *  libnet 1.1
        !             5:  *  Build an STP frame
        !             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: #define CONF    1
        !            39: #define TCN     2
        !            40: 
        !            41: int
        !            42: main(int argc, char *argv[])
        !            43: {
        !            44:     int c, len, type;
        !            45:     libnet_t *l;
        !            46:     libnet_ptag_t t;
        !            47:     u_char *dst, *src;
        !            48:     u_char rootid[8], bridgeid[8];
        !            49:     char *device = NULL;
        !            50:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            51: 
        !            52:     printf("libnet 1.1 packet shaping: [STP]\n"); 
        !            53: 
        !            54:     device = NULL;
        !            55:     src = dst = NULL;
        !            56:     type = CONF;
        !            57:     while ((c = getopt(argc, argv, "cd:i:s:t")) != EOF)
        !            58:     {
        !            59:         switch (c)
        !            60:         {
        !            61:             case 'c':
        !            62:                 type = CONF;
        !            63:                 break;
        !            64:             case 'd':
        !            65:                 dst = libnet_hex_aton(optarg, &len);
        !            66:                 break;
        !            67:             case 'i':
        !            68:                 device = optarg;
        !            69:                 break;
        !            70:             case 's':
        !            71:                 src = libnet_hex_aton(optarg, &len);
        !            72:                 break;
        !            73:             case 't':
        !            74:                 type = TCN;
        !            75:                 break;
        !            76:             default:
        !            77:                 usage(argv[0]);
        !            78:                 exit(EXIT_FAILURE);
        !            79:         }
        !            80:     }
        !            81: 
        !            82:     if (src == NULL || dst == NULL)
        !            83:     {
        !            84:         usage(argv[0]);
        !            85:         exit(EXIT_FAILURE);
        !            86:     }
        !            87: 
        !            88:     /*
        !            89:      *  Initialize the library.  Root priviledges are required.
        !            90:      */
        !            91:     l = libnet_init(
        !            92:             LIBNET_LINK,                            /* injection type */
        !            93:             device,                                 /* network interface */
        !            94:             errbuf);                                /* errbuf */
        !            95: 
        !            96:     if (l == NULL)
        !            97:     {
        !            98:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !            99:         exit(EXIT_FAILURE);
        !           100:     }
        !           101: 
        !           102:     if (type == CONF)
        !           103:     {
        !           104:         rootid[0] = 0x80;
        !           105:         rootid[1] = 0x00;
        !           106:         rootid[2] = 0x00;
        !           107:         rootid[3] = 0x07;
        !           108:         rootid[4] = 0xec;
        !           109:         rootid[5] = 0xae;
        !           110:         rootid[6] = 0x30;
        !           111:         rootid[7] = 0x41;
        !           112: 
        !           113:         bridgeid[0] = 0x80;
        !           114:         bridgeid[1] = 0x00;
        !           115:         bridgeid[2] = 0x00;
        !           116:         bridgeid[3] = 0x07;
        !           117:         bridgeid[4] = 0xec;
        !           118:         bridgeid[5] = 0xae;
        !           119:         bridgeid[6] = 0x30;
        !           120:         bridgeid[7] = 0x41;
        !           121: 
        !           122:         t = libnet_build_stp_conf(
        !           123:             0x0000,                             /* protocol id */
        !           124:             0x00,                               /* protocol version */
        !           125:             0x00,                               /* BPDU type */
        !           126:             0x00,                               /* BPDU flags */
        !           127:             rootid,                             /* root id */
        !           128:             0x00000001,                         /* root path cost */
        !           129:             bridgeid,                           /* bridge id */
        !           130:             0x8002,                             /* port id */
        !           131:             0x00,                               /* message age */
        !           132:             0x0014,                             /* max age */
        !           133:             0x0002,                             /* hello time */
        !           134:             0x000f,                             /* forward delay */
        !           135:             NULL,                               /* payload */
        !           136:             0,                                  /* payload size */
        !           137:             l,                                  /* libnet handle */
        !           138:             0);                                 /* libnet id */
        !           139:         if (t == -1)
        !           140:         {
        !           141:             fprintf(stderr, "Can't build STP conf header: %s\n",
        !           142:                     libnet_geterror(l));
        !           143:             goto bad;
        !           144:         }
        !           145:     }
        !           146:     else
        !           147:     {
        !           148:         t = libnet_build_stp_tcn(
        !           149:             0x0000,                             /* protocol id */
        !           150:             0x00,                               /* protocol version */
        !           151:             0x80,                               /* BPDU type */
        !           152:             NULL,                               /* payload */
        !           153:             0,                                  /* payload size */
        !           154:             l,                                  /* libnet handle */
        !           155:             0);                                 /* libnet id */
        !           156:         if (t == -1)
        !           157:         {
        !           158:             fprintf(stderr, "Can't build STP tcn header: %s\n",
        !           159:                     libnet_geterror(l));
        !           160:             goto bad;
        !           161:         }
        !           162:     }
        !           163: 
        !           164:     t = libnet_build_802_2(
        !           165:         LIBNET_SAP_STP,                         /* DSAP */   
        !           166:         LIBNET_SAP_STP,                         /* SSAP */
        !           167:         0x03,                                   /* control */
        !           168:         NULL,                                   /* payload */  
        !           169:         0,                                      /* payload size */
        !           170:         l,                                      /* libnet handle */
        !           171:         0);                                     /* libnet id */
        !           172:     if (t == -1) 
        !           173:     {
        !           174:         fprintf(stderr, "Can't build ethernet header: %s\n",
        !           175:                 libnet_geterror(l));
        !           176:         goto bad;
        !           177:     }  
        !           178: 
        !           179:     t = libnet_build_802_3(
        !           180:         dst,                                    /* ethernet destination */
        !           181:         src,                                    /* ethernet source */
        !           182:         LIBNET_802_2_H + ((type == CONF) ? LIBNET_STP_CONF_H : 
        !           183:         LIBNET_STP_TCN_H),                       /* frame size */
        !           184:         NULL,                                   /* payload */
        !           185:         0,                                      /* payload size */
        !           186:         l,                                      /* libnet handle */
        !           187:         0);                                     /* libnet id */
        !           188:     if (t == -1)
        !           189:     {
        !           190:         fprintf(stderr, "Can't build ethernet header: %s\n",
        !           191:                 libnet_geterror(l));
        !           192:         goto bad;
        !           193:     }
        !           194:     /*
        !           195:      *  Write it to the wire.
        !           196:      */
        !           197:     c = libnet_write(l);
        !           198: 
        !           199:     if (c == -1)
        !           200:     {
        !           201:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           202:         goto bad;
        !           203:     }
        !           204:     else
        !           205:     {
        !           206:         fprintf(stderr, "Wrote %d byte STP packet; check the wire.\n", c);
        !           207:     }
        !           208:     free(dst);
        !           209:     free(src);
        !           210:     libnet_destroy(l);
        !           211:     return (EXIT_SUCCESS);
        !           212: bad:
        !           213:     free(dst);
        !           214:     free(src);
        !           215:     libnet_destroy(l);
        !           216:     return (EXIT_FAILURE);
        !           217: }
        !           218: 
        !           219: 
        !           220: void
        !           221: usage(char *name)
        !           222: {
        !           223:     fprintf(stderr, "usage %s -d dst -s src -t -c [-i interface]\n",
        !           224:                 name);
        !           225: }
        !           226: 
        !           227: /* EOF */

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