summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-resolve/test-resolve.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-04-29 15:44:31 +0200
committerLennart Poettering <lennart@poettering.net>2014-04-29 15:45:16 +0200
commit93f1bcf40030a9e98bf2b71c177cbea36c64d5c9 (patch)
tree96f63cc636ffa43f92f805a7f0bac73d48faa18d /src/libsystemd/sd-resolve/test-resolve.c
parent4c06153f7b7d1af4fc8df4602c1d6714734bf9fd (diff)
sd-resolve: rework sd-resolve to be callback based, similar in style to sd-bus and sd-event
Diffstat (limited to 'src/libsystemd/sd-resolve/test-resolve.c')
-rw-r--r--src/libsystemd/sd-resolve/test-resolve.c177
1 files changed, 95 insertions, 82 deletions
diff --git a/src/libsystemd/sd-resolve/test-resolve.c b/src/libsystemd/sd-resolve/test-resolve.c
index 6334dad982..3717286c44 100644
--- a/src/libsystemd/sd-resolve/test-resolve.c
+++ b/src/libsystemd/sd-resolve/test-resolve.c
@@ -37,13 +37,95 @@
#include "resolve-util.h"
#include "macro.h"
+static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
+ const struct addrinfo *i;
+
+ assert(q);
+
+ if (ret != 0) {
+ log_error("getaddrinfo error: %s %i\n", gai_strerror(ret), ret);
+ return 0;
+ }
+
+ for (i = ai; i; i = i->ai_next) {
+ _cleanup_free_ char *addr = NULL;
+
+ assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, &addr) == 0);
+ puts(addr);
+ }
+
+ printf("canonical name: %s\n", strna(ai->ai_canonname));
+
+ return 0;
+}
+
+static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
+ assert(q);
+
+ if (ret != 0) {
+ log_error("getnameinfo error: %s %i\n", gai_strerror(ret), ret);
+ return 0;
+ }
+
+ printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
+ return 0;
+}
+
+static int res_handler(sd_resolve_query *q, int ret, unsigned char *answer, void *userdata) {
+ int qdcount, ancount, len;
+ const unsigned char *pos = answer + sizeof(HEADER);
+ unsigned char *end = answer + ret;
+ HEADER *head = (HEADER *) answer;
+ char name[256];
+ assert(q);
+
+ if (ret < 0) {
+ log_error("res_query() error: %s %i\n", strerror(errno), errno);
+ return 0;
+ }
+
+ if (ret == 0) {
+ log_error("No reply for SRV lookup\n");
+ return 0;
+ }
+
+ qdcount = ntohs(head->qdcount);
+ ancount = ntohs(head->ancount);
+
+ printf("%d answers for srv lookup:\n", ancount);
+
+ /* Ignore the questions */
+ while (qdcount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) {
+ assert(len >= 0);
+ pos += len + QFIXEDSZ;
+ }
+
+ /* Parse the answers */
+ while (ancount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) {
+ /* Ignore the initial string */
+ uint16_t pref, weight, port;
+ assert(len >= 0);
+ pos += len;
+ /* Ignore type, ttl, class and dlen */
+ pos += 10;
+
+ GETSHORT(pref, pos);
+ GETSHORT(weight, pos);
+ GETSHORT(port, pos);
+ len = dn_expand(answer, end, pos, name, 255);
+ printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
+ pref, weight, port, name);
+
+ pos += len;
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[]) {
- int r = 0;
+ _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
_cleanup_resolve_unref_ sd_resolve *resolve = NULL;
- _cleanup_resolve_addrinfo_free_ struct addrinfo *ai = NULL;
- _cleanup_free_ unsigned char *srv = NULL;
- _cleanup_free_ char *host = NULL, *serv = NULL;
- sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
+ int r = 0;
struct addrinfo hints = {
.ai_family = PF_UNSPEC,
@@ -56,28 +138,28 @@ int main(int argc, char *argv[]) {
.sin_port = htons(80)
};
- assert_se(sd_resolve_new(&resolve) >= 0);
+ assert_se(sd_resolve_default(&resolve) >= 0);
/* Make a name -> address query */
- r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
+ r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
if (r < 0)
log_error("sd_resolve_getaddrinfo(): %s\n", strerror(-r));
/* Make an address -> name query */
- sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71"),
- r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, true, true);
+ sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
+ r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
if (r < 0)
log_error("sd_resolve_getnameinfo(): %s\n", strerror(-r));
/* Make a res_query() call */
- r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
+ r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV, res_handler, NULL);
if (r < 0)
log_error("sd_resolve_res_query(): %s\n", strerror(-r));
/* Wait until the three queries are completed */
- while (sd_resolve_is_done(q1) == 0 ||
- sd_resolve_is_done(q2) == 0 ||
- sd_resolve_is_done(q3) == 0) {
+ while (sd_resolve_query_is_done(q1) == 0 ||
+ sd_resolve_query_is_done(q2) == 0 ||
+ sd_resolve_query_is_done(q3) == 0) {
r = sd_resolve_wait(resolve, (uint64_t) -1);
if (r < 0) {
@@ -86,74 +168,5 @@ int main(int argc, char *argv[]) {
}
}
- /* Interpret the result of the name -> addr query */
- r = sd_resolve_getaddrinfo_done(q1, &ai);
- if (r != 0)
- log_error("error: %s %i\n", gai_strerror(r), r);
- else {
- struct addrinfo *i;
-
- for (i = ai; i; i = i->ai_next) {
- _cleanup_free_ char *addr = NULL;
-
- assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, &addr) == 0);
-
- puts(addr);
- }
-
- printf("canonical name: %s\n", strna(ai->ai_canonname));
- }
-
- /* Interpret the result of the addr -> name query */
- r = sd_resolve_getnameinfo_done(q2, &host, &serv);
- if (r)
- log_error("error: %s %i\n", gai_strerror(r), r);
- else
- printf("Host: %s -- Serv: %s\n", host, serv);
-
- /* Interpret the result of the SRV lookup */
- r = sd_resolve_res_done(q3, &srv);
- if (r < 0)
- log_error("error: %s %i\n", strerror(-r), r);
- else if (r == 0)
- log_error("No reply for SRV lookup\n");
- else {
- int qdcount, ancount, len;
- const unsigned char *pos = srv + sizeof(HEADER);
- unsigned char *end = srv + r;
- HEADER *head = (HEADER *)srv;
- char name[256];
-
- qdcount = ntohs(head->qdcount);
- ancount = ntohs(head->ancount);
-
- printf("%d answers for srv lookup:\n", ancount);
-
- /* Ignore the questions */
- while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- assert(len >= 0);
- pos += len + QFIXEDSZ;
- }
-
- /* Parse the answers */
- while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- /* Ignore the initial string */
- uint16_t pref, weight, port;
- assert(len >= 0);
- pos += len;
- /* Ignore type, ttl, class and dlen */
- pos += 10;
-
- GETSHORT(pref, pos);
- GETSHORT(weight, pos);
- GETSHORT(port, pos);
- len = dn_expand(srv, end, pos, name, 255);
- printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
- pref, weight, port, name);
-
- pos += len;
- }
- }
-
return 0;
}