File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_dhcp.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, 1 week ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_build_dhcp.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet
    5:  *  libnet_build_dhcp.c - DHCP/BOOTP 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_dhcpv4(uint8_t opcode, uint8_t htype, uint8_t hlen, 
   37: uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags,
   38: uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr,
   39: const char *sname, const char *file, const uint8_t *payload, uint32_t payload_s,
   40: libnet_t *l, libnet_ptag_t ptag)
   41: {
   42:     uint32_t n, h;
   43:     libnet_pblock_t *p; 
   44:     struct libnet_dhcpv4_hdr dhcp_hdr;
   45: 
   46:     if (l == NULL)
   47:     { 
   48:         return (-1);
   49:     } 
   50: 
   51:     n = LIBNET_DHCPV4_H + payload_s;
   52:     h = 0;          /* no checksum */
   53:  
   54:     /*
   55:      *  Find the existing protocol block if a ptag is specified, or create
   56:      *  a new one.
   57:      */
   58:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_DHCPV4_H);
   59:     if (p == NULL)
   60:     {
   61:         return (-1);
   62:     }
   63: 
   64:     memset(&dhcp_hdr, 0, sizeof(dhcp_hdr));
   65:     dhcp_hdr.dhcp_opcode    = opcode;
   66:     dhcp_hdr.dhcp_htype     = htype;
   67:     dhcp_hdr.dhcp_hlen      = hlen;
   68:     dhcp_hdr.dhcp_hopcount  = hopcount;
   69:     dhcp_hdr.dhcp_xid       = htonl(xid);
   70:     dhcp_hdr.dhcp_secs      = htons(secs);
   71:     dhcp_hdr.dhcp_flags     = htons(flags);
   72:     dhcp_hdr.dhcp_cip       = htonl(cip);
   73:     dhcp_hdr.dhcp_yip       = htonl(yip);
   74:     dhcp_hdr.dhcp_sip       = htonl(sip);
   75:     dhcp_hdr.dhcp_gip       = htonl(gip);
   76: 
   77:     if (chaddr)
   78:     {
   79:         size_t n = sizeof (dhcp_hdr.dhcp_chaddr);
   80:         if (hlen < n)
   81:             n = hlen;
   82:         memcpy(dhcp_hdr.dhcp_chaddr, chaddr, n);
   83:     }
   84:     if (sname)
   85:     { 
   86:         strncpy(dhcp_hdr.dhcp_sname, sname, sizeof (dhcp_hdr.dhcp_sname) - 1);
   87:     }
   88:     if (file)
   89:     {
   90:         strncpy(dhcp_hdr.dhcp_file, file, sizeof (dhcp_hdr.dhcp_file) - 1);
   91:     }
   92:     dhcp_hdr.dhcp_magic = htonl(DHCP_MAGIC);
   93: 
   94:     n = libnet_pblock_append(l, p, (uint8_t *)&dhcp_hdr, LIBNET_DHCPV4_H);
   95:     if (n == -1)
   96:     {
   97:         goto bad;
   98:     }
   99: 
  100:     if (payload_s && !payload)
  101:     {
  102:          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
  103:                  "%s(): payload inconsistency", __func__);
  104:         goto bad;
  105:     }
  106:  
  107:     if (payload_s)
  108:     {
  109:         n = libnet_pblock_append(l, p, payload, payload_s);
  110:         if (n == -1)
  111:         {
  112:             goto bad;
  113:         }
  114:     }
  115:  
  116:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
  117:             LIBNET_PBLOCK_DHCPV4_H));
  118: bad:
  119:     libnet_pblock_delete(l, p);
  120:     return (-1);
  121: }
  122: 
  123: libnet_ptag_t
  124: libnet_build_bootpv4(uint8_t opcode, uint8_t htype, uint8_t hlen,
  125: uint8_t hopcount, uint32_t xid, uint16_t secs, uint16_t flags,
  126: uint32_t cip, uint32_t yip, uint32_t sip, uint32_t gip, const uint8_t *chaddr,
  127: const char *sname, const char *file, const uint8_t *payload, uint32_t payload_s,
  128: libnet_t *l, libnet_ptag_t ptag)
  129: {
  130:     return (libnet_build_dhcpv4(opcode, htype, hlen, hopcount, xid, secs,
  131:         flags, cip, yip, sip, gip, chaddr, sname, file, payload, payload_s, 
  132:         l, ptag));
  133: }
  134: 
  135: /**
  136:  * Local Variables:
  137:  *  indent-tabs-mode: nil
  138:  *  c-file-style: "stroustrup"
  139:  * End:
  140:  */

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