blob: 1a5863286316eab1f00b78fd19dbad7e5d8a54fd [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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
20uint32_t ntohl (uint32_t x)
21{
22 return x;
23}
24
25uint16_t ntohs (uint16_t x)
26{
27 return x;
28}
29
30uint32_t htonl (uint32_t x)
31{
32 return x;
33}
34
35uint16_t htons (uint16_t x)
36{
37 return x;
38}
39#elif __BYTE_ORDER == __LITTLE_ENDIAN
40uint32_t ntohl (uint32_t x)
41{
42 return __bswap_32(x);
43}
44
45uint16_t ntohs (uint16_t x)
46{
47 return __bswap_16(x);
48}
49
50uint32_t htonl (uint32_t x)
51{
52 return __bswap_32(x);
53}
54
55uint16_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
63libc_hidden_def(ntohl)
64libc_hidden_def(ntohs)
65libc_hidden_def(htonl)
66libc_hidden_def(htons)