-void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr)
-{
- char address[NI_MAXHOST];
- char port[NI_MAXSERV];
- char *scopeid;
- int err;
-cp
- if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
- {
- syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
- cp_trace();
- raise(SIGFPE);
- exit(0);
- }
-
- if((scopeid = strchr(address, '%')))
- *scopeid = '\0'; /* Descope. */
-
- *addrstr = xstrdup(address);
- *portstr = xstrdup(port);
-cp
+sockaddr_t str2sockaddr_random(struct meshlink_handle *mesh, const char *address, const char *port) {
+ struct addrinfo *ai;
+ sockaddr_t result;
+ int err;
+
+ memset(&result, 0, sizeof(result));
+
+ struct addrinfo hint = {
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
+ .ai_socktype = SOCK_STREAM,
+ };
+
+ err = getaddrinfo(address, port, &hint, &ai);
+
+ if(err || !ai) {
+ result.sa.sa_family = AF_UNKNOWN;
+ result.unknown.address = NULL;
+ result.unknown.port = NULL;
+ return result;
+ }
+
+ int count = 0;
+
+ for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
+ count++;
+ }
+
+ struct addrinfo *aip = ai;
+
+ for(count = prng(mesh, count); count--; aip = aip->ai_next);
+
+ memcpy(&result, aip->ai_addr, aip->ai_addrlen);
+ freeaddrinfo(ai);
+
+ return result;