Annotation of embedaddon/libnet/src/libnet_build_stp.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *  $Id: libnet_build_stp.c,v 1.8 2004/01/15 20:11:15 mike Exp $
        !             3:  *
        !             4:  *  libnet
        !             5:  *  libnet_build_stp.c - STP packet assembler
        !             6:  *
        !             7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
        !             8:  *  All rights reserved.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  *
        !            19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            29:  * SUCH DAMAGE.
        !            30:  *
        !            31:  */
        !            32: 
        !            33: #if (HAVE_CONFIG_H)
        !            34: #include "../include/config.h"
        !            35: #endif
        !            36: #if (!(_WIN32) || (__CYGWIN__)) 
        !            37: #include "../include/libnet.h"
        !            38: #else
        !            39: #include "../include/win32/libnet.h"
        !            40: #endif
        !            41: 
        !            42: libnet_ptag_t
        !            43: libnet_build_stp_conf(u_int16_t id, u_int8_t version, u_int8_t bpdu_type,
        !            44: u_int8_t flags, u_int8_t *root_id, u_int32_t root_pc, u_int8_t *bridge_id,
        !            45: u_int16_t port_id, u_int16_t message_age, u_int16_t max_age,
        !            46: u_int16_t hello_time, u_int16_t f_delay, u_int8_t *payload,
        !            47: u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
        !            48: {
        !            49:     u_int32_t n, h;
        !            50:     libnet_pblock_t *p;
        !            51: 
        !            52:     /* until we get some data marshalling in place we can't use this */
        !            53:     /* struct libnet_stp_conf_hdr stp_hdr; */
        !            54:     u_int8_t stp_hdr[35];
        !            55:     u_int16_t value_s;
        !            56:     u_int32_t value_l;
        !            57: 
        !            58:     if (l == NULL)
        !            59:     { 
        !            60:         return (-1);
        !            61:     } 
        !            62: 
        !            63:     n = LIBNET_STP_CONF_H + payload_s;          /* size of memory block */
        !            64:     h = 0;                                      /* no checksum */
        !            65: 
        !            66:     /*
        !            67:      *  Find the existing protocol block if a ptag is specified, or create
        !            68:      *  a new one.
        !            69:      */
        !            70:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_CONF_H);
        !            71:     if (p == NULL)
        !            72:     {
        !            73:         return (-1);
        !            74:     }
        !            75: 
        !            76:     /* until we get some data marshalling in place we can't use this */
        !            77:     /*
        !            78:     stp_hdr.stp_id      = htons(id);
        !            79:     stp_hdr.stp_version = version;
        !            80:     stp_hdr.stp_bpdu_type = bpdu_type;
        !            81:     stp_hdr.stp_flags   = flags;
        !            82:     memcpy(&stp_hdr.stp_rootid, root_id, 8);
        !            83:     stp_hdr.stp_rootpc = htonl(root_pc);
        !            84:     memcpy(&stp_hdr.stp_bridgeid, bridge_id, 8);
        !            85:     stp_hdr.stp_portid  = htons(port_id);
        !            86:     stp_hdr.stp_mage    = htons(message_age);
        !            87:     stp_hdr.stp_maxage  = htons(max_age);
        !            88:     stp_hdr.stp_hellot  = htons(hello_time);
        !            89:     stp_hdr.stp_fdelay  = htons(f_delay);
        !            90:     */
        !            91: 
        !            92:     value_s = htons(id);
        !            93:     memcpy(stp_hdr, &value_s, 2);
        !            94:     stp_hdr[2] = version;
        !            95:     stp_hdr[3] = bpdu_type;
        !            96:     stp_hdr[4] = flags;
        !            97:     memcpy(&stp_hdr[5], root_id, 8);
        !            98:     value_l = htonl(root_pc);
        !            99:     memcpy(&stp_hdr[13], &value_l, 4);
        !           100:     memcpy(&stp_hdr[17], bridge_id, 8);
        !           101:     value_s = htons(port_id);
        !           102:     memcpy(&stp_hdr[25], &value_s, 2);
        !           103: #if (LIBNET_BIG_ENDIAN == 1)
        !           104:     value_s = htons(message_age);
        !           105: #else
        !           106:     value_s = message_age;
        !           107: #endif
        !           108:     memcpy(&stp_hdr[27], &value_s, 2);
        !           109: #if (LIBNET_BIG_ENDIAN == 1)
        !           110:     value_s = htons(max_age);
        !           111: #else
        !           112:     value_s = max_age;
        !           113: #endif
        !           114:     memcpy(&stp_hdr[29], &value_s, 2);
        !           115: #if (LIBNET_BIG_ENDIAN == 1)
        !           116:     value_s = htons(hello_time);
        !           117: #else
        !           118:     value_s = hello_time;
        !           119: #endif
        !           120:     memcpy(&stp_hdr[31], &value_s, 2);
        !           121: #if (LIBNET_BIG_ENDIAN == 1)
        !           122:     value_s = htons(f_delay);
        !           123: #else
        !           124:     value_s = f_delay;
        !           125: #endif
        !           126:     memcpy(&stp_hdr[33], &value_s, 2);
        !           127: 
        !           128: 
        !           129:     /* until we get some data marshalling in place we can't use this */
        !           130:     /*n = libnet_pblock_append(l, p, (u_int8_t *)&stp_hdr, LIBNET_STP_CONF_H); */
        !           131:     n = libnet_pblock_append(l, p, stp_hdr, LIBNET_STP_CONF_H);
        !           132:     if (n == -1)
        !           133:     {
        !           134:         goto bad;
        !           135:     }
        !           136: 
        !           137:     if ((payload && !payload_s) || (!payload && payload_s))
        !           138:     {
        !           139:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
        !           140:                            "%s(): payload inconsistency\n", __func__);
        !           141:         goto bad;
        !           142:     }
        !           143:  
        !           144:     if (payload && payload_s)
        !           145:     {
        !           146:         n = libnet_pblock_append(l, p, payload, payload_s);
        !           147:         if (n == -1)
        !           148:         {
        !           149:             goto bad;
        !           150:         }
        !           151:     }
        !           152:  
        !           153:     return (ptag ? ptag : libnet_pblock_update(l, p, h,
        !           154:             LIBNET_PBLOCK_STP_CONF_H));
        !           155: bad:
        !           156:     libnet_pblock_delete(l, p);
        !           157:     return (-1);
        !           158: }
        !           159: 
        !           160: 
        !           161: libnet_ptag_t
        !           162: libnet_build_stp_tcn(u_int16_t id, u_int8_t version, u_int8_t bpdu_type,
        !           163:             u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
        !           164: {
        !           165:     u_int32_t n, h;
        !           166:     libnet_pblock_t *p;
        !           167: 
        !           168:     struct libnet_stp_tcn_hdr stp_hdr;
        !           169: 
        !           170:     if (l == NULL)
        !           171:     { 
        !           172:         return (-1);
        !           173:     } 
        !           174: 
        !           175:     n = LIBNET_STP_TCN_H + payload_s;           /* size of memory block */
        !           176:     h = 0;                                      /* no checksum */
        !           177: 
        !           178:     /*
        !           179:      *  Find the existing protocol block if a ptag is specified, or create
        !           180:      *  a new one.
        !           181:      */
        !           182:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_TCN_H);
        !           183:     if (p == NULL)
        !           184:     {
        !           185:         return (-1);
        !           186:     }
        !           187: 
        !           188:        memset(&stp_hdr, 0, sizeof(stp_hdr));
        !           189:        stp_hdr.stp_id        = htons(id);
        !           190:     stp_hdr.stp_version   = version;
        !           191:     stp_hdr.stp_bpdu_type = bpdu_type;
        !           192: 
        !           193:     n = libnet_pblock_append(l, p, (u_int8_t *)&stp_hdr, LIBNET_STP_TCN_H);
        !           194:     if (n == -1)
        !           195:     {
        !           196:         goto bad;
        !           197:     }
        !           198:  
        !           199:     if ((payload && !payload_s) || (!payload && payload_s))
        !           200:     {
        !           201:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
        !           202:                            "%s(): payload inconsistency\n", __func__);
        !           203:         goto bad;
        !           204:     }
        !           205: 
        !           206:     if (payload && payload_s)
        !           207:     {
        !           208:         n = libnet_pblock_append(l, p, payload, payload_s);
        !           209:         if (n == -1)
        !           210:         {
        !           211:             goto bad;
        !           212:         }
        !           213:     }
        !           214:  
        !           215:     return (ptag ? ptag : libnet_pblock_update(l, p, h,
        !           216:             LIBNET_PBLOCK_STP_TCN_H));
        !           217: bad:
        !           218:     libnet_pblock_delete(l, p);
        !           219:     return (-1);
        !           220: }
        !           221: 
        !           222: /* EOF */

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