Skip to content

Commit d16a918

Browse files
committed
C library: network byteorder functions
On some systems htonl etc. are just macros (always true for big-endian systems, where they are null macros; and also true on Mac OS). For others we to provide the implementation. In all cases the full support for bswap* is necessary, and that is now available as of the preceding commit. Also fixes arpa/inet.h being unavailable on Windows/Visual Studio.
1 parent 05bc9ed commit d16a918

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

regression/cbmc/inet_endian1/main.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _WIN32
2+
#include <arpa/inet.h>
3+
#include <assert.h>
4+
5+
int main()
6+
{
7+
uint32_t l;
8+
assert(l == ntohl(htonl(l)));
9+
10+
uint16_t s;
11+
assert(s == ntohs(htons(s)));
12+
13+
return 0;
14+
}
15+
16+
#else
17+
18+
int main()
19+
{
20+
return 0;
21+
}
22+
23+
#endif
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/library/inet.c

+107
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* FUNCTION: inet_addr */
22

3+
#ifndef _WIN32
4+
5+
#ifndef __CPROVER_INET_H_INCLUDED
36
#include <arpa/inet.h>
7+
#define __CPROVER_INET_H_INCLUDED
8+
#endif
49

510
in_addr_t __VERIFIER_nondet_in_addr_t();
611

@@ -16,8 +21,17 @@ in_addr_t inet_addr(const char *cp)
1621
return result;
1722
}
1823

24+
#endif
25+
1926
/* FUNCTION: inet_aton */
2027

28+
#ifndef _WIN32
29+
30+
#ifndef __CPROVER_INET_H_INCLUDED
31+
#include <arpa/inet.h>
32+
#define __CPROVER_INET_H_INCLUDED
33+
#endif
34+
2135
int __VERIFIER_nondet_int();
2236

2337
int inet_aton(const char *cp, struct in_addr *pin)
@@ -33,8 +47,17 @@ int inet_aton(const char *cp, struct in_addr *pin)
3347
return result;
3448
}
3549

50+
#endif
51+
3652
/* FUNCTION: inet_network */
3753

54+
#ifndef _WIN32
55+
56+
#ifndef __CPROVER_INET_H_INCLUDED
57+
#include <arpa/inet.h>
58+
#define __CPROVER_INET_H_INCLUDED
59+
#endif
60+
3861
in_addr_t __VERIFIER_nondet_in_addr_t();
3962

4063
in_addr_t inet_network(const char *cp)
@@ -48,3 +71,87 @@ in_addr_t inet_network(const char *cp)
4871
in_addr_t result=__VERIFIER_nondet_in_addr_t();
4972
return result;
5073
}
74+
75+
#endif
76+
77+
/* FUNCTION: htonl */
78+
79+
#ifndef __CPROVER_STDINT_H_INCLUDED
80+
#include <stdint.h>
81+
#define __CPROVER_STDINT_H_INCLUDED
82+
#endif
83+
84+
#undef htonl
85+
86+
uint32_t __builtin_bswap32(uint32_t);
87+
88+
uint32_t htonl(uint32_t hostlong)
89+
{
90+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
91+
return __builtin_bswap32(hostlong);
92+
#else
93+
return hostlong;
94+
#endif
95+
}
96+
97+
/* FUNCTION: htons */
98+
99+
#ifndef __CPROVER_STDINT_H_INCLUDED
100+
#include <stdint.h>
101+
#define __CPROVER_STDINT_H_INCLUDED
102+
#endif
103+
104+
#undef htons
105+
106+
uint16_t __builtin_bswap16(uint16_t);
107+
108+
uint16_t htons(uint16_t hostshort)
109+
{
110+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
111+
return __builtin_bswap16(hostshort);
112+
#else
113+
return hostlong;
114+
#endif
115+
}
116+
117+
118+
/* FUNCTION: ntohl */
119+
120+
#ifndef __CPROVER_STDINT_H_INCLUDED
121+
#include <stdint.h>
122+
#define __CPROVER_STDINT_H_INCLUDED
123+
#endif
124+
125+
#undef ntohl
126+
127+
uint32_t __builtin_bswap32(uint32_t);
128+
129+
uint32_t ntohl(uint32_t netlong)
130+
{
131+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
132+
return __builtin_bswap32(netlong);
133+
#else
134+
return netlong;
135+
#endif
136+
}
137+
138+
139+
/* FUNCTION: ntohs */
140+
141+
#ifndef __CPROVER_STDINT_H_INCLUDED
142+
#include <stdint.h>
143+
#define __CPROVER_STDINT_H_INCLUDED
144+
#endif
145+
146+
#undef ntohs
147+
148+
uint16_t __builtin_bswap16(uint16_t);
149+
150+
uint16_t ntohs(uint16_t netshort)
151+
{
152+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
153+
return __builtin_bswap16(netshort);
154+
#else
155+
return netshort;
156+
#endif
157+
}

0 commit comments

Comments
 (0)