Skip to content

Commit 6312a2d

Browse files
authored
Support Linux v5.8+ (#5)
kernel_setsockopt was removed since Linux v5.8. Since Linux v5.8, sock_setsockopt only accepts user space value as the argument.
1 parent 60d88c4 commit 6312a2d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

main.c

+62
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/kthread.h>
44
#include <linux/sched/signal.h>
55
#include <linux/tcp.h>
6+
#include <linux/version.h>
67
#include <net/sock.h>
78

89
#include "http_server.h"
@@ -19,6 +20,67 @@ static struct socket *listen_socket;
1920
static struct http_server_param param;
2021
static struct task_struct *http_server;
2122

23+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
24+
static int set_sock_opt(struct socket *sock,
25+
int level,
26+
int optname,
27+
char *optval,
28+
unsigned int optlen)
29+
{
30+
int ret = 0;
31+
32+
if (optlen < sizeof(int))
33+
return -EINVAL;
34+
35+
switch (optname) {
36+
case SO_REUSEADDR:
37+
sock_set_reuseaddr(sock->sk);
38+
break;
39+
case SO_RCVBUF:
40+
sock_set_rcvbuf(sock->sk, *(int *) optval);
41+
break;
42+
}
43+
44+
return ret;
45+
}
46+
47+
static int set_tcp_opt(struct socket *sock,
48+
int level,
49+
int optname,
50+
char *optval,
51+
unsigned int optlen)
52+
{
53+
int ret = 0;
54+
55+
if (optlen < sizeof(int))
56+
return -EINVAL;
57+
58+
switch (optname) {
59+
case TCP_NODELAY:
60+
tcp_sock_set_nodelay(sock->sk);
61+
break;
62+
case TCP_CORK:
63+
tcp_sock_set_cork(sock->sk, *(bool *) optval);
64+
break;
65+
}
66+
67+
return ret;
68+
}
69+
70+
static int kernel_setsockopt(struct socket *sock,
71+
int level,
72+
int optname,
73+
char *optval,
74+
unsigned int optlen)
75+
{
76+
if (level == SOL_SOCKET)
77+
return set_sock_opt(sock, level, optname, optval, optlen);
78+
else if (level == SOL_TCP)
79+
return set_tcp_opt(sock, level, optname, optval, optlen);
80+
return -EINVAL;
81+
}
82+
#endif
83+
2284
static inline int setsockopt(struct socket *sock,
2385
int level,
2486
int optname,

0 commit comments

Comments
 (0)