linux-old/net/khttpd/sockets.c History
<<
>>
Prefs
   1/*
   2
   3kHTTPd -- the next generation
   4
   5Basic socket functions
   6
   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.
  13 *
  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.
  18 *
  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.
  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
  35MainSocket is shared by all threads, therefore it has to be
  36a global variable.
  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        /* First create a socket */
  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        /* Now bind the socket */
  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        /* Grrr... setsockopt() does this. */
  73        sock->sk->reuse   = 1;
  74
  75        /* Now, start listening on the socket */
  76        
  77        /* I have no idea what a sane backlog-value is. 48 works so far. */
  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
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.