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

1.1       misho       1: /*
1.1.1.2 ! misho       2:  *  $Id: cdp.c,v 1.3 2004/11/09 07:05:07 mike Exp $
1.1       misho       3:  *
1.1.1.2 ! misho       4:  *  cdppoke
        !             5:  *  CDP information injection tool
        !             6:  *  Released as part of the MXFP Layer 2 Toolkit
        !             7:  *  http://www.packetfactory.net/MXFP
        !             8:  *
        !             9:  *  Copyright (c) 2004 Mike D. Schiffman  <mike@infonexus.com>
        !            10:  *  Copyright (c) 2004 Jeremy Rauch       <jrauch@cadre.org>
1.1       misho      11:  *
                     12:  *  All rights reserved.
                     13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
                     35:  */
                     36: 
                     37: #if (HAVE_CONFIG_H)
1.1.1.2 ! misho      38: #if ((_WIN32) && !(__CYGWIN__)) 
        !            39: #include "../include/win32/config.h"
        !            40: #else
1.1       misho      41: #include "../include/config.h"
                     42: #endif
1.1.1.2 ! misho      43: #endif
1.1       misho      44: #include "./libnet_test.h"
                     45: 
1.1.1.2 ! misho      46: 
1.1       misho      47: int
                     48: main(int argc, char *argv[])
                     49: {
1.1.1.2 ! misho      50:     int c, len, index;
1.1       misho      51:     libnet_t *l;
                     52:     libnet_ptag_t t;
                     53:     u_char *value;
                     54:     u_char values[100];
                     55:     u_short tmp;
                     56:     char errbuf[LIBNET_ERRBUF_SIZE];
1.1.1.2 ! misho      57:     uint8_t oui[3] = { 0x00, 0x00, 0x0c };
        !            58:     uint8_t cdp_mac[6] = {0x01, 0x0, 0xc, 0xcc, 0xcc, 0xcc};
1.1       misho      59: 
1.1.1.2 ! misho      60:     if (argc != 3)
1.1       misho      61:     {
1.1.1.2 ! misho      62:         fprintf(stderr, "usage %s device device-id\n", argv[0]);
        !            63:         return (EXIT_FAILURE);
1.1       misho      64:     }
                     65: 
1.1.1.2 ! misho      66:     fprintf(stderr, "cdppoke...\n");
1.1       misho      67: 
1.1.1.2 ! misho      68:     l = libnet_init(LIBNET_LINK, argv[1], errbuf);
        !            69:     if (l == NULL)
1.1       misho      70:     {
1.1.1.2 ! misho      71:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !            72:         return (EXIT_FAILURE);
1.1       misho      73:     }
                     74: 
1.1.1.2 ! misho      75:     /* build the TLV's by hand until we get something better */
1.1       misho      76:     memset(values, 0, sizeof(values));
1.1.1.2 ! misho      77:     index = 0;
        !            78:  
1.1       misho      79:     tmp = htons(LIBNET_CDP_VERSION);
                     80:     memcpy(values, &tmp, 2);
1.1.1.2 ! misho      81:     index += 2;
        !            82:     tmp = htons(9); /* length of string below plus type and length fields */
        !            83:     memcpy(values + index, &tmp, 2);
        !            84:     index += 2;
        !            85:     memcpy(values + index, (u_char *)"1.1.1", 5);
        !            86:     index += 5;
        !            87: 
        !            88:     /* this TLV is handled by the libnet builder */
        !            89:     value = argv[2];
        !            90:     len = strlen(argv[2]);
        !            91: 
        !            92:     /* build CDP header */
        !            93:     t = libnet_build_cdp(
        !            94:         1,                                      /* version */
        !            95:         30,                                     /* time to live */
        !            96:         0x0,                                    /* checksum */
        !            97:         0x1,                                    /* type */
        !            98:         len,                                    /* length */
        !            99:         value,                                  /* value */
        !           100:         values,                                 /* payload */
        !           101:         index,                                  /* payload size */
        !           102:         l,                                      /* libnet context */
        !           103:         0);                                     /* libnet ptag */
1.1       misho     104:     if (t == -1)
                    105:     {
1.1.1.2 ! misho     106:         fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
1.1       misho     107:         goto bad;
                    108:     }
1.1.1.2 ! misho     109: 
        !           110:     /* build 802.2 header */ 
        !           111:     t = libnet_build_802_2snap(
        !           112:         LIBNET_SAP_SNAP,                       /* SAP SNAP code */
        !           113:         LIBNET_SAP_SNAP,                       /* SAP SNAP code */
        !           114:         0x03,                                  /* control */
        !           115:        oui,                                   /* OUI */
        !           116:         0x2000,                                /* upper layer protocol type */ 
        !           117:         NULL,                                  /* payload */
        !           118:         0,                                     /* payload size */
        !           119:         l,                                     /* libnet context */
        !           120:         0);                                    /* libnet ptag */
1.1       misho     121:     if (t == -1)
                    122:     {
1.1.1.2 ! misho     123:         fprintf(stderr, "Can't build SNAP header: %s\n", libnet_geterror(l));
1.1       misho     124:         goto bad;
                    125:     }
                    126: 
1.1.1.2 ! misho     127:     /* build 802.3 header */ 
        !           128:     t = libnet_build_802_3(
        !           129:             cdp_mac,                           /* ethernet destination */
        !           130:             (uint8_t *)libnet_get_hwaddr(l),  /* ethernet source */
        !           131:             LIBNET_802_2_H + LIBNET_802_2SNAP_H + LIBNET_CDP_H,   /* packet len */
        !           132:             NULL,                              /* payload */
        !           133:             0,                                 /* payload size */
        !           134:             l,                                 /* libnet context */
        !           135:             0);                                /* libnet ptag */
1.1       misho     136:     if (t == -1)
                    137:     {
1.1.1.2 ! misho     138:         fprintf(stderr, "Can't build 802.3 header: %s\n", libnet_geterror(l));
1.1       misho     139:         goto bad;
                    140:     }
                    141: 
1.1.1.2 ! misho     142:     /* write the packet out */
1.1       misho     143:     c = libnet_write(l);
                    144:     if (c == -1)
                    145:     {
                    146:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    147:         goto bad;
                    148:     }
                    149:     else
                    150:     {
1.1.1.2 ! misho     151:         fprintf(stderr, "Wrote %d byte CDP frame \"%s\"\n", c, argv[2]);
1.1       misho     152:     }
                    153:     libnet_destroy(l);
                    154:     return (EXIT_SUCCESS);
                    155: bad:
                    156:     libnet_destroy(l);
                    157:     return (EXIT_FAILURE);
                    158: }
                    159: 
                    160: /* EOF */

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