From 79ff87bd02d21379c33496c1fa50a89312773140 Mon Sep 17 00:00:00 2001 From: Beniamin Sandu Date: Fri, 23 Sep 2022 14:10:07 +0200 Subject: libnet_raw: don't change the TX buffer size for raw sockets Instead of trying to set the max TX buffer size for every raw socket, export this to a separate interface that can be used when needed, called libnet_setfd_max_sndbuf(). Signed-off-by: Beniamin Sandu --- ChangeLog.md | 3 +++ include/libnet/libnet-functions.h | 12 ++++++++++++ src/libnet_init.c | 20 ++++++++++++++++++++ src/libnet_raw.c | 37 ------------------------------------- 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) { -- cgit v1.2.1