summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLukáš Ondráček <lukas.ondracek@nic.cz>2024-04-03 20:53:52 +0200
committerLukáš Ondráček <lukas.ondracek@nic.cz>2024-04-03 21:07:34 +0200
commit6bb31c0c4499359973a904ccf591154235d4a7e8 (patch)
tree4a05f0a12a5e3489ea5388bddb80588a693c8d90 /lib
parentfixup! rrl: allow changing configuration on reload (diff)
downloadknot-resolver-6bb31c0c4499359973a904ccf591154235d4a7e8.tar.xz
knot-resolver-6bb31c0c4499359973a904ccf591154235d4a7e8.zip
rrl: porting unit tests from Knot DNS
Diffstat (limited to 'lib')
-rw-r--r--lib/utils.c34
-rw-r--r--lib/utils.h2
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/utils.c b/lib/utils.c
index 8b7e1270..6d215760 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -599,6 +599,40 @@ struct sockaddr * kr_straddr_socket(const char *addr, int port, knot_mm_t *pool)
}
}
+struct sockaddr * kr_straddr_socket_set(struct sockaddr *sa, const char *addr, int port)
+{
+ switch (kr_straddr_family(addr)) {
+ case AF_INET: {
+ struct sockaddr_in *res = (struct sockaddr_in *) sa;
+ if (uv_ip4_addr(addr, port, res) >= 0) {
+ return sa;
+ } else {
+ return NULL;
+ }
+ }
+ case AF_INET6: {
+ struct sockaddr_in6 *res = (struct sockaddr_in6 *) sa;
+ if (uv_ip6_addr(addr, port, res) >= 0) {
+ return sa;
+ } else {
+ return NULL;
+ }
+ }
+ case AF_UNIX: {
+ struct sockaddr_un *res = (struct sockaddr_un *) sa;
+ const size_t alen = strlen(addr) + 1;
+ if (alen > sizeof(res->sun_path)) {
+ return NULL;
+ }
+ res->sun_family = AF_UNIX;
+ memcpy(res->sun_path, addr, alen);
+ return sa;
+ }
+ default:
+ return NULL;
+ }
+}
+
int kr_straddr_subnet(void *dst, const char *addr)
{
if (!dst || !addr) {
diff --git a/lib/utils.h b/lib/utils.h
index fab13fe8..6fbdeed2 100644
--- a/lib/utils.h
+++ b/lib/utils.h
@@ -351,6 +351,8 @@ int kr_family_len(int family);
* Also accepts IPv6 link-local and AF_UNIX starting with "/" (ignoring port) */
KR_EXPORT
struct sockaddr * kr_straddr_socket(const char *addr, int port, knot_mm_t *pool);
+KR_EXPORT
+struct sockaddr * kr_straddr_socket_set(struct sockaddr *sa, const char *addr, int port);
/** Parse address and return subnet length (bits).
* @warning 'dst' must be at least `sizeof(struct in6_addr)` long. */