1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

sd-resolve: add sd_resolve_get{addr,info}_with_destroy_callback() and typesafe macros

This commit is contained in:
Yu Watanabe 2018-12-10 15:45:48 +09:00
parent bd1c8360a4
commit ceb26cdbc2
4 changed files with 86 additions and 13 deletions

View File

@ -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',
'.')

View File

@ -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

View File

@ -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); \
})

View File

@ -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);