Skip to content

Commit 6299bdb

Browse files
committed
src/common: return -1 from get_addr on error
getaddrinfo will typically return negative values for errors, but this is not the case for all systems. For example, glibc defines the errors as negative, but the WSA equivalents are all positive. This commit unifies the approach within iodine by always returning -1 in the event getaddrinfo is unsuccessful.
1 parent 68b0a7b commit 6299bdb

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/common.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ get_addr(char *host, int port, int addr_family, int flags, struct sockaddr_stora
142142
struct addrinfo hints, *addr;
143143
int res;
144144
char portnum[8];
145+
int addrlen;
145146

146147
memset(portnum, 0, sizeof(portnum));
147148
snprintf(portnum, sizeof(portnum) - 1, "%d", port);
@@ -158,14 +159,15 @@ get_addr(char *host, int port, int addr_family, int flags, struct sockaddr_stora
158159
hints.ai_protocol = IPPROTO_UDP;
159160

160161
res = getaddrinfo(host, portnum, &hints, &addr);
161-
if (res == 0) {
162-
int addrlen = addr->ai_addrlen;
163-
/* Grab first result */
164-
memcpy(out, addr->ai_addr, addr->ai_addrlen);
165-
freeaddrinfo(addr);
166-
return addrlen;
162+
if (res != 0) {
163+
return -1;
167164
}
168-
return res;
165+
166+
addrlen = addr->ai_addrlen;
167+
/* Grab first result */
168+
memcpy(out, addr->ai_addr, addr->ai_addrlen);
169+
freeaddrinfo(addr);
170+
return addrlen;
169171
}
170172

171173
int

0 commit comments

Comments
 (0)