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

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)
                     38: #include "../include/config.h"
                     39: #endif
                     40: #include "./libnet_test.h"
                     41: 
1.1.1.2   misho      42: 
1.1       misho      43: int
                     44: main(int argc, char *argv[])
                     45: {
1.1.1.2   misho      46:     int c, len, index;
1.1       misho      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];
1.1.1.2   misho      53:     uint8_t oui[3] = { 0x00, 0x00, 0x0c };
                     54:     uint8_t cdp_mac[6] = {0x01, 0x0, 0xc, 0xcc, 0xcc, 0xcc};
1.1       misho      55: 
1.1.1.2   misho      56:     if (argc != 3)
1.1       misho      57:     {
1.1.1.2   misho      58:         fprintf(stderr, "usage %s device device-id\n", argv[0]);
                     59:         return (EXIT_FAILURE);
1.1       misho      60:     }
                     61: 
1.1.1.2   misho      62:     fprintf(stderr, "cdppoke...\n");
1.1       misho      63: 
1.1.1.2   misho      64:     l = libnet_init(LIBNET_LINK, argv[1], errbuf);
                     65:     if (l == NULL)
1.1       misho      66:     {
1.1.1.2   misho      67:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
                     68:         return (EXIT_FAILURE);
1.1       misho      69:     }
                     70: 
1.1.1.2   misho      71:     /* build the TLV's by hand until we get something better */
1.1       misho      72:     memset(values, 0, sizeof(values));
1.1.1.2   misho      73:     index = 0;
                     74:  
1.1       misho      75:     tmp = htons(LIBNET_CDP_VERSION);
                     76:     memcpy(values, &tmp, 2);
1.1.1.2   misho      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 */
1.1.1.3 ! misho      85:     value = (u_char *)argv[2];
1.1.1.2   misho      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 */
1.1       misho     100:     if (t == -1)
                    101:     {
1.1.1.2   misho     102:         fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
1.1       misho     103:         goto bad;
                    104:     }
1.1.1.2   misho     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 */
1.1       misho     117:     if (t == -1)
                    118:     {
1.1.1.2   misho     119:         fprintf(stderr, "Can't build SNAP header: %s\n", libnet_geterror(l));
1.1       misho     120:         goto bad;
                    121:     }
                    122: 
1.1.1.2   misho     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 */
1.1       misho     132:     if (t == -1)
                    133:     {
1.1.1.2   misho     134:         fprintf(stderr, "Can't build 802.3 header: %s\n", libnet_geterror(l));
1.1       misho     135:         goto bad;
                    136:     }
                    137: 
1.1.1.2   misho     138:     /* write the packet out */
1.1       misho     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:     {
1.1.1.2   misho     147:         fprintf(stderr, "Wrote %d byte CDP frame \"%s\"\n", c, argv[2]);
1.1       misho     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>