2
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
* bind.c - bind a network port to look at incoming web requests.
5
* Luca Deri <deri@ntop.org>
6
* Rocco Carbone <rocco@ntop.org>
7
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36
int intop_bind (char * address)
39
short port = NTOP_DEFAULT_WEB_PORT; /* default ntop mini-web port */
40
struct hostent * host;
41
struct sockaddr_in sockaddr = {0};
47
/* try to decode address in the hostname:port syntax */
48
c = strchr (address, ':');
55
host = gethostbyname (address);
57
memcpy (& sockaddr . sin_addr, host -> h_addr_list [0], host -> h_length);
59
sockaddr . sin_addr . s_addr = inet_addr (address);
61
if (sockaddr . sin_addr . s_addr == -1)
63
printf ("Warning: wrong address '%s'. continue to all available interfaces!\n", address);
64
sockaddr . sin_addr . s_addr = htonl (INADDR_ANY);
68
sockaddr . sin_addr . s_addr = htonl (INADDR_ANY);
71
sockaddr . sin_family = AF_INET;
72
sockaddr . sin_port = htons (port);
76
* create a socket for the management of all the incoming HTTP calls
78
sockfd = socket (AF_INET, SOCK_STREAM, 0);
81
printf ("Cannot open socket: errno = %d\n", errno);
86
* set option in order to reuse address if busy
88
if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
89
(const char *) & sockopt, sizeof (sockopt)))
91
printf ("Cannot set socket options: errno = %d\n", errno);
98
* bind to local address
100
if (bind (sockfd, (struct sockaddr *) & sockaddr, sizeof (sockaddr)))
102
printf ("Cannot bind at port %d: errno = %d\n", port, errno);
109
* Set queue limit for incoming calls.
110
* The maximum length of the queue for pending connections is set to 256
112
if (listen (sockfd, 256))
114
printf ("Cannot listen: errno = %d\n", errno);