#include <arpa/inet.h>
#include <netdb.h>
-static int connect_friend(struct sockaddr *saddr, char *address, char *port, int *c)
+static int connect_friend(struct sockaddr **saddr, char *address, char *port)
{
struct addrinfo *addresses;
struct addrinfo *addr;
struct addrinfo hint;
int r;
- int fd = *c = -1;
- int i;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
if (r) {
return r;
}
- for (addr = addresses; addr != NULL; addr = addr->ai_next) {
- fd = socket(addr->ai_family, addr->ai_socktype,
- addr->ai_protocol);
- if (fd >= 0)
- break;
- close(fd);
- fd = -1;
+ if (addresses != NULL) {
+ *saddr = malloc(addresses->ai_addrlen);
+ if (!*saddr) {
+ r = -1;
+ } else {
+ memcpy(*saddr, addresses->ai_addr, addresses->ai_addrlen);
+ }
}
freeaddrinfo(addresses);
- *c = fd;
- if (fd == -1)
+ if (r == -1)
return EAI_SYSTEM;
return 0;
}
tfriend->name = friend;
tfriend->address = address;
tfriend->port = port;
- tfriend->saddr = NULL;
+ connect_friend(&tfriend->saddr, tfriend->address, tfriend->port);
return 0;
}