| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "utils/s2n_rfc5952.h" |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#ifdef _WIN32 |
| 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 |
#include <winsock2.h> |
| 28 |
#else |
| 29 |
#include <sys/socket.h> |
| 30 |
#endif |
| 31 |
#include <sys/types.h> |
| 32 |
|
| 33 |
#include "error/s2n_errno.h" |
| 34 |
#include "utils/s2n_safety.h" |
| 35 |
|
| 36 |
static uint8_t dec[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; |
| 37 |
static uint8_t hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 38 |
|
| 39 |
S2N_RESULT s2n_inet_ntop(int af, const void *addr, struct s2n_blob *dst) |
| 40 |
{ |
| 41 |
const uint8_t *bytes = addr; |
| 42 |
uint8_t *cursor = dst->data; |
| 43 |
|
| 44 |
if (af == AF_INET) { |
| 45 |
RESULT_ENSURE(dst->size >= sizeof("111.222.333.444"), S2N_ERR_SIZE_MISMATCH); |
| 46 |
|
| 47 |
for (int i = 0; i < 4; i++) { |
| 48 |
if (bytes[i] / 100) { |
| 49 |
*cursor++ = dec[bytes[i] / 100]; |
| 50 |
} |
| 51 |
if (bytes[i] >= 10) { |
| 52 |
*cursor++ = dec[(bytes[i] % 100) / 10]; |
| 53 |
} |
| 54 |
*cursor++ = dec[(bytes[i] % 10)]; |
| 55 |
*cursor++ = '.'; |
| 56 |
} |
| 57 |
|
| 58 |
*--cursor = '\0'; |
| 59 |
|
| 60 |
return S2N_RESULT_OK; |
| 61 |
} |
| 62 |
|
| 63 |
if (af == AF_INET6) { |
| 64 |
RESULT_ENSURE(dst->size >= sizeof("1111:2222:3333:4444:5555:6666:7777:8888"), S2N_ERR_SIZE_MISMATCH); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
uint16_t octets[8] = { 0 }; |
| 78 |
|
| 79 |
int longest_run_start = 0; |
| 80 |
int longest_run_length = 0; |
| 81 |
int current_run_length = 0; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
for (int i = 0; i < 8; i++) { |
| 87 |
octets[i] = (bytes[i * 2] << 8) + bytes[(i * 2) + 1]; |
| 88 |
|
| 89 |
if (octets[i]) { |
| 90 |
current_run_length = 0; |
| 91 |
} else { |
| 92 |
current_run_length++; |
| 93 |
} |
| 94 |
|
| 95 |
if (current_run_length > longest_run_length) { |
| 96 |
longest_run_length = current_run_length; |
| 97 |
longest_run_start = (i - current_run_length) + 1; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
for (int i = 0; i < 8; i++) { |
| 102 |
if (i == longest_run_start && longest_run_length > 1) { |
| 103 |
if (i == 0) { |
| 104 |
*cursor++ = ':'; |
| 105 |
} |
| 106 |
|
| 107 |
if (longest_run_length == 8) { |
| 108 |
*cursor++ = ':'; |
| 109 |
} |
| 110 |
|
| 111 |
i += longest_run_length - 1; |
| 112 |
|
| 113 |
} else { |
| 114 |
uint8_t nibbles[4] = { (octets[i] & 0xF000) >> 12, |
| 115 |
(octets[i] & 0x0F00) >> 8, |
| 116 |
(octets[i] & 0x00F0) >> 4, |
| 117 |
(octets[i] & 0x000F) }; |
| 118 |
|
| 119 |
|
| 120 |
int j = 0; |
| 121 |
for (j = 0; j < 3; j++) { |
| 122 |
if (nibbles[j]) { |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
for (; j < 4; j++) { |
| 128 |
*cursor++ = hex[nibbles[j]]; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
*cursor++ = ':'; |
| 133 |
} |
| 134 |
|
| 135 |
*--cursor = '\0'; |
| 136 |
|
| 137 |
return S2N_RESULT_OK; |
| 138 |
} |
| 139 |
|
| 140 |
RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT); |
| 141 |
} |