+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;
+}
+