File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_hsrp.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 11:54:42 2013 UTC (10 years, 11 months 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_hsrp.c - HSRP packet assembler
    4:  *
    5:  *  Copyright (c) 2004 David Barroso Berrueta <tomac@wasahero.org>
    6:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.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_hsrp(uint8_t version, uint8_t opcode, uint8_t state, 
   43: uint8_t hello_time, uint8_t hold_time, uint8_t priority, uint8_t group,
   44: uint8_t reserved, uint8_t authdata[HSRP_AUTHDATA_LENGTH], uint32_t virtual_ip,
   45: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
   46: {
   47:     uint32_t n;
   48:     libnet_pblock_t *p;
   49:     struct libnet_hsrp_hdr hsrp_hdr;
   50: 
   51:     if (l == NULL)
   52:     { 
   53:         return (-1);
   54:     } 
   55: 
   56:     /*
   57:      *  Find the existing protocol block if a ptag is specified, or create
   58:      *  a new one.
   59:      */
   60:     p = libnet_pblock_probe(l, ptag, LIBNET_HSRP_H + payload_s, LIBNET_PBLOCK_HSRP_H);
   61:     if (p == NULL)
   62:     {
   63:         return (-1);
   64:     }
   65: 
   66:     memset(&hsrp_hdr, 0, sizeof(hsrp_hdr));
   67:     hsrp_hdr.version = version;
   68:     hsrp_hdr.opcode = opcode;
   69:     hsrp_hdr.state = state;
   70:     hsrp_hdr.hello_time = hello_time;
   71:     hsrp_hdr.hold_time = hold_time;
   72:     hsrp_hdr.priority = priority;
   73:     hsrp_hdr.group = group;
   74:     hsrp_hdr.reserved = reserved;
   75:     memcpy(hsrp_hdr.authdata, authdata, HSRP_AUTHDATA_LENGTH*sizeof(uint8_t));
   76:     hsrp_hdr.virtual_ip = virtual_ip;
   77: 
   78:     n = libnet_pblock_append(l, p, (uint8_t *)&hsrp_hdr, LIBNET_HSRP_H);
   79:     if (n == -1)
   80:     {
   81:         goto bad;
   82:     }
   83: 
   84:     if (payload_s && !payload)
   85:     {
   86:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
   87:                 "%s(): payload inconsistency\n", __func__);
   88:         goto bad;
   89:     }
   90:  
   91:     if (payload_s)
   92:     {
   93:         n = libnet_pblock_append(l, p, payload, payload_s);
   94:         if (n == -1)
   95:         {
   96:             goto bad;
   97:         }
   98:     }
   99:  
  100:     return (ptag ? ptag : libnet_pblock_update(l, p, 0, LIBNET_PBLOCK_HSRP_H));
  101: bad:
  102:     libnet_pblock_delete(l, p);
  103:     return (-1);
  104: }
  105: /* EOF */

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