| 315 | } |
| 316 | |
| 317 | int hostname2endpoint(const char* str, EndPoint* point) { |
| 318 | // Should be enough to hold ip address |
| 319 | // The definitive descriptions of the rules for forming domain names appear in RFC 1035, RFC 1123, RFC 2181, |
| 320 | // and RFC 5892. The full domain name may not exceed the length of 253 characters in its textual representation |
| 321 | // (Domain Names - Domain Concepts and Facilities. IETF. doi:10.17487/RFC1034. RFC 1034.). |
| 322 | // For cacheline optimize, use buf size as 256; |
| 323 | char buf[256]; |
| 324 | size_t i = 0; |
| 325 | for (; i < MAX_DOMAIN_LENGTH && str[i] != '\0' && str[i] != ':'; ++i) { |
| 326 | buf[i] = str[i]; |
| 327 | } |
| 328 | |
| 329 | if (i >= MAX_DOMAIN_LENGTH || str[i] != ':') { |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | buf[i] = '\0'; |
| 334 | if (hostname2ip(buf, &point->ip) != 0) { |
| 335 | return -1; |
| 336 | } |
| 337 | if (str[i] == ':') { |
| 338 | ++i; |
| 339 | } |
| 340 | char* end = NULL; |
| 341 | point->port = strtol(str + i, &end, 10); |
| 342 | if (end == str + i) { |
| 343 | return -1; |
| 344 | } else if (*end) { |
| 345 | for (; isspace(*end); ++end); |
| 346 | if (*end) { |
| 347 | return -1; |
| 348 | } |
| 349 | } |
| 350 | if (point->port < 0 || point->port > 65535) { |
| 351 | return -1; |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | int hostname2endpoint(const char* name_str, int port, EndPoint* point) { |
| 357 | if (hostname2ip(name_str, &point->ip) != 0) { |