~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to ntop/intop/bind.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
 
 * bind.c - bind a network port to look at incoming web requests.
4
 
 *
5
 
 * Luca Deri     <deri@ntop.org>
6
 
 * Rocco Carbone <rocco@ntop.org>
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 of the License, or
12
 
 * (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
 
 */
23
 
 
24
 
 
25
 
/*
26
 
 * ntop header file(s)
27
 
 */
28
 
#include "ntop.h"
29
 
 
30
 
#include "intop.h"
31
 
 
32
 
 
33
 
/*
34
 
 *
35
 
 */
36
 
int intop_bind (char * address)
37
 
{
38
 
  char * c;
39
 
  short port = NTOP_DEFAULT_WEB_PORT;                    /* default ntop mini-web port */
40
 
  struct hostent * host;
41
 
  struct sockaddr_in sockaddr = {0};
42
 
  int sockfd;
43
 
  int sockopt = 1;
44
 
 
45
 
  if (address)
46
 
    {
47
 
      /* try to decode address in the hostname:port syntax */
48
 
      c = strchr (address, ':');
49
 
      if (c)
50
 
        {
51
 
          port = atoi (c + 1);
52
 
          * c = '\0';
53
 
        }
54
 
 
55
 
      host = gethostbyname (address);
56
 
      if (host)
57
 
        memcpy (& sockaddr . sin_addr, host -> h_addr_list [0], host -> h_length);
58
 
      else
59
 
        sockaddr . sin_addr . s_addr = inet_addr (address);
60
 
 
61
 
      if (sockaddr . sin_addr . s_addr == -1)
62
 
        {
63
 
          printf ("Warning: wrong address '%s'. continue to all available interfaces!\n", address);
64
 
          sockaddr . sin_addr . s_addr = htonl (INADDR_ANY);
65
 
        }
66
 
    }
67
 
  else
68
 
    sockaddr . sin_addr . s_addr = htonl (INADDR_ANY);
69
 
 
70
 
 
71
 
  sockaddr . sin_family = AF_INET;
72
 
  sockaddr . sin_port = htons (port);
73
 
 
74
 
 
75
 
  /*
76
 
   * create a socket for the management of all the incoming HTTP calls
77
 
   */
78
 
  sockfd = socket (AF_INET, SOCK_STREAM, 0);
79
 
  if (sockfd == -1)
80
 
    {
81
 
      printf ("Cannot open socket: errno = %d\n", errno);
82
 
      return (-1);
83
 
    }
84
 
 
85
 
  /*
86
 
   * set option in order to reuse address if busy
87
 
   */
88
 
  if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
89
 
                  (const char *) & sockopt, sizeof (sockopt)))
90
 
    {
91
 
      printf ("Cannot set socket options: errno = %d\n", errno);
92
 
      close (sockfd);
93
 
      return (-1);
94
 
    }
95
 
 
96
 
 
97
 
  /*
98
 
   * bind to local address
99
 
   */
100
 
  if (bind (sockfd, (struct sockaddr *) & sockaddr, sizeof (sockaddr)))
101
 
    {
102
 
      printf ("Cannot bind at port %d: errno = %d\n", port, errno);
103
 
      close (sockfd);
104
 
      return (-1);
105
 
    }
106
 
 
107
 
 
108
 
  /*
109
 
   * Set queue limit for incoming calls.
110
 
   * The maximum length of the queue for pending connections is set to 256
111
 
   */
112
 
  if (listen (sockfd, 256))
113
 
    {
114
 
      printf ("Cannot listen: errno = %d\n", errno);
115
 
      close (sockfd);
116
 
      return (-1);
117
 
    }
118
 
 
119
 
  return (sockfd);
120
 
}