]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/listen.c
2 * "$Id: listen.c,v 1.8 2001/01/22 15:04:00 mike Exp $"
4 * Server listening routines for the Common UNIX Printing System (CUPS)
7 * Copyright 1997-2001 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636-3111 USA
21 * Voice: (301) 373-9603
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
27 * PauseListening() - Clear input polling on all listening sockets...
28 * ResumeListening() - Set input polling on all listening sockets...
29 * StartListening() - Create all listening sockets...
30 * StopListening() - Close all listening sockets...
34 * Include necessary headers...
41 * 'PauseListening()' - Clear input polling on all listening sockets...
47 int i
; /* Looping var */
48 listener_t
*lis
; /* Current listening socket */
51 if (!FD_ISSET(Listeners
[0].fd
, &InputSet
))
54 if (NumClients
== MaxClients
)
55 LogMessage(L_WARN
, "Max clients reached, holding new connections...");
57 LogMessage(L_DEBUG
, "PauseListening: clearing input bits...");
59 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
60 FD_CLR(lis
->fd
, &InputSet
);
65 * 'ResumeListening()' - Set input polling on all listening sockets...
71 int i
; /* Looping var */
72 listener_t
*lis
; /* Current listening socket */
75 if (FD_ISSET(Listeners
[0].fd
, &InputSet
))
78 if (NumClients
>= (MaxClients
- 1))
79 LogMessage(L_WARN
, "Resuming new connection processing...");
81 LogMessage(L_DEBUG
, "ResumeListening: setting input bits...");
83 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
84 FD_SET(lis
->fd
, &InputSet
);
89 * 'StartListening()' - Create all listening sockets...
95 int i
, /* Looping var */
96 val
; /* Parameter value */
97 listener_t
*lis
; /* Current listening socket */
98 struct hostent
*host
; /* Host entry for server address */
101 LogMessage(L_DEBUG
, "StartListening: NumListeners=%d", NumListeners
);
104 * Get the server's IP address...
107 memset(&ServerAddr
, 0, sizeof(ServerAddr
));
109 if ((host
= gethostbyname(ServerName
)) != NULL
)
112 * Found the server's address!
115 memcpy((char *)&(ServerAddr
.sin_addr
), host
->h_addr
, host
->h_length
);
116 ServerAddr
.sin_family
= host
->h_addrtype
;
121 * Didn't find it! Use an address of 0...
124 LogMessage(L_ERROR
, "StartListening: Unable to find IP address for server name \"%s\"!\n",
127 ServerAddr
.sin_family
= AF_INET
;
131 * Setup socket listeners...
134 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
136 LogMessage(L_DEBUG
, "StartListening: address=%08x port=%d",
137 ntohl(lis
->address
.sin_addr
.s_addr
),
138 ntohs(lis
->address
.sin_port
));
140 if ((lis
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
142 LogMessage(L_ERROR
, "StartListening: Unable to open listen socket - %s.",
147 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
150 * Set things up to reuse the local address for this port.
155 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
157 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
161 * Bind to the port we found...
164 if (bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
), sizeof(lis
->address
)) < 0)
166 LogMessage(L_ERROR
, "StartListening: Unable to bind socket - %s.", strerror(errno
));
171 * Listen for new clients.
174 if (listen(lis
->fd
, SOMAXCONN
) < 0)
176 LogMessage(L_ERROR
, "StartListening: Unable to listen for clients - %s.",
187 * 'StopListening()' - Close all listening sockets...
193 int i
; /* Looping var */
194 listener_t
*lis
; /* Current listening socket */
197 LogMessage(L_DEBUG
, "StopListening: closing all listen sockets.");
201 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
202 #if defined(WIN32) || defined(__EMX__)
203 closesocket(lis
->fd
);
206 #endif /* WIN32 || __EMX__ */
211 * End of "$Id: listen.c,v 1.8 2001/01/22 15:04:00 mike Exp $".