xref: /openbsd-src/usr.sbin/dhcpd/convert.c (revision 837cddff4b95f9432151663c8b92d709c11482aa)
1*837cddffSkrw /*	$OpenBSD: convert.c,v 1.5 2016/02/06 23:50:10 krw Exp $	*/
2e853bc5dShenning 
3c824f21bShenning /*
4c824f21bShenning  * Safe copying of option values into and out of the option buffer,
5c824f21bShenning  * which can't be assumed to be aligned.
6c824f21bShenning  */
7e853bc5dShenning 
8e853bc5dShenning /*
9e853bc5dShenning  * Copyright (c) 1995, 1996 The Internet Software Consortium.
10e853bc5dShenning  * All rights reserved.
11e853bc5dShenning  *
12e853bc5dShenning  * Redistribution and use in source and binary forms, with or without
13e853bc5dShenning  * modification, are permitted provided that the following conditions
14e853bc5dShenning  * are met:
15e853bc5dShenning  *
16e853bc5dShenning  * 1. Redistributions of source code must retain the above copyright
17e853bc5dShenning  *    notice, this list of conditions and the following disclaimer.
18e853bc5dShenning  * 2. Redistributions in binary form must reproduce the above copyright
19e853bc5dShenning  *    notice, this list of conditions and the following disclaimer in the
20e853bc5dShenning  *    documentation and/or other materials provided with the distribution.
21e853bc5dShenning  * 3. Neither the name of The Internet Software Consortium nor the names
22e853bc5dShenning  *    of its contributors may be used to endorse or promote products derived
23e853bc5dShenning  *    from this software without specific prior written permission.
24e853bc5dShenning  *
25e853bc5dShenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
26e853bc5dShenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27e853bc5dShenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28e853bc5dShenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29e853bc5dShenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
30e853bc5dShenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31e853bc5dShenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32e853bc5dShenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33e853bc5dShenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34e853bc5dShenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35e853bc5dShenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36e853bc5dShenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37e853bc5dShenning  * SUCH DAMAGE.
38e853bc5dShenning  *
39e853bc5dShenning  * This software has been written for the Internet Software Consortium
40e853bc5dShenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
41e853bc5dShenning  * Enterprises.  To learn more about the Internet Software Consortium,
42e853bc5dShenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
43e853bc5dShenning  * Enterprises, see ``http://www.vix.com''.
44e853bc5dShenning  */
45e853bc5dShenning 
46*837cddffSkrw #include <sys/types.h>
47*837cddffSkrw #include <sys/socket.h>
48*837cddffSkrw 
49*837cddffSkrw #include <net/if.h>
50*837cddffSkrw 
51*837cddffSkrw #include <netinet/in.h>
52*837cddffSkrw 
53*837cddffSkrw #include <stdio.h>
54*837cddffSkrw #include <stdlib.h>
55*837cddffSkrw #include <string.h>
56*837cddffSkrw 
57*837cddffSkrw #include "dhcp.h"
58*837cddffSkrw #include "tree.h"
59e853bc5dShenning #include "dhcpd.h"
60e853bc5dShenning 
61c824f21bShenning u_int32_t
getULong(unsigned char * buf)62c824f21bShenning getULong(unsigned char *buf)
63e853bc5dShenning {
64e853bc5dShenning 	u_int32_t ibuf;
65e853bc5dShenning 
66e853bc5dShenning 	memcpy(&ibuf, buf, sizeof(ibuf));
67c824f21bShenning 	return (ntohl(ibuf));
68e853bc5dShenning }
69e853bc5dShenning 
70c824f21bShenning u_int16_t
getUShort(unsigned char * buf)71c824f21bShenning getUShort(unsigned char *buf)
72e853bc5dShenning {
73e853bc5dShenning 	u_int16_t ibuf;
74e853bc5dShenning 
75e853bc5dShenning 	memcpy(&ibuf, buf, sizeof(ibuf));
76c824f21bShenning 	return (ntohs(ibuf));
77e853bc5dShenning }
78e853bc5dShenning 
79c824f21bShenning void
putULong(unsigned char * obuf,u_int32_t val)80c824f21bShenning putULong(unsigned char *obuf, u_int32_t val)
81e853bc5dShenning {
82e853bc5dShenning 	u_int32_t tmp = htonl(val);
83c824f21bShenning 
84c824f21bShenning 	memcpy(obuf, &tmp, sizeof(tmp));
85e853bc5dShenning }
86e853bc5dShenning 
87c824f21bShenning void
putLong(unsigned char * obuf,int32_t val)88c824f21bShenning putLong(unsigned char *obuf, int32_t val)
89e853bc5dShenning {
90e853bc5dShenning 	int32_t tmp = htonl(val);
91c824f21bShenning 
92c824f21bShenning 	memcpy(obuf, &tmp, sizeof(tmp));
93e853bc5dShenning }
94e853bc5dShenning 
95c824f21bShenning void
putUShort(unsigned char * obuf,unsigned int val)96c824f21bShenning putUShort(unsigned char *obuf, unsigned int val)
97e853bc5dShenning {
98e853bc5dShenning 	u_int16_t tmp = htons(val);
99c824f21bShenning 
100c824f21bShenning 	memcpy(obuf, &tmp, sizeof(tmp));
101e853bc5dShenning }
102e853bc5dShenning 
103c824f21bShenning void
putShort(unsigned char * obuf,int val)104c824f21bShenning putShort(unsigned char *obuf, int val)
105e853bc5dShenning {
106e853bc5dShenning 	int16_t tmp = htons(val);
107e853bc5dShenning 
108c824f21bShenning 	memcpy(obuf, &tmp, sizeof(tmp));
109c824f21bShenning }
110