File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_cdp.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_build_cdp.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet
    5:  *  libnet_build_cdp.c - CDP 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: 
   33: #include "common.h"
   34: 
   35: libnet_ptag_t
   36: libnet_build_cdp(uint8_t version, uint8_t ttl, uint16_t sum, uint16_t type,
   37: uint16_t len, const uint8_t *value, const uint8_t *payload, uint32_t payload_s,
   38: libnet_t *l, libnet_ptag_t ptag)
   39: {
   40:     uint32_t n,h;
   41:     libnet_pblock_t *p;
   42:     struct libnet_cdp_hdr cdp_hdr;
   43: 
   44:     if (l == NULL)
   45:     { 
   46:         return (-1); 
   47:     }
   48: 
   49:     n = LIBNET_CDP_H + LIBNET_CDP_H + len + payload_s;
   50:     h = LIBNET_CDP_H + LIBNET_CDP_H + len + payload_s;
   51: 
   52:     /*
   53:      *  Find the existing protocol block if a ptag is specified, or create
   54:      *  a new one.
   55:      */
   56:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_CDP_H);
   57:     if (p == NULL)
   58:     {
   59:         return (-1);
   60:     }
   61: 
   62:     memset(&cdp_hdr, 0, sizeof(cdp_hdr));
   63:     cdp_hdr.cdp_version = version;
   64:     cdp_hdr.cdp_ttl     = ttl;
   65:     cdp_hdr.cdp_sum     = (sum ? htons(sum) : 0);
   66:     cdp_hdr.cdp_type    = htons(type);
   67:     cdp_hdr.cdp_len     = htons(len + 4);   /* 4 bytes for len and type */
   68: 
   69:     n = libnet_pblock_append(l, p, (uint8_t *)&cdp_hdr, LIBNET_CDP_H);
   70:     if (n == -1)
   71:     {
   72:         goto bad;
   73:     }
   74: 
   75:     n = libnet_pblock_append(l, p, value, len);
   76:     if (n == -1)
   77:     {
   78:         /* err msg set in libnet_pblock_append() */
   79:         goto bad;
   80:     }
   81: 
   82:     /* boilerplate payload sanity check / append macro */
   83:     LIBNET_DO_PAYLOAD(l, p);
   84: 
   85:     if (sum == 0)
   86:     {
   87:         /*
   88:          *  If checksum is zero, by default libnet will compute a checksum
   89:          *  for the user.  The programmer can override this by calling
   90:          *  libnet_toggle_checksum(l, ptag, 1);
   91:          */
   92:         libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
   93:     }
   94:     return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_CDP_H));
   95: bad:
   96:     libnet_pblock_delete(l, p);
   97:     return (-1);
   98: }
   99: 
  100: 
  101: int
  102: /* Not Yet Implemented */
  103: libnet_build_cdp_value(uint16_t type, uint16_t len, uint8_t *value, libnet_t *l,
  104:         libnet_ptag_t ptag)
  105: {
  106:     uint32_t n;
  107:     libnet_pblock_t *p;
  108:     struct libnet_cdp_value_hdr cdp_value_hdr;
  109: 
  110:     if (l == NULL)
  111:     { 
  112:         return (-1); 
  113:     }
  114: 
  115:     /*
  116:      *  Find the existing protocol block.
  117:      */
  118:     p = libnet_pblock_find(l, ptag);
  119:     if (p == NULL)
  120:     {
  121:         /* err msg set in libnet_pblock_find */
  122:         return (-1);
  123:     }
  124:     if (p->type != LIBNET_PBLOCK_CDP_H)
  125:     {
  126:          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
  127:             "libnet_build_cdp_value: ptag references different type than expected");
  128:         return (-1);
  129:     }
  130: 
  131:     memset(&cdp_value_hdr, 0, sizeof(cdp_value_hdr));
  132:     cdp_value_hdr.cdp_type  = htons(type);
  133:     cdp_value_hdr.cdp_len   = htons(len + LIBNET_CDP_VALUE_H);   /* 4 bytes for len and type */
  134: 
  135:     switch (type)
  136:     {
  137:         case LIBNET_CDP_DEVID:
  138:             break;
  139:         case LIBNET_CDP_ADDRESS:
  140:             break;
  141:         case LIBNET_CDP_PORTID:
  142:             break;
  143:         case LIBNET_CDP_CAPABIL:
  144:             break;
  145:         case LIBNET_CDP_VERSION:
  146:             break;
  147:         case LIBNET_CDP_PLATFORM:
  148:             break;
  149:         case LIBNET_CDP_IPPREFIX:
  150:             break;
  151:         default:
  152:             break;
  153:     }
  154: 
  155:     n = libnet_pblock_append(l, p, (uint8_t *)&cdp_value_hdr, LIBNET_CDP_VALUE_H);
  156:     if (n == -1)
  157:     {
  158:         return (-1);
  159:     }
  160: 
  161:     n = libnet_pblock_append(l, p, value, len);
  162:     if (n == -1)
  163:     {
  164:         /* err msg set in libnet_pblock_append() */
  165:         return (-1);
  166:     }
  167: 
  168:     return (1);
  169: }
  170: 
  171: /**
  172:  * Local Variables:
  173:  *  indent-tabs-mode: nil
  174:  *  c-file-style: "stroustrup"
  175:  * End:
  176:  */

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