File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / cdp.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 11:54:41 2013 UTC (10 years, 11 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_1_6p5, v1_1_6p4, v1_1_6p0, v1_1_6, HEAD
1.1.6

    1: /*
    2:  *  $Id: cdp.c,v 1.1.1.2 2013/07/22 11:54:41 misho Exp $
    3:  *
    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>
   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)
   38: #if ((_WIN32) && !(__CYGWIN__)) 
   39: #include "../include/win32/config.h"
   40: #else
   41: #include "../include/config.h"
   42: #endif
   43: #endif
   44: #include "./libnet_test.h"
   45: 
   46: 
   47: int
   48: main(int argc, char *argv[])
   49: {
   50:     int c, len, index;
   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];
   57:     uint8_t oui[3] = { 0x00, 0x00, 0x0c };
   58:     uint8_t cdp_mac[6] = {0x01, 0x0, 0xc, 0xcc, 0xcc, 0xcc};
   59: 
   60:     if (argc != 3)
   61:     {
   62:         fprintf(stderr, "usage %s device device-id\n", argv[0]);
   63:         return (EXIT_FAILURE);
   64:     }
   65: 
   66:     fprintf(stderr, "cdppoke...\n");
   67: 
   68:     l = libnet_init(LIBNET_LINK, argv[1], errbuf);
   69:     if (l == NULL)
   70:     {
   71:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   72:         return (EXIT_FAILURE);
   73:     }
   74: 
   75:     /* build the TLV's by hand until we get something better */
   76:     memset(values, 0, sizeof(values));
   77:     index = 0;
   78:  
   79:     tmp = htons(LIBNET_CDP_VERSION);
   80:     memcpy(values, &tmp, 2);
   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 */
  104:     if (t == -1)
  105:     {
  106:         fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
  107:         goto bad;
  108:     }
  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 */
  121:     if (t == -1)
  122:     {
  123:         fprintf(stderr, "Can't build SNAP header: %s\n", libnet_geterror(l));
  124:         goto bad;
  125:     }
  126: 
  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 */
  136:     if (t == -1)
  137:     {
  138:         fprintf(stderr, "Can't build 802.3 header: %s\n", libnet_geterror(l));
  139:         goto bad;
  140:     }
  141: 
  142:     /* write the packet out */
  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:     {
  151:         fprintf(stderr, "Wrote %d byte CDP frame \"%s\"\n", c, argv[2]);
  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>