1
0

Sockaddr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <stdio.h>
  23. #include <stdlib.h>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. struct Sockaddr_pvt
  27. {
  28. struct Sockaddr pub;
  29. struct sockaddr_storage ss;
  30. };
  31. struct Sockaddr_in_pvt
  32. {
  33. struct Sockaddr pub;
  34. struct sockaddr_in si;
  35. };
  36. struct Sockaddr_in6_pvt
  37. {
  38. struct Sockaddr pub;
  39. struct sockaddr_in6 si;
  40. };
  41. const struct Sockaddr* const Sockaddr_LOOPBACK_be =
  42. (const struct Sockaddr*) &((const struct Sockaddr_in_pvt) {
  43. .pub = { .addrLen = sizeof(struct Sockaddr_in_pvt) },
  44. .si = {
  45. .sin_family = AF_INET,
  46. .sin_addr = { .s_addr = 0x7f000001 }
  47. }
  48. });
  49. const struct Sockaddr* const Sockaddr_LOOPBACK_le =
  50. (const struct Sockaddr*) &((const struct Sockaddr_in_pvt) {
  51. .pub = { .addrLen = sizeof(struct Sockaddr_in_pvt) },
  52. .si = {
  53. .sin_family = AF_INET,
  54. .sin_addr = { .s_addr = 0x0100007f }
  55. }
  56. });
  57. const struct Sockaddr* const Sockaddr_LOOPBACK6 =
  58. (const struct Sockaddr*) &((const struct Sockaddr_in6_pvt) {
  59. .pub = { .addrLen = sizeof(struct Sockaddr_in6_pvt) },
  60. .si = {
  61. .sin6_family = AF_INET6,
  62. .sin6_addr = { .s6_addr = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}
  63. }
  64. });
  65. struct Sockaddr* Sockaddr_fromNative(const void* ss, int addrLen, struct Allocator* alloc)
  66. {
  67. struct Sockaddr_pvt* out = Allocator_malloc(alloc, addrLen + Sockaddr_OVERHEAD);
  68. Bits_memcpy(&out->ss, ss, addrLen);
  69. out->pub.addrLen = addrLen + Sockaddr_OVERHEAD;
  70. Sockaddr_normalizeNative(&out->ss);
  71. return &out->pub;
  72. }
  73. int Sockaddr_parse(const char* str, struct Sockaddr_storage* out)
  74. {
  75. struct Sockaddr_storage unusedOut;
  76. if (!out) {
  77. out = &unusedOut;
  78. }
  79. Bits_memset(out, 0, sizeof(struct Sockaddr_storage));
  80. char* lastColon = CString_strrchr(str, ':');
  81. if (!lastColon || lastColon == CString_strchr(str, ':')) {
  82. // IPv4
  83. int port = 0;
  84. int addrLen;
  85. if (lastColon) {
  86. addrLen = (lastColon - str);
  87. port = atoi(lastColon+1);
  88. if (port > 65535 || port < 0) {
  89. return -1;
  90. }
  91. } else {
  92. addrLen = CString_strlen(str);
  93. }
  94. uint8_t addr[16] = {0};
  95. if (addrLen > 15 || addrLen < 7) {
  96. return -1;
  97. }
  98. Bits_memcpy(addr, str, addrLen);
  99. struct sockaddr_in* in = ((struct sockaddr_in*) Sockaddr_asNative(&out->addr));
  100. if (uv_inet_pton(AF_INET, (char*) addr, &in->sin_addr).code != UV_OK) {
  101. return -1;
  102. }
  103. out->addr.addrLen = sizeof(struct sockaddr_in) + Sockaddr_OVERHEAD;
  104. in->sin_port = Endian_hostToBigEndian16(port);
  105. in->sin_family = AF_INET;
  106. } else {
  107. // IPv6
  108. int port = 0;
  109. int addrLen;
  110. if (*str == '[') {
  111. str++;
  112. {
  113. char* endBracket = CString_strchr(str, ']');
  114. if (!endBracket) {
  115. return -1;
  116. }
  117. addrLen = (endBracket - str);
  118. }
  119. if (str[addrLen+1] == ':') {
  120. port = atoi(&str[addrLen+2]);
  121. if (port > 65535 || port < 0) {
  122. return -1;
  123. }
  124. }
  125. } else {
  126. addrLen = CString_strlen(str);
  127. }
  128. uint8_t addr[40] = {0};
  129. if (addrLen > 39 || addrLen < 2) {
  130. return -1;
  131. }
  132. Bits_memcpy(addr, str, addrLen);
  133. struct sockaddr_in6* in6 = (struct sockaddr_in6*) Sockaddr_asNative(&out->addr);
  134. uv_err_t ret = uv_inet_pton(AF_INET6, (char*) addr, &in6->sin6_addr);
  135. if (ret.code != UV_OK) {
  136. return -1;
  137. }
  138. out->addr.addrLen = sizeof(struct sockaddr_in6) + Sockaddr_OVERHEAD;
  139. in6->sin6_port = Endian_hostToBigEndian16(port);
  140. in6->sin6_family = AF_INET6;
  141. }
  142. return 0;
  143. }
  144. struct Sockaddr* Sockaddr_clone(const struct Sockaddr* addr, struct Allocator* alloc)
  145. {
  146. return Sockaddr_fromNative(Sockaddr_asNativeConst(addr),
  147. addr->addrLen - Sockaddr_OVERHEAD,
  148. alloc);
  149. }
  150. char* Sockaddr_print(struct Sockaddr* sockaddr, struct Allocator* alloc)
  151. {
  152. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)
  153. || sockaddr->addrLen > Sockaddr_MAXSIZE + Sockaddr_OVERHEAD) {
  154. return "invalid";
  155. }
  156. struct Sockaddr_pvt* addr = (struct Sockaddr_pvt*) sockaddr;
  157. void* inAddr;
  158. uint16_t port;
  159. switch (addr->ss.ss_family) {
  160. case AF_INET:
  161. inAddr = &((struct sockaddr_in*) &addr->ss)->sin_addr;
  162. port = Endian_bigEndianToHost16(((struct sockaddr_in*)&addr->ss)->sin_port);
  163. break;
  164. case AF_INET6:
  165. inAddr = &((struct sockaddr_in6*) &addr->ss)->sin6_addr;
  166. port = Endian_bigEndianToHost16(((struct sockaddr_in6*)&addr->ss)->sin6_port);
  167. break;
  168. default: {
  169. uint8_t buff[Sockaddr_MAXSIZE * 2 + 1] = {0};
  170. Hex_encode(buff, sizeof(buff), (uint8_t*)&addr->ss, sockaddr->addrLen);
  171. String* out = String_printf(alloc, "unknown (%s)", buff);
  172. return out->bytes;
  173. }
  174. };
  175. #define BUFF_SZ 64
  176. char printedAddr[BUFF_SZ] = {0};
  177. uv_err_t ret = uv_inet_ntop(addr->ss.ss_family, inAddr, printedAddr, BUFF_SZ - 1);
  178. if (ret.code != UV_OK) {
  179. return "invalid";
  180. }
  181. if (port) {
  182. int totalLength = CString_strlen(printedAddr) + CString_strlen("[]:65535") + 1;
  183. char* out = Allocator_malloc(alloc, totalLength);
  184. const char* format = (addr->ss.ss_family == AF_INET6) ? "[%s]:%u" : "%s:%u";
  185. snprintf(out, totalLength, format, printedAddr, port);
  186. return out;
  187. }
  188. int totalLength = CString_strlen(printedAddr) + 1;
  189. char* out = Allocator_calloc(alloc, totalLength, 1);
  190. Bits_memcpy(out, printedAddr, totalLength);
  191. return out;
  192. }
  193. static uint16_t* getPortPtr(struct Sockaddr* sockaddr)
  194. {
  195. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  196. return NULL;
  197. }
  198. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  199. switch (sa->ss.ss_family) {
  200. case AF_INET: return &((struct sockaddr_in*)&sa->ss)->sin_port;
  201. case AF_INET6: return &((struct sockaddr_in6*)&sa->ss)->sin6_port;
  202. }
  203. return NULL;
  204. }
  205. int Sockaddr_getPort(struct Sockaddr* sockaddr)
  206. {
  207. uint16_t* pp = getPortPtr(sockaddr);
  208. return (pp) ? Endian_bigEndianToHost16(*pp) : -1;
  209. }
  210. int Sockaddr_setPort(struct Sockaddr* sockaddr, uint16_t port)
  211. {
  212. uint16_t* pp = getPortPtr(sockaddr);
  213. if (pp) {
  214. *pp = Endian_hostToBigEndian16(port);
  215. return 0;
  216. }
  217. return -1;
  218. }
  219. int Sockaddr_getAddress(struct Sockaddr* sockaddr, void* addrPtr)
  220. {
  221. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  222. return -1;
  223. }
  224. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  225. if (addrPtr) {
  226. void** ap = (void**) addrPtr;
  227. switch (sa->ss.ss_family) {
  228. case AF_INET: *ap = &((struct sockaddr_in*)&sa->ss)->sin_addr; break;
  229. case AF_INET6: *ap = &((struct sockaddr_in6*)&sa->ss)->sin6_addr; break;
  230. }
  231. }
  232. switch (sa->ss.ss_family) {
  233. case AF_INET: return 4;
  234. case AF_INET6: return 16;
  235. }
  236. return -1;
  237. }
  238. const int Sockaddr_AF_INET = AF_INET;
  239. const int Sockaddr_AF_INET6 = AF_INET6;
  240. int Sockaddr_getFamily(struct Sockaddr* sockaddr)
  241. {
  242. if (sockaddr->addrLen < (2 + Sockaddr_OVERHEAD)) {
  243. return -1;
  244. }
  245. struct Sockaddr_pvt* sa = (struct Sockaddr_pvt*) sockaddr;
  246. return sa->ss.ss_family;
  247. }
  248. struct Sockaddr* Sockaddr_fromBytes(const uint8_t* bytes, int addrFamily, struct Allocator* alloc)
  249. {
  250. struct sockaddr_storage ss;
  251. Bits_memset(&ss, 0, sizeof(struct sockaddr_storage));
  252. int addrLen;
  253. switch (addrFamily) {
  254. case AF_INET: {
  255. ss.ss_family = AF_INET;
  256. Bits_memcpyConst(&((struct sockaddr_in*)&ss)->sin_addr, bytes, 4);
  257. addrLen = sizeof(struct sockaddr_in);
  258. break;
  259. }
  260. case AF_INET6: {
  261. ss.ss_family = AF_INET6;
  262. Bits_memcpyConst(&((struct sockaddr_in6*)&ss)->sin6_addr, bytes, 16);
  263. addrLen = sizeof(struct sockaddr_in6);
  264. break;
  265. }
  266. default: return NULL;
  267. }
  268. struct Sockaddr_pvt* out = Allocator_malloc(alloc, addrLen + Sockaddr_OVERHEAD);
  269. Bits_memcpy(&out->ss, &ss, addrLen);
  270. out->pub.addrLen = addrLen + Sockaddr_OVERHEAD;
  271. return &out->pub;
  272. }
  273. void Sockaddr_normalizeNative(void* nativeSockaddr)
  274. {
  275. #if defined(freebsd) || defined(openbsd) || defined(darwin)
  276. ((struct sockaddr*)nativeSockaddr)->sa_len = 0;
  277. #endif
  278. }
  279. struct Sockaddr* Sockaddr_fromName(char* name, struct Allocator* alloc)
  280. {
  281. struct addrinfo* servinfo;
  282. if (getaddrinfo(name, 0, NULL, &servinfo) == 0) {
  283. struct Sockaddr* adr;
  284. adr = Sockaddr_fromNative(servinfo->ai_addr, servinfo->ai_addrlen, alloc);
  285. freeaddrinfo(servinfo);
  286. return adr;
  287. }
  288. return NULL;
  289. }