summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c40
1 files changed, 26 insertions, 14 deletions
diff --git a/util.c b/util.c
index dc36ed9..876cb6c 100644
--- a/util.c
+++ b/util.c
@@ -34,8 +34,10 @@ char *stringcopy(const char *s, int len) {
r = malloc(len + 1);
if (!r)
debug(DBG_ERR, "stringcopy: malloc failed");
- memcpy(r, s, len);
- r[len] = '\0';
+ else {
+ memcpy(r, s, len);
+ r[len] = '\0';
+ }
return r;
}
@@ -66,17 +68,17 @@ struct sockaddr *addr_copy(struct sockaddr *in) {
switch (in->sa_family) {
case AF_INET:
out = malloc(sizeof(struct sockaddr_in));
- if (out) {
- memset(out, 0, sizeof(struct sockaddr_in));
- ((struct sockaddr_in *)out)->sin_addr = ((struct sockaddr_in *)in)->sin_addr;
- }
+ if (out == NULL)
+ return NULL;
+ memset(out, 0, sizeof(struct sockaddr_in));
+ ((struct sockaddr_in *)out)->sin_addr = ((struct sockaddr_in *)in)->sin_addr;
break;
case AF_INET6:
out = malloc(sizeof(struct sockaddr_in6));
- if (out) {
- memset(out, 0, sizeof(struct sockaddr_in6));
- ((struct sockaddr_in6 *)out)->sin6_addr = ((struct sockaddr_in6 *)in)->sin6_addr;
- }
+ if (out == NULL)
+ return NULL;
+ memset(out, 0, sizeof(struct sockaddr_in6));
+ ((struct sockaddr_in6 *)out)->sin6_addr = ((struct sockaddr_in6 *)in)->sin6_addr;
break;
}
out->sa_family = in->sa_family;
@@ -148,10 +150,12 @@ int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only) {
disable_DF_bit(s,res);
if (reuse)
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
+ debugerrno(errno, DBG_WARN, "Failed to set SO_REUSEADDR");
#ifdef IPV6_V6ONLY
if (v6only)
- setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
+ if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1)
+ debugerrno(errno, DBG_INFO, "Failed to set IPV6_V6ONLY");
#endif
if (!bind(s, res->ai_addr, res->ai_addrlen))
return s;
@@ -167,7 +171,14 @@ int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, st
socklen_t len;
origflags = fcntl(s, F_GETFL, 0);
- fcntl(s, F_SETFL, origflags | O_NONBLOCK);
+ if (origflags == -1) {
+ debugerrno(errno, DBG_WARN, "Failed to get flags");
+ return -1;
+ }
+ if (fcntl(s, F_SETFL, origflags | O_NONBLOCK) == -1) {
+ debugerrno(errno, DBG_WARN, "Failed to set O_NONBLOCK");
+ return -1;
+ }
if (!connect(s, addr, addrlen)) {
r = 0;
goto exit;
@@ -185,7 +196,8 @@ int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, st
r = 0;
exit:
- fcntl(s, F_SETFL, origflags);
+ if (fcntl(s, F_SETFL, origflags) == -1)
+ debugerrno(errno, DBG_WARN, "Failed to set original flags back");
return r;
}