File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_fddi.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 11:54:42 2013 UTC (11 years ago) by misho
Branches: libnet, MAIN
CVS tags: v1_1_6p5, v1_1_6p4, v1_1_6p0, v1_1_6, HEAD
1.1.6

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

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