|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_build_stp.c,v 1.9 2004/04/13 17:32:28 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_build_stp.c - STP packet assembler
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_stp_conf(uint16_t id, uint8_t version, uint8_t bpdu_type,
|
|
Packit |
03b34a |
uint8_t flags, const uint8_t *root_id, uint32_t root_pc, const uint8_t *bridge_id,
|
|
Packit |
03b34a |
uint16_t port_id, uint16_t message_age, uint16_t max_age,
|
|
Packit |
03b34a |
uint16_t hello_time, uint16_t f_delay, const uint8_t *payload,
|
|
Packit |
03b34a |
uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* until we get some data marshalling in place we can't use this */
|
|
Packit |
03b34a |
/* struct libnet_stp_conf_hdr stp_hdr; */
|
|
Packit |
03b34a |
uint8_t stp_hdr[35];
|
|
Packit |
03b34a |
uint16_t value_s;
|
|
Packit |
03b34a |
uint32_t value_l;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_STP_CONF_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
h = 0; /* no checksum */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_CONF_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* until we get some data marshalling in place we can't use this */
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
stp_hdr.stp_id = htons(id);
|
|
Packit |
03b34a |
stp_hdr.stp_version = version;
|
|
Packit |
03b34a |
stp_hdr.stp_bpdu_type = bpdu_type;
|
|
Packit |
03b34a |
stp_hdr.stp_flags = flags;
|
|
Packit |
03b34a |
memcpy(&stp_hdr.stp_rootid, root_id, 8);
|
|
Packit |
03b34a |
stp_hdr.stp_rootpc = htonl(root_pc);
|
|
Packit |
03b34a |
memcpy(&stp_hdr.stp_bridgeid, bridge_id, 8);
|
|
Packit |
03b34a |
stp_hdr.stp_portid = htons(port_id);
|
|
Packit |
03b34a |
stp_hdr.stp_mage = htons(message_age);
|
|
Packit |
03b34a |
stp_hdr.stp_maxage = htons(max_age);
|
|
Packit |
03b34a |
stp_hdr.stp_hellot = htons(hello_time);
|
|
Packit |
03b34a |
stp_hdr.stp_fdelay = htons(f_delay);
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
value_s = htons(id);
|
|
Packit |
03b34a |
memcpy(stp_hdr, &value_s, 2);
|
|
Packit |
03b34a |
stp_hdr[2] = version;
|
|
Packit |
03b34a |
stp_hdr[3] = bpdu_type;
|
|
Packit |
03b34a |
stp_hdr[4] = flags;
|
|
Packit |
03b34a |
memcpy(&stp_hdr[5], root_id, 8);
|
|
Packit |
03b34a |
value_l = htonl(root_pc);
|
|
Packit |
03b34a |
memcpy(&stp_hdr[13], &value_l, 4);
|
|
Packit |
03b34a |
memcpy(&stp_hdr[17], bridge_id, 8);
|
|
Packit |
03b34a |
value_s = htons(port_id);
|
|
Packit |
03b34a |
memcpy(&stp_hdr[25], &value_s, 2);
|
|
Packit |
03b34a |
#if (LIBNET_BIG_ENDIAN == 1)
|
|
Packit |
03b34a |
value_s = htons(message_age);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
value_s = message_age;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
memcpy(&stp_hdr[27], &value_s, 2);
|
|
Packit |
03b34a |
#if (LIBNET_BIG_ENDIAN == 1)
|
|
Packit |
03b34a |
value_s = htons(max_age);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
value_s = max_age;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
memcpy(&stp_hdr[29], &value_s, 2);
|
|
Packit |
03b34a |
#if (LIBNET_BIG_ENDIAN == 1)
|
|
Packit |
03b34a |
value_s = htons(hello_time);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
value_s = hello_time;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
memcpy(&stp_hdr[31], &value_s, 2);
|
|
Packit |
03b34a |
#if (LIBNET_BIG_ENDIAN == 1)
|
|
Packit |
03b34a |
value_s = htons(f_delay);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
value_s = f_delay;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
memcpy(&stp_hdr[33], &value_s, 2);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* until we get some data marshalling in place we can't use this */
|
|
Packit |
03b34a |
/*n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_CONF_H); */
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, stp_hdr, LIBNET_STP_CONF_H);
|
|
Packit |
03b34a |
if (n == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* boilerplate payload sanity check / append macro */
|
|
Packit |
03b34a |
LIBNET_DO_PAYLOAD(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_STP_CONF_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_stp_tcn(uint16_t id, uint8_t version, uint8_t bpdu_type,
|
|
Packit |
03b34a |
const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
struct libnet_stp_tcn_hdr stp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_STP_TCN_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
h = 0; /* no checksum */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_TCN_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&stp_hdr, 0, sizeof(stp_hdr));
|
|
Packit |
03b34a |
stp_hdr.stp_id = htons(id);
|
|
Packit |
03b34a |
stp_hdr.stp_version = version;
|
|
Packit |
03b34a |
stp_hdr.stp_bpdu_type = bpdu_type;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_TCN_H);
|
|
Packit |
03b34a |
if (n == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* boilerplate payload sanity check / append macro */
|
|
Packit |
03b34a |
LIBNET_DO_PAYLOAD(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_STP_TCN_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* EOF */
|