KallistiOS  2.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
inet.h
Go to the documentation of this file.
1 /* KallistiOS 2.0.0
2 
3  arpa/inet.h
4  Copyright (C) 2006, 2007, 2010 Lawrence Sebald
5 
6 */
7 
8 /** \file arpa/inet.h
9  \brief Definitions for internet operations.
10 
11  This file contains the standard definitions (as directed by the POSIX 2008
12  standard) for several internet-related functions.
13 
14  \author Lawrence Sebald
15 */
16 
17 #ifndef __ARPA_INET_H
18 #define __ARPA_INET_H
19 
20 #include <sys/cdefs.h>
21 
22 __BEGIN_DECLS
23 
24 /* Bring in <netinet/in.h> to get the in_port_t, in_addr_t, and struct in_addr
25  types. Bring in <inttypes.h> for uint32_t and uint16_t. IEEE Std 1003.1-2008
26  specifically says that <arpa/inet.h> can make all the symbols from these
27  headers visible. */
28 #include <netinet/in.h>
29 #include <inttypes.h>
30 
31 /** \brief Convert a 32-bit value from host byte order to network byte order.
32  \param value The value to convert.
33  \return value converted to network byte order.
34 */
35 uint32_t htonl(uint32_t value);
36 
37 /** \brief Convert a 32-bit value from network byte order to host byte order.
38  \param value The value to convert.
39  \return value converted to host byte order.
40 */
41 uint32_t ntohl(uint32_t value);
42 
43 /** \brief Convert a 16-bit value from host byte order to network byte order.
44  \param value The value to convert.
45  \return value converted to network byte order.
46 */
47 uint16_t htons(uint16_t value);
48 
49 /** \brief Convert a 16-bit value from network byte order to host byte order.
50  \param value The value to convert.
51  \return value converted to host byte order.
52 */
53 uint16_t ntohs(uint16_t value);
54 
55 /** \brief Convert a string representation of an IPv4 address to an in_addr_t.
56 
57  This function converts a "dotted-decimal" string representation of an IPv4
58  address to an in_addr_t for use in a struct in_addr. This function supports
59  all POSIX-required formats for the representation of the address.
60 
61  \param cp A string representation of an IPv4 address.
62  \return The binary representation of the requested IPv4
63  address. (in_addr_t)(-1) is returned on error.
64 */
65 in_addr_t inet_addr(const char *cp);
66 
67 /** \brief Convert a string representation of an IPv4 address to a struct
68  in_addr.
69 
70  This function, much like inet_addr, converts a string representation of an
71  IPv4 address to a binary representation. This function, however, is
72  non-standard (but seems to appear a lot of places). This function is a
73  little nicer to work with than inet_addr simply because of the fact that the
74  error return from inet_addr happens to actually correspond to a real IPv4
75  address (255.255.255.255). This version actually distinguishes between that
76  address and invalid addresses.
77 
78  \param cp A string representation of an IPv4 address.
79  \param pin The destination for the conversion.
80  \retval 0 An invalid IPv4 address was given.
81  \retval 1 Upon successful conversion.
82 */
83 int inet_aton(const char *cp, struct in_addr *pin);
84 
85 /** \brief Convert a string representation of an IP address to its binary
86  representation.
87 
88  This function, like inet_addr, converts a string representation of an IP
89  address to its binary representation. This function, unlike inet_aton, is
90  actually standard (in POSIX 2008), and operates very similarly. The only
91  differences between this function and inet_aton are that this function does
92  not support hexadecimal or octal representations and that this function has
93  the ability to support IPv6. This is the function that you should actually
94  use to convert addresses from strings to binary in new code, rather than
95  inet_addr or inet_aton.
96 
97  \param af The address family that src is an address in. The
98  only supported values are AF_INET and AF_INET6.
99  \param src A string representation of the address.
100  \param dst Storage for the result. For AF_INET, this must be at
101  least 32-bits in size (the function treats it as a
102  struct in_addr). For AF_INET6, this must be at least
103  128-bits in size (the function treats it as a struct
104  in6_addr).
105  \retval -1 af is unsupported.
106  \retval 0 An invalid address was given.
107  \retval 1 Upon successful conversion.
108 
109  \par Error Conditions:
110  \em EAFNOSUPPORT - the specified address family is unsupported
111 */
112 int inet_pton(int af, const char *src, void *dst);
113 
114 /** \brief Convert a binary representation of an IP address to a string.
115 
116  This function does the exact oposite of the inet_pton function, converting
117  a binary form of an address to a string. This function, unlike inet_ntoa, is
118  reentrant, and is the function that you should generally use if you need to
119  convert a binary representation of an IP address to a string.
120 
121  \param af The address family that src is in. The only
122  supported values are AF_INET and AF_INET6.
123  \param src A binary representation of an IP address.
124  \param dst Storage for the resulting string. This string should
125  be at least 16-bytes long for IPv4, and 46 bytes for
126  IPv6.
127  \param size The length of dst.
128  \retval NULL Upon failed conversion.
129  \retval dst Upon successful conversion.
130 
131  \par Error Conditions:
132  \em EAFNOSUPPORT - the specified address family is unsupported \n
133  \em ENOSPC - the size given is insufficient
134 */
135 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
136 
137 /** \brief Convert a binary representation of an IPv4 address to a string.
138 
139  This function does the exact opposite of the inet_addr function, converting
140  a binary form of an address to a string. This function, unlike inet_ntop
141  is non-reentrant (not thread-safe), and will always only support IPv4
142  addresses. It is suggested to use inet_ntop in any new code.
143 
144  \param addr The address to convert.
145  \return A string representation of addr (in dotted-decimal
146  form).
147 */
148 char *inet_ntoa(struct in_addr addr);
149 
150 __END_DECLS
151 
152 #endif /* __ARPA_INET_H */