Blame __dist_sample/sample/cdp.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: cdp.c,v 1.3 2004/11/09 07:05:07 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  cdppoke
Packit Service b25606
 *  CDP information injection tool
Packit Service b25606
 *  Released as part of the MXFP Layer 2 Toolkit
Packit Service b25606
 *  http://www.packetfactory.net/MXFP
Packit Service b25606
 *
Packit Service b25606
 *  Copyright (c) 2004 Mike D. Schiffman  <mike@infonexus.com>
Packit Service b25606
 *  Copyright (c) 2004 Jeremy Rauch       <jrauch@cadre.org>
Packit Service b25606
 *
Packit Service b25606
 *  All rights reserved.
Packit Service b25606
 *
Packit Service b25606
 * Redistribution and use in source and binary forms, with or without
Packit Service b25606
 * modification, are permitted provided that the following conditions
Packit Service b25606
 * are met:
Packit Service b25606
 * 1. Redistributions of source code must retain the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer.
Packit Service b25606
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer in the
Packit Service b25606
 *    documentation and/or other materials provided with the distribution.
Packit Service b25606
 *
Packit Service b25606
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit Service b25606
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service b25606
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service b25606
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit Service b25606
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service b25606
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service b25606
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service b25606
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service b25606
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service b25606
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service b25606
 * SUCH DAMAGE.
Packit Service b25606
 *
Packit Service b25606
 */
Packit Service b25606
Packit Service b25606
#if (HAVE_CONFIG_H)
Packit Service b25606
#if ((_WIN32) && !(__CYGWIN__)) 
Packit Service b25606
#include "../include/win32/config.h"
Packit Service b25606
#else
Packit Service b25606
#include "../include/config.h"
Packit Service b25606
#endif
Packit Service b25606
#endif
Packit Service b25606
#include "./libnet_test.h"
Packit Service b25606
Packit Service b25606
Packit Service b25606
int
Packit Service b25606
main(int argc, char *argv[])
Packit Service b25606
{
Packit Service b25606
    int c, len, index;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    libnet_ptag_t t;
Packit Service b25606
    u_char *value;
Packit Service b25606
    u_char values[100];
Packit Service b25606
    u_short tmp;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
    uint8_t oui[3] = { 0x00, 0x00, 0x0c };
Packit Service b25606
    uint8_t cdp_mac[6] = {0x01, 0x0, 0xc, 0xcc, 0xcc, 0xcc};
Packit Service b25606
Packit Service b25606
    if (argc != 3)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "usage %s device device-id\n", argv[0]);
Packit Service b25606
        return (EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    fprintf(stderr, "cdppoke...\n");
Packit Service b25606
Packit Service b25606
    l = libnet_init(LIBNET_LINK, argv[1], errbuf);
Packit Service b25606
    if (l == NULL)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "libnet_init() failed: %s", errbuf);
Packit Service b25606
        return (EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /* build the TLV's by hand until we get something better */
Packit Service b25606
    memset(values, 0, sizeof(values));
Packit Service b25606
    index = 0;
Packit Service b25606
 
Packit Service b25606
    tmp = htons(LIBNET_CDP_VERSION);
Packit Service b25606
    memcpy(values, &tmp, 2);
Packit Service b25606
    index += 2;
Packit Service b25606
    tmp = htons(9); /* length of string below plus type and length fields */
Packit Service b25606
    memcpy(values + index, &tmp, 2);
Packit Service b25606
    index += 2;
Packit Service b25606
    memcpy(values + index, (u_char *)"1.1.1", 5);
Packit Service b25606
    index += 5;
Packit Service b25606
Packit Service b25606
    /* this TLV is handled by the libnet builder */
Packit Service b25606
    value = argv[2];
Packit Service b25606
    len = strlen(argv[2]);
Packit Service b25606
Packit Service b25606
    /* build CDP header */
Packit Service b25606
    t = libnet_build_cdp(
Packit Service b25606
        1,                                      /* version */
Packit Service b25606
        30,                                     /* time to live */
Packit Service b25606
        0x0,                                    /* checksum */
Packit Service b25606
        0x1,                                    /* type */
Packit Service b25606
        len,                                    /* length */
Packit Service b25606
        value,                                  /* value */
Packit Service b25606
        values,                                 /* payload */
Packit Service b25606
        index,                                  /* payload size */
Packit Service b25606
        l,                                      /* libnet context */
Packit Service b25606
        0);                                     /* libnet ptag */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build CDP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /* build 802.2 header */ 
Packit Service b25606
    t = libnet_build_802_2snap(
Packit Service b25606
        LIBNET_SAP_SNAP,                       /* SAP SNAP code */
Packit Service b25606
        LIBNET_SAP_SNAP,                       /* SAP SNAP code */
Packit Service b25606
        0x03,                                  /* control */
Packit Service b25606
	oui,                                   /* OUI */
Packit Service b25606
        0x2000,                                /* upper layer protocol type */ 
Packit Service b25606
        NULL,                                  /* payload */
Packit Service b25606
        0,                                     /* payload size */
Packit Service b25606
        l,                                     /* libnet context */
Packit Service b25606
        0);                                    /* libnet ptag */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build SNAP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /* build 802.3 header */ 
Packit Service b25606
    t = libnet_build_802_3(
Packit Service b25606
            cdp_mac,                           /* ethernet destination */
Packit Service b25606
            (uint8_t *)libnet_get_hwaddr(l),  /* ethernet source */
Packit Service b25606
            LIBNET_802_2_H + LIBNET_802_2SNAP_H + LIBNET_CDP_H,   /* packet len */
Packit Service b25606
            NULL,                              /* payload */
Packit Service b25606
            0,                                 /* payload size */
Packit Service b25606
            l,                                 /* libnet context */
Packit Service b25606
            0);                                /* libnet ptag */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build 802.3 header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /* write the packet out */
Packit Service b25606
    c = libnet_write(l);
Packit Service b25606
    if (c == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Wrote %d byte CDP frame \"%s\"\n", c, argv[2]);
Packit Service b25606
    }
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_SUCCESS);
Packit Service b25606
bad:
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_FAILURE);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */