Sockaddr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/events/libuv/UvWrapper.h"
  16. #include "benc/String.h"
  17. #include "memory/Allocator.h"
  18. #include "util/platform/Sockaddr.h"
  19. #include "util/CString.h"
  20. #include "util/Bits.h"
  21. #include "util/Hex.h"
  22. #include "util/Hash.h"
  23. #include "util/Base10.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. struct Sockaddr_pvt
  29. {
  30. struct Sockaddr pub;
  31. struct sockaddr_storage ss;
  32. };
  33. struct Sockaddr_in_pvt
  34. {
  35. struct Sockaddr pub;
  36. struct sockaddr_in si;
  37. };
  38. struct Sockaddr_in6_pvt
  39. {
  40. struct Sockaddr pub;
  41. struct sockaddr_in6 si;
  42. };
  43. const struct Sockaddr* const Sockaddr_LOOPBACK_be =
  44. (const struct Sockaddr*) &((const struct Sockaddr_in_pvt) {
  45. .pub = { .addrLen = sizeof(struct Sockaddr_in_pvt) },
  46. .si = {
  47. .sin_family = AF_INET,
  48. .sin_addr = { .s_addr = 0x7f000001 }
  49. }
  50. });
  51. const struct Sockaddr* const Sockaddr_LOOPBACK_le =
  52. (const struct Sockaddr*) &((const struct Sockaddr_in_pvt) {
  53. .pub = { .addrLen = sizeof(struct Sockaddr_in_pvt) },
  54. .si = {
  55. .sin_family = AF_INET,
  56. .sin_addr = { .s_addr = 0x0100007f }
  57. }
  58. });
  59. const struct Sockaddr* const Sockaddr_LOOPBACK6 =
  60. (const struct Sockaddr*) &((const struct Sockaddr_in6_pvt) {
  61. .pub = { .addrLen = sizeof(struct Sockaddr_in6_pvt) },
  62. .si = {
  63. .sin6_family = AF_INET6,
  64. .sin6_addr = { .s6_addr = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}
  65. }
  66. });
  67. struct Sockaddr* Sockaddr_fromNative(const void* ss, int addrLen, struct Allocator* alloc)
  68. {
  69. struct Sockaddr_pvt* out = Allocator_calloc(alloc, addrLen + Sockaddr_OVERHEAD, 1);
  70. Bits_memcpy(&out->ss, ss, addrLen);
  71. out->pub.addrLen = addrLen + Sockaddr_OVERHEAD;
  72. Sockaddr_normalizeNative(&out->ss);
  73. return &out->pub;
  74. }
  75. int Sockaddr_getPrefix(struct Sockaddr* addr)
  76. {
  77. if (addr->flags & Sockaddr_flags_PREFIX) {
  78. return addr->prefix;
  79. }
  80. int af = Sockaddr_getFamily(addr);
  81. if (af == Sockaddr_AF_INET) {
  82. return 32;
  83. } else if (af == Sockaddr_AF_INET6) {
  84. return 128;
  85. } else {
  86. return -1;
  87. }
  88. }
  89. int Sockaddr_parse(const char* input, struct Sockaddr_storage* out)
  90. {
  91. struct Sockaddr_storage unusedOut;
  92. if (!out) {
  93. out = &unusedOut;
  94. }
  95. uint8_t buff[64] = {0};
  96. if (CString_strlen(input) > 63) {
  97. return -1;
  98. }
  99. CString_strncpy(buff, input, 63);
  100. int64_t port = 0;
  101. char* lastColon = CString_strrchr(buff, ':');
  102. char* firstColon = CString_strchr(buff, ':');
  103. char* bracket = CString_strchr(buff, ']');
  104. if (!lastColon) {
  105. // ipv4, no port
  106. } else if (lastColon != firstColon && (!bracket || lastColon < bracket)) {
  107. // ipv6, no port
  108. } else {
  109. if (bracket && lastColon != &bracket[1]) { return -1; }
  110. if (Base10_fromString(&lastColon[1], &port)) { return -1; }
  111. if (port > 65535) { return -1; }
  112. *lastColon = '\0';
  113. }
  114. if (bracket) {
  115. *bracket = '\0';
  116. if (buff[0] != '[') { return -1; }
  117. } else if (buff[0] == '[') { return -1; }
  118. int64_t prefix = -1;
  119. char* slash = CString_strchr(buff, '/');
  120. if (slash) {
  121. *slash = '\0';
  122. if (!slash[1]) { return -1; }
  123. if (Base10_fromString(&slash[1], &prefix)) { return -1; }
  124. }
  125. Bits_memset(out, 0, sizeof(struct Sockaddr_storage));
  126. if (lastColon != firstColon) {
  127. // ipv6
  128. struct sockaddr_in6* in6 = (struct sockaddr_in6*) Sockaddr_asNative(&out->addr);
  129. if (uv_inet_pton(AF_INET6, (char*) ((buff[0] == '[') ? &buff[1] : buff), &in6->sin6_addr)) {
  130. return -1;
  131. }
  132. out->addr.addrLen = sizeof(struct sockaddr_in6) + Sockaddr_OVERHEAD;
  133. in6->sin6_port = Endian_hostToBigEndian16(port);
  134. in6->sin6_family = AF_INET6;
  135. } else {
  136. struct sockaddr_in* in = ((struct sockaddr_in*) Sockaddr_asNative(&out->addr));
  137. if (uv_inet_pton(AF_INET, (char*) buff, &in->sin_addr)) {
  138. return -1;
  139. }
  140. out->addr.addrLen = sizeof(struct sockaddr_in) + Sockaddr_OVERHEAD;
  141. in->sin_port = Endian_hostToBigEndian16(port);
  142. in->sin_family = AF_INET;
  143. }
  144. if (prefix != -1) {
  145. if (prefix < 0 || prefix > 128) { return -1; }
  146. if (Sockaddr_getFamily(&out->addr) == Sockaddr_AF_INET && prefix > 32) { return -1; }
  147. out->addr.prefix = prefix;
  148. out->addr.flags |= Sockaddr_flags_PREFIX;
  149. }
  150. return 0;
  151. }
  152. struct Sockaddr* Sockaddr_clone(const struct Sockaddr* addr, struct Allocator* alloc)
  153. {
  154. struct Sockaddr* out = Allocator_malloc(alloc, addr->addrLen);
  155. Bits_memcpy(out, addr, addr->addrLen);
  156. return out;
  157. }
  158. char* Sockaddr_print(struct Sockaddr* sockaddr, struct Allocator* alloc)
  159. {
  160. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)
  161. || sockaddr->addrLen > Sockaddr_MAXSIZE + Sockaddr_OVERHEAD) {
  162. return "invalid";
  163. }
  164. struct Sockaddr_pvt* addr = (struct Sockaddr_pvt*) sockaddr;
  165. void* inAddr;
  166. uint16_t port;
  167. switch (addr->ss.ss_family) {
  168. case AF_INET:
  169. inAddr = &((struct sockaddr_in*) &addr->ss)->sin_addr;
  170. port = Endian_bigEndianToHost16(((struct sockaddr_in*)&addr->ss)->sin_port);
  171. break;
  172. case AF_INET6:
  173. inAddr = &((struct sockaddr_in6*) &addr->ss)->sin6_addr;
  174. port = Endian_bigEndianToHost16(((struct sockaddr_in6*)&addr->ss)->sin6_port);
  175. break;
  176. default: {
  177. uint8_t buff[Sockaddr_MAXSIZE * 2 + 1] = {0};
  178. Hex_encode(buff, sizeof(buff), (uint8_t*)&addr->ss, sockaddr->addrLen);
  179. String* out = String_printf(alloc, "unknown (%s)", buff);
  180. return out->bytes;
  181. }
  182. };
  183. #define BUFF_SZ 64
  184. char printedAddr[BUFF_SZ] = {0};
  185. int ret = uv_inet_ntop(addr->ss.ss_family, inAddr, printedAddr, BUFF_SZ - 1);
  186. if (ret != 0) {
  187. return "invalid";
  188. }
  189. char printedPrefix[16] = {0};
  190. if (addr->pub.flags & Sockaddr_flags_PREFIX) {
  191. snprintf(printedPrefix, 15, "/%u", addr->pub.prefix);
  192. }
  193. char printedPort[16] = {0};
  194. if (port) {
  195. snprintf(printedPort, 15, ":%u", port);
  196. }
  197. char finalAddr[128] = {0};
  198. const char* format = (port && addr->ss.ss_family == AF_INET6) ? "[%s%s]%s" : "%s%s%s";
  199. snprintf(finalAddr, 127, format, printedAddr, printedPrefix, printedPort);
  200. int totalLength = CString_strlen(finalAddr) + 1;
  201. char* out = Allocator_calloc(alloc, totalLength, 1);
  202. Bits_memcpy(out, finalAddr, totalLength);
  203. return out;
  204. }
  205. static uint16_t* getPortPtr(struct Sockaddr* sockaddr)
  206. {
  207. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  208. return NULL;
  209. }
  210. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  211. switch (sa->ss.ss_family) {
  212. case AF_INET: return &((struct sockaddr_in*)&sa->ss)->sin_port;
  213. case AF_INET6: return &((struct sockaddr_in6*)&sa->ss)->sin6_port;
  214. }
  215. return NULL;
  216. }
  217. int Sockaddr_getPort(struct Sockaddr* sockaddr)
  218. {
  219. uint16_t* pp = getPortPtr(sockaddr);
  220. return (pp) ? Endian_bigEndianToHost16(*pp) : -1;
  221. }
  222. int Sockaddr_setPort(struct Sockaddr* sockaddr, uint16_t port)
  223. {
  224. uint16_t* pp = getPortPtr(sockaddr);
  225. if (pp) {
  226. *pp = Endian_hostToBigEndian16(port);
  227. return 0;
  228. }
  229. return -1;
  230. }
  231. int Sockaddr_getAddress(struct Sockaddr* sockaddr, void* addrPtr)
  232. {
  233. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  234. return -1;
  235. }
  236. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  237. if (addrPtr) {
  238. void** ap = (void**) addrPtr;
  239. switch (sa->ss.ss_family) {
  240. case AF_INET: *ap = &((struct sockaddr_in*)&sa->ss)->sin_addr; break;
  241. case AF_INET6: *ap = &((struct sockaddr_in6*)&sa->ss)->sin6_addr; break;
  242. }
  243. }
  244. switch (sa->ss.ss_family) {
  245. case AF_INET: return 4;
  246. case AF_INET6: return 16;
  247. }
  248. return -1;
  249. }
  250. const int Sockaddr_AF_INET = AF_INET;
  251. const int Sockaddr_AF_INET6 = AF_INET6;
  252. int Sockaddr_getFamily(struct Sockaddr* sockaddr)
  253. {
  254. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  255. return -1;
  256. }
  257. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  258. return sa->ss.ss_family;
  259. }
  260. struct Sockaddr* Sockaddr_fromBytes(const uint8_t* bytes, int addrFamily, struct Allocator* alloc)
  261. {
  262. struct sockaddr_storage ss;
  263. Bits_memset(&ss, 0, sizeof(struct sockaddr_storage));
  264. int addrLen;
  265. switch (addrFamily) {
  266. case AF_INET: {
  267. ss.ss_family = AF_INET;
  268. Bits_memcpy(&((struct sockaddr_in*)&ss)->sin_addr, bytes, 4);
  269. addrLen = sizeof(struct sockaddr_in);
  270. break;
  271. }
  272. case AF_INET6: {
  273. ss.ss_family = AF_INET6;
  274. Bits_memcpy(&((struct sockaddr_in6*)&ss)->sin6_addr, bytes, 16);
  275. addrLen = sizeof(struct sockaddr_in6);
  276. break;
  277. }
  278. default: Assert_failure("unrecognized address type [%d]", addrFamily);
  279. }
  280. struct Sockaddr_pvt* out = Allocator_calloc(alloc, addrLen + Sockaddr_OVERHEAD, 1);
  281. Bits_memcpy(&out->ss, &ss, addrLen);
  282. out->pub.addrLen = addrLen + Sockaddr_OVERHEAD;
  283. return &out->pub;
  284. }
  285. void Sockaddr_normalizeNative(void* nativeSockaddr)
  286. {
  287. #if defined(freebsd) || defined(openbsd) || defined(darwin)
  288. ((struct sockaddr*)nativeSockaddr)->sa_len = 0;
  289. #endif
  290. }
  291. struct Sockaddr* Sockaddr_fromName(char* name, struct Allocator* alloc)
  292. {
  293. struct addrinfo* servinfo;
  294. if (getaddrinfo(name, 0, NULL, &servinfo) == 0) {
  295. struct Sockaddr* adr;
  296. adr = Sockaddr_fromNative(servinfo->ai_addr, servinfo->ai_addrlen, alloc);
  297. freeaddrinfo(servinfo);
  298. return adr;
  299. }
  300. return NULL;
  301. }
  302. uint32_t Sockaddr_hash(const struct Sockaddr* addr)
  303. {
  304. return Hash_compute((uint8_t*)addr, addr->addrLen);
  305. }
  306. int Sockaddr_compare(const struct Sockaddr* a, const struct Sockaddr* b)
  307. {
  308. return Bits_memcmp(a, b, a->addrLen);
  309. }