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

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
                     43: libnet_build_dhcpv4(u_int8_t opcode, u_int8_t htype, u_int8_t hlen, 
                     44: u_int8_t hopcount, u_int32_t xid, u_int16_t secs, u_int16_t flags,
                     45: u_int32_t cip, u_int32_t yip, u_int32_t sip, u_int32_t gip, u_int8_t *chaddr,
                     46: u_int8_t *sname, u_int8_t *file, u_int8_t *payload, u_int32_t payload_s,
                     47: libnet_t *l, libnet_ptag_t ptag)
                     48: {
                     49:     u_int32_t n, h;
                     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:     { 
                     86:         memcpy(dhcp_hdr.dhcp_chaddr, chaddr, sizeof (dhcp_hdr.dhcp_chaddr));
                     87:         dhcp_hdr.dhcp_chaddr[sizeof(dhcp_hdr.dhcp_chaddr) - 1] = 0;
                     88:     }
                     89:     else
                     90:     {
                     91:         memset(dhcp_hdr.dhcp_chaddr, 0, sizeof (dhcp_hdr.dhcp_chaddr));
                     92:     }
                     93:  
                     94:     if (sname)
                     95:     { 
                     96:         memcpy(dhcp_hdr.dhcp_sname, sname, sizeof (dhcp_hdr.dhcp_sname));
                     97:         dhcp_hdr.dhcp_sname[sizeof(dhcp_hdr.dhcp_sname) - 1] = 0;
                     98:     }
                     99:     else
                    100:     {
                    101:         memset(dhcp_hdr.dhcp_sname, 0, sizeof (dhcp_hdr.dhcp_sname));
                    102:     }
                    103: 
                    104:     if (file)
                    105:     {
                    106:         memcpy(dhcp_hdr.dhcp_file, file, sizeof (dhcp_hdr.dhcp_file));
                    107:         dhcp_hdr.dhcp_file[sizeof(dhcp_hdr.dhcp_file) - 1] = 0;
                    108:     }
                    109:     else
                    110:     {
                    111:         memset(dhcp_hdr.dhcp_file, 0, sizeof(dhcp_hdr.dhcp_file));
                    112:     }
                    113:     dhcp_hdr.dhcp_magic = htonl(DHCP_MAGIC);    /* should this be tunable? */
                    114: 
                    115:     n = libnet_pblock_append(l, p, (u_int8_t *)&dhcp_hdr, LIBNET_DHCPV4_H);
                    116:     if (n == -1)
                    117:     {
                    118:         goto bad;
                    119:     }
                    120: 
                    121:     if ((payload && !payload_s) || (!payload && payload_s))
                    122:     {
                    123:          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    124:                  "%s(): payload inconsistency\n", __func__);
                    125:         goto bad;
                    126:     }
                    127:  
                    128:     if (payload && payload_s)
                    129:     {
                    130:         n = libnet_pblock_append(l, p, payload, payload_s);
                    131:         if (n == -1)
                    132:         {
                    133:             goto bad;
                    134:         }
                    135:     }
                    136:  
                    137:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                    138:             LIBNET_PBLOCK_DHCPV4_H));
                    139: bad:
                    140:     libnet_pblock_delete(l, p);
                    141:     return (-1);
                    142: }
                    143: 
                    144: libnet_ptag_t
                    145: libnet_build_bootpv4(u_int8_t opcode, u_int8_t htype, u_int8_t hlen,
                    146: u_int8_t hopcount, u_int32_t xid, u_int16_t secs, u_int16_t flags,
                    147: u_int32_t cip, u_int32_t yip, u_int32_t sip, u_int32_t gip, u_int8_t *chaddr,
                    148: u_int8_t *sname, u_int8_t *file, u_int8_t *payload, u_int32_t payload_s,
                    149: libnet_t *l, libnet_ptag_t ptag)
                    150: {
                    151:     return (libnet_build_dhcpv4(opcode, htype, hlen, hopcount, xid, secs,
                    152:         flags, cip, yip, sip, gip, chaddr, sname, file, payload, payload_s, 
                    153:         l, ptag));
                    154: }
                    155: 
                    156: /* EOF */

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