projects
/
cascardo
/
f2fchat.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
9952158
)
Get the address for a friend
author
Thadeu Lima de Souza Cascardo
<cascardo@cascardo.info>
Mon, 7 Oct 2013 11:32:47 +0000
(08:32 -0300)
committer
Thadeu Lima de Souza Cascardo
<cascardo@cascardo.info>
Mon, 7 Oct 2013 11:32:47 +0000
(08:32 -0300)
friend.c
patch
|
blob
|
history
diff --git
a/friend.c
b/friend.c
index
58e7bc5
..
753a588
100644
(file)
--- a/
friend.c
+++ b/
friend.c
@@
-26,14
+26,12
@@
#include <arpa/inet.h>
#include <netdb.h>
#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;
{
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;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
@@
-43,17
+41,16
@@
static int connect_friend(struct sockaddr *saddr, char *address, char *port, int
if (r) {
return r;
}
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);
}
freeaddrinfo(addresses);
- *c = fd;
- if (fd == -1)
+ if (r == -1)
return EAI_SYSTEM;
return 0;
}
return EAI_SYSTEM;
return 0;
}
@@
-107,7
+104,7
@@
int cache_add_friend(struct cache *cache, char *friend, char *address, char *por
tfriend->name = friend;
tfriend->address = address;
tfriend->port = port;
tfriend->name = friend;
tfriend->address = address;
tfriend->port = port;
-
tfriend->saddr = NULL
;
+
connect_friend(&tfriend->saddr, tfriend->address, tfriend->port)
;
return 0;
}
return 0;
}