summaryrefslogtreecommitdiff
path: root/xutils.c
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-06-04 11:21:48 +0200
committerDaniel Borkmann <dborkman@redhat.com>2013-06-04 11:22:25 +0200
commita66fe185ab6a3eb7a483fd9888b54ee43328836e (patch)
tree527c53135af3c6d2c9b27e6de628c1ac5e2e2754 /xutils.c
parent0b65b09f15783b067343f7a2dcf67ae1f1e27dc7 (diff)
sock: add socket management functions
Remove them from xutils, and add them to socket management. Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Diffstat (limited to 'xutils.c')
-rw-r--r--xutils.c173
1 files changed, 1 insertions, 172 deletions
diff --git a/xutils.c b/xutils.c
index e07ce9e..e4cc1a4 100644
--- a/xutils.c
+++ b/xutils.c
@@ -44,117 +44,9 @@
#include "str.h"
#include "xutils.h"
#include "ring.h"
+#include "sock.h"
#include "built_in.h"
-enum {
- sock_rmem_max = 0,
- sock_rmem_def,
- sock_wmem_max,
- sock_wmem_def,
-};
-
-#define SMEM_SUG_MAX 104857600
-#define SMEM_SUG_DEF 4194304
-
-static const char *const to_prio[] = {
- "none",
- "realtime",
- "best-effort",
- "idle",
-};
-
-static const char *const sock_mem[] = {
- "/proc/sys/net/core/rmem_max",
- "/proc/sys/net/core/rmem_default",
- "/proc/sys/net/core/wmem_max",
- "/proc/sys/net/core/wmem_default",
-};
-
-int af_socket(int af)
-{
- int sock;
-
- if (unlikely(af != AF_INET && af != AF_INET6))
- panic("Wrong AF socket type!\n");
-
- sock = socket(af, SOCK_DGRAM, 0);
- if (unlikely(sock < 0))
- panic("Creation AF socket failed!\n");
-
- return sock;
-}
-
-int pf_socket(void)
-{
- int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
- if (unlikely(sock < 0))
- panic("Creation of PF socket failed!\n");
-
- return sock;
-}
-
-void set_sock_prio(int fd, int prio)
-{
- int ret, val = prio;
-
- ret = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &val, sizeof(val));
- if (unlikely(ret))
- panic("Cannot set socket priority!\n");
-}
-
-void set_nonblocking(int fd)
-{
- int ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK);
- if (unlikely(ret < 0))
- panic("Cannot fcntl!\n");
-}
-
-int set_nonblocking_sloppy(int fd)
-{
- return fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK);
-}
-
-void set_socket_keepalive(int fd)
-{
- int ret, one = 1;
-
- ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
- if (unlikely(ret))
- panic("Cannot set TCP keepalive!\n");
-}
-
-void set_tcp_nodelay(int fd)
-{
- int one = 1;
- setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
-}
-
-int set_ipv6_only(int fd)
-{
- int one = 1;
- return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
-}
-
-int set_reuseaddr(int fd)
-{
- int ret, one = 1;
-
- ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
- if (unlikely(ret < 0))
- panic("Cannot reuse addr!\n");
-
- return 0;
-}
-
-void set_mtu_disc_dont(int fd)
-{
- int mtu = IP_PMTUDISC_DONT, ret;
-
- ret = setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu));
- if (unlikely(ret))
- panic("Cannot set MTU discovery options!\n");
-}
-
void set_epoll_descriptor(int fd_epoll, int action, int fd_toadd, int events)
{
int ret;
@@ -201,45 +93,6 @@ u32 wireless_bitrate(const char *ifname)
return rate_in_mbit;
}
-int get_system_socket_mem(int which)
-{
- int fd, val = -1;
- ssize_t ret;
- const char *file = sock_mem[which];
- char buff[64];
-
- fd = open(file, O_RDONLY);
- if (fd < 0)
- return val;
-
- ret = read(fd, buff, sizeof(buff));
- if (ret > 0)
- val = atoi(buff);
-
- close(fd);
- return val;
-}
-
-void set_system_socket_mem(int which, int val)
-{
- int fd;
- const char *file = sock_mem[which];
- ssize_t ret;
- char buff[64];
-
- fd = open(file, O_WRONLY);
- if (fd < 0)
- return;
-
- memset(buff, 0, sizeof(buff));
- slprintf(buff, sizeof(buff), "%d", val);
-
- ret = write(fd, buff, strlen(buff));
- ret = ret;
-
- close(fd);
-}
-
int wireless_sigqual(const char *ifname, struct iw_statistics *stats)
{
int ret, sock;
@@ -405,30 +258,6 @@ void register_signal_f(int signal, void (*handler)(int), int flags)
sigaction(signal, &saction, NULL);
}
-void set_system_socket_memory(int *vals, size_t len)
-{
- bug_on(len != 4);
-
- if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
- set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
- if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
- set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
- if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
- set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
- if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
- set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
-}
-
-void reset_system_socket_memory(int *vals, size_t len)
-{
- bug_on(len != 4);
-
- set_system_socket_mem(sock_rmem_max, vals[0]);
- set_system_socket_mem(sock_rmem_def, vals[1]);
- set_system_socket_mem(sock_wmem_max, vals[2]);
- set_system_socket_mem(sock_wmem_def, vals[3]);
-}
-
void set_itimer_interval_value(struct itimerval *itimer, unsigned long sec,
unsigned long usec)
{