summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Wiberg <troglobit@gmail.com>2022-09-26 13:18:20 +0200
committerGitHub <noreply@github.com>2022-09-26 13:18:20 +0200
commitcdd8d4e59c8a042f97f3983bfe26ccf4172c1884 (patch)
tree56256068e13c92b7f9d576c65bb219c8a74ba580
parent76ce72d1bce9615fe8860ad322597a410bb8d437 (diff)
parent79ff87bd02d21379c33496c1fa50a89312773140 (diff)
downloadlibnet-cdd8d4e59c8a042f97f3983bfe26ccf4172c1884.tar.gz
Merge pull request #148 from beni-sandu/master
libnet_raw: don't change the TX buffer size for raw sockets Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
-rw-r--r--ChangeLog.md3
-rw-r--r--include/libnet/libnet-functions.h12
-rw-r--r--src/libnet_init.c20
-rw-r--r--src/libnet_raw.c37
4 files changed, 35 insertions, 37 deletions
diff --git a/ChangeLog.md b/ChangeLog.md
index 7a9d7fd..cfe649e 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -10,6 +10,9 @@ of contributors, see the GIT commit log.
### Changes
+- Don't try to configure TX buffer max size for every raw socket. Instead, add a new API
+ that can be used when needed, called `libnet_setfd_max_sndbuf()`.
+
### Fixes
diff --git a/include/libnet/libnet-functions.h b/include/libnet/libnet-functions.h
index a4e00f5..45e3656 100644
--- a/include/libnet/libnet-functions.h
+++ b/include/libnet/libnet-functions.h
@@ -101,6 +101,18 @@ LIBNET_API
int
libnet_getfd(libnet_t *l);
+#ifdef SO_SNDBUF
+/**
+ * Tries to set the TX buffer size to a max_bytes value
+ * @param l pointer to a libnet context
+ * @param max_bytes new TX buffer size
+ * @return 0 on success, -1 on failure
+ */
+LIBNET_API
+int
+libnet_setfd_max_sndbuf(libnet_t *l, int max_bytes);
+#endif /* SO_SNDBUF */
+
/**
* Returns the canonical name of the device used for packet injection.
* @param l pointer to a libnet context
diff --git a/src/libnet_init.c b/src/libnet_init.c
index fe5b613..5350aab 100644
--- a/src/libnet_init.c
+++ b/src/libnet_init.c
@@ -172,6 +172,26 @@ libnet_getfd(libnet_t *l)
return (int)(l->fd);
}
+#ifdef SO_SNDBUF
+int
+libnet_setfd_max_sndbuf(libnet_t *l, int max_bytes)
+{
+ if (l == NULL)
+ return (-1);
+
+ /* Try to set the buffer size to max_bytes */
+ if (setsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &max_bytes, sizeof(max_bytes)) < 0)
+ {
+ snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
+ "%s(): set SO_SNDBUF failed: %s",
+ __func__, strerror(errno));
+ return (-1);
+ }
+
+ return (0);
+}
+#endif /* SO_SNDBUF */
+
const char *
libnet_getdevice(libnet_t *l)
{
diff --git a/src/libnet_raw.c b/src/libnet_raw.c
index 5195b75..f74d7b0 100644
--- a/src/libnet_raw.c
+++ b/src/libnet_raw.c
@@ -79,40 +79,6 @@ static int libnet_finish_setup_socket(libnet_t *l)
#endif
unsigned len;
-#ifdef SO_SNDBUF
-
-/*
- * man 7 socket
- *
- * Sets and gets the maximum socket send buffer in bytes.
- *
- * Taken from libdnet by Dug Song
- */
- len = sizeof(n);
- if (getsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, &len) < 0)
- {
- snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
- "%s(): get SO_SNDBUF failed: %s",
- __func__, strerror(errno));
- goto bad;
- }
-
- for (n += 128; n < 1048576; n += 128)
- {
- if (setsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, len) < 0)
- {
- if (errno == ENOBUFS)
- {
- break;
- }
- snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
- "%s(): set SO_SNDBUF failed: %s",
- __func__, strerror(errno));
- goto bad;
- }
- }
-#endif
-
#ifdef SO_BROADCAST
/*
* man 7 socket
@@ -146,9 +112,6 @@ bad:
return (-1);
}
-
-
-
int
libnet_open_raw4(libnet_t *l)
{