2 * "$Id: listen.c 6649 2007-07-11 21:46:42Z mike $"
4 * Server listening routines for the Common UNIX Printing System (CUPS)
7 * Copyright 2007 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * cupsdDeleteAllListeners() - Delete all listeners.
19 * cupsdPauseListening() - Clear input polling on all listening sockets...
20 * cupsdResumeListening() - Set input polling on all listening sockets...
21 * cupsdStartListening() - Create all listening sockets...
22 * cupsdStopListening() - Close all listening sockets...
26 * Include necessary headers...
33 * Make sure the IPV6_V6ONLY is defined on Linux - older versions of
34 * glibc don't define it even if the kernel supports it...
37 #if defined(__linux) && !defined(IPV6_V6ONLY)
38 # define IPV6_V6ONLY 26
39 #endif /* __linux && !IPV6_V6ONLY */
43 * 'cupsdDeleteAllListeners()' - Delete all listeners.
47 cupsdDeleteAllListeners(void)
49 cupsd_listener_t
*lis
; /* Current listening socket */
52 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
54 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
57 cupsArrayDelete(Listeners
);
63 * 'cupsdPauseListening()' - Clear input polling on all listening sockets...
67 cupsdPauseListening(void)
69 cupsd_listener_t
*lis
; /* Current listening socket */
72 if (cupsArrayCount(Listeners
) < 1)
75 if (cupsArrayCount(Clients
) == MaxClients
)
76 cupsdLogMessage(CUPSD_LOG_WARN
,
77 "Max clients reached, holding new connections...");
79 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdPauseListening: Clearing input bits...");
81 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
83 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
84 cupsdRemoveSelect(lis
->fd
);
89 * 'cupsdResumeListening()' - Set input polling on all listening sockets...
93 cupsdResumeListening(void)
95 cupsd_listener_t
*lis
; /* Current listening socket */
98 if (cupsArrayCount(Listeners
) < 1)
101 if (cupsArrayCount(Clients
) >= (MaxClients
- 1))
102 cupsdLogMessage(CUPSD_LOG_WARN
, "Resuming new connection processing...");
104 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
105 "cupsdResumeListening: Setting input bits...");
107 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
109 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
110 cupsdAddSelect(lis
->fd
, (cupsd_selfunc_t
)cupsdAcceptClient
, NULL
, lis
);
115 * 'cupsdStartListening()' - Create all listening sockets...
119 cupsdStartListening(void)
121 int status
; /* Bind result */
122 int p
, /* Port number */
123 val
; /* Parameter value */
124 cupsd_listener_t
*lis
; /* Current listening socket */
125 char s
[256]; /* String addresss */
126 const char *have_domain
; /* Have a domain socket? */
127 static const char * const encryptions
[] =
128 { /* Encryption values */
136 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdStartListening: %d Listeners",
137 cupsArrayCount(Listeners
));
140 * Get the server's IP address...
144 httpAddrFreeList(ServerAddrs
);
146 if ((ServerAddrs
= httpAddrGetList(ServerName
, AF_UNSPEC
, NULL
)) == NULL
)
147 cupsdLogMessage(CUPSD_LOG_ERROR
,
148 "Unable to find IP address for server name \"%s\"!\n",
152 * Setup socket listeners...
155 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
), LocalPort
= 0,
158 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
160 httpAddrString(&(lis
->address
), s
, sizeof(s
));
163 if (lis
->address
.addr
.sa_family
== AF_INET6
)
164 p
= ntohs(lis
->address
.ipv6
.sin6_port
);
166 #endif /* AF_INET6 */
168 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
171 #endif /* AF_LOCAL */
172 p
= ntohs(lis
->address
.ipv4
.sin_port
);
175 * If needed, create a socket for listening...
181 * Create a socket for listening...
184 lis
->fd
= socket(lis
->address
.addr
.sa_family
, SOCK_STREAM
, 0);
188 cupsdLogMessage(CUPSD_LOG_ERROR
,
189 "Unable to open listen socket for address %s:%d - %s.",
190 s
, p
, strerror(errno
));
195 * Set things up to reuse the local address for this port.
200 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
202 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
206 * Bind to the port we found...
210 if (lis
->address
.addr
.sa_family
== AF_INET6
)
214 * Accept only IPv6 connections on this socket, to avoid
215 * potential security issues and to make all platforms behave
221 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *)&val
, sizeof(val
));
223 setsockopt(lis
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
225 # endif /* IPV6_V6ONLY */
227 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
228 httpAddrLength(&(lis
->address
)));
231 #endif /* AF_INET6 */
233 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
235 mode_t mask
; /* Umask setting */
239 * Remove any existing domain socket file...
242 unlink(lis
->address
.un
.sun_path
);
245 * Save the curent umask and set it to 0...
251 * Bind the domain socket...
254 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
255 httpAddrLength(&(lis
->address
)));
258 * Restore the umask...
264 #endif /* AF_LOCAL */
265 status
= bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
),
266 sizeof(lis
->address
.ipv4
));
270 cupsdLogMessage(CUPSD_LOG_ERROR
,
271 "Unable to bind socket for address %s:%d - %s.",
272 s
, p
, strerror(errno
));
279 * Listen for new clients.
282 if (listen(lis
->fd
, ListenBackLog
) < 0)
284 cupsdLogMessage(CUPSD_LOG_ERROR
,
285 "Unable to listen for clients on address %s:%d - %s.",
286 s
, p
, strerror(errno
));
291 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
294 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s:%d on fd %d...",
298 cupsdLogMessage(CUPSD_LOG_INFO
, "Listening to %s on fd %d...",
301 if (chmod(s
, 0140777))
302 cupsdLogMessage(CUPSD_LOG_ERROR
,
303 "Unable to change permisssions on domain socket "
304 "\"%s\" - %s", s
, strerror(errno
));
308 * Save the first port that is bound to the local loopback or
312 if ((!LocalPort
|| LocalEncryption
== HTTP_ENCRYPT_ALWAYS
) && p
> 0 &&
313 (httpAddrLocalhost(&(lis
->address
)) ||
314 httpAddrAny(&(lis
->address
))))
317 LocalEncryption
= lis
->encryption
;
321 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !have_domain
)
322 have_domain
= lis
->address
.un
.sun_path
;
323 #endif /* AF_LOCAL */
327 * Make sure that we are listening on localhost!
330 if (!LocalPort
&& !have_domain
)
332 cupsdLogMessage(CUPSD_LOG_EMERG
,
333 "No Listen or Port lines were found to allow access via "
340 cupsdEndProcess(getpid(), 0);
344 * Set the CUPS_SERVER, IPP_PORT, and CUPS_ENCRYPTION variables based on
351 * Use domain sockets for the local connection...
354 cupsdSetEnv("CUPS_SERVER", have_domain
);
356 LocalEncryption
= HTTP_ENCRYPT_IF_REQUESTED
;
361 * Use the default local loopback address for the server...
364 cupsdSetEnv("CUPS_SERVER", "localhost");
367 cupsdSetEnv("CUPS_ENCRYPTION", encryptions
[LocalEncryption
]);
370 cupsdSetEnvf("IPP_PORT", "%d", LocalPort
);
373 * Resume listening for connections...
376 cupsdResumeListening();
381 * 'cupsdStopListening()' - Close all listening sockets...
385 cupsdStopListening(void)
387 cupsd_listener_t
*lis
; /* Current listening socket */
390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
391 "cupsdStopListening: closing all listen sockets.");
393 cupsdPauseListening();
395 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
397 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
402 closesocket(lis
->fd
);
409 * Remove domain sockets...
412 # ifdef HAVE_LAUNCH_H
413 if (lis
->address
.addr
.sa_family
== AF_LOCAL
&& !Launchd
)
415 if (lis
->address
.addr
.sa_family
== AF_LOCAL
)
416 # endif /* HAVE_LAUNCH_H */
417 unlink(lis
->address
.un
.sun_path
);
418 #endif /* AF_LOCAL */
425 * End of "$Id: listen.c 6649 2007-07-11 21:46:42Z mike $".