2 * Simple UDP implementation
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/net.h>
14 MODULE_DESCRIPTION("P2P Swift Protocol");
15 MODULE_AUTHOR("Razvan Crainea and Tudor Cazangiu");
16 MODULE_LICENSE("GPL");
18 #define LOG_LEVEL KERN_ALERT
19 #define P2PSP_LOCAL_PORT 60000
20 #define P2PSP_REMOTE_PORT1 60001
21 #define P2PSP_REMOTE_PORT2 60002
22 #define MY_TEST_MESSAGE "kernelsocket\n"
31 printk(KERN_DEBUG s "\n"); \
38 #define print_sock_address(addr) \
40 printk(LOG_LEVEL "connection established to " \
41 NIPQUAD_FMT ":%d\n", \
42 NIPQUAD(addr.sin_addr.s_addr), \
43 ntohs(addr.sin_port)); \
46 static struct socket *sock; /* UDP server */
49 static int p2psp_msgsend(struct socket *s, short port)
51 /* address to send to */
52 struct sockaddr_in raddr = {
53 .sin_family = AF_INET,
54 .sin_port = htons(port),
55 .sin_addr = { htonl(INADDR_LOOPBACK) }
57 int raddrlen = sizeof(raddr);
61 char *buffer = MY_TEST_MESSAGE;
62 int len = strlen(buffer) + 1;
65 iov.iov_base = buffer;
67 msg.msg_name = &raddr;
68 msg.msg_namelen = raddrlen;
71 msg.msg_control = NULL;
72 msg.msg_controllen = 0;
75 /* send the message down the socket */
76 return kernel_sendmsg(s, &msg, (struct kvec *) msg.msg_iov, 1, len);
79 int __init p2psp_sock_init(void)
82 /* address to bind on */
83 struct sockaddr_in addr = {
84 .sin_family = AF_INET,
85 .sin_port = htons(P2PSP_LOCAL_PORT),
86 .sin_addr = { htonl(INADDR_LOOPBACK) }
88 int addrlen = sizeof(addr);
90 /* create UDP socket */
91 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
93 printk(LOG_LEVEL "can't create socket\n");
97 /* bind socket to loopback on port P2PSP_LOCAL_PORT */
98 err = sock->ops->bind(sock, (struct sockaddr *) &addr, addrlen);
100 printk(LOG_LEVEL "can't bind socket\n");
104 /* send first message */
105 err = p2psp_msgsend(sock, P2PSP_REMOTE_PORT1);
107 printk(LOG_LEVEL "can't send first message\n");
111 /* send second message */
112 err = p2psp_msgsend(sock, P2PSP_REMOTE_PORT2);
114 printk(LOG_LEVEL "can't send second message\n");
128 void __exit p2psp_sock_exit(void)
134 module_init(p2psp_sock_init);
135 module_exit(p2psp_sock_exit);