File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / cdp.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (10 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: cdp.c,v 1.1.1.3 2023/09/27 11:11:38 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: #include "../include/config.h"
   39: #endif
   40: #include "./libnet_test.h"
   41: 
   42: 
   43: int
   44: main(int argc, char *argv[])
   45: {
   46:     int c, len, index;
   47:     libnet_t *l;
   48:     libnet_ptag_t t;
   49:     u_char *value;
   50:     u_char values[100];
   51:     u_short tmp;
   52:     char errbuf[LIBNET_ERRBUF_SIZE];
   53:     uint8_t oui[3] = { 0x00, 0x00, 0x0c };
   54:     uint8_t cdp_mac[6] = {0x01, 0x0, 0xc, 0xcc, 0xcc, 0xcc};
   55: 
   56:     if (argc != 3)
   57:     {
   58:         fprintf(stderr, "usage %s device device-id\n", argv[0]);
   59:         return (EXIT_FAILURE);
   60:     }
   61: 
   62:     fprintf(stderr, "cdppoke...\n");
   63: 
   64:     l = libnet_init(LIBNET_LINK, argv[1], errbuf);
   65:     if (l == NULL)
   66:     {
   67:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   68:         return (EXIT_FAILURE);
   69:     }
   70: 
   71:     /* build the TLV's by hand until we get something better */
   72:     memset(values, 0, sizeof(values));
   73:     index = 0;
   74:  
   75:     tmp = htons(LIBNET_CDP_VERSION);
   76:     memcpy(values, &tmp, 2);
   77:     index += 2;
   78:     tmp = htons(9); /* length of string below plus type and length fields */
   79:     memcpy(values + index, &tmp, 2);
   80:     index += 2;
   81:     memcpy(values + index, (u_char *)"1.1.1", 5);
   82:     index += 5;
   83: 
   84:     /* this TLV is handled by the libnet builder */
   85:     value = (u_char *)argv[2];
   86:     len = strlen(argv[2]);
   87: 
   88:     /* build CDP header */
   89:     t = libnet_build_cdp(
   90:         1,                                      /* version */
   91:         30,                                     /* time to live */
   92:         0x0,                                    /* checksum */
   93:         0x1,                                    /* type */
   94:         len,                                    /* length */
   95:         value,                                  /* value */
   96:         values,                                 /* payload */
   97:         index,                                  /* payload size */
   98:         l,                                      /* libnet context */
   99:         0);                                     /* libnet ptag */
  100:     if (t == -1)
  101:     {
  102:         fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
  103:         goto bad;
  104:     }
  105: 
  106:     /* build 802.2 header */ 
  107:     t = libnet_build_802_2snap(
  108:         LIBNET_SAP_SNAP,                       /* SAP SNAP code */
  109:         LIBNET_SAP_SNAP,                       /* SAP SNAP code */
  110:         0x03,                                  /* control */
  111: 	oui,                                   /* OUI */
  112:         0x2000,                                /* upper layer protocol type */ 
  113:         NULL,                                  /* payload */
  114:         0,                                     /* payload size */
  115:         l,                                     /* libnet context */
  116:         0);                                    /* libnet ptag */
  117:     if (t == -1)
  118:     {
  119:         fprintf(stderr, "Can't build SNAP header: %s\n", libnet_geterror(l));
  120:         goto bad;
  121:     }
  122: 
  123:     /* build 802.3 header */ 
  124:     t = libnet_build_802_3(
  125:             cdp_mac,                           /* ethernet destination */
  126:             (uint8_t *)libnet_get_hwaddr(l),  /* ethernet source */
  127:             LIBNET_802_2_H + LIBNET_802_2SNAP_H + LIBNET_CDP_H,   /* packet len */
  128:             NULL,                              /* payload */
  129:             0,                                 /* payload size */
  130:             l,                                 /* libnet context */
  131:             0);                                /* libnet ptag */
  132:     if (t == -1)
  133:     {
  134:         fprintf(stderr, "Can't build 802.3 header: %s\n", libnet_geterror(l));
  135:         goto bad;
  136:     }
  137: 
  138:     /* write the packet out */
  139:     c = libnet_write(l);
  140:     if (c == -1)
  141:     {
  142:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  143:         goto bad;
  144:     }
  145:     else
  146:     {
  147:         fprintf(stderr, "Wrote %d byte CDP frame \"%s\"\n", c, argv[2]);
  148:     }
  149:     libnet_destroy(l);
  150:     return (EXIT_SUCCESS);
  151: bad:
  152:     libnet_destroy(l);
  153:     return (EXIT_FAILURE);
  154: }
  155: 

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