]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - net.c
05_dccp-supported-ccid-lookup.diff
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index 126b51400a677a98d431e38214c82451a22f63fc..b83ab8c61d34c3a52f8e0f35dfa63d05c3584f21 100644 (file)
--- a/net.c
+++ b/net.c
@@ -187,6 +187,31 @@ failed:
        return NULL;
 }
 
+/**
+ * Stringify port number, resolve into service name where defined.
+ * \param port 2-byte port number, in host-byte-order.
+ * \param transport Transport protocol name (e.g. "udp", "tcp"), or NULL.
+ * \return Pointer to static result buffer.
+ *
+ * \sa getservent(3), services(5), nsswitch.conf(5)
+ */
+const char *stringify_port(int port, const char *transport)
+{
+       static char service[NI_MAXSERV];
+
+       if (port < 0 || port > 0xFFFF) {
+               snprintf(service, sizeof(service), "undefined (%d)", port);
+       } else {
+               struct servent *se = getservbyport(htons(port), transport);
+
+               if (se == NULL)
+                       snprintf(service, sizeof(service), "%u", port);
+               else
+                       snprintf(service, sizeof(service), "%s", se->s_name);
+       }
+       return service;
+}
+
 /**
  * Determine the socket type for a given layer-4 protocol.
  *
@@ -412,7 +437,7 @@ normalize_ip_address(const struct sockaddr_storage *ss)
  *
  * \param sa The IPv4/IPv6 socket address to use.
  *
- * \sa getnameinfo(3).
+ * \sa getnameinfo(3), services(5), nsswitch.conf(5)
  */
 static char *host_and_port(const struct sockaddr_storage *ss)
 {
@@ -424,7 +449,7 @@ static char *host_and_port(const struct sockaddr_storage *ss)
        ret = getnameinfo(sa, salen(sa),
                          hbuf, sizeof(hbuf),
                          sbuf, sizeof(sbuf),
-                         NI_NUMERICHOST | NI_NUMERICSERV);
+                         NI_NUMERICHOST);
        if (ret == 0) {
                snprintf(output, sizeof(output), "%s#%s", hbuf, sbuf);
        } else {
@@ -636,6 +661,33 @@ int para_accept(int fd, void *addr, socklen_t size)
        return new_fd < 0? -ERRNO_TO_PARA_ERROR(errno) : new_fd;
 }
 
+/**
+ * Probe the list of DCCP CCIDs supported locally by the host.
+ * \param ccids Array to be filled in.
+ * \param nccids Length of \a ccids.
+ * \return Pointer to \a ccids, NULL on failure.
+ *
+ * NB: This feature is only available on Linux > 2.6.30; on older kernels
+ * ENOPROTOOPT ("Protocol not available") will be returned.
+ */
+const uint8_t *dccp_available_ccids(uint8_t *ccids, uint8_t *nccids)
+{
+       int fd = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, NULL, 0);
+
+       if (fd < 0)
+               return NULL;
+
+       if (getsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_AVAILABLE_CCIDS,
+                                       ccids, (socklen_t *)nccids) < 0) {
+               PARA_ERROR_LOG("No DCCP_SOCKOPT_AVAILABLE_CCIDS: %s\n",
+                               strerror(errno));
+               *nccids = 0;
+       }
+       close(fd);
+
+       return *nccids ? ccids : NULL;
+}
+
 /**
  * Prepare a structure for \p AF_UNIX socket addresses.
  *