xref: /freebsd/usr.sbin/ppp/ua.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
19e8ec64bSBrian Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
49e8ec64bSBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
59e8ec64bSBrian Somers  * All rights reserved.
69e8ec64bSBrian Somers  *
79e8ec64bSBrian Somers  * Redistribution and use in source and binary forms, with or without
89e8ec64bSBrian Somers  * modification, are permitted provided that the following conditions
99e8ec64bSBrian Somers  * are met:
109e8ec64bSBrian Somers  * 1. Redistributions of source code must retain the above copyright
119e8ec64bSBrian Somers  *    notice, this list of conditions and the following disclaimer.
129e8ec64bSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
139e8ec64bSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
149e8ec64bSBrian Somers  *    documentation and/or other materials provided with the distribution.
159e8ec64bSBrian Somers  *
169e8ec64bSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179e8ec64bSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189e8ec64bSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199e8ec64bSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209e8ec64bSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219e8ec64bSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229e8ec64bSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239e8ec64bSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249e8ec64bSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259e8ec64bSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269e8ec64bSBrian Somers  * SUCH DAMAGE.
279e8ec64bSBrian Somers  */
289e8ec64bSBrian Somers 
299e8ec64bSBrian Somers #ifdef __i386__		/* Do any other archs not care about alignment ? */
309e8ec64bSBrian Somers 
319e8ec64bSBrian Somers #  define ua_htonl(src, tgt) (*(u_int32_t *)(tgt) = htonl(*(u_int32_t *)(src)))
329e8ec64bSBrian Somers #  define ua_ntohl(src, tgt) (*(u_int32_t *)(tgt) = ntohl(*(u_int32_t *)(src)))
339e8ec64bSBrian Somers #  define ua_htons(src, tgt) (*(u_int16_t *)(tgt) = htons(*(u_int16_t *)(src)))
349e8ec64bSBrian Somers #  define ua_ntohs(src, tgt) (*(u_int16_t *)(tgt) = ntohs(*(u_int16_t *)(src)))
359e8ec64bSBrian Somers 
369e8ec64bSBrian Somers #else	/* We care about alignment (or else drop a core !) */
379e8ec64bSBrian Somers 
389e8ec64bSBrian Somers #  define ua_htonl(src, tgt)				\
399e8ec64bSBrian Somers     do {						\
409e8ec64bSBrian Somers       u_int32_t __oh;					\
419e8ec64bSBrian Somers       memcpy(&__oh, (src), sizeof __oh);		\
429e8ec64bSBrian Somers       *(u_char *)(tgt) = __oh >> 24;			\
439e8ec64bSBrian Somers       *((u_char *)(tgt) + 1) = (__oh >> 16) & 0xff;	\
449e8ec64bSBrian Somers       *((u_char *)(tgt) + 2) = (__oh >> 8) & 0xff;	\
459e8ec64bSBrian Somers       *((u_char *)(tgt) + 3) = __oh & 0xff;		\
469e8ec64bSBrian Somers     } while (0)
479e8ec64bSBrian Somers 
489e8ec64bSBrian Somers #  define ua_ntohl(src, tgt)				\
499e8ec64bSBrian Somers     do {						\
509e8ec64bSBrian Somers       u_int32_t __nh;					\
519e8ec64bSBrian Somers       __nh = ((u_int32_t)*(u_char *)(src) << 24) |	\
529e8ec64bSBrian Somers           ((u_int32_t)*((u_char *)(src) + 1) << 16) |	\
539e8ec64bSBrian Somers           ((u_int32_t)*((u_char *)(src) + 2) << 8) |	\
549e8ec64bSBrian Somers           (u_int32_t)*((u_char *)(src) + 3);		\
559e8ec64bSBrian Somers       memcpy((tgt), &__nh, sizeof __nh);		\
569e8ec64bSBrian Somers     } while (0)
579e8ec64bSBrian Somers 
589e8ec64bSBrian Somers #  define ua_htons(src, tgt)				\
599e8ec64bSBrian Somers     do {						\
609e8ec64bSBrian Somers       u_int16_t __oh;					\
619e8ec64bSBrian Somers       memcpy(&__oh, (src), sizeof __oh);		\
629e8ec64bSBrian Somers       *(u_char *)(tgt) = __oh >> 8;			\
639e8ec64bSBrian Somers       *((u_char *)(tgt) + 1) = __oh & 0xff;		\
649e8ec64bSBrian Somers     } while (0)
659e8ec64bSBrian Somers 
669e8ec64bSBrian Somers #  define ua_ntohs(src, tgt)				\
679e8ec64bSBrian Somers     do {						\
6842628155SBrian Somers       u_int16_t __nh;					\
6942628155SBrian Somers       __nh = ((u_int16_t)*(u_char *)(src) << 8) |	\
7042628155SBrian Somers           (u_int16_t)*((u_char *)(src) + 1);		\
719e8ec64bSBrian Somers       memcpy((tgt), &__nh, sizeof __nh);		\
729e8ec64bSBrian Somers     } while (0)
739e8ec64bSBrian Somers 
749e8ec64bSBrian Somers #endif
75