1 |
27 |
unneback |
.\" $OpenBSD: byteorder.3,v 1.8 2000/04/18 03:01:30 aaron Exp $
|
2 |
|
|
.\"
|
3 |
|
|
.\" Copyright (c) 1983, 1991, 1993
|
4 |
|
|
.\" The Regents of the University of California. All rights reserved.
|
5 |
|
|
.\"
|
6 |
|
|
.\" Redistribution and use in source and binary forms, with or without
|
7 |
|
|
.\" modification, are permitted provided that the following conditions
|
8 |
|
|
.\" are met:
|
9 |
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
10 |
|
|
.\" notice, this list of conditions and the following disclaimer.
|
11 |
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
12 |
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
13 |
|
|
.\" documentation and/or other materials provided with the distribution.
|
14 |
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
15 |
|
|
.\" must display the following acknowledgement:
|
16 |
|
|
.\" This product includes software developed by the University of
|
17 |
|
|
.\" California, Berkeley and its contributors.
|
18 |
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
19 |
|
|
.\" may be used to endorse or promote products derived from this software
|
20 |
|
|
.\" without specific prior written permission.
|
21 |
|
|
.\"
|
22 |
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
23 |
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24 |
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25 |
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
26 |
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27 |
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
28 |
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
29 |
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
30 |
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
31 |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
32 |
|
|
.\" SUCH DAMAGE.
|
33 |
|
|
.\"
|
34 |
|
|
.Dd June 4, 1993
|
35 |
|
|
.Dt BYTEORDER 3
|
36 |
|
|
.Os
|
37 |
|
|
.Sh NAME
|
38 |
|
|
.Nm htonl ,
|
39 |
|
|
.Nm htons ,
|
40 |
|
|
.Nm ntohl ,
|
41 |
|
|
.Nm ntohs ,
|
42 |
|
|
.Nm htobe32 ,
|
43 |
|
|
.Nm htobe16 ,
|
44 |
|
|
.Nm betoh32 ,
|
45 |
|
|
.Nm betoh16 ,
|
46 |
|
|
.Nm htole32 ,
|
47 |
|
|
.Nm htole16 ,
|
48 |
|
|
.Nm letoh32 ,
|
49 |
|
|
.Nm letoh16 ,
|
50 |
|
|
.Nm swap32 ,
|
51 |
|
|
.Nm swap16
|
52 |
|
|
.Nd convert values between different byte orderings
|
53 |
|
|
.Sh SYNOPSIS
|
54 |
|
|
.Fd #include
|
55 |
|
|
.Fd #include
|
56 |
|
|
.Ft u_int32_t
|
57 |
|
|
.Fn htonl "u_int32_t host32"
|
58 |
|
|
.Ft u_int16_t
|
59 |
|
|
.Fn htons "u_int16_t host16"
|
60 |
|
|
.Ft u_int32_t
|
61 |
|
|
.Fn ntohl "u_int32_t net32"
|
62 |
|
|
.Ft u_int16_t
|
63 |
|
|
.Fn ntohs "u_int16_t net16"
|
64 |
|
|
.Ft u_int32_t
|
65 |
|
|
.Fn htobe32 "u_int32_t host32"
|
66 |
|
|
.Ft u_int16_t
|
67 |
|
|
.Fn htobe16 "u_int16_t host16"
|
68 |
|
|
.Ft u_int32_t
|
69 |
|
|
.Fn betoh32 "u_int32_t big32"
|
70 |
|
|
.Ft u_int16_t
|
71 |
|
|
.Fn betoh16 "u_int16_t big16"
|
72 |
|
|
.Ft u_int32_t
|
73 |
|
|
.Fn htole32 "u_int32_t host32"
|
74 |
|
|
.Ft u_int16_t
|
75 |
|
|
.Fn htole16 "u_int16_t host16"
|
76 |
|
|
.Ft u_int32_t
|
77 |
|
|
.Fn letoh32 "u_int32_t little32"
|
78 |
|
|
.Ft u_int16_t
|
79 |
|
|
.Fn letoh16 "u_int16_t little16"
|
80 |
|
|
.Ft u_int32_t
|
81 |
|
|
.Fn swap32 "u_int32_t val32"
|
82 |
|
|
.Ft u_int16_t
|
83 |
|
|
.Fn swap16 "u_int16_t val16"
|
84 |
|
|
.Sh DESCRIPTION
|
85 |
|
|
These routines convert 16- and 32-bit quantities between different
|
86 |
|
|
byte orderings.
|
87 |
|
|
The
|
88 |
|
|
.Dq swap
|
89 |
|
|
functions reverse the byte ordering of
|
90 |
|
|
the given quantity, the others converts either from/to the native
|
91 |
|
|
byte order used by the host to/from either little- or big-endian (a.k.a
|
92 |
|
|
network) order.
|
93 |
|
|
.Pp
|
94 |
|
|
Apart from the swap functions, the names can be described by this form:
|
95 |
|
|
{src-order}to{dst-order}{size}.
|
96 |
|
|
Both {src-order} and {dst-order} can take the following forms:
|
97 |
|
|
.Pp
|
98 |
|
|
.Bl -tag -width "be " -offset indent -compact
|
99 |
|
|
.It h
|
100 |
|
|
Host order.
|
101 |
|
|
.It n
|
102 |
|
|
Network order (big-endian).
|
103 |
|
|
.It be
|
104 |
|
|
Big-endian (most significant byte first).
|
105 |
|
|
.It le
|
106 |
|
|
Little-endian (least significant byte first).
|
107 |
|
|
.El
|
108 |
|
|
.Pp
|
109 |
|
|
One of the specified orderings must be
|
110 |
|
|
.Sq h .
|
111 |
|
|
{size} will take these forms:
|
112 |
|
|
.Pp
|
113 |
|
|
.Bl -tag -width "32 " -offset indent -compact
|
114 |
|
|
.It l
|
115 |
|
|
Long (32-bit, used in conjunction with forms involving
|
116 |
|
|
.Sq n ) .
|
117 |
|
|
.It s
|
118 |
|
|
Short (16-bit, used in conjunction with forms involving
|
119 |
|
|
.Sq n ) .
|
120 |
|
|
.It 16
|
121 |
|
|
16-bit.
|
122 |
|
|
.It 32
|
123 |
|
|
32-bit.
|
124 |
|
|
.El
|
125 |
|
|
.Pp
|
126 |
|
|
The swap functions are of the form: swap{size}.
|
127 |
|
|
.Pp
|
128 |
|
|
Names involving
|
129 |
|
|
.Sq n
|
130 |
|
|
convert quantities between network
|
131 |
|
|
byte order and host byte order.
|
132 |
|
|
The last letter
|
133 |
|
|
.Pf ( Sq s
|
134 |
|
|
or
|
135 |
|
|
.Sq l )
|
136 |
|
|
is a mnemonic
|
137 |
|
|
for the traditional names for such quantities,
|
138 |
|
|
.Li short
|
139 |
|
|
and
|
140 |
|
|
.Li long ,
|
141 |
|
|
respectively.
|
142 |
|
|
Today, the C concept of
|
143 |
|
|
.Li short
|
144 |
|
|
and
|
145 |
|
|
.Li long
|
146 |
|
|
integers need not coincide with this traditional misunderstanding.
|
147 |
|
|
On machines which have a byte order which is the same as the network
|
148 |
|
|
order, routines are defined as null macros.
|
149 |
|
|
.Pp
|
150 |
|
|
The functions involving either
|
151 |
|
|
.Dq be ,
|
152 |
|
|
.Dq le ,
|
153 |
|
|
or
|
154 |
|
|
.Dq swap
|
155 |
|
|
use the numbers
|
156 |
|
|
16 and 32 for specifying the bitwidth of the quantities they operate on.
|
157 |
|
|
Currently all supported architectures are either big- or little-endian
|
158 |
|
|
so either the
|
159 |
|
|
.Dq be
|
160 |
|
|
or
|
161 |
|
|
.Dq le
|
162 |
|
|
variants are implemented as null macros.
|
163 |
|
|
.Pp
|
164 |
|
|
The routines mentioned above which have either {src-order} or {dst-order}
|
165 |
|
|
set to
|
166 |
|
|
.Sq n
|
167 |
|
|
are most often used in
|
168 |
|
|
conjunction with Internet addresses and ports as returned by
|
169 |
|
|
.Xr gethostbyname 3
|
170 |
|
|
and
|
171 |
|
|
.Xr getservent 3 .
|
172 |
|
|
.Sh SEE ALSO
|
173 |
|
|
.Xr gethostbyname 3 ,
|
174 |
|
|
.Xr getservent 3
|
175 |
|
|
.Sh HISTORY
|
176 |
|
|
The
|
177 |
|
|
.Nm byteorder
|
178 |
|
|
functions appeared in
|
179 |
|
|
.Bx 4.2 .
|
180 |
|
|
.Sh BUGS
|
181 |
|
|
On the vax, alpha, i386, and so far mips,
|
182 |
|
|
bytes are handled backwards from most everyone else in the world.
|
183 |
|
|
This is not expected to be fixed in the near future.
|