]>
Commit | Line | Data |
---|---|---|
edc6f2f5 TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
e963e3ad | 3 | /*** |
edc6f2f5 | 4 | This file is part of systemd. |
e963e3ad DB |
5 | |
6 | Copyright 2005-2008 Lennart Poettering | |
77c98a9e | 7 | Copyright 2014 Daniel Buch |
e963e3ad | 8 | |
edc6f2f5 TG |
9 | systemd is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
e963e3ad | 13 | |
edc6f2f5 | 14 | systemd is distributed in the hope that it will be useful, but |
e963e3ad DB |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
edc6f2f5 TG |
19 | You should have received a copy of the GNU Lesser General Public License |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
e963e3ad | 22 | |
e963e3ad DB |
23 | #include <string.h> |
24 | #include <sys/types.h> | |
25 | #include <sys/socket.h> | |
26 | #include <arpa/inet.h> | |
27 | #include <stdio.h> | |
28 | #include <netinet/in.h> | |
29 | #include <arpa/nameser.h> | |
30 | #include <resolv.h> | |
31 | #include <assert.h> | |
32 | #include <signal.h> | |
33 | #include <errno.h> | |
34 | ||
77c98a9e | 35 | #include "socket-util.h" |
3bedba4a TG |
36 | #include "sd-resolve.h" |
37 | #include "resolve-util.h" | |
e963e3ad DB |
38 | #include "macro.h" |
39 | ||
93f1bcf4 LP |
40 | static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) { |
41 | const struct addrinfo *i; | |
42 | ||
43 | assert(q); | |
44 | ||
45 | if (ret != 0) { | |
ff49bc32 | 46 | log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret); |
93f1bcf4 LP |
47 | return 0; |
48 | } | |
49 | ||
50 | for (i = ai; i; i = i->ai_next) { | |
51 | _cleanup_free_ char *addr = NULL; | |
52 | ||
53 | assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, &addr) == 0); | |
54 | puts(addr); | |
55 | } | |
56 | ||
57 | printf("canonical name: %s\n", strna(ai->ai_canonname)); | |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
62 | static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) { | |
63 | assert(q); | |
64 | ||
65 | if (ret != 0) { | |
ff49bc32 | 66 | log_error("getnameinfo error: %s %i", gai_strerror(ret), ret); |
93f1bcf4 LP |
67 | return 0; |
68 | } | |
69 | ||
70 | printf("Host: %s -- Serv: %s\n", strna(host), strna(serv)); | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static int res_handler(sd_resolve_query *q, int ret, unsigned char *answer, void *userdata) { | |
75 | int qdcount, ancount, len; | |
76 | const unsigned char *pos = answer + sizeof(HEADER); | |
77 | unsigned char *end = answer + ret; | |
78 | HEADER *head = (HEADER *) answer; | |
79 | char name[256]; | |
80 | assert(q); | |
81 | ||
82 | if (ret < 0) { | |
ff49bc32 | 83 | log_error("res_query() error: %s %i", strerror(errno), errno); |
93f1bcf4 LP |
84 | return 0; |
85 | } | |
86 | ||
87 | if (ret == 0) { | |
ff49bc32 | 88 | log_error("No reply for SRV lookup"); |
93f1bcf4 LP |
89 | return 0; |
90 | } | |
91 | ||
92 | qdcount = ntohs(head->qdcount); | |
93 | ancount = ntohs(head->ancount); | |
94 | ||
95 | printf("%d answers for srv lookup:\n", ancount); | |
96 | ||
97 | /* Ignore the questions */ | |
98 | while (qdcount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) { | |
99 | assert(len >= 0); | |
100 | pos += len + QFIXEDSZ; | |
101 | } | |
102 | ||
103 | /* Parse the answers */ | |
104 | while (ancount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) { | |
105 | /* Ignore the initial string */ | |
106 | uint16_t pref, weight, port; | |
107 | assert(len >= 0); | |
108 | pos += len; | |
109 | /* Ignore type, ttl, class and dlen */ | |
110 | pos += 10; | |
111 | ||
112 | GETSHORT(pref, pos); | |
113 | GETSHORT(weight, pos); | |
114 | GETSHORT(port, pos); | |
115 | len = dn_expand(answer, end, pos, name, 255); | |
116 | printf("\tpreference: %2d weight: %2d port: %d host: %s\n", | |
117 | pref, weight, port, name); | |
118 | ||
119 | pos += len; | |
120 | } | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
e963e3ad | 125 | int main(int argc, char *argv[]) { |
93f1bcf4 | 126 | _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL; |
968d3d24 | 127 | _cleanup_resolve_unref_ sd_resolve *resolve = NULL; |
93f1bcf4 | 128 | int r = 0; |
77c98a9e DB |
129 | |
130 | struct addrinfo hints = { | |
131 | .ai_family = PF_UNSPEC, | |
132 | .ai_socktype = SOCK_STREAM, | |
133 | .ai_flags = AI_CANONNAME | |
134 | }; | |
135 | ||
136 | struct sockaddr_in sa = { | |
137 | .sin_family = AF_INET, | |
138 | .sin_port = htons(80) | |
139 | }; | |
e963e3ad | 140 | |
93f1bcf4 | 141 | assert_se(sd_resolve_default(&resolve) >= 0); |
e963e3ad | 142 | |
4a134c49 LP |
143 | /* Test a floating resolver query */ |
144 | sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL); | |
145 | ||
e963e3ad | 146 | /* Make a name -> address query */ |
93f1bcf4 | 147 | r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL); |
968d3d24 | 148 | if (r < 0) |
279d3c9c | 149 | log_error_errno(r, "sd_resolve_getaddrinfo(): %m"); |
e963e3ad DB |
150 | |
151 | /* Make an address -> name query */ | |
93f1bcf4 LP |
152 | sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71"); |
153 | r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL); | |
968d3d24 | 154 | if (r < 0) |
279d3c9c | 155 | log_error_errno(r, "sd_resolve_getnameinfo(): %m"); |
e963e3ad DB |
156 | |
157 | /* Make a res_query() call */ | |
93f1bcf4 | 158 | r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV, res_handler, NULL); |
968d3d24 | 159 | if (r < 0) |
279d3c9c | 160 | log_error_errno(r, "sd_resolve_res_query(): %m"); |
e963e3ad DB |
161 | |
162 | /* Wait until the three queries are completed */ | |
93f1bcf4 LP |
163 | while (sd_resolve_query_is_done(q1) == 0 || |
164 | sd_resolve_query_is_done(q2) == 0 || | |
165 | sd_resolve_query_is_done(q3) == 0) { | |
968d3d24 LP |
166 | |
167 | r = sd_resolve_wait(resolve, (uint64_t) -1); | |
168 | if (r < 0) { | |
279d3c9c | 169 | log_error_errno(r, "sd_resolve_wait(): %m"); |
968d3d24 LP |
170 | assert_not_reached("sd_resolve_wait() failed"); |
171 | } | |
e963e3ad DB |
172 | } |
173 | ||
77c98a9e | 174 | return 0; |
e963e3ad | 175 | } |