summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2018-08-27 19:10:18 +0100
committerRoy Marples <roy@marples.name>2018-08-27 19:10:18 +0100
commitd244fb35fefbb2dbad5150a81ae2a54cb20b014d (patch)
treee4c5f7c6cf78f188b814a34038b2042d7a66adbd
parent857f58377d95818286c92fe09730a9470df253fa (diff)
downloaddhcpcd-ia.tar.xz
Add sa_cpy. While here allow NULL in sa_cmp.ia
-rw-r--r--src/sa.c36
-rw-r--r--src/sa.h1
2 files changed, 35 insertions, 2 deletions
diff --git a/src/sa.c b/src/sa.c
index dc9c2415..9ca0fa1f 100644
--- a/src/sa.c
+++ b/src/sa.c
@@ -371,8 +371,12 @@ sa_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
socklen_t offset, len;
- assert(sa1 != NULL);
- assert(sa2 != NULL);
+ if (sa == NULL && sa == NULL)
+ return 0;
+ if (sa1 == NULL && sa2 != NULL)
+ return -1;
+ if (sa1 != NULL && sa2 == NULL)
+ return 1;
/* Treat AF_UNSPEC as the unspecified address. */
if ((sa1->sa_family == AF_UNSPEC || sa2->sa_family == AF_UNSPEC) &&
@@ -456,3 +460,31 @@ sa_in6_init(struct sockaddr *sa, const struct in6_addr *addr)
sizeof(sin6->sin6_addr.s6_addr));
}
#endif
+
+void
+sa_cpy(struct sockaddr *src, const struct sockaddr *dst)
+{
+#ifdef HAVE_SA_LEN
+ if (src->sa_len == 0)
+ memset(src, 0, sizeof(*src));
+ else
+ memcpy(src, dst, src->sa_len);
+#else
+ size_t sa_len;
+
+ switch (dst->sa_family) {
+ case AF_INET:
+ sa_len = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ sa_len = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ sa_len = 0;
+ }
+ if (sa_len == 0)
+ memset(src, 0, sizeof(*src));
+ else
+ memcpy(src, dst, sa_len);
+#endif
+}
diff --git a/src/sa.h b/src/sa.h
index 26c741cc..bd4cf0d6 100644
--- a/src/sa.h
+++ b/src/sa.h
@@ -64,5 +64,6 @@ const char *sa_addrtop(const struct sockaddr *, char *, socklen_t);
int sa_cmp(const struct sockaddr *, const struct sockaddr *);
void sa_in_init(struct sockaddr *, const struct in_addr *);
void sa_in6_init(struct sockaddr *, const struct in6_addr *);
+void sa_cpy(struct sockaddr *, const struct sockaddr *);
#endif