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

1.1       misho       1: /*
                      2:  *  libnet
                      3:  *  libnet_build_token_ring.c - Token Ring (802.5)  packet assembler
                      4:  *
                      5:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      6:  *                            Jason Damron      <jsdamron@hushmail.com>
                      7:  *  All rights reserved.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     19:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     20:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     21:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     22:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     23:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     24:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     25:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     26:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     27:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     28:  * SUCH DAMAGE.
                     29:  *
                     30:  */
                     31: 
1.1.1.3 ! misho      32: #include "common.h"
1.1       misho      33: 
                     34: libnet_ptag_t
1.1.1.2   misho      35: libnet_build_token_ring(uint8_t ac, uint8_t fc, const uint8_t *dst, const uint8_t *src, 
                     36: uint8_t dsap, uint8_t ssap, uint8_t cf, const uint8_t *org, uint16_t type,
                     37: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1       misho      38: {
1.1.1.2   misho      39:     uint32_t n, h;
1.1       misho      40:     libnet_pblock_t *p;
                     41:     struct libnet_token_ring_hdr token_ring_hdr;
                     42: 
                     43:     if (l == NULL)
                     44:     { 
                     45:         return (-1);
                     46:     } 
                     47: 
                     48:     /* sanity check injection type if we're not in advanced mode */
                     49:     if (l->injection_type != LIBNET_LINK &&
                     50:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                     51:     {
                     52:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho      53:                 "%s(): called with non-link layer wire injection primitive",
1.1       misho      54:                 __func__);
                     55:         p = NULL;
                     56:         goto bad;
                     57:     }
                     58: 
                     59:     n = LIBNET_TOKEN_RING_H + payload_s;
                     60:     h = 0;
                     61:  
                     62:     /*
                     63:      *  Find the existing protocol block if a ptag is specified, or create
                     64:      *  a new one.
                     65:      */
                     66:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
                     67:     if (p == NULL)
                     68:     {
                     69:         return (-1);
                     70:     }
                     71: 
                     72:     memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
                     73:     token_ring_hdr.token_ring_access_control    = ac;
                     74:     token_ring_hdr.token_ring_frame_control     = fc;
                     75:     memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
                     76:     memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
                     77:     token_ring_hdr.token_ring_llc_dsap          = dsap;
                     78:     token_ring_hdr.token_ring_llc_ssap          = ssap;
                     79:     token_ring_hdr.token_ring_llc_control_field = cf;
                     80:     memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                     81:     token_ring_hdr.token_ring_type              = htons(type);
                     82: 
1.1.1.2   misho      83:     n = libnet_pblock_append(l, p, (uint8_t *)&token_ring_hdr, 
1.1       misho      84:             LIBNET_TOKEN_RING_H);
                     85:     if (n == -1)
                     86:     {
                     87:         goto bad;
                     88:     }
                     89:  
1.1.1.2   misho      90:     /* boilerplate payload sanity check / append macro */
                     91:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho      92:  
                     93:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                     94:             LIBNET_PBLOCK_TOKEN_RING_H));
                     95: bad:
                     96:     libnet_pblock_delete(l, p);
                     97:     return (-1);
                     98: }
                     99: 
                    100: 
                    101: libnet_ptag_t
1.1.1.2   misho     102: libnet_autobuild_token_ring(uint8_t ac, uint8_t fc, const uint8_t *dst, 
                    103: uint8_t dsap, uint8_t ssap, uint8_t cf, const uint8_t *org, uint16_t type, 
1.1       misho     104: libnet_t *l)
                    105: {
1.1.1.2   misho     106:     uint32_t n, h;
1.1       misho     107:     struct libnet_token_ring_addr *src;
                    108:     libnet_pblock_t *p;
                    109:     libnet_ptag_t ptag;
                    110:     struct libnet_token_ring_hdr token_ring_hdr;
                    111: 
                    112:     if (l == NULL)
                    113:     { 
                    114:         return (-1);
                    115:     } 
                    116: 
                    117:     /* sanity check injection type if we're not in advanced mode */
                    118:     if (l->injection_type != LIBNET_LINK &&
                    119:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                    120:     {
                    121:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     122:                 "%s(): called with non-link layer wire injection primitive",
1.1       misho     123:                 __func__);         
                    124:         p = NULL;
                    125:         goto bad;
                    126:     }
                    127: 
                    128:     n = LIBNET_TOKEN_RING_H;
                    129:     h = 0;
                    130:     ptag = LIBNET_PTAG_INITIALIZER;
                    131: 
                    132:     /* Token Ring and Ethernet have the same address size - so just typecast */
                    133:     src = (struct libnet_token_ring_addr *) libnet_get_hwaddr(l);
                    134:     if (src == NULL)
                    135:     {
                    136:         /* err msg set in libnet_get_hwaddr() */
                    137:         return (-1);
                    138:     }
                    139: 
                    140:     /*
                    141:      *  Create a new pblock. 
                    142:      */
                    143:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
                    144:     if (p == NULL)
                    145:     {
                    146:         return (-1);
                    147:     }
                    148: 
                    149:     memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
                    150:     token_ring_hdr.token_ring_access_control    = ac;
                    151:     token_ring_hdr.token_ring_frame_control     = fc;
                    152:     memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
                    153:     memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
                    154:     token_ring_hdr.token_ring_llc_dsap          = dsap;
                    155:     token_ring_hdr.token_ring_llc_ssap          = ssap;
                    156:     token_ring_hdr.token_ring_llc_control_field = cf;
                    157:     memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                    158:     token_ring_hdr.token_ring_type              = htons(type);
                    159: 
                    160: 
1.1.1.2   misho     161:     n = libnet_pblock_append(l, p, (uint8_t *)&token_ring_hdr, 
1.1       misho     162:             LIBNET_TOKEN_RING_H);
                    163:     if (n == -1)
                    164:     {
                    165:         goto bad;
                    166:     }
                    167: 
                    168:     return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_TOKEN_RING_H));
                    169: bad:
                    170:     libnet_pblock_delete(l, p);
                    171:     return (-1); 
                    172: }
1.1.1.3 ! misho     173: 
        !           174: /**
        !           175:  * Local Variables:
        !           176:  *  indent-tabs-mode: nil
        !           177:  *  c-file-style: "stroustrup"
        !           178:  * End:
        !           179:  */

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