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

1.1       misho       1: /*
                      2:  *  libnet
                      3:  *  libnet_build_fddi.c - Fiber Distributed Data Interface 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_fddi(uint8_t fc, const uint8_t *dst, const uint8_t *src, uint8_t dsap,
                     36: uint8_t ssap, uint8_t cf, const uint8_t *org, uint16_t type, const uint8_t *payload,
                     37: 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;
                     40:     uint16_t protocol_type;
1.1       misho      41:     libnet_pblock_t *p;
                     42:     struct libnet_fddi_hdr fddi_hdr;
                     43: 
                     44:     if (l == NULL)
                     45:     { 
                     46:         return (-1);
                     47:     } 
                     48: 
                     49:     /* sanity check injection type if we're not in advanced mode */
                     50:     if (l->injection_type != LIBNET_LINK &&
                     51:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                     52:     {
                     53:          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     54:             "%s(): called with non-link layer wire injection primitive",
                     55:                     __func__);
                     56:         p = NULL;
                     57:         goto bad;
                     58:     }
                     59: 
                     60:     n = LIBNET_FDDI_H + payload_s;
                     61:     h = 0;
                     62:  
                     63:     /*
                     64:      *  Find the existing protocol block if a ptag is specified, or create
                     65:      *  a new one.
                     66:      */
                     67:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_FDDI_H);
                     68:     if (p == NULL)
                     69:     {
                     70:         return (-1);
                     71:     }
                     72: 
                     73:     memset(&fddi_hdr, 0, sizeof(fddi_hdr));
                     74:     fddi_hdr.fddi_frame_control     = fc;            /* Class/Format/Priority */
                     75:     memcpy(fddi_hdr.fddi_dhost, dst, FDDI_ADDR_LEN);  /* dst fddi address */
                     76:     memcpy(fddi_hdr.fddi_shost, src, FDDI_ADDR_LEN);  /* source fddi address */
                     77:     fddi_hdr.fddi_llc_dsap          = dsap;           /* */
                     78:     fddi_hdr.fddi_llc_ssap          = ssap;           /* */
                     79:     fddi_hdr.fddi_llc_control_field = cf;             /* Class/Format/Priority */
                     80:     memcpy(&fddi_hdr.fddi_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                     81: 
                     82:     /* Deal with unaligned int16_t for type */
                     83:     protocol_type = htons(type);
                     84:     memcpy(&fddi_hdr.fddi_type, &protocol_type, sizeof(int16_t));   /* Protocol Type */
                     85: 
1.1.1.2   misho      86:     n = libnet_pblock_append(l, p, (uint8_t *)&fddi_hdr, LIBNET_FDDI_H);
1.1       misho      87:     if (n == -1)
                     88:     {
                     89:         goto bad;
                     90:     }
                     91:  
1.1.1.2   misho      92:     /* boilerplate payload sanity check / append macro */
                     93:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho      94:  
                     95:     return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_FDDI_H));
                     96: bad:
                     97:     libnet_pblock_delete(l, p);
                     98:     return (-1);
                     99: }
                    100: 
                    101: 
                    102: libnet_ptag_t
1.1.1.2   misho     103: libnet_autobuild_fddi(uint8_t fc, const uint8_t *dst, uint8_t dsap, uint8_t ssap,
                    104: uint8_t cf, const uint8_t *org, uint16_t type, libnet_t *l)
1.1       misho     105: {
1.1.1.2   misho     106:     uint32_t n, h;
                    107:     uint16_t protocol_type;
1.1       misho     108:     struct libnet_fddi_addr *src;
                    109:     libnet_pblock_t *p;
                    110:     libnet_ptag_t ptag;
                    111:     struct libnet_fddi_hdr fddi_hdr;
                    112: 
                    113:     if (l == NULL)
                    114:     { 
                    115:         return (-1);
                    116:     } 
                    117: 
                    118:     /* sanity check injection type if we're not in advanced mode */
                    119:     if (l->injection_type != LIBNET_LINK &&
                    120:             !(((l->injection_type) & LIBNET_ADV_MASK)))
                    121:     {
                    122:          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    123:             "%s(): called with non-link layer wire injection primitive",
                    124:                     __func__);
                    125:         p = NULL;
                    126:         goto bad;
                    127:     }
                    128: 
                    129:     n = LIBNET_FDDI_H;
                    130:     h = 0;
                    131:     ptag = LIBNET_PTAG_INITIALIZER;
                    132: 
                    133:     /* FDDI and Ethernet have the same address size - so just typecast */
                    134:     src = (struct libnet_fddi_addr *) libnet_get_hwaddr(l);
                    135:     if (src == NULL)
                    136:     {
                    137:         /* err msg set in libnet_get_hwaddr() */
                    138:         return (-1);
                    139:     }
                    140: 
                    141:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_FDDI_H);
                    142:     if (p == NULL)
                    143:     {
                    144:         return (-1);
                    145:     }
                    146: 
1.1.1.2   misho     147:     memset(&fddi_hdr, 0, sizeof(fddi_hdr));
                    148:     fddi_hdr.fddi_frame_control     = fc;            /* Class/Format/Priority */
                    149:     memcpy(fddi_hdr.fddi_dhost, dst, FDDI_ADDR_LEN); /* destination fddi addr */
                    150:     memcpy(fddi_hdr.fddi_shost, src, FDDI_ADDR_LEN); /* source fddi addr */
                    151:     fddi_hdr.fddi_llc_dsap          = dsap;          /* */
                    152:     fddi_hdr.fddi_llc_ssap          = ssap;          /* */
                    153:     fddi_hdr.fddi_llc_control_field = cf;            /* Class/Format/Priority */
1.1       misho     154:     memcpy(&fddi_hdr.fddi_llc_org_code, org, LIBNET_ORG_CODE_SIZE); 
                    155: 
                    156:     /* Deal with unaligned int16_t for type */
                    157:     protocol_type = htons(type);
1.1.1.2   misho     158:     memcpy(&fddi_hdr.fddi_type, &protocol_type, sizeof(int16_t));
1.1       misho     159: 
1.1.1.2   misho     160:     n = libnet_pblock_append(l, p, (uint8_t *)&fddi_hdr, LIBNET_FDDI_H);
1.1       misho     161:     if (n == -1)
                    162:     {
                    163:         goto bad;
                    164:     }
                    165: 
                    166:     return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_FDDI_H));
                    167: bad:
                    168:     libnet_pblock_delete(l, p);
                    169:     return (-1); 
                    170: }
1.1.1.3 ! misho     171: 
        !           172: /**
        !           173:  * Local Variables:
        !           174:  *  indent-tabs-mode: nil
        !           175:  *  c-file-style: "stroustrup"
        !           176:  * End:
        !           177:  */

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