build: move trafgen_stddef.h to stddef.h
[netsniff-ng.git] / sock.c
blob1cefb83247a71f03594cb6edb1d70cd0af312eb1
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <fcntl.h>
4 #include <arpa/inet.h>
5 #include <linux/if_ether.h>
6 #include <linux/tcp.h>
8 #include "sock.h"
9 #include "die.h"
10 #include "str.h"
11 #include "built_in.h"
13 int af_socket(int af)
15 int sock;
17 if (unlikely(af != AF_INET && af != AF_INET6))
18 panic("Wrong AF socket type!\n");
20 sock = socket(af, SOCK_DGRAM, 0);
21 if (unlikely(sock < 0))
22 panic("Creation AF socket failed!\n");
24 return sock;
27 int pf_socket(void)
29 int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
30 if (unlikely(sock < 0))
31 panic("Creation of PF socket failed!\n");
33 return sock;
36 void set_sock_prio(int fd, int prio)
38 int ret, val = prio;
40 ret = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &val, sizeof(val));
41 if (unlikely(ret))
42 panic("Cannot set socket priority!\n");
45 void set_nonblocking(int fd)
47 int ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK);
48 if (unlikely(ret < 0))
49 panic("Cannot fcntl!\n");
52 int set_nonblocking_sloppy(int fd)
54 return fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK);
57 void set_socket_keepalive(int fd)
59 int ret, one = 1;
61 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
62 if (unlikely(ret))
63 panic("Cannot set TCP keepalive!\n");
66 void set_tcp_nodelay(int fd)
68 int ret, one = 1;
70 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
71 if (unlikely(ret))
72 panic("Cannot set TCP nodelay!\n");
75 int set_ipv6_only(int fd)
77 int one = 1;
78 return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
81 int set_reuseaddr(int fd)
83 int ret, one = 1;
85 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
86 if (unlikely(ret < 0))
87 panic("Cannot reuse addr!\n");
89 return 0;
92 void set_mtu_disc_dont(int fd)
94 int mtu = IP_PMTUDISC_DONT, ret;
96 ret = setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu));
97 if (unlikely(ret))
98 panic("Cannot set MTU discovery options!\n");
101 enum {
102 sock_rmem_max = 0,
103 sock_rmem_def,
104 sock_wmem_max,
105 sock_wmem_def,
108 #define SMEM_SUG_MAX 104857600
109 #define SMEM_SUG_DEF 4194304
111 static const char *const sock_mem[] = {
112 [sock_rmem_max] = "/proc/sys/net/core/rmem_max",
113 [sock_rmem_def] = "/proc/sys/net/core/rmem_default",
114 [sock_wmem_max] = "/proc/sys/net/core/wmem_max",
115 [sock_wmem_def] = "/proc/sys/net/core/wmem_default",
118 static int get_system_socket_mem(int which)
120 int fd, val = -1;
121 ssize_t ret;
122 const char *file = sock_mem[which];
123 char buff[64];
125 fd = open(file, O_RDONLY);
126 if (fd < 0)
127 return val;
129 ret = read(fd, buff, sizeof(buff));
130 if (ret > 0)
131 val = atoi(buff);
133 close(fd);
134 return val;
137 static void set_system_socket_mem(int which, int val)
139 int fd;
140 const char *file = sock_mem[which];
141 ssize_t ret;
142 char buff[64];
144 fd = open(file, O_WRONLY);
145 if (fd < 0)
146 return;
148 memset(buff, 0, sizeof(buff));
149 slprintf(buff, sizeof(buff), "%d", val);
151 ret = write(fd, buff, strlen(buff));
152 ret = ret;
154 close(fd);
157 void set_system_socket_memory(int *vals, size_t len)
159 bug_on(len != 4);
161 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
162 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
163 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
164 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
165 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
166 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
167 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
168 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
171 void reset_system_socket_memory(int *vals, size_t len)
173 bug_on(len != 4);
175 set_system_socket_mem(sock_rmem_max, vals[0]);
176 set_system_socket_mem(sock_rmem_def, vals[1]);
177 set_system_socket_mem(sock_wmem_max, vals[2]);
178 set_system_socket_mem(sock_wmem_def, vals[3]);