Annotation of embedaddon/libnet/src/libnet_build_dhcp.c, revision 1.1.1.2

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

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