summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1fa5bd2)
raw | patch | inline | side by side (parent: 1fa5bd2)
author | Florian Forster <ff@octo.it> | |
Fri, 5 May 2017 07:17:20 +0000 (09:17 +0200) | ||
committer | Florian Forster <ff@octo.it> | |
Fri, 5 May 2017 08:47:21 +0000 (10:47 +0200) |
Also make sure that obj->errmsg is set on all error paths and evaluate
errno as soon as possible so that calls to close() and such don't reset it.
errno as soon as possible so that calls to close() and such don't reset it.
src/liboping.c | patch | blob | history |
diff --git a/src/liboping.c b/src/liboping.c
index edc3522f2a93a9c9102adfe4707e3487db047537..4579ab04e84558adc31697d485d955feb384508f 100644 (file)
--- a/src/liboping.c
+++ b/src/liboping.c
free (ph);
}
+/* ping_open_socket opens, initializes and returns a new raw socket to use for
+ * ICMPv4 or ICMPv6 packets. addrfam must be either AF_INET or AF_INET6. On
+ * error, -1 is returned and obj->errmsg is set appropriately. */
static int ping_open_socket(pingobj_t *obj, int addrfam)
{
int fd;
}
else /* this should not happen */
{
+ ping_set_error (obj, "ping_open_socket", "Unknown address family");
dprintf ("Unknown address family: %i\n", addrfam);
- return (-1);
+ return -1;
}
if (fd == -1)
{
+ ping_set_errno (obj, errno);
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
dprintf ("socket: %s\n",
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
- ping_set_errno (obj, errno);
return -1;
}
else if (fd >= FD_SETSIZE)
{
+ ping_set_errno (obj, EMFILE);
dprintf ("socket(2) returned file descriptor %d, which is above the file "
"descriptor limit for select(2) (FD_SETSIZE = %d)\n",
fd, FD_SETSIZE);
close (fd);
- ping_set_errno (obj, EMFILE);
return -1;
}
if (bind (fd, obj->srcaddr, obj->srcaddrlen) == -1)
{
+ ping_set_errno (obj, errno);
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
dprintf ("bind: %s\n",
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
- ping_set_errno (obj, errno);
close (fd);
return -1;
}
if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
obj->device, strlen (obj->device) + 1) != 0)
{
+ ping_set_errno (obj, errno);
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
- ping_set_errno (obj, errno);
close (fd);
return -1;
}
&opt, sizeof (opt));
if (status != 0)
{
+ ping_set_errno (obj, errno);
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
- ping_set_errno (obj, errno);
close (fd);
return -1;
}