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

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: 
                     32: #if (HAVE_CONFIG_H)
                     33: #include "../include/config.h"
                     34: #endif
                     35: #if (!(_WIN32) || (__CYGWIN__)) 
                     36: #include "../include/libnet.h"
                     37: #else
                     38: #include "../include/win32/libnet.h"
                     39: #endif
                     40: 
                     41: libnet_ptag_t
1.1.1.2 ! misho      42: libnet_build_token_ring(uint8_t ac, uint8_t fc, const uint8_t *dst, const uint8_t *src, 
        !            43: uint8_t dsap, uint8_t ssap, uint8_t cf, const uint8_t *org, uint16_t type,
        !            44: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1       misho      45: {
1.1.1.2 ! misho      46:     uint32_t n, h;
1.1       misho      47:     libnet_pblock_t *p;
                     48:     struct libnet_token_ring_hdr token_ring_hdr;
                     49: 
                     50:     if (l == NULL)
                     51:     { 
                     52:         return (-1);
                     53:     } 
                     54: 
                     55:     /* sanity check injection type if we're not in advanced mode */
                     56:     if (l->injection_type != LIBNET_LINK &&
                     57:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                     58:     {
                     59:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     60:                 "%s(): called with non-link layer wire injection primitive\n",
                     61:                 __func__);
                     62:         p = NULL;
                     63:         goto bad;
                     64:     }
                     65: 
                     66:     n = LIBNET_TOKEN_RING_H + payload_s;
                     67:     h = 0;
                     68:  
                     69:     /*
                     70:      *  Find the existing protocol block if a ptag is specified, or create
                     71:      *  a new one.
                     72:      */
                     73:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
                     74:     if (p == NULL)
                     75:     {
                     76:         return (-1);
                     77:     }
                     78: 
                     79:     memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
                     80:     token_ring_hdr.token_ring_access_control    = ac;
                     81:     token_ring_hdr.token_ring_frame_control     = fc;
                     82:     memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
                     83:     memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
                     84:     token_ring_hdr.token_ring_llc_dsap          = dsap;
                     85:     token_ring_hdr.token_ring_llc_ssap          = ssap;
                     86:     token_ring_hdr.token_ring_llc_control_field = cf;
                     87:     memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                     88:     token_ring_hdr.token_ring_type              = htons(type);
                     89: 
1.1.1.2 ! misho      90:     n = libnet_pblock_append(l, p, (uint8_t *)&token_ring_hdr, 
1.1       misho      91:             LIBNET_TOKEN_RING_H);
                     92:     if (n == -1)
                     93:     {
                     94:         goto bad;
                     95:     }
                     96:  
1.1.1.2 ! misho      97:     /* boilerplate payload sanity check / append macro */
        !            98:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho      99:  
                    100:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                    101:             LIBNET_PBLOCK_TOKEN_RING_H));
                    102: bad:
                    103:     libnet_pblock_delete(l, p);
                    104:     return (-1);
                    105: }
                    106: 
                    107: 
                    108: libnet_ptag_t
1.1.1.2 ! misho     109: libnet_autobuild_token_ring(uint8_t ac, uint8_t fc, const uint8_t *dst, 
        !           110: uint8_t dsap, uint8_t ssap, uint8_t cf, const uint8_t *org, uint16_t type, 
1.1       misho     111: libnet_t *l)
                    112: {
1.1.1.2 ! misho     113:     uint32_t n, h;
1.1       misho     114:     struct libnet_token_ring_addr *src;
                    115:     libnet_pblock_t *p;
                    116:     libnet_ptag_t ptag;
                    117:     struct libnet_token_ring_hdr token_ring_hdr;
                    118: 
                    119:     if (l == NULL)
                    120:     { 
                    121:         return (-1);
                    122:     } 
                    123: 
                    124:     /* sanity check injection type if we're not in advanced mode */
                    125:     if (l->injection_type != LIBNET_LINK &&
                    126:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                    127:     {
                    128:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    129:                 "%s(): called with non-link layer wire injection primitive\n",
                    130:                 __func__);         
                    131:         p = NULL;
                    132:         goto bad;
                    133:     }
                    134: 
                    135:     n = LIBNET_TOKEN_RING_H;
                    136:     h = 0;
                    137:     ptag = LIBNET_PTAG_INITIALIZER;
                    138: 
                    139:     /* Token Ring and Ethernet have the same address size - so just typecast */
                    140:     src = (struct libnet_token_ring_addr *) libnet_get_hwaddr(l);
                    141:     if (src == NULL)
                    142:     {
                    143:         /* err msg set in libnet_get_hwaddr() */
                    144:         return (-1);
                    145:     }
                    146: 
                    147:     /*
                    148:      *  Create a new pblock. 
                    149:      */
                    150:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_TOKEN_RING_H);
                    151:     if (p == NULL)
                    152:     {
                    153:         return (-1);
                    154:     }
                    155: 
                    156:     memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
                    157:     token_ring_hdr.token_ring_access_control    = ac;
                    158:     token_ring_hdr.token_ring_frame_control     = fc;
                    159:     memcpy(token_ring_hdr.token_ring_dhost, dst, TOKEN_RING_ADDR_LEN);
                    160:     memcpy(token_ring_hdr.token_ring_shost, src, TOKEN_RING_ADDR_LEN);
                    161:     token_ring_hdr.token_ring_llc_dsap          = dsap;
                    162:     token_ring_hdr.token_ring_llc_ssap          = ssap;
                    163:     token_ring_hdr.token_ring_llc_control_field = cf;
                    164:     memcpy(&token_ring_hdr.token_ring_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                    165:     token_ring_hdr.token_ring_type              = htons(type);
                    166: 
                    167: 
1.1.1.2 ! misho     168:     n = libnet_pblock_append(l, p, (uint8_t *)&token_ring_hdr, 
1.1       misho     169:             LIBNET_TOKEN_RING_H);
                    170:     if (n == -1)
                    171:     {
                    172:         goto bad;
                    173:     }
                    174: 
                    175:     return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_TOKEN_RING_H));
                    176: bad:
                    177:     libnet_pblock_delete(l, p);
                    178:     return (-1); 
                    179: }
                    180: /* EOF */

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