diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-12-10 15:45:48 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-12-17 10:02:36 +0900 |
commit | ceb26cdbc212bc189491e36f6af7a6fce1fe0e2e (patch) | |
tree | f41f3a58ff43cc66fd7504f8639165f1dd99ff4d | |
parent | bd1c8360a47451341d261b877629693e37233c4c (diff) | |
download | systemd-ceb26cdbc212bc189491e36f6af7a6fce1fe0e2e.tar.gz |
sd-resolve: add sd_resolve_get{addr,info}_with_destroy_callback() and typesafe macros
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/libsystemd/meson.build | 1 | ||||
-rw-r--r-- | src/libsystemd/sd-resolve/resolve-private.h | 39 | ||||
-rw-r--r-- | src/libsystemd/sd-resolve/sd-resolve.c | 58 |
4 files changed, 86 insertions, 13 deletions
diff --git a/meson.build b/meson.build index d8e35e618d..b9b26cec24 100644 --- a/meson.build +++ b/meson.build @@ -1437,6 +1437,7 @@ includes = include_directories('src/basic', 'src/libsystemd/sd-id128', 'src/libsystemd/sd-netlink', 'src/libsystemd/sd-network', + 'src/libsystemd/sd-resolve', 'src/libsystemd-network', '.') diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index f441bb6972..2576fe7322 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -88,6 +88,7 @@ libsystemd_sources = files(''' sd-network/network-util.h sd-network/sd-network.c sd-path/sd-path.c + sd-resolve/resolve-private.h sd-resolve/sd-resolve.c sd-utf8/sd-utf8.c '''.split()) + id128_sources + sd_daemon_c + sd_event_sources + sd_login_c diff --git a/src/libsystemd/sd-resolve/resolve-private.h b/src/libsystemd/sd-resolve/resolve-private.h new file mode 100644 index 0000000000..a0feb36f7c --- /dev/null +++ b/src/libsystemd/sd-resolve/resolve-private.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include "sd-resolve.h" + +int resolve_getaddrinfo_with_destroy_callback( + sd_resolve *resolve, sd_resolve_query **q, + const char *node, const char *service, const struct addrinfo *hints, + sd_resolve_getaddrinfo_handler_t callback, + sd_resolve_destroy_t destroy_callback, void *userdata); +int resolve_getnameinfo_with_destroy_callback( + sd_resolve *resolve, sd_resolve_query **q, + const struct sockaddr *sa, socklen_t salen, int flags, uint64_t get, + sd_resolve_getnameinfo_handler_t callback, + sd_resolve_destroy_t destroy_callback, void *userdata); + +#define resolve_getaddrinfo(resolve, ret_query, node, service, hints, callback, destroy_callback, userdata) \ + ({ \ + int (*_callback_)(sd_resolve_query*, int, const struct addrinfo*, typeof(userdata)) = callback; \ + void (*_destroy_)(typeof(userdata)) = destroy_callback; \ + resolve_getaddrinfo_with_destroy_callback( \ + resolve, ret_query, \ + node, service, hints, \ + (sd_resolve_getaddrinfo_handler_t) _callback_, \ + (sd_resolve_destroy_t) _destroy_, \ + userdata); \ + }) + +#define resolve_getnameinfo(resolve, ret_query, sa, salen, flags, get, callback, destroy_callback, userdata) \ + ({ \ + int (*_callback_)(sd_resolve_query*, int, const char*, const char*, typeof(userdata)) = callback; \ + void (*_destroy_)(typeof(userdata)) = destroy_callback; \ + resolve_getaddrinfo_with_destroy_callback( \ + resolve, ret_query, \ + sa, salen, flags, get, \ + (sd_resolve_getnameinfo_handler_t) _callback_, \ + (sd_resolve_destroy_t) _destroy_, \ + userdata); \ + }) diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c index e31b808c66..47986a4a63 100644 --- a/src/libsystemd/sd-resolve/sd-resolve.c +++ b/src/libsystemd/sd-resolve/sd-resolve.c @@ -20,6 +20,7 @@ #include "io-util.h" #include "list.h" #include "missing.h" +#include "resolve-private.h" #include "socket-util.h" #include "util.h" #include "process-util.h" @@ -912,26 +913,29 @@ static int alloc_query(sd_resolve *resolve, bool floating, sd_resolve_query **_q return 0; } -_public_ int sd_resolve_getaddrinfo( + +int resolve_getaddrinfo_with_destroy_callback( sd_resolve *resolve, - sd_resolve_query **_q, + sd_resolve_query **ret_query, const char *node, const char *service, const struct addrinfo *hints, - sd_resolve_getaddrinfo_handler_t callback, void *userdata) { + sd_resolve_getaddrinfo_handler_t callback, + sd_resolve_destroy_t destroy_callback, + void *userdata) { _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q = NULL; + size_t node_len, service_len; AddrInfoRequest req = {}; struct iovec iov[3]; struct msghdr mh = {}; int r; - size_t node_len, service_len; assert_return(resolve, -EINVAL); assert_return(node || service, -EINVAL); assert_return(callback, -EINVAL); assert_return(!resolve_pid_changed(resolve), -ECHILD); - r = alloc_query(resolve, !_q, &q); + r = alloc_query(resolve, !ret_query, &q); if (r < 0) return r; @@ -968,14 +972,27 @@ _public_ int sd_resolve_getaddrinfo( return -errno; resolve->n_outstanding++; + q->destroy_callback = destroy_callback; + + if (ret_query) + *ret_query = q; - if (_q) - *_q = q; TAKE_PTR(q); return 0; } +_public_ int sd_resolve_getaddrinfo( + sd_resolve *resolve, + sd_resolve_query **ret_query, + const char *node, const char *service, + const struct addrinfo *hints, + sd_resolve_getaddrinfo_handler_t callback, + void *userdata) { + + return resolve_getaddrinfo_with_destroy_callback(resolve, ret_query, node, service, hints, callback, NULL, userdata); +} + static int getaddrinfo_done(sd_resolve_query* q) { assert(q); assert(q->done); @@ -987,13 +1004,14 @@ static int getaddrinfo_done(sd_resolve_query* q) { return q->getaddrinfo_handler(q, q->ret, q->addrinfo, q->userdata); } -_public_ int sd_resolve_getnameinfo( +int resolve_getnameinfo_with_destroy_callback( sd_resolve *resolve, - sd_resolve_query**_q, + sd_resolve_query **ret_query, const struct sockaddr *sa, socklen_t salen, int flags, uint64_t get, sd_resolve_getnameinfo_handler_t callback, + sd_resolve_destroy_t destroy_callback, void *userdata) { _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q = NULL; @@ -1010,7 +1028,7 @@ _public_ int sd_resolve_getnameinfo( assert_return(callback, -EINVAL); assert_return(!resolve_pid_changed(resolve), -ECHILD); - r = alloc_query(resolve, !_q, &q); + r = alloc_query(resolve, !ret_query, &q); if (r < 0) return r; @@ -1040,15 +1058,29 @@ _public_ int sd_resolve_getnameinfo( if (sendmsg(resolve->fds[REQUEST_SEND_FD], &mh, MSG_NOSIGNAL) < 0) return -errno; - if (_q) - *_q = q; - resolve->n_outstanding++; + q->destroy_callback = destroy_callback; + + if (ret_query) + *ret_query = q; + TAKE_PTR(q); return 0; } +_public_ int sd_resolve_getnameinfo( + sd_resolve *resolve, + sd_resolve_query **ret_query, + const struct sockaddr *sa, socklen_t salen, + int flags, + uint64_t get, + sd_resolve_getnameinfo_handler_t callback, + void *userdata) { + + return resolve_getnameinfo_with_destroy_callback(resolve, ret_query, sa, salen, flags, get, callback, NULL, userdata); +} + static int getnameinfo_done(sd_resolve_query *q) { assert(q); |