2 * "$Id: listen.c 6123 2006-11-21 15:36:04Z mike $"
4 * Server listening routines for the Common UNIX Printing System (CUPS)
7 * Copyright 1997-2006 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 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
27 * cupsdDeleteAllListeners() - Delete all listeners.
28 * cupsdPauseListening() - Clear input polling on all listening sockets...
29 * cupsdResumeListening() - Set input polling on all listening sockets...
30 * cupsdStartListening() - Create all listening sockets...
31 * cupsdStopListening() - Close all listening sockets...
35 * Include necessary headers...
42 * Make sure the IPV6_V6ONLY is defined on Linux - older versions of
43 * glibc don't define it even if the kernel supports it...
46 #if defined(__linux) && !defined(IPV6_V6ONLY)
47 # define IPV6_V6ONLY 26
48 #endif /* __linux && !IPV6_V6ONLY */
52 * 'cupsdDeleteAllListeners()' - Delete all listeners.
56 cupsdDeleteAllListeners(void)
58 cupsd_listener_t
*lis
; /* Current listening socket */
61 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
63 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
66 cupsArrayDelete(Listeners
);
72 * 'cupsdPauseListening()' - Clear input polling on all listening sockets...
76 cupsdPauseListening(void)
78 cupsd_listener_t
*lis
; /* Current listening socket */
81 if (cupsArrayCount(Listeners
) < 1)
84 if (cupsArrayCount(Clients
) == MaxClients
)
85 cupsdLogMessage(CUPSD_LOG_WARN
,
86 "Max clients reached, holding new connections...");
88 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdPauseListening: Clearing input bits...");
90 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
92 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
93 cupsdRemoveSelect(lis
->fd
);
98 * 'cupsdResumeListening()' - Set input polling on all listening sockets...
102 cupsdResumeListening(void)
104 cupsd_listener_t
*lis
; /* Current listening socket */
107 if (cupsArrayCount(Listeners
) < 1)
110 if (cupsArrayCount(Clients
) >= (MaxClients
- 1))
111 cupsdLogMessage(CUPSD_LOG_WARN
, "Resuming new connection processing...");
113 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
114 "cupsdResumeListening: Setting input bits...");
116 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
118 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
119 cupsdAddSelect(lis
->fd
, (cupsd_selfunc_t
)cupsdAcceptClient
, NULL
, lis
);
124 * 'cupsdStartListening()' - Create all listening sockets...
128 cupsdStartListening(void)
130 int status
; /* Bind result */
131 int p
, /* Port number */
132 val
; /* Parameter value */
133 cupsd_listener_t
*lis
; /* Current listening socket */
134 char s
[256]; /* String addresss */
135 const char *have_domain
; /* Have a domain socket? */
136 static const char * const encryptions
[] =
137 { /* Encryption values */
145 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartListening: %d Listeners",
146 cupsArrayCount(Listeners
));
149 * Get the server's IP address...
153 httpAddrFreeList(ServerAddrs
);
155 if ((ServerAddrs
= httpAddrGetList(ServerName
, AF_UNSPEC
, NULL
)) == NULL
)
156 cupsdLogMessage(CUPSD_LOG_ERROR
,
157 "Unable to find IP address for server name \"%s\"!\n",
161 * Setup socket listeners...
164 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
), LocalPort
= 0,
167 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
169 httpAddrString(&(lis
->address
), s
, sizeof(s
));
172 if (lis
->address
.addr
.sa_family
== AF_INET6
)
173 p
= ntohs(lis
->address
.ipv6
.sin6_port
);
175 #endif /* AF_INET6 */
177 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
180 #endif /* AF_LOCAL */
181 p
= ntohs(lis
->address
.ipv4
.sin_port
);
184 * If needed, create a socket for listening...
190 * Create a socket for listening...
193 lis
->fd
= socket(lis
->address
.addr
.sa_family
, SOCK_STREAM
, 0);
197 cupsdLogMessage(CUPSD_LOG_ERROR
,
198 "Unable to open listen socket for address %s:%d - %s.",
199 s
, p
, strerror(errno
));
204 * Set things up to reuse the local address for this port.
209 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
211 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
215 * Bind to the port we found...
219 if (lis
->address
.addr
.sa_family
== AF_INET6
)
223 * Accept only IPv6 connections on this socket, to avoid
224 * potential security issues and to make all platforms behave
230 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *)&val
, sizeof(val
));
232 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
234 # endif /* IPV6_V6ONLY */
236 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
237 httpAddrLength(&(lis
->address
)));
240 #endif /* AF_INET6 */
242 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
244 mode_t mask
; /* Umask setting */
248 * Remove any existing domain socket file...
251 unlink(lis
->address
.un
.sun_path
);
254 * Save the curent umask and set it to 0...
260 * Bind the domain socket...
263 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
264 httpAddrLength(&(lis
->address
)));
267 * Restore the umask...
273 #endif /* AF_LOCAL */
274 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
275 sizeof(lis
->address
.ipv4
));
279 cupsdLogMessage(CUPSD_LOG_ERROR
,
280 "Unable to bind socket for address %s:%d - %s.",
281 s
, p
, strerror(errno
));
288 * Listen for new clients.
291 if (listen(lis
->fd
, ListenBackLog
) < 0)
293 cupsdLogMessage(CUPSD_LOG_ERROR
,
294 "Unable to listen for clients on address %s:%d - %s.",
295 s
, p
, strerror(errno
));
300 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
303 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s:%d on fd %d...",
307 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s on fd %d...",
310 if (chmod(s
, 0140777))
311 cupsdLogMessage(CUPSD_LOG_ERROR
,
312 "Unable to change permisssions on domain socket "
313 "\"%s\" - %s", s
, strerror(errno
));
317 * Save the first port that is bound to the local loopback or
321 if ((!LocalPort
|| LocalEncryption
== HTTP_ENCRYPT_ALWAYS
) && p
> 0 &&
322 (httpAddrLocalhost(&(lis
->address
)) ||
323 httpAddrAny(&(lis
->address
))))
326 LocalEncryption
= lis
->encryption
;
330 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !have_domain
)
331 have_domain
= lis
->address
.un
.sun_path
;
332 #endif /* AF_LOCAL */
336 * Make sure that we are listening on localhost!
339 if (!LocalPort
&& !have_domain
)
341 cupsdLogMessage(CUPSD_LOG_EMERG
,
342 "No Listen or Port lines were found to allow access via "
349 cupsdEndProcess(getpid(), 0);
353 * Set the CUPS_SERVER, IPP_PORT, and CUPS_ENCRYPTION variables based on
360 * Use domain sockets for the local connection...
363 cupsdSetEnv("CUPS_SERVER", have_domain
);
365 LocalEncryption
= HTTP_ENCRYPT_IF_REQUESTED
;
370 * Use the default local loopback address for the server...
373 cupsdSetEnv("CUPS_SERVER", "localhost");
376 cupsdSetEnv("CUPS_ENCRYPTION", encryptions
[LocalEncryption
]);
379 cupsdSetEnvf("IPP_PORT", "%d", LocalPort
);
382 * Resume listening for connections...
385 cupsdResumeListening();
390 * 'cupsdStopListening()' - Close all listening sockets...
394 cupsdStopListening(void)
396 cupsd_listener_t
*lis
; /* Current listening socket */
399 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
400 "cupsdStopListening: closing all listen sockets.");
402 cupsdPauseListening();
404 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
406 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
411 closesocket(lis
->fd
);
418 * Remove domain sockets...
421 # ifdef HAVE_LAUNCH_H
422 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !Launchd
)
424 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
425 # endif /* HAVE_LAUNCH_H */
426 unlink(lis
->address
.un
.sun_path
);
427 #endif /* AF_LOCAL */
434 * End of "$Id: listen.c 6123 2006-11-21 15:36:04Z mike $".