dissector: eth: Free allocated record if line is skipped
[netsniff-ng.git] / sock.c
blob40e4504bdc34952aed7bab328d63d930c49ea1b6
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 one = 1;
69 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
72 int set_ipv6_only(int fd)
74 int one = 1;
75 return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
78 int set_reuseaddr(int fd)
80 int ret, one = 1;
82 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
83 if (unlikely(ret < 0))
84 panic("Cannot reuse addr!\n");
86 return 0;
89 void set_mtu_disc_dont(int fd)
91 int mtu = IP_PMTUDISC_DONT, ret;
93 ret = setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &mtu, sizeof(mtu));
94 if (unlikely(ret))
95 panic("Cannot set MTU discovery options!\n");
98 enum {
99 sock_rmem_max = 0,
100 sock_rmem_def,
101 sock_wmem_max,
102 sock_wmem_def,
105 #define SMEM_SUG_MAX 104857600
106 #define SMEM_SUG_DEF 4194304
108 static const char *const sock_mem[] = {
109 [sock_rmem_max] = "/proc/sys/net/core/rmem_max",
110 [sock_rmem_def] = "/proc/sys/net/core/rmem_default",
111 [sock_wmem_max] = "/proc/sys/net/core/wmem_max",
112 [sock_wmem_def] = "/proc/sys/net/core/wmem_default",
115 static int get_system_socket_mem(int which)
117 int fd, val = -1;
118 ssize_t ret;
119 const char *file = sock_mem[which];
120 char buff[64];
122 fd = open(file, O_RDONLY);
123 if (fd < 0)
124 return val;
126 ret = read(fd, buff, sizeof(buff));
127 if (ret > 0)
128 val = atoi(buff);
130 close(fd);
131 return val;
134 static void set_system_socket_mem(int which, int val)
136 int fd;
137 const char *file = sock_mem[which];
138 ssize_t ret;
139 char buff[64];
141 fd = open(file, O_WRONLY);
142 if (fd < 0)
143 return;
145 memset(buff, 0, sizeof(buff));
146 slprintf(buff, sizeof(buff), "%d", val);
148 ret = write(fd, buff, strlen(buff));
149 ret = ret;
151 close(fd);
154 void set_system_socket_memory(int *vals, size_t len)
156 bug_on(len != 4);
158 if ((vals[0] = get_system_socket_mem(sock_rmem_max)) < SMEM_SUG_MAX)
159 set_system_socket_mem(sock_rmem_max, SMEM_SUG_MAX);
160 if ((vals[1] = get_system_socket_mem(sock_rmem_def)) < SMEM_SUG_DEF)
161 set_system_socket_mem(sock_rmem_def, SMEM_SUG_DEF);
162 if ((vals[2] = get_system_socket_mem(sock_wmem_max)) < SMEM_SUG_MAX)
163 set_system_socket_mem(sock_wmem_max, SMEM_SUG_MAX);
164 if ((vals[3] = get_system_socket_mem(sock_wmem_def)) < SMEM_SUG_DEF)
165 set_system_socket_mem(sock_wmem_def, SMEM_SUG_DEF);
168 void reset_system_socket_memory(int *vals, size_t len)
170 bug_on(len != 4);
172 set_system_socket_mem(sock_rmem_max, vals[0]);
173 set_system_socket_mem(sock_rmem_def, vals[1]);
174 set_system_socket_mem(sock_wmem_max, vals[2]);
175 set_system_socket_mem(sock_wmem_def, vals[3]);