]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http-addr.c
Save work; public accessors for more stuff, continue transition away from private
[thirdparty/cups.git] / cups / http-addr.c
index 4b09d8eaceb108962f94b40de2cbecdcbf784bb5..d9ab970991e0dd23f61b8daf8e5c7a181d16f8e9 100644 (file)
@@ -1,29 +1,16 @@
 /*
- * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $"
+ * "$Id$"
  *
- *   HTTP address routines for CUPS.
+ * HTTP address routines for CUPS.
  *
- *   Copyright 2007-2012 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2013 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   httpAddrAny()       - Check for the "any" address.
- *   httpAddrEqual()     - Compare two addresses.
- *   httpAddrLocalhost() - Check for the local loopback address.
- *   httpAddrLookup()    - Lookup the hostname associated with the address.
- *   _httpAddrPort()     - Get the port number associated with an address.
- *   _httpAddrSetPort()  - Set the port number associated with an address.
- *   httpAddrString()    - Convert an IP address to a dotted string.
- *   httpGetHostByName() - Lookup a hostname or IP address, and return
- *                         address records for the specified name.
- *   httpGetHostname()   - Get the FQDN for the local system.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
@@ -130,6 +117,75 @@ httpAddrLength(const http_addr_t *addr)    /* I - Address */
 }
 
 
+/*
+ * 'httpAddrListen()' - Create a listening socket bound to the specified
+ *                      address and port.
+ *
+ * @since CUPS 1.7/OS X 10.9@
+ */
+
+int                                    /* O - Socket or -1 on error */
+httpAddrListen(http_addr_t *addr,      /* I - Address to bind to */
+               int         port)       /* I - Port number to bind to */
+{
+  int          fd = -1,                /* Socket */
+               val;                    /* Socket value */
+
+
+ /*
+  * Range check input...
+  */
+
+  if (!addr || port <= 0)
+    return (-1);
+
+  if ((fd = socket(addr->addr.sa_family, SOCK_STREAM, 0)) < 0)
+  {
+    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    return (-1);
+  }
+
+  val = 1;
+  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val));
+
+#ifdef IPV6_V6ONLY
+  if (addr->addr.sa_family == AF_INET6)
+    setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, CUPS_SOCAST &val, sizeof(val));
+#endif /* IPV6_V6ONLY */
+
+  _httpAddrSetPort(addr, port);
+
+  if (bind(fd, (struct sockaddr *)addr, httpAddrLength(addr)))
+  {
+    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+
+    close(fd);
+
+    return (-1);
+  }
+
+  if (listen(fd, 5))
+  {
+    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+
+    close(fd);
+
+    return (-1);
+  }
+
+#ifdef SO_NOSIGPIPE
+ /*
+  * Disable SIGPIPE for this socket.
+  */
+
+  val = 1;
+  setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
+#endif /* SO_NOSIGPIPE */
+
+  return (fd);
+}
+
+
 /*
  * 'httpAddrLocalhost()' - Check for the local loopback address.
  *
@@ -162,13 +218,6 @@ httpAddrLocalhost(
 }
 
 
-#ifdef __sgi
-#  define ADDR_CAST (struct sockaddr *)
-#else
-#  define ADDR_CAST (char *)
-#endif /* __sgi */
-
-
 /*
  * 'httpAddrLookup()' - Lookup the hostname associated with the address.
  *
@@ -267,11 +316,11 @@ httpAddrLookup(
 
 #  ifdef AF_INET6
     if (addr->addr.sa_family == AF_INET6)
-      host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr),
+      host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr),
                           sizeof(struct in_addr), AF_INET6);
     else
 #  endif /* AF_INET6 */
-    host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr),
+    host = gethostbyaddr((char *)&(addr->ipv4.sin_addr),
                         sizeof(struct in_addr), AF_INET);
 
     if (host == NULL)
@@ -297,14 +346,30 @@ httpAddrLookup(
 
 
 /*
- * '_httpAddrPort()' - Get the port number associated with an address.
+ * 'httpAddrFamily()' - Get the address family of an address.
+ */
+
+int                                    /* O - Address family */
+httpAddrFamily(http_addr_t *addr)      /* I - Address */
+{
+  if (addr)
+    return (addr->addr.sa_family);
+  else
+    return (0);
+}
+
+
+/*
+ * 'httpAddrPort()' - Get the port number associated with an address.
+ *
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 int                                    /* O - Port number */
-_httpAddrPort(http_addr_t *addr)       /* I - Address */
+httpAddrPort(http_addr_t *addr)                /* I - Address */
 {
   if (!addr)
-    return (ippPort());
+    return (-1);
 #ifdef AF_INET6
   else if (addr->addr.sa_family == AF_INET6)
     return (ntohs(addr->ipv6.sin6_port));
@@ -312,9 +377,12 @@ _httpAddrPort(http_addr_t *addr)   /* I - Address */
   else if (addr->addr.sa_family == AF_INET)
     return (ntohs(addr->ipv4.sin_port));
   else
-    return (ippPort());
+    return (0);
 }
 
+/* For OS X 10.8 and earlier */
+int _httpAddrPort(http_addr_t *addr) { return (httpAddrPort(addr)); }
+
 
 /*
  * '_httpAddrSetPort()' - Set the port number associated with an address.
@@ -498,6 +566,24 @@ httpAddrString(const http_addr_t *addr,    /* I - Address to convert */
 }
 
 
+/*
+ * 'httpGetAddress()' - Get the address of the connected peer of a connection.
+ *
+ * Returns @code NULL@ if the socket is currently unconnected.
+ *
+ * @since CUPS 2.0@
+ */
+
+http_addr_t *                          /* O - Connected address or @code NULL@ */
+httpGetAddress(http_t *http)           /* I - HTTP connection */
+{
+  if (http)
+    return (http->hostaddr);
+  else
+    return (NULL);
+}
+
+
 /*
  * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
  *                         address records for the specified name.
@@ -622,12 +708,16 @@ httpGetHostname(http_t *http,             /* I - HTTP connection or NULL */
                 char   *s,             /* I - String buffer for name */
                 int    slen)           /* I - Size of buffer */
 {
-  if (!s || slen <= 1)
-    return (NULL);
-
   if (http)
   {
-    if (http->hostname[0] == '/')
+    if (!s || slen <= 1)
+    {
+      if (http->hostname[0] == '/')
+       return ("localhost");
+      else
+       return (http->hostname);
+    }
+    else if (http->hostname[0] == '/')
       strlcpy(s, "localhost", slen);
     else
       strlcpy(s, http->hostname, slen);
@@ -638,6 +728,9 @@ httpGetHostname(http_t *http,               /* I - HTTP connection or NULL */
     * Get the hostname...
     */
 
+    if (!s || slen <= 1)
+      return (NULL);
+    
     if (gethostname(s, slen) < 0)
       strlcpy(s, "localhost", slen);
 
@@ -699,5 +792,5 @@ httpGetHostname(http_t *http,               /* I - HTTP connection or NULL */
 
 
 /*
- * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".
+ * End of "$Id$".
  */