1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<refentry id='atoaddr3'>
<refmeta>
<refentrytitle>IPSEC-ATOADDR</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo class='date'>25 February 2008</refmiscinfo>
<refmiscinfo class="source">libreswan</refmiscinfo>
<refmiscinfo class="manual">Executable programs</refmiscinfo>
</refmeta>
<refnamediv id='name'>
<refname>ipsec-atoaddr</refname>
<refname>ipsec-addrtoa</refname>
<refname>ipsec-atosubnet</refname>
<refname>ipsec-subnettoa</refname>
<refpurpose>convert Internet addresses and Subnet masks to and from ASCII</refpurpose>
</refnamediv>
<!-- body begins here -->
<refsynopsisdiv id='synopsis'>
<funcsynopsis>
<funcsynopsisinfo>
#include <libreswan.h>
</funcsynopsisinfo>
<funcprototype>
<funcdef>const char *<function>atoaddr</function></funcdef>
<paramdef>const char * <parameter>src</parameter></paramdef>
<paramdef>size_t <parameter>srclen</parameter></paramdef>
<paramdef>struct in_addr * <parameter>addr</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>size_t <function>addrtoa</function></funcdef>
<paramdef>struct in_addr <parameter>addr</parameter></paramdef>
<paramdef>int <parameter>format</parameter></paramdef>
<paramdef>char * <parameter>dst</parameter></paramdef>
<paramdef>size_t <parameter>dstlen</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>const char *<function>atosubnet</function></funcdef>
<paramdef>const char * <parameter>src</parameter></paramdef>
<paramdef>size_t <parameter>srclen</parameter></paramdef>
<paramdef>struct in_addr * <parameter>addr</parameter></paramdef>
<paramdef>struct in_addr * <parameter>mask</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>size_t <function>subnettoa</function></funcdef>
<paramdef>struct in_addr <parameter>addr</parameter></paramdef>
<paramdef>struct in_addr <parameter>mask</parameter></paramdef>
<paramdef>int <parameter>format</parameter></paramdef>
<paramdef>char * <parameter>dst</parameter></paramdef>
<paramdef>size_t <parameter>dstlen</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1 id='description'><title>DESCRIPTION</title>
<para>These functions are obsolete; see
<citerefentry><refentrytitle>ipsec-ttoaddr</refentrytitle><manvolnum>3</manvolnum></citerefentry>
for their replacements.
<function>atoaddr</function> and <function>atosubnet</function> have already been deleted.</para>
<para><emphasis>Atoaddr</emphasis>
converts an ASCII name or dotted-decimal address into a binary address
(in network byte order).
<emphasis>Addrtoa</emphasis>
does the reverse conversion, back to an ASCII dotted-decimal address.
<emphasis>Atosubnet</emphasis>
and
<function>subnettoa</function>
do likewise for the “address/mask” ASCII form used to write a
specification of a subnet.</para>
<para>An address is specified in ASCII as a
dotted-decimal address (e.g.
<literal>1.2.3.4</literal>),
an eight-digit network-order hexadecimal number with the usual C prefix (e.g.
<emphasis>0x01020304</emphasis>,
which is synonymous with
<literal>1.2.3.4</literal>),
an eight-digit host-order hexadecimal number with a
<emphasis>0h</emphasis>
prefix (e.g.
<emphasis>0h01020304</emphasis>,
which is synonymous with
<literal>1.2.3.4</literal>
on a big-endian host and
<literal>4.3.2.1</literal>
on a little-endian host),
a DNS name to be looked up via
<citerefentry><refentrytitle>gethostbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
or an old-style network name to be looked up via
<citerefentry><refentrytitle>getnetbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
<para>A dotted-decimal address may be incomplete, in which case
ASCII-to-binary conversion implicitly appends
as many instances of
<literal>.0</literal>
as necessary to bring it up to four components.
The components of a dotted-decimal address are always taken as
decimal, and leading zeros are ignored.
For example,
<literal>10</literal>
is synonymous with
<literal>10.0.0.0</literal>,
and
<literal>128.009.000.032</literal>
is synonymous with
<literal>128.9.0.32</literal>
(the latter example is verbatim from RFC 1166).
The result of
<function>addrtoa</function>
is always complete and does not contain leading zeros.</para>
<para>The letters in
a hexadecimal address may be uppercase or lowercase or any mixture thereof.
Use of hexadecimal addresses is
<emphasis>strongly discouraged</emphasis>;
they are included only to save hassles when dealing with
the handful of perverted programs that already print
network addresses in hexadecimal.</para>
<para>DNS names may be complete (optionally terminated with a “.”)
or incomplete, and are looked up as specified by local system configuration
(see
<citerefentry><refentrytitle>resolver</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
The
<emphasis>h_addr</emphasis>
value returned by
<citerefentry><refentrytitle>gethostbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>
is used,
so with current DNS implementations,
the result when the name corresponds to more than one address is
difficult to predict.
Name lookup resorts to
<citerefentry><refentrytitle>getnetbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>
only if
<citerefentry><refentrytitle>gethostbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>
fails.</para>
<para>A subnet specification is of the form <emphasis>network</emphasis><emphasis>/</emphasis><varname role='parameter'>mask</varname>.
The
<emphasis>network</emphasis>
and
<varname role='parameter'>mask</varname>
can be any form acceptable to
<function>atoaddr</function>.
In addition, the
<varname role='parameter'>mask</varname>
can be a decimal integer (leading zeros ignored) giving a bit count,
in which case
it stands for a mask with that number of high bits on and all others off
(e.g.,
<literal>24</literal>
means
<literal>255.255.255.0</literal>).
In any case, the mask must be contiguous
(a sequence of high bits on and all remaining low bits off).
As a special case, the subnet specification
<emphasis>%default</emphasis>
is a synonym for
<emphasis>0.0.0.0/0</emphasis>.</para>
<para><emphasis>Atosubnet</emphasis>
ANDs the mask with the address before returning,
so that any non-network bits in the address are turned off
(e.g.,
<emphasis>10.1.2.3/24</emphasis>
is synonymous with
<emphasis>10.1.2.0/24</emphasis>).
<emphasis>Subnettoa</emphasis>
generates the decimal-integer-bit-count
form of the mask,
with no leading zeros,
unless the mask is non-contiguous.</para>
<para>The
<varname role='parameter'>srclen</varname>
parameter of
<function>atoaddr</function>
and
<function>atosubnet</function>
specifies the length of the ASCII string pointed to by
<varname role='parameter'>src</varname>;
it is an error for there to be anything else
(e.g., a terminating NUL) within that length.
As a convenience for cases where an entire NUL-terminated string is
to be converted,
a
<varname role='parameter'>srclen</varname>
value of
<literal>0</literal>
is taken to mean
<emphasis>strlen(src)</emphasis>.</para>
<para>The
<varname role='parameter'>dstlen</varname>
parameter of
<function>addrtoa</function>
and
<function>subnettoa</function>
specifies the size of the
<varname role='parameter'>dst</varname>
parameter;
under no circumstances are more than
<varname role='parameter'>dstlen</varname>
bytes written to
<varname role='parameter'>dst</varname>.
A result that will not fit is truncated.
<emphasis>Dstlen</emphasis>
can be zero, in which case
<varname role='parameter'>dst</varname>
need not be valid and no result is written,
but the return value is unaffected;
in all other cases, the (possibly truncated) result is NUL-terminated.
The
<emphasis>libreswan.h</emphasis>
header file defines constants,
<emphasis>ADDRTOA_BUF</emphasis>
and
<emphasis>SUBNETTOA_BUF</emphasis>,
that are the sizes of buffers just large enough for worst-case results.</para>
<para>The
<varname role='parameter'>format</varname>
parameter of
<function>addrtoa</function>
and
<function>subnettoa</function>
specifies what format is to be used for the conversion.
The value
<literal>0</literal>
(not the ASCII character
<emphasis>'0'</emphasis>,
but a zero value)
specifies a reasonable default,
and is in fact the only format currently available.
This parameter is a hedge against future needs.</para>
<para>The ASCII-to-binary functions return NULL for success and
a pointer to a string-literal error message for failure;
see DIAGNOSTICS.
The binary-to-ASCII functions return
<literal>0</literal>
for a failure, and otherwise
always return the size of buffer that would
be needed to
accommodate the full conversion result, including terminating NUL;
it is the caller's responsibility to check this against the size of
the provided buffer to determine whether truncation has occurred.</para>
</refsect1>
<refsect1 id='see_also'><title>SEE ALSO</title>
<para><citerefentry><refentrytitle>inet</refentrytitle><manvolnum>3</manvolnum></citerefentry></para>
</refsect1>
<refsect1 id='diagnostics'><title>DIAGNOSTICS</title>
<para>Fatal errors in
<function>atoaddr</function>
are:
empty input;
attempt to allocate temporary storage for a very long name failed;
name lookup failed;
syntax error in dotted-decimal form;
dotted-decimal component too large to fit in 8 bits.</para>
<para>Fatal errors in
<function>atosubnet</function>
are:
no
<emphasis>/</emphasis>
in
<varname role='parameter'>src</varname>;
<function>atoaddr</function>
error in conversion of
<emphasis>network</emphasis>
or
<varname role='parameter'>mask</varname>;
bit-count mask too big;
mask non-contiguous.</para>
<para>Fatal errors in
<function>addrtoa</function>
and
<function>subnettoa</function>
are:
unknown format.</para>
</refsect1>
<refsect1 id='history'><title>HISTORY</title>
<para>Written for the FreeS/WAN project by Henry Spencer.</para>
</refsect1>
<refsect1 id='bugs'><title>BUGS</title>
<para>The interpretation of incomplete dotted-decimal addresses
(e.g.
<emphasis>10/24</emphasis>
means
<emphasis>10.0.0.0/24</emphasis>)
differs from that of some older conversion
functions, e.g. those of
<citerefentry><refentrytitle>inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
The behavior of the older functions has never been
particularly consistent or particularly useful.</para>
<para>Ignoring leading zeros in dotted-decimal components and bit counts
is arguably the most useful behavior in this application,
but it might occasionally cause confusion with the historical use of leading
zeros to denote octal numbers.</para>
<para>It is barely possible that somebody, somewhere,
might have a legitimate use for non-contiguous subnet masks.</para>
<para><citerefentry><refentrytitle>Getnetbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry>
is a historical dreg.</para>
<para>The restriction of ASCII-to-binary error reports to literal strings
(so that callers don't need to worry about freeing them or copying them)
does limit the precision of error reporting.</para>
<para>The ASCII-to-binary error-reporting convention lends itself
to slightly obscure code,
because many readers will not think of NULL as signifying success.
A good way to make it clearer is to write something like:</para>
<literallayout>
<emphasis>const char *error;</emphasis>
<emphasis>error = atoaddr( /* ... */ );</emphasis>
<emphasis>if (error != NULL) {</emphasis>
<emphasis> /* something went wrong */</emphasis>
</literallayout> <!-- .fi -->
</refsect1>
<refsect1 id='author'>
<title>AUTHOR</title>
<para>
<emphasis role='strong'>Paul Wouters</emphasis>
</para>
</refsect1>
</refentry>
|