lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * Functions to convert between host and network byte order. |
| 3 | * |
| 4 | * Copyright (C) 2003-2006 by Erik Andersen <andersen@uclibc.org> |
| 5 | * |
| 6 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include <endian.h> |
| 11 | #include <byteswap.h> |
| 12 | #include <netinet/in.h> |
| 13 | |
| 14 | #undef ntohl |
| 15 | #undef ntohs |
| 16 | #undef htonl |
| 17 | #undef htons |
| 18 | |
| 19 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 20 | uint32_t ntohl (uint32_t x) |
| 21 | { |
| 22 | return x; |
| 23 | } |
| 24 | |
| 25 | uint16_t ntohs (uint16_t x) |
| 26 | { |
| 27 | return x; |
| 28 | } |
| 29 | |
| 30 | uint32_t htonl (uint32_t x) |
| 31 | { |
| 32 | return x; |
| 33 | } |
| 34 | |
| 35 | uint16_t htons (uint16_t x) |
| 36 | { |
| 37 | return x; |
| 38 | } |
| 39 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
| 40 | uint32_t ntohl (uint32_t x) |
| 41 | { |
| 42 | return __bswap_32(x); |
| 43 | } |
| 44 | |
| 45 | uint16_t ntohs (uint16_t x) |
| 46 | { |
| 47 | return __bswap_16(x); |
| 48 | } |
| 49 | |
| 50 | uint32_t htonl (uint32_t x) |
| 51 | { |
| 52 | return __bswap_32(x); |
| 53 | } |
| 54 | |
| 55 | uint16_t htons (uint16_t x) |
| 56 | { |
| 57 | return __bswap_16(x); |
| 58 | } |
| 59 | #else |
| 60 | #error "You seem to have an unsupported byteorder" |
| 61 | #endif |
| 62 | |
| 63 | libc_hidden_def(ntohl) |
| 64 | libc_hidden_def(ntohs) |
| 65 | libc_hidden_def(htonl) |
| 66 | libc_hidden_def(htons) |