Import 2.3.41pre2
[davej-history.git] / net / khttpd / sockets.c
blob0d575abdfb5fad9f5e7861ddc462eb7647e6262b
1 /*
3 kHTTPd -- the next generation
5 Basic socket functions
7 */
8 /****************************************************************
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ****************************************************************/
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>
35 MainSocket is shared by all threads, therefore it has to be
36 a global variable.
39 struct socket *MainSocket=NULL;
42 int StartListening(const int Port)
44 struct socket *sock;
45 struct sockaddr_in sin;
46 int error;
48 EnterFunction("StartListening");
50 /* First create a socket */
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");
58 /* Now bind the socket */
60 sin.sin_family = AF_INET;
61 sin.sin_addr.s_addr = INADDR_ANY;
62 sin.sin_port = htons((unsigned short)Port);
64 error = sock->ops->bind(sock,(struct sockaddr*)&sin,sizeof(sin));
65 if (error<0)
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;
72 /* Grrr... setsockopt() does this. */
73 sock->sk->reuse = 1;
74 /* Wow!!! */
75 sock->sk->linger = 1;
77 /* Now, start listening on the socket */
79 /* I have no idea what a sane backlog-value is. 48 works so far. */
81 error=sock->ops->listen(sock,48);
82 if (error!=0)
83 (void)printk(KERN_ERR "kHTTPd: Error listening on socket \n");
84 sock->flags |= SO_ACCEPTCON;
86 MainSocket = sock;
88 EnterFunction("StartListening");
89 return 1;
92 void StopListening(void)
94 struct socket *sock;
96 EnterFunction("StopListening");
97 if (MainSocket==NULL) return;
99 sock=MainSocket;
100 MainSocket = NULL;
101 sock_release(sock);
103 LeaveFunction("StopListening");