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

1.1       misho       1: /*
1.1.1.2   misho       2:  *  $Id: libnet_build_stp.c,v 1.9 2004/04/13 17:32:28 mike Exp $
1.1       misho       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: 
1.1.1.3 ! misho      33: #include "common.h"
1.1       misho      34: 
                     35: libnet_ptag_t
1.1.1.2   misho      36: libnet_build_stp_conf(uint16_t id, uint8_t version, uint8_t bpdu_type,
                     37: uint8_t flags, const uint8_t *root_id, uint32_t root_pc, const uint8_t *bridge_id,
                     38: uint16_t port_id, uint16_t message_age, uint16_t max_age,
                     39: uint16_t hello_time, uint16_t f_delay, const uint8_t *payload,
                     40: uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1       misho      41: {
1.1.1.2   misho      42:     uint32_t n, h;
1.1       misho      43:     libnet_pblock_t *p;
                     44: 
                     45:     /* until we get some data marshalling in place we can't use this */
                     46:     /* struct libnet_stp_conf_hdr stp_hdr; */
1.1.1.2   misho      47:     uint8_t stp_hdr[35];
                     48:     uint16_t value_s;
                     49:     uint32_t value_l;
1.1       misho      50: 
                     51:     if (l == NULL)
                     52:     { 
                     53:         return (-1);
                     54:     } 
                     55: 
                     56:     n = LIBNET_STP_CONF_H + payload_s;          /* size of memory block */
                     57:     h = 0;                                      /* no checksum */
                     58: 
                     59:     /*
                     60:      *  Find the existing protocol block if a ptag is specified, or create
                     61:      *  a new one.
                     62:      */
                     63:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_CONF_H);
                     64:     if (p == NULL)
                     65:     {
                     66:         return (-1);
                     67:     }
                     68: 
                     69:     /* until we get some data marshalling in place we can't use this */
                     70:     /*
                     71:     stp_hdr.stp_id      = htons(id);
                     72:     stp_hdr.stp_version = version;
                     73:     stp_hdr.stp_bpdu_type = bpdu_type;
                     74:     stp_hdr.stp_flags   = flags;
                     75:     memcpy(&stp_hdr.stp_rootid, root_id, 8);
                     76:     stp_hdr.stp_rootpc = htonl(root_pc);
                     77:     memcpy(&stp_hdr.stp_bridgeid, bridge_id, 8);
                     78:     stp_hdr.stp_portid  = htons(port_id);
                     79:     stp_hdr.stp_mage    = htons(message_age);
                     80:     stp_hdr.stp_maxage  = htons(max_age);
                     81:     stp_hdr.stp_hellot  = htons(hello_time);
                     82:     stp_hdr.stp_fdelay  = htons(f_delay);
                     83:     */
                     84: 
                     85:     value_s = htons(id);
                     86:     memcpy(stp_hdr, &value_s, 2);
                     87:     stp_hdr[2] = version;
                     88:     stp_hdr[3] = bpdu_type;
                     89:     stp_hdr[4] = flags;
                     90:     memcpy(&stp_hdr[5], root_id, 8);
                     91:     value_l = htonl(root_pc);
                     92:     memcpy(&stp_hdr[13], &value_l, 4);
                     93:     memcpy(&stp_hdr[17], bridge_id, 8);
                     94:     value_s = htons(port_id);
                     95:     memcpy(&stp_hdr[25], &value_s, 2);
                     96: #if (LIBNET_BIG_ENDIAN == 1)
                     97:     value_s = htons(message_age);
                     98: #else
                     99:     value_s = message_age;
                    100: #endif
                    101:     memcpy(&stp_hdr[27], &value_s, 2);
                    102: #if (LIBNET_BIG_ENDIAN == 1)
                    103:     value_s = htons(max_age);
                    104: #else
                    105:     value_s = max_age;
                    106: #endif
                    107:     memcpy(&stp_hdr[29], &value_s, 2);
                    108: #if (LIBNET_BIG_ENDIAN == 1)
                    109:     value_s = htons(hello_time);
                    110: #else
                    111:     value_s = hello_time;
                    112: #endif
                    113:     memcpy(&stp_hdr[31], &value_s, 2);
                    114: #if (LIBNET_BIG_ENDIAN == 1)
                    115:     value_s = htons(f_delay);
                    116: #else
                    117:     value_s = f_delay;
                    118: #endif
                    119:     memcpy(&stp_hdr[33], &value_s, 2);
                    120: 
                    121: 
                    122:     /* until we get some data marshalling in place we can't use this */
1.1.1.2   misho     123:     /*n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_CONF_H); */
1.1       misho     124:     n = libnet_pblock_append(l, p, stp_hdr, LIBNET_STP_CONF_H);
                    125:     if (n == -1)
                    126:     {
                    127:         goto bad;
                    128:     }
                    129: 
1.1.1.2   misho     130:     /* boilerplate payload sanity check / append macro */
                    131:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho     132:  
                    133:     return (ptag ? ptag : libnet_pblock_update(l, p, h,
                    134:             LIBNET_PBLOCK_STP_CONF_H));
                    135: bad:
                    136:     libnet_pblock_delete(l, p);
                    137:     return (-1);
                    138: }
                    139: 
                    140: 
                    141: libnet_ptag_t
1.1.1.2   misho     142: libnet_build_stp_tcn(uint16_t id, uint8_t version, uint8_t bpdu_type,
                    143: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1       misho     144: {
1.1.1.2   misho     145:     uint32_t n, h;
1.1       misho     146:     libnet_pblock_t *p;
                    147: 
                    148:     struct libnet_stp_tcn_hdr stp_hdr;
                    149: 
                    150:     if (l == NULL)
                    151:     { 
                    152:         return (-1);
                    153:     } 
                    154: 
                    155:     n = LIBNET_STP_TCN_H + payload_s;           /* size of memory block */
                    156:     h = 0;                                      /* no checksum */
                    157: 
                    158:     /*
                    159:      *  Find the existing protocol block if a ptag is specified, or create
                    160:      *  a new one.
                    161:      */
                    162:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_TCN_H);
                    163:     if (p == NULL)
                    164:     {
                    165:         return (-1);
                    166:     }
                    167: 
1.1.1.2   misho     168:     memset(&stp_hdr, 0, sizeof(stp_hdr));
                    169:     stp_hdr.stp_id        = htons(id);
1.1       misho     170:     stp_hdr.stp_version   = version;
                    171:     stp_hdr.stp_bpdu_type = bpdu_type;
                    172: 
1.1.1.2   misho     173:     n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_TCN_H);
1.1       misho     174:     if (n == -1)
                    175:     {
                    176:         goto bad;
                    177:     }
                    178:  
1.1.1.2   misho     179:     /* boilerplate payload sanity check / append macro */
                    180:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho     181:  
                    182:     return (ptag ? ptag : libnet_pblock_update(l, p, h,
                    183:             LIBNET_PBLOCK_STP_TCN_H));
                    184: bad:
                    185:     libnet_pblock_delete(l, p);
                    186:     return (-1);
                    187: }
                    188: 
1.1.1.3 ! misho     189: /**
        !           190:  * Local Variables:
        !           191:  *  indent-tabs-mode: nil
        !           192:  *  c-file-style: "stroustrup"
        !           193:  * End:
        !           194:  */

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