-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp.c
221 lines (177 loc) · 6.1 KB
/
udp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/delay.h>
#define DEFAULT_PORT 2325
#define CONNECT_PORT 23
#define MODULE_NAME "m_server"
#define INADDR_SEND INADDR_LOOPBACK
struct kthread_t
{
struct task_struct *thread;
struct socket *sock;
struct sockaddr_in addr;
int running;
};
struct kthread_t *kthread = NULL;
int ksocket_receive(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf, int len);
int ksocket_send(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf, int len);
static void ksocket_start(void)
{
int size, err;
int bufsize = 10;
unsigned char buf[bufsize+1];
/* kernel thread initialization */
lock_kernel();
current->flags |= PF_NOFREEZE;
/* daemonize (take care with signals, after daemonize() they are disabled) */
daemonize(MODULE_NAME);
allow_signal(SIGKILL);
unlock_kernel();
kthread->running = 0;
/* create a socket */
if ( ( (err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &kthread->sock)) < 0) )
{
printk(KERN_INFO MODULE_NAME": Could not create a datagram socket, error = %d\n", -ENXIO);
goto out;
}
memset(&kthread->addr, 0, sizeof(struct sockaddr));
kthread->addr.sin_family = AF_INET;
kthread->addr.sin_addr.s_addr = htonl(INADDR_ANY);
kthread->addr.sin_port = htons(DEFAULT_PORT);
if ( ( (err = kthread->sock->ops->bind(kthread->sock, (struct sockaddr *)&kthread->addr, sizeof(struct sockaddr) ) ) < 0))
{
printk(KERN_INFO MODULE_NAME": Could not bind or connect to socket, error = %d\n", -err);
goto close_and_out;
}
printk(KERN_INFO MODULE_NAME": listening on port %d\n", DEFAULT_PORT);
/* main loop */
for (;;)
{
memset(&buf, 0, bufsize+1);
size = ksocket_receive(kthread->sock, &kthread->addr, buf, bufsize);
if (signal_pending(current))
break;
if (size < 0)
printk(KERN_INFO MODULE_NAME": error getting datagram, sock_recvmsg error = %d\n", size);
else
{
printk(KERN_INFO MODULE_NAME": received %d bytes\n", size);
/* data processing */
printk("\n data: %s\n", buf);
/* sending */
memset(&buf, 0, bufsize+1);
strcat(buf, "testing...");
ksocket_send(kthread->sock, &kthread->addr, buf, strlen(buf));
}
}
close_and_out:
sock_release(kthread->sock);
kthread->sock = NULL;
out:
kthread->thread = NULL;
kthread->running = 0;
}
int ksocket_send(struct socket *sock, struct sockaddr_in *addr, unsigned char *buf, int len)
{
struct msghdr msg;
struct iovec iov;
mm_segment_t oldfs;
int size = 0;
if (sock->sk==NULL)
return 0;
iov.iov_base = buf;
iov.iov_len = len;
msg.msg_flags = 0;
msg.msg_name = addr;
msg.msg_namelen = sizeof(struct sockaddr_in);
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
oldfs = get_fs();
set_fs(KERNEL_DS);
size = sock_sendmsg(sock,&msg,len);
set_fs(oldfs);
return size;
}
int ksocket_receive(struct socket* sock, struct sockaddr_in* addr, unsigned char* buf, int len)
{
struct msghdr msg;
struct iovec iov;
mm_segment_t oldfs;
int size = 0;
if (sock->sk==NULL) return 0;
iov.iov_base = buf;
iov.iov_len = len;
msg.msg_flags = 0;
msg.msg_name = addr;
msg.msg_namelen = sizeof(struct sockaddr_in);
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
oldfs = get_fs();
set_fs(KERNEL_DS);
size = sock_recvmsg(sock,&msg,len,msg.msg_flags);
set_fs(oldfs);
return size;
}
int __init ksocket_init(void)
{
kthread = kmalloc(sizeof(struct kthread_t), GFP_KERNEL);
memset(kthread, 0, sizeof(struct kthread_t));
kthread->thread = kthread_run((void *)ksocket_start, NULL, MODULE_NAME);
if (IS_ERR(kthread->thread))
{
printk(KERN_INFO MODULE_NAME": unable to start kernel thread\n");
kfree(kthread);
kthread = NULL;
return -ENOMEM;
}
return 0;
}
void __exit ksocket_exit(void)
{
#if 0
if (kthread->thread==NULL)
printk(KERN_INFO MODULE_NAME": no kernel thread to kill\n");
else
{
lock_kernel();
err = kill_proc(kthread->thread->pid, SIGKILL, 1);
unlock_kernel();
if (err < 0)
printk(KERN_INFO MODULE_NAME": unknown error %d while trying to terminate kernel thread\n",-err);
else
{
while (kthread->running == 1)
msleep(10);
printk(KERN_INFO MODULE_NAME": succesfully killed kernel thread!\n");
}
}
#endif
if (kthread->running)
kthread_stop(kthread->thread);
if (kthread->sock != NULL)
{
sock_release(kthread->sock);
kthread->sock = NULL;
}
kfree(kthread);
kthread = NULL;
printk(KERN_INFO MODULE_NAME": module unloaded\n");
}
module_init(ksocket_init);
module_exit(ksocket_exit);
MODULE_DESCRIPTION("UDP socket example");
MODULE_AUTHOR("Toni Garcia-Navarro <[email protected]>");
MODULE_LICENSE("GPL");