]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: Add port_to_str helper
authorSimon Horman <horms@verge.net.au>
Mon, 16 Jun 2014 00:39:41 +0000 (09:39 +0900)
committerWilly Tarreau <w@1wt.eu>
Mon, 16 Jun 2014 08:10:33 +0000 (10:10 +0200)
This helper is similar to addr_to_str but
tries to convert the port rather than the address
of a struct sockaddr_storage.

This is in preparation for supporting
an external agent check.

Signed-off-by: Simon Horman <horms@verge.net.au>
include/common/standard.h
src/standard.c

index 8acd2778ea8d93f2803a7083f0cd7f5bdcbabe02..ecac1e02e4981e77d33bc831b0e1b3c4da9da049 100644 (file)
@@ -291,6 +291,14 @@ int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct spli
  */
 int addr_to_str(struct sockaddr_storage *addr, char *str, int size);
 
+/* Tries to convert a sockaddr_storage port to text form. Upon success, the
+ * address family is returned so that it's easy for the caller to adapt to the
+ * output format. Zero is returned if the address family is not supported. -1
+ * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
+ * supported.
+ */
+int port_to_str(struct sockaddr_storage *addr, char *str, int size);
+
 /* will try to encode the string <string> replacing all characters tagged in
  * <map> with the hexadecimal representation of their ASCII-code (2 digits)
  * prefixed by <escape>, and will store the result between <start> (included)
index 06176d70866976b7c7324e4794802f8dcff4f40c..b0c5fe6eb23488cf7a576b02a117cf3359cc678a 100644 (file)
@@ -1156,6 +1156,40 @@ int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
        return -1;
 }
 
+/* Tries to convert a sockaddr_storage port to text form. Upon success, the
+ * address family is returned so that it's easy for the caller to adapt to the
+ * output format. Zero is returned if the address family is not supported. -1
+ * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
+ * supported.
+ */
+int port_to_str(struct sockaddr_storage *addr, char *str, int size)
+{
+
+       uint16_t port;
+
+
+       if (size < 5)
+               return 0;
+       *str = '\0';
+
+       switch (addr->ss_family) {
+       case AF_INET:
+               port = ((struct sockaddr_in *)addr)->sin_port;
+               break;
+       case AF_INET6:
+               port = ((struct sockaddr_in6 *)addr)->sin6_port;
+               break;
+       case AF_UNIX:
+               memcpy(str, "unix", 5);
+               return addr->ss_family;
+       default:
+               return 0;
+       }
+
+       snprintf(str, size, "%u", ntohs(port));
+       return addr->ss_family;
+}
+
 /* will try to encode the string <string> replacing all characters tagged in
  * <map> with the hexadecimal representation of their ASCII-code (2 digits)
  * prefixed by <escape>, and will store the result between <start> (included)