]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http-addr.c
Merge changes from CUPS 1.7svn-r10704.
[thirdparty/cups.git] / cups / http-addr.c
index d80bb8d017641b3ae19b1960c0705513463ab8d0..9cb9bee30544cd6ca73308f1ec3aa8a21a5d658f 100644 (file)
  *
  * Contents:
  *
- *   httpAddrAny()       - Check for the "any" address.
- *   httpAddrEqual()     - Compare two addresses.
+ *   httpAddrAny()      - Check for the "any" address.
+ *   httpAddrEqual()    - Compare two addresses.
+ *   httpAddrLength()   - Return the length of the address in bytes.
+ *   httpAddrListen()   - Create a listening socket bound to the specified
+ *                        address and port.
  *   httpAddrLocalhost() - Check for the local loopback address.
- *   httpAddrLookup()    - Lookup the hostname associated with the address.
- *   _httpAddrPort()     - Get the port number associated with an 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.
+ *   httpAddrString()   - Convert an address to a numeric string.
+ *   httpGetHostByName() - Lookup a hostname or IPv4 address, and return
+ *                        address records for the specified name.
+ *   httpGetHostname()  - Get the FQDN for the connection or local system.
  */
 
 /*
@@ -130,6 +133,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@
+ */
+
+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, &val, sizeof(val));
+
+#ifdef IPV6_V6ONLY
+  if (addr->addr.sa_family == AF_INET6)
+    setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &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, &val, sizeof(val));
+#endif /* SO_NOSIGPIPE */
+
+  return (fd);
+}
+
+
 /*
  * 'httpAddrLocalhost()' - Check for the local loopback address.
  *
@@ -290,11 +362,13 @@ httpAddrLookup(
 
 
 /*
- * '_httpAddrPort()' - Get the port number associated with an address.
+ * 'httpAddrPort()' - Get the port number associated with an address.
+ *
+ * @since CUPS 1.7@
  */
 
 int                                    /* O - Port number */
-_httpAddrPort(http_addr_t *addr)       /* I - Address */
+httpAddrPort(http_addr_t *addr)                /* I - Address */
 {
   if (!addr)
     return (ippPort());
@@ -308,6 +382,13 @@ _httpAddrPort(http_addr_t *addr)   /* I - Address */
     return (ippPort());
 }
 
+int                                    /* O - Port number */
+_httpAddrPort(http_addr_t *addr)       /* I - Address */
+{
+ /* TODO: Remove in CUPS 1.8 */
+  return (httpAddrPort(addr));
+}
+
 
 /*
  * '_httpAddrSetPort()' - Set the port number associated with an address.