]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard.c: ipcpy() function to copy an IP address from a struct sockaddr_stor...
authorBaptiste Assmann <bedis9@gmail.com>
Sat, 30 Jan 2016 23:27:17 +0000 (00:27 +0100)
committerWilly Tarreau <w@1wt.eu>
Sun, 14 Aug 2016 10:16:43 +0000 (12:16 +0200)
The function ipcpy() simply duplicates the IP address found in one
struct sockaddr_storage into an other struct sockaddr_storage.
It also update the family on the destination structure.

Memory of destination structure must be allocated and cleared by the
caller.

include/common/standard.h
src/standard.c

index bc1ab403d2bb9c5e99fcd63e3a75a2d8d5ae0033..d4f2448ef1ebca982c26f4fa1f26a28e16cbab64 100644 (file)
@@ -886,6 +886,12 @@ extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr);
  */
 int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2);
 
+/* copy ip from <source> into <dest>
+ * the caller must clear <dest> before calling.
+ * Returns a pointer to the destination
+ */
+struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest);
+
 char *human_time(int t, short hz_div);
 
 extern const char *monthname[];
index d85c72020c0cb66d17e7e44a5e36378ffbfe347d..5937b482f7506eb4e109129210b9fbb406baea16 100644 (file)
@@ -2588,6 +2588,27 @@ int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2)
        return 1;
 }
 
+/* copy IP address from <source> into <dest>
+ * the caller must allocate and clear <dest> before calling.
+ * Returns a pointer to the destination.
+ */
+struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest)
+{
+       dest->ss_family = source->ss_family;
+
+       /* copy new addr and apply it */
+       switch (source->ss_family) {
+               case AF_INET:
+                       ((struct sockaddr_in *)dest)->sin_addr.s_addr = ((struct sockaddr_in *)source)->sin_addr.s_addr;
+                       break;
+               case AF_INET6:
+                       memcpy(((struct sockaddr_in6 *)dest)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)source)->sin6_addr.s6_addr, sizeof(struct in6_addr));
+                       break;
+       }
+
+       return dest;
+}
+
 char *human_time(int t, short hz_div) {
        static char rv[sizeof("24855d23h")+1];  // longest of "23h59m" and "59m59s"
        char *p = rv;