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#include "prototypes.h"
26#include <linux/kernel.h>
27#include <linux/net.h>
28#include <linux/version.h>
29#include <linux/smp_lock.h>
30#include <net/sock.h>
31
32
33
34
35
36
37
38
39struct socket *MainSocket=NULL;
40
41
42int StartListening(const int Port)
43{
44 struct socket *sock;
45 struct sockaddr_in sin;
46 int error;
47
48 EnterFunction("StartListening");
49
50
51
52 error = sock_create(PF_INET,SOCK_STREAM,IPPROTO_TCP,&sock);
53 if (error<0)
54 (void)printk(KERN_ERR "Error during creation of socket; terminating\n");
55
56
57
58
59
60 sin.sin_family = AF_INET;
61 sin.sin_addr.s_addr = INADDR_ANY;
62 sin.sin_port = htons((unsigned short)Port);
63
64 error = sock->ops->bind(sock,(struct sockaddr*)&sin,sizeof(sin));
65 if (error<0)
66 {
67 (void)printk(KERN_ERR "kHTTPd: Error binding socket. This means that some other \n");
68 (void)printk(KERN_ERR " daemon is (or was a short time ago) using port %i.\n",Port);
69 return 0;
70 }
71
72
73 sock->sk->reuse = 1;
74
75
76
77
78
79 error=sock->ops->listen(sock,48);
80 if (error!=0)
81 (void)printk(KERN_ERR "kHTTPd: Error listening on socket \n");
82
83 MainSocket = sock;
84
85 LeaveFunction("StartListening");
86 return 1;
87}
88
89void StopListening(void)
90{
91 struct socket *sock;
92
93 EnterFunction("StopListening");
94 if (MainSocket==NULL) return;
95
96 sock=MainSocket;
97 MainSocket = NULL;
98 sock_release(sock);
99
100 LeaveFunction("StopListening");
101}
102