26
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
* Internet protocol address conversion functions implementation.
34
* Internet protocol address conversion functions implementation.
37
#include <net/socket_codes.h>
45
#include <socket_codes.h>
47
int inet_ntop(uint16_t family, const uint8_t * data, char * address, size_t length){
48
if((! data) || (! address)){
47
/** Prints the address into the character buffer.
49
* @param[in] family The address family.
50
* @param[in] data The address data.
51
* @param[out] address The character buffer to be filled.
52
* @param[in] length The buffer length.
53
* @return EOK on success.
54
* @return EINVAL if the data or address parameter is NULL.
55
* @return ENOMEM if the character buffer is not long enough.
56
* @return ENOTSUP if the address family is not supported.
59
inet_ntop(uint16_t family, const uint8_t *data, char *address, size_t length)
61
if ((!data) || (!address))
54
// check the output buffer size
55
if(length < INET_ADDRSTRLEN){
58
// fill the buffer with the IPv4 address
59
snprintf(address, length, "%hhu.%hhu.%hhu.%hhu", data[0], data[1], data[2], data[3]);
62
// check the output buffer size
63
if(length < INET6_ADDRSTRLEN){
66
// fill the buffer with the IPv6 address
67
snprintf(address, length, "%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
66
/* Check output buffer size */
67
if (length < INET_ADDRSTRLEN)
70
/* Fill buffer with IPv4 address */
71
snprintf(address, length, "%hhu.%hhu.%hhu.%hhu",
72
data[0], data[1], data[2], data[3]);
77
/* Check output buffer size */
78
if (length < INET6_ADDRSTRLEN)
81
/* Fill buffer with IPv6 address */
82
snprintf(address, length,
83
"%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:%hhx%hhx:"
85
data[0], data[1], data[2], data[3], data[4], data[5],
86
data[6], data[7], data[8], data[9], data[10], data[11],
87
data[12], data[13], data[14], data[15]);
74
int inet_pton(uint16_t family, const char * address, uint8_t * data){
75
/** The base number of the values.
96
/** Parses the character string into the address.
98
* If the string is shorter than the full address, zero bytes are added.
100
* @param[in] family The address family.
101
* @param[in] address The character buffer to be parsed.
102
* @param[out] data The address data to be filled.
103
* @return EOK on success.
104
* @return EINVAL if the data parameter is NULL.
105
* @return ENOENT if the address parameter is NULL.
106
* @return ENOTSUP if the address family is not supported.
108
int inet_pton(uint16_t family, const char *address, uint8_t *data)
110
/** The base number of the values. */
78
/** The number of bytes per a section.
112
/** The number of bytes per a section. */
81
/** The number of bytes of the address data.
114
/** The number of bytes of the address data. */
89
121
unsigned long value;
95
// set the processing parameters
111
// erase if no address
126
/* Set processing parameters */
144
/* Erase if no address */
113
146
bzero(data, count);
117
// process the string from the beginning
150
/* Process string from the beginning */
121
// if the actual character is set
154
/* If the actual character is set */
124
// if not on the first character
126
// move to the next character
157
/* If not on the first character */
159
/* Move to the next character */
130
// parse the actual integral value
163
/* Parse the actual integral value */
131
164
value = strtoul(next, &last, base);
132
// remember the last problematic character
133
// should be either '.' or ':' but is ignored to be more generic
166
* Remember the last problematic character
167
* should be either '.' or ':' but is ignored to be
136
// fill the address data byte by byte
172
/* Fill the address data byte by byte */
137
173
shift = bytes - 1;
139
// like little endian
175
/* like little endian */
140
176
data[index + shift] = value;
146
// erase the rest of the address
182
/* Erase the rest of the address */
147
183
bzero(data + index, count - index);
150
}while(index < count);
186
} while (index < count);