KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
inet.h
Go to the documentation of this file.
1/* KallistiOS ##version##
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 \ingroup networking_utils
11
12 This file contains the standard definitions (as directed by the POSIX 2008
13 standard) for several internet-related functions.
14
15 \author Lawrence Sebald
16*/
17
18#ifndef __ARPA_INET_H
19#define __ARPA_INET_H
20
21#include <kos/cdefs.h>
22
23__BEGIN_DECLS
24
25/* Bring in <netinet/in.h> to get the in_port_t, in_addr_t, and struct in_addr
26 types. Bring in <inttypes.h> for uint32_t and uint16_t. IEEE Std 1003.1-2008
27 specifically says that <arpa/inet.h> can make all the symbols from these
28 headers visible. */
29#include <netinet/in.h>
30#include <inttypes.h>
31
32/** \defgroup networking_utils Utilities
33 \brief Miscellaneous networking utilities
34 \ingroup networking
35
36 @{
37*/
38
39/** \brief Convert a 32-bit value from host byte order to network byte order.
40 \param value The value to convert.
41 \return value converted to network byte order.
42*/
43static inline uint32_t htonl(uint32_t value) {
44 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? value : __builtin_bswap32(value);
45}
46
47/** \brief Convert a 32-bit value from network byte order to host byte order.
48 \param value The value to convert.
49 \return value converted to host byte order.
50*/
51static inline uint32_t ntohl(uint32_t value) {
52 return htonl(value);
53}
54
55/** \brief Convert a 16-bit value from host byte order to network byte order.
56 \param value The value to convert.
57 \return value converted to network byte order.
58*/
59static inline uint16_t htons(uint16_t value) {
60 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? value : __builtin_bswap16(value);
61}
62
63/** \brief Convert a 16-bit value from network byte order to host byte order.
64 \param value The value to convert.
65 \return value converted to host byte order.
66*/
67static inline uint16_t ntohs(uint16_t value) {
68 return htons(value);
69}
70
71/** \brief Convert a string representation of an IPv4 address to an in_addr_t.
72
73 This function converts a "dotted-decimal" string representation of an IPv4
74 address to an in_addr_t for use in a struct in_addr. This function supports
75 all POSIX-required formats for the representation of the address.
76
77 \param cp A string representation of an IPv4 address.
78 \return The binary representation of the requested IPv4
79 address. (in_addr_t)(-1) is returned on error.
80*/
81in_addr_t inet_addr(const char *cp);
82
83/** \brief Convert a string representation of an IPv4 address to a struct
84 in_addr.
85
86 This function, much like inet_addr, converts a string representation of an
87 IPv4 address to a binary representation. This function, however, is
88 non-standard (but seems to appear a lot of places). This function is a
89 little nicer to work with than inet_addr simply because of the fact that the
90 error return from inet_addr happens to actually correspond to a real IPv4
91 address (255.255.255.255). This version actually distinguishes between that
92 address and invalid addresses.
93
94 \param cp A string representation of an IPv4 address.
95 \param pin The destination for the conversion.
96 \retval 0 An invalid IPv4 address was given.
97 \retval 1 Upon successful conversion.
98*/
99int inet_aton(const char *cp, struct in_addr *pin);
100
101/** \brief Convert a string representation of an IP address to its binary
102 representation.
103
104 This function, like inet_addr, converts a string representation of an IP
105 address to its binary representation. This function, unlike inet_aton, is
106 actually standard (in POSIX 2008), and operates very similarly. The only
107 differences between this function and inet_aton are that this function does
108 not support hexadecimal or octal representations and that this function has
109 the ability to support IPv6. This is the function that you should actually
110 use to convert addresses from strings to binary in new code, rather than
111 inet_addr or inet_aton.
112
113 \param af The address family that src is an address in. The
114 only supported values are AF_INET and AF_INET6.
115 \param src A string representation of the address.
116 \param dst Storage for the result. For AF_INET, this must be at
117 least 32-bits in size (the function treats it as a
118 struct in_addr). For AF_INET6, this must be at least
119 128-bits in size (the function treats it as a struct
120 in6_addr).
121 \retval -1 af is unsupported.
122 \retval 0 An invalid address was given.
123 \retval 1 Upon successful conversion.
124
125 \par Error Conditions:
126 \em EAFNOSUPPORT - the specified address family is unsupported
127*/
128int inet_pton(int af, const char *src, void *dst);
129
130/** \brief Convert a binary representation of an IP address to a string.
131
132 This function does the exact opposite of the inet_pton function, converting
133 a binary form of an address to a string. This function, unlike inet_ntoa, is
134 reentrant, and is the function that you should generally use if you need to
135 convert a binary representation of an IP address to a string.
136
137 \param af The address family that src is in. The only
138 supported values are AF_INET and AF_INET6.
139 \param src A binary representation of an IP address.
140 \param dst Storage for the resulting string. This string should
141 be at least 16-bytes long for IPv4, and 46 bytes for
142 IPv6.
143 \param size The length of dst.
144 \retval NULL Upon failed conversion.
145 \retval dst Upon successful conversion.
146
147 \par Error Conditions:
148 \em EAFNOSUPPORT - the specified address family is unsupported \n
149 \em ENOSPC - the size given is insufficient
150*/
151const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
152
153/** \brief Convert a binary representation of an IPv4 address to a string.
154
155 This function does the exact opposite of the inet_addr function, converting
156 a binary form of an address to a string. This function, unlike inet_ntop
157 is non-reentrant (not thread-safe), and will always only support IPv4
158 addresses. It is suggested to use inet_ntop in any new code.
159
160 \param addr The address to convert.
161 \return A string representation of addr (in dotted-decimal
162 form).
163*/
164char *inet_ntoa(struct in_addr addr);
165
166/** @} */
167
168__END_DECLS
169
170#endif /* __ARPA_INET_H */
Various common macros used throughout the codebase.
uint32_t in_addr_t
32-bit value used to store an IPv4 address.
Definition in.h:43
__uint32_t socklen_t
Socket length type.
Definition socket.h:39
in_addr_t inet_addr(const char *cp)
Convert a string representation of an IPv4 address to an in_addr_t.
int inet_aton(const char *cp, struct in_addr *pin)
Convert a string representation of an IPv4 address to a struct in_addr.
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)
Convert a binary representation of an IP address to a string.
char * inet_ntoa(struct in_addr addr)
Convert a binary representation of an IPv4 address to a string.
int inet_pton(int af, const char *src, void *dst)
Convert a string representation of an IP address to its binary representation.
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
use __builtin_bswap32().") static inline uint32_t arch_swap32(uint32_t x)
Definition byteorder.h:69
use ntohs() from< arpa/inet.h >") static inline uint16_t arch_ntohs(uint16_t x)
Definition byteorder.h:85
use __builtin_bswap16().") static inline uint16_t arch_swap16(uint16_t x)
Definition byteorder.h:55
use htonl() from< arpa/inet.h >") static inline uint32_t arch_htonl(uint32_t x)
Definition byteorder.h:133
use ntohl() from< arpa/inet.h >") static inline uint32_t arch_ntohl(uint32_t x)
Definition byteorder.h:101
use htons() from< arpa/inet.h >") static inline uint16_t arch_htons(uint16_t x)
Definition byteorder.h:117
Definitions for the Internet address family.
Structure used to store an IPv4 address.
Definition in.h:49