summaryrefslogtreecommitdiff
path: root/src/libsystemd/test-resolve.c
blob: 7c1e7aa32cab687227aaab29d050e60aef480acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2005-2008 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <assert.h>
#include <signal.h>
#include <errno.h>

#include "sd-resolve.h"
#include "resolve-util.h"
#include "macro.h"

int main(int argc, char *argv[]) {
        int r = 1, ret;
        _cleanup_resolve_free_ sd_resolve *resolve = NULL;
        _cleanup_resolve_addrinfo_free_ struct addrinfo *ai = NULL;
        _cleanup_resolve_answer_free_ unsigned char *srv = NULL;
        sd_resolve_query *q1, *q2, *q3;
        struct addrinfo hints = {};
        struct sockaddr_in sa = {};
        char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";

        signal(SIGCHLD, SIG_IGN);

        resolve = sd_resolve_new(2);
        if (!resolve)
                log_oom();

        /* Make a name -> address query */
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        q1 = sd_resolve_getaddrinfo(resolve, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
        if (!q1)
                fprintf(stderr, "sd_resolve_getaddrinfo(): %s\n", strerror(errno));

        /* Make an address -> name query */
        sa.sin_family = AF_INET;
        sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
        sa.sin_port = htons(80);

        q2 = sd_resolve_getnameinfo(resolve, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
        if (!q2)
                fprintf(stderr, "sd_resolve_getnameinfo(): %s\n", strerror(errno));

        /* Make a res_query() call */
        q3 = sd_resolve_res_query(resolve, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
        if (!q3)
                fprintf(stderr, "sd_resolve_res_query(): %s\n", strerror(errno));

        /* Wait until the three queries are completed */
        while (!sd_resolve_isdone(resolve, q1) ||
               !sd_resolve_isdone(resolve, q2) ||
               !sd_resolve_isdone(resolve, q3)) {
                if (sd_resolve_wait(resolve, 1) < 0)
                        fprintf(stderr, "sd_resolve_wait(): %s\n", strerror(errno));
        }

        /* Interpret the result of the name -> addr query */
        ret = sd_resolve_getaddrinfo_done(resolve, q1, &ai);
        if (ret)
                fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
        else {
                struct addrinfo *i;

                for (i = ai; i; i = i->ai_next) {
                        char t[256];
                        const char *p = NULL;

                        if (i->ai_family == PF_INET)
                                p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
                        else if (i->ai_family == PF_INET6)
                                p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));

                        printf("%s\n", p);
                }
        }

        /* Interpret the result of the addr -> name query */
        ret = sd_resolve_getnameinfo_done(resolve, q2, host, sizeof(host), serv, sizeof(serv));
        if (ret)
                fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
        else
                printf("%s -- %s\n", host, serv);

        /* Interpret the result of the SRV lookup */
        ret = sd_resolve_res_done(resolve, q3, &srv);
        if (ret < 0) {
                fprintf(stderr, "error: %s %i\n", strerror(errno), ret);
        } else if (ret == 0) {
                fprintf(stderr, "No reply for SRV lookup\n");
        } else {
                int qdcount;
                int ancount;
                int len;
                const unsigned char *pos = srv + sizeof(HEADER);
                unsigned char *end = srv + ret;
                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;
                }
        }

        r = 0;

        return r;
}