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