138814Sbrian/*- 238814Sbrian * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 338814Sbrian * All rights reserved. 438814Sbrian * 538814Sbrian * Redistribution and use in source and binary forms, with or without 638814Sbrian * modification, are permitted provided that the following conditions 738814Sbrian * are met: 838814Sbrian * 1. Redistributions of source code must retain the above copyright 938814Sbrian * notice, this list of conditions and the following disclaimer. 1038814Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1138814Sbrian * notice, this list of conditions and the following disclaimer in the 1238814Sbrian * documentation and/or other materials provided with the distribution. 1338814Sbrian * 1438814Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1538814Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1638814Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1738814Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1838814Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1938814Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2038814Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2138814Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2238814Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2338814Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2438814Sbrian * SUCH DAMAGE. 2538814Sbrian * 2650479Speter * $FreeBSD: releng/10.2/usr.sbin/ppp/ua.h 50479 1999-08-28 01:35:59Z peter $ 2738814Sbrian */ 2838814Sbrian 2938814Sbrian#ifdef __i386__ /* Do any other archs not care about alignment ? */ 3038814Sbrian 3138814Sbrian# define ua_htonl(src, tgt) (*(u_int32_t *)(tgt) = htonl(*(u_int32_t *)(src))) 3238814Sbrian# define ua_ntohl(src, tgt) (*(u_int32_t *)(tgt) = ntohl(*(u_int32_t *)(src))) 3338814Sbrian# define ua_htons(src, tgt) (*(u_int16_t *)(tgt) = htons(*(u_int16_t *)(src))) 3438814Sbrian# define ua_ntohs(src, tgt) (*(u_int16_t *)(tgt) = ntohs(*(u_int16_t *)(src))) 3538814Sbrian 3638814Sbrian#else /* We care about alignment (or else drop a core !) */ 3738814Sbrian 3838814Sbrian# define ua_htonl(src, tgt) \ 3938814Sbrian do { \ 4038814Sbrian u_int32_t __oh; \ 4138814Sbrian memcpy(&__oh, (src), sizeof __oh); \ 4238814Sbrian *(u_char *)(tgt) = __oh >> 24; \ 4338814Sbrian *((u_char *)(tgt) + 1) = (__oh >> 16) & 0xff; \ 4438814Sbrian *((u_char *)(tgt) + 2) = (__oh >> 8) & 0xff; \ 4538814Sbrian *((u_char *)(tgt) + 3) = __oh & 0xff; \ 4638814Sbrian } while (0) 4738814Sbrian 4838814Sbrian# define ua_ntohl(src, tgt) \ 4938814Sbrian do { \ 5038814Sbrian u_int32_t __nh; \ 5138814Sbrian __nh = ((u_int32_t)*(u_char *)(src) << 24) | \ 5238814Sbrian ((u_int32_t)*((u_char *)(src) + 1) << 16) | \ 5338814Sbrian ((u_int32_t)*((u_char *)(src) + 2) << 8) | \ 5438814Sbrian (u_int32_t)*((u_char *)(src) + 3); \ 5538814Sbrian memcpy((tgt), &__nh, sizeof __nh); \ 5638814Sbrian } while (0) 5738814Sbrian 5838814Sbrian# define ua_htons(src, tgt) \ 5938814Sbrian do { \ 6038814Sbrian u_int16_t __oh; \ 6138814Sbrian memcpy(&__oh, (src), sizeof __oh); \ 6238814Sbrian *(u_char *)(tgt) = __oh >> 8; \ 6338814Sbrian *((u_char *)(tgt) + 1) = __oh & 0xff; \ 6438814Sbrian } while (0) 6538814Sbrian 6638814Sbrian# define ua_ntohs(src, tgt) \ 6738814Sbrian do { \ 6839285Sbrian u_int16_t __nh; \ 6939285Sbrian __nh = ((u_int16_t)*(u_char *)(src) << 8) | \ 7039285Sbrian (u_int16_t)*((u_char *)(src) + 1); \ 7138814Sbrian memcpy((tgt), &__nh, sizeof __nh); \ 7238814Sbrian } while (0) 7338814Sbrian 7438814Sbrian#endif 75