|
8487
|
1 /**
|
|
|
2 * @file mdns.c Multicast DNS connection code used by rendezvous.
|
|
|
3 *
|
|
|
4 * gaim
|
|
|
5 *
|
|
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
|
8 * source distribution.
|
|
|
9 *
|
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
|
13 * (at your option) any later version.
|
|
|
14 *
|
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 * GNU General Public License for more details.
|
|
|
19 *
|
|
|
20 * You should have received a copy of the GNU General Public License
|
|
|
21 * along with this program; if not, write to the Free Software
|
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
23 *
|
|
|
24 */
|
|
|
25
|
|
|
26 /*
|
|
|
27 * If you want to understand this, read RFC1035 and
|
|
8612
|
28 * draft-cheshire-dnsext-multicastdns.txt, and buy
|
|
|
29 * me a doughnut. thx k bye.
|
|
8487
|
30 */
|
|
|
31
|
|
|
32 /*
|
|
|
33 * XXX - THIS DOESN'T DO BOUNDS CHECKING!!! DON'T USE IT ON AN UNTRUSTED
|
|
|
34 * NETWORK UNTIL IT DOES!!! THERE ARE POSSIBLE REMOTE ACCESS VIA BUFFER
|
|
|
35 * OVERFLOW SECURITY HOLES!!!
|
|
|
36 */
|
|
|
37
|
|
8546
|
38 #include "internal.h"
|
|
8487
|
39 #include "debug.h"
|
|
|
40
|
|
|
41 #include "mdns.h"
|
|
|
42 #include "util.h"
|
|
|
43
|
|
8612
|
44 /******************************************/
|
|
|
45 /* Functions for connection establishment */
|
|
|
46 /******************************************/
|
|
|
47
|
|
8487
|
48 int
|
|
|
49 mdns_establish_socket()
|
|
|
50 {
|
|
|
51 int fd = -1;
|
|
|
52 struct sockaddr_in addr;
|
|
|
53 struct ip_mreq mreq;
|
|
|
54 unsigned char loop;
|
|
|
55 unsigned char ttl;
|
|
|
56 int reuseaddr;
|
|
|
57
|
|
|
58 gaim_debug_info("mdns", "Establishing multicast socket\n");
|
|
|
59
|
|
|
60 /* What's the difference between AF_INET and PF_INET? */
|
|
|
61 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
62 gaim_debug_error("mdns", "Unable to create socket: %s\n", strerror(errno));
|
|
|
63 return -1;
|
|
|
64 }
|
|
|
65
|
|
|
66 /* Make the socket non-blocking (although it shouldn't matter) */
|
|
|
67 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
68
|
|
|
69 /* Bind the socket to a local IP and port */
|
|
|
70 addr.sin_family = AF_INET;
|
|
|
71 addr.sin_port = htons(5353);
|
|
|
72 addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
73 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
|
|
|
74 gaim_debug_error("mdns", "Unable to bind socket to interface.\n");
|
|
|
75 close(fd);
|
|
|
76 return -1;
|
|
|
77 }
|
|
|
78
|
|
8631
|
79 /* Ensure loopback is enabled (it should be enabled by default, but let's be sure) */
|
|
8487
|
80 loop = 1;
|
|
|
81 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(unsigned char)) == -1) {
|
|
|
82 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_LOOP\n");
|
|
|
83 }
|
|
|
84
|
|
|
85 /* Set TTL to 255--required by mDNS */
|
|
|
86 ttl = 255;
|
|
|
87 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(unsigned char)) == -1) {
|
|
|
88 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_TTL\n");
|
|
|
89 close(fd);
|
|
|
90 return -1;
|
|
|
91 }
|
|
|
92
|
|
|
93 /* Join the .local multicast group */
|
|
|
94 mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.251");
|
|
|
95 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
96 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq)) == -1) {
|
|
|
97 gaim_debug_error("mdns", "Error calling setsockopt for IP_ADD_MEMBERSHIP\n");
|
|
|
98 close(fd);
|
|
|
99 return -1;
|
|
|
100 }
|
|
|
101
|
|
|
102 /* Make the local IP re-usable */
|
|
|
103 reuseaddr = 1;
|
|
|
104 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
|
|
|
105 gaim_debug_error("mdns", "Error calling setsockopt for SO_REUSEADDR: %s\n", strerror(errno));
|
|
|
106 }
|
|
|
107
|
|
|
108 return fd;
|
|
|
109 }
|
|
|
110
|
|
8612
|
111 static int
|
|
|
112 mdns_send_raw(int fd, unsigned int datalen, unsigned char *data)
|
|
|
113 {
|
|
|
114 struct sockaddr_in addr;
|
|
|
115 int n;
|
|
|
116
|
|
|
117 addr.sin_family = AF_INET;
|
|
|
118 addr.sin_port = htons(5353);
|
|
|
119 addr.sin_addr.s_addr = inet_addr("224.0.0.251");
|
|
|
120 n = sendto(fd, data, datalen, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
|
|
121
|
|
|
122 if (n == -1) {
|
|
|
123 gaim_debug_error("mdns", "Error sending packet: %d\n", errno);
|
|
|
124 return -1;
|
|
|
125 } else if (n != datalen) {
|
|
|
126 gaim_debug_error("mdns", "Only sent %d of %d bytes of data.\n", n, datalen);
|
|
|
127 return -1;
|
|
|
128 }
|
|
|
129
|
|
|
130 return 0;
|
|
|
131 }
|
|
|
132
|
|
|
133 /***************************************/
|
|
|
134 /* Functions for sending mDNS messages */
|
|
|
135 /***************************************/
|
|
|
136
|
|
|
137 static int
|
|
8631
|
138 mdns_getlength_name(const void *name)
|
|
8629
|
139 {
|
|
8631
|
140 return strlen((const char *)name) + 2;
|
|
|
141 }
|
|
|
142
|
|
|
143 static int
|
|
|
144 mdns_getlength_RR_rdata(unsigned short type, const void *rdata)
|
|
|
145 {
|
|
8629
|
146 int rdlength = 0;
|
|
|
147
|
|
8631
|
148 switch (type) {
|
|
|
149 case RENDEZVOUS_RRTYPE_PTR:
|
|
|
150 rdlength = mdns_getlength_name(rdata);
|
|
|
151 break;
|
|
|
152
|
|
|
153 case RENDEZVOUS_RRTYPE_TXT: {
|
|
|
154 GSList *cur;
|
|
|
155 ResourceRecordRDataTXTNode *node;
|
|
|
156
|
|
|
157 for (cur = (GSList *)rdata; cur != NULL; cur = cur->next) {
|
|
|
158 node = (ResourceRecordRDataTXTNode *)cur->data;
|
|
|
159 rdlength += 1 + strlen(node->name);
|
|
|
160 if (node->value != NULL)
|
|
|
161 rdlength += 1 + strlen(node->value);
|
|
|
162 }
|
|
|
163 } break;
|
|
|
164
|
|
|
165 case RENDEZVOUS_RRTYPE_SRV:
|
|
|
166 rdlength = 6 + mdns_getlength_name(((const ResourceRecordRDataSRV *)rdata)->target);
|
|
|
167 break;
|
|
8629
|
168 }
|
|
|
169
|
|
|
170 return rdlength;
|
|
|
171 }
|
|
|
172
|
|
|
173 static int
|
|
8631
|
174 mdns_getlength_RR(ResourceRecord *rr)
|
|
8612
|
175 {
|
|
8631
|
176 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
|
|
|
177
|
|
8634
|
178 return mdns_getlength_name(rr->name) + 10 + rr->rdlength;
|
|
8612
|
179 }
|
|
|
180
|
|
|
181 static int
|
|
8634
|
182 mdns_put_name(char *data, unsigned int datalen, unsigned int offset, const char *name)
|
|
8612
|
183 {
|
|
|
184 int i = 0;
|
|
|
185 char *b, *c;
|
|
|
186
|
|
|
187 b = (char *)name;
|
|
|
188 while ((c = strchr(b, '.'))) {
|
|
|
189 i += util_put8(&data[offset + i], c - b); /* Length of domain-name segment */
|
|
|
190 memcpy(&data[offset + i], b, c - b); /* Domain-name segment */
|
|
|
191 i += c - b; /* Increment the destination pointer */
|
|
|
192 b = c + 1;
|
|
|
193 }
|
|
|
194 i += util_put8(&data[offset + i], strlen(b)); /* Length of domain-name segment */
|
|
|
195 strcpy(&data[offset + i], b); /* Domain-name segment */
|
|
|
196 i += strlen(b) + 1; /* Increment the destination pointer */
|
|
|
197
|
|
|
198 return i;
|
|
|
199 }
|
|
|
200
|
|
|
201 static int
|
|
8634
|
202 mdns_put_RR(char *data, unsigned int datalen, unsigned int offset, const ResourceRecord *rr)
|
|
8612
|
203 {
|
|
|
204 int i = 0;
|
|
|
205
|
|
|
206 i += mdns_put_name(data, datalen, offset + i, rr->name);
|
|
|
207 i += util_put16(&data[offset + i], rr->type);
|
|
|
208 i += util_put16(&data[offset + i], rr->class);
|
|
|
209 i += util_put32(&data[offset + i], rr->ttl);
|
|
8631
|
210 i += util_put16(&data[offset + i], rr->rdlength);
|
|
8612
|
211
|
|
|
212 switch (rr->type) {
|
|
|
213 case RENDEZVOUS_RRTYPE_PTR:
|
|
|
214 i += mdns_put_name(data, datalen, offset + i, (const char *)rr->rdata);
|
|
|
215 break;
|
|
8629
|
216
|
|
|
217 case RENDEZVOUS_RRTYPE_TXT: {
|
|
|
218 GSList *cur;
|
|
8631
|
219 ResourceRecordRDataTXTNode *node;
|
|
8629
|
220 int mylength;
|
|
|
221
|
|
|
222 for (cur = (GSList *)rr->rdata; cur != NULL; cur = cur->next) {
|
|
8631
|
223 node = (ResourceRecordRDataTXTNode *)cur->data;
|
|
8629
|
224 mylength = 1 + strlen(node->name);
|
|
|
225 if (node->value)
|
|
|
226 mylength += 1 + strlen(node->value);
|
|
|
227 i += util_put8(&data[offset + i], mylength - 1);
|
|
|
228 memcpy(&data[offset + i], node->name, strlen(node->name));
|
|
|
229 i += strlen(node->name);
|
|
|
230 if (node->value) {
|
|
|
231 data[offset + i] = '=';
|
|
|
232 i++;
|
|
|
233 memcpy(&data[offset + i], node->value, strlen(node->value));
|
|
|
234 i += strlen(node->value);
|
|
|
235 }
|
|
|
236 }
|
|
|
237 } break;
|
|
8631
|
238
|
|
|
239 case RENDEZVOUS_RRTYPE_SRV: {
|
|
|
240 ResourceRecordRDataSRV *srv = rr->rdata;
|
|
|
241 i += util_put16(&data[offset + i], 0);
|
|
|
242 i += util_put16(&data[offset + i], 0);
|
|
|
243 i += util_put16(&data[offset + i], srv->port);
|
|
|
244 i += mdns_put_name(data, datalen, offset + i, srv->target);
|
|
|
245 } break;
|
|
8612
|
246 }
|
|
|
247
|
|
|
248 return i;
|
|
|
249 }
|
|
|
250
|
|
|
251 int
|
|
|
252 mdns_send_dns(int fd, const DNSPacket *dns)
|
|
|
253 {
|
|
|
254 int ret;
|
|
|
255 unsigned int datalen;
|
|
|
256 unsigned char *data;
|
|
8634
|
257 unsigned int offset;
|
|
8612
|
258 int i;
|
|
|
259
|
|
|
260 /* Calculate the length of the buffer we'll need to hold the DNS packet */
|
|
|
261 datalen = 0;
|
|
|
262
|
|
|
263 /* Header */
|
|
|
264 datalen += 12;
|
|
|
265
|
|
|
266 /* Questions */
|
|
|
267 for (i = 0; i < dns->header.numquestions; i++)
|
|
8631
|
268 datalen += mdns_getlength_name(dns->questions[i].name) + 4;
|
|
8612
|
269
|
|
|
270 /* Resource records */
|
|
|
271 for (i = 0; i < dns->header.numanswers; i++)
|
|
|
272 datalen += mdns_getlength_RR(&dns->answers[i]);
|
|
|
273 for (i = 0; i < dns->header.numauthority; i++)
|
|
|
274 datalen += mdns_getlength_RR(&dns->authority[i]);
|
|
|
275 for (i = 0; i < dns->header.numadditional; i++)
|
|
|
276 datalen += mdns_getlength_RR(&dns->additional[i]);
|
|
|
277
|
|
|
278 /* Allocate a buffer */
|
|
|
279 if (!(data = (unsigned char *)g_malloc(datalen))) {
|
|
|
280 return -ENOMEM;
|
|
|
281 }
|
|
|
282
|
|
|
283 /* Construct the datagram */
|
|
|
284 /* Header */
|
|
|
285 offset = 0;
|
|
|
286 offset += util_put16(&data[offset], dns->header.id); /* ID */
|
|
|
287 offset += util_put16(&data[offset], dns->header.flags);
|
|
|
288 offset += util_put16(&data[offset], dns->header.numquestions); /* QDCOUNT */
|
|
|
289 offset += util_put16(&data[offset], dns->header.numanswers); /* ANCOUNT */
|
|
|
290 offset += util_put16(&data[offset], dns->header.numauthority); /* NSCOUNT */
|
|
|
291 offset += util_put16(&data[offset], dns->header.numadditional); /* ARCOUNT */
|
|
|
292
|
|
|
293 /* Questions */
|
|
|
294 for (i = 0; i < dns->header.numquestions; i++) {
|
|
|
295 offset += mdns_put_name(data, datalen, offset, dns->questions[i].name); /* QNAME */
|
|
|
296 offset += util_put16(&data[offset], dns->questions[i].type); /* QTYPE */
|
|
|
297 offset += util_put16(&data[offset], dns->questions[i].class); /* QCLASS */
|
|
|
298 }
|
|
|
299
|
|
|
300 /* Resource records */
|
|
|
301 for (i = 0; i < dns->header.numanswers; i++)
|
|
|
302 offset += mdns_put_RR(data, datalen, offset, &dns->answers[i]);
|
|
|
303 for (i = 0; i < dns->header.numauthority; i++)
|
|
|
304 offset += mdns_put_RR(data, datalen, offset, &dns->authority[i]);
|
|
|
305 for (i = 0; i < dns->header.numadditional; i++)
|
|
|
306 offset += mdns_put_RR(data, datalen, offset, &dns->additional[i]);
|
|
|
307
|
|
|
308 /* Send the datagram */
|
|
8634
|
309 /* Offset can be shorter than datalen because of name compression */
|
|
|
310 ret = mdns_send_raw(fd, offset, data);
|
|
8612
|
311
|
|
|
312 g_free(data);
|
|
|
313
|
|
|
314 return ret;
|
|
|
315 }
|
|
|
316
|
|
8487
|
317 int
|
|
|
318 mdns_query(int fd, const char *domain)
|
|
|
319 {
|
|
8612
|
320 int ret;
|
|
|
321 DNSPacket *dns;
|
|
8487
|
322
|
|
|
323 if (strlen(domain) > 255) {
|
|
|
324 return -EINVAL;
|
|
|
325 }
|
|
|
326
|
|
8612
|
327 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
|
328 dns->header.id = 0x0000;
|
|
|
329 dns->header.flags = 0x0000;
|
|
|
330 dns->header.numquestions = 0x0001;
|
|
|
331 dns->header.numanswers = 0x0000;
|
|
|
332 dns->header.numauthority = 0x0000;
|
|
|
333 dns->header.numadditional = 0x0000;
|
|
|
334
|
|
|
335 dns->questions = (Question *)g_malloc(1 * sizeof(Question));
|
|
|
336 dns->questions[0].name = g_strdup(domain);
|
|
|
337 dns->questions[0].type = RENDEZVOUS_RRTYPE_PTR;
|
|
|
338 dns->questions[0].class = 0x8001;
|
|
|
339
|
|
|
340 dns->answers = NULL;
|
|
|
341 dns->authority = NULL;
|
|
|
342 dns->additional = NULL;
|
|
|
343
|
|
|
344 mdns_send_dns(fd, dns);
|
|
|
345
|
|
|
346 mdns_free(dns);
|
|
|
347
|
|
|
348 return ret;
|
|
|
349 }
|
|
|
350
|
|
|
351 int
|
|
|
352 mdns_advertise_ptr(int fd, const char *name, const char *domain)
|
|
|
353 {
|
|
|
354 int ret;
|
|
|
355 DNSPacket *dns;
|
|
|
356
|
|
|
357 if ((strlen(name) > 255) || (strlen(domain) > 255)) {
|
|
|
358 return -EINVAL;
|
|
8487
|
359 }
|
|
|
360
|
|
8612
|
361 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
|
362 dns->header.id = 0x0000;
|
|
|
363 dns->header.flags = 0x8400;
|
|
|
364 dns->header.numquestions = 0x0000;
|
|
|
365 dns->header.numanswers = 0x0001;
|
|
|
366 dns->header.numauthority = 0x0000;
|
|
|
367 dns->header.numadditional = 0x0000;
|
|
|
368 dns->questions = NULL;
|
|
8487
|
369
|
|
8612
|
370 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
|
|
|
371 dns->answers[0].name = g_strdup(name);
|
|
|
372 dns->answers[0].type = RENDEZVOUS_RRTYPE_PTR;
|
|
8631
|
373 dns->answers[0].class = 0x8001;
|
|
8612
|
374 dns->answers[0].ttl = 0x00001c20;
|
|
8629
|
375 dns->answers[0].rdlength = 0x0000; /* Set automatically */
|
|
8612
|
376 dns->answers[0].rdata = (void *)g_strdup(domain);
|
|
|
377
|
|
|
378 dns->authority = NULL;
|
|
|
379 dns->additional = NULL;
|
|
8487
|
380
|
|
8612
|
381 mdns_send_dns(fd, dns);
|
|
|
382
|
|
|
383 mdns_free(dns);
|
|
8487
|
384
|
|
8612
|
385 return ret;
|
|
|
386 }
|
|
8487
|
387
|
|
8629
|
388 int
|
|
|
389 mdns_advertise_txt(int fd, const char *name, const GSList *rdata)
|
|
|
390 {
|
|
|
391 int ret;
|
|
|
392 DNSPacket *dns;
|
|
|
393
|
|
|
394 if ((strlen(name) > 255)) {
|
|
|
395 return -EINVAL;
|
|
|
396 }
|
|
|
397
|
|
|
398 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
|
399 dns->header.id = 0x0000;
|
|
|
400 dns->header.flags = 0x8400;
|
|
|
401 dns->header.numquestions = 0x0000;
|
|
|
402 dns->header.numanswers = 0x0001;
|
|
|
403 dns->header.numauthority = 0x0000;
|
|
|
404 dns->header.numadditional = 0x0000;
|
|
|
405 dns->questions = NULL;
|
|
|
406
|
|
|
407 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
|
|
|
408 dns->answers[0].name = g_strdup(name);
|
|
|
409 dns->answers[0].type = RENDEZVOUS_RRTYPE_TXT;
|
|
8631
|
410 dns->answers[0].class = 0x8001;
|
|
8629
|
411 dns->answers[0].ttl = 0x00001c20;
|
|
|
412 dns->answers[0].rdlength = 0x0000; /* Set automatically */
|
|
|
413 dns->answers[0].rdata = (void *)rdata;
|
|
|
414
|
|
|
415 dns->authority = NULL;
|
|
|
416 dns->additional = NULL;
|
|
|
417
|
|
|
418 mdns_send_dns(fd, dns);
|
|
|
419
|
|
8631
|
420 /* The rdata should be freed by the caller of this function */
|
|
|
421 dns->answers[0].rdata = NULL;
|
|
|
422
|
|
|
423 mdns_free(dns);
|
|
|
424
|
|
|
425 return ret;
|
|
|
426 }
|
|
|
427
|
|
|
428 int
|
|
|
429 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target)
|
|
|
430 {
|
|
|
431 int ret;
|
|
|
432 DNSPacket *dns;
|
|
|
433 ResourceRecordRDataSRV *rdata;
|
|
|
434
|
|
|
435 if ((strlen(target) > 255)) {
|
|
|
436 return -EINVAL;
|
|
|
437 }
|
|
|
438
|
|
|
439 rdata = g_malloc(sizeof(ResourceRecordRDataSRV));
|
|
|
440 rdata->port = port;
|
|
8634
|
441 rdata->target = g_strdup(target);
|
|
8631
|
442
|
|
|
443 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
|
444 dns->header.id = 0x0000;
|
|
|
445 dns->header.flags = 0x8400;
|
|
|
446 dns->header.numquestions = 0x0000;
|
|
|
447 dns->header.numanswers = 0x0001;
|
|
|
448 dns->header.numauthority = 0x0000;
|
|
|
449 dns->header.numadditional = 0x0000;
|
|
|
450 dns->questions = NULL;
|
|
|
451
|
|
|
452 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
|
|
|
453 dns->answers[0].name = g_strdup(name);
|
|
|
454 dns->answers[0].type = RENDEZVOUS_RRTYPE_SRV;
|
|
|
455 dns->answers[0].class = 0x8001;
|
|
|
456 dns->answers[0].ttl = 0x00001c20;
|
|
|
457 dns->answers[0].rdlength = 0x0000; /* Set automatically */
|
|
|
458 dns->answers[0].rdata = rdata;
|
|
|
459
|
|
|
460 dns->authority = NULL;
|
|
|
461 dns->additional = NULL;
|
|
|
462
|
|
|
463 mdns_send_dns(fd, dns);
|
|
|
464
|
|
8629
|
465 mdns_free(dns);
|
|
|
466
|
|
|
467 return ret;
|
|
|
468 }
|
|
|
469
|
|
8612
|
470 /***************************************/
|
|
|
471 /* Functions for parsing mDNS messages */
|
|
|
472 /***************************************/
|
|
8487
|
473
|
|
|
474 /*
|
|
|
475 * Read in a domain name from the given buffer starting at the given
|
|
|
476 * offset. This handles using domain name compression to jump around
|
|
|
477 * the data buffer, if needed.
|
|
|
478 *
|
|
|
479 * @return A null-terminated string representation of the domain name.
|
|
|
480 * This should be g_free'd when no longer needed.
|
|
|
481 */
|
|
|
482 static gchar *
|
|
8634
|
483 mdns_read_name(const char *data, int datalen, unsigned int offset)
|
|
8487
|
484 {
|
|
|
485 GString *ret = g_string_new("");
|
|
8634
|
486 unsigned char tmp, newoffset;
|
|
8487
|
487
|
|
8634
|
488 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) {
|
|
|
489 offset++;
|
|
8487
|
490
|
|
|
491 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
|
|
8634
|
492 if (offset + tmp > datalen)
|
|
|
493 /* Attempt to read past end of data! Bailing! */
|
|
|
494 return g_string_free(ret, TRUE);
|
|
|
495 if (*ret->str != '\0')
|
|
8487
|
496 g_string_append_c(ret, '.');
|
|
8634
|
497 g_string_append_len(ret, &data[offset], tmp);
|
|
|
498 offset += tmp;
|
|
8487
|
499
|
|
|
500 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
|
|
|
501 /* Reserved for future use */
|
|
|
502
|
|
|
503 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
|
|
|
504 /* Reserved for future use */
|
|
|
505
|
|
|
506 } else { /* First two bits are 11 */
|
|
|
507 /* Jump to another position in the data */
|
|
8634
|
508 newoffset = util_get8(&data[offset]);
|
|
|
509 if (newoffset >= offset)
|
|
|
510 /* Invalid pointer! Could lead to infinite recursion! Bailing! */
|
|
|
511 g_string_free(ret, TRUE);
|
|
|
512 offset = newoffset;
|
|
8487
|
513 }
|
|
|
514 }
|
|
|
515
|
|
|
516 return g_string_free(ret, FALSE);
|
|
|
517 }
|
|
|
518
|
|
|
519 /*
|
|
|
520 * Determine how many bytes long a portion of the domain name is
|
|
|
521 * at the given offset. This does NOT jump around the data array
|
|
|
522 * in the case of domain name compression.
|
|
|
523 *
|
|
|
524 * @return The length of the portion of the domain name.
|
|
|
525 */
|
|
|
526 static int
|
|
8634
|
527 mdns_read_name_len(const char *data, unsigned int datalen, unsigned int offset)
|
|
8487
|
528 {
|
|
8634
|
529 int startoffset = offset;
|
|
8487
|
530 unsigned char tmp;
|
|
|
531
|
|
8634
|
532 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) {
|
|
|
533 offset++;
|
|
8487
|
534
|
|
|
535 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
|
|
8634
|
536 if (offset + tmp > datalen)
|
|
|
537 /* Attempt to read past end of data! Bailing! */
|
|
|
538 return 0;
|
|
|
539 offset += tmp;
|
|
8487
|
540
|
|
|
541 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
|
|
|
542 /* Reserved for future use */
|
|
|
543
|
|
|
544 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
|
|
|
545 /* Reserved for future use */
|
|
|
546
|
|
|
547 } else { /* First two bits are 11 */
|
|
|
548 /* End of this portion of the domain name */
|
|
|
549 break;
|
|
|
550
|
|
|
551 }
|
|
|
552 }
|
|
|
553
|
|
8634
|
554 return offset - startoffset + 1;
|
|
8487
|
555 }
|
|
|
556
|
|
|
557 /*
|
|
|
558 * XXX - Needs bounds checking!
|
|
|
559 *
|
|
|
560 */
|
|
|
561 static Question *
|
|
8634
|
562 mdns_read_questions(int numquestions, const char *data, unsigned int datalen, int *offset)
|
|
8487
|
563 {
|
|
|
564 Question *ret;
|
|
|
565 int i;
|
|
|
566
|
|
|
567 ret = (Question *)g_malloc0(numquestions * sizeof(Question));
|
|
|
568 for (i = 0; i < numquestions; i++) {
|
|
8634
|
569 ret[i].name = mdns_read_name(data, datalen, *offset);
|
|
|
570 *offset += mdns_read_name_len(data, datalen, *offset);
|
|
8487
|
571 ret[i].type = util_get16(&data[*offset]); /* QTYPE */
|
|
|
572 *offset += 2;
|
|
|
573 ret[i].class = util_get16(&data[*offset]); /* QCLASS */
|
|
|
574 *offset += 2;
|
|
|
575 }
|
|
|
576
|
|
|
577 return ret;
|
|
|
578 }
|
|
|
579
|
|
|
580 /*
|
|
|
581 * Read in a chunk of data, probably a buddy icon.
|
|
|
582 *
|
|
|
583 */
|
|
|
584 static unsigned char *
|
|
8634
|
585 mdns_read_rr_rdata_null(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
|
|
8487
|
586 {
|
|
|
587 unsigned char *ret = NULL;
|
|
|
588
|
|
|
589 if (offset + rdlength > datalen)
|
|
|
590 return NULL;
|
|
|
591
|
|
|
592 ret = (unsigned char *)g_malloc(rdlength);
|
|
|
593 memcpy(ret, &data[offset], rdlength);
|
|
|
594
|
|
|
595 return ret;
|
|
|
596 }
|
|
|
597
|
|
|
598 /*
|
|
|
599 * XXX - Needs bounds checking!
|
|
|
600 *
|
|
|
601 */
|
|
|
602 static char *
|
|
8634
|
603 mdns_read_rr_rdata_ptr(const char *data, unsigned int datalen, unsigned int offset)
|
|
8487
|
604 {
|
|
|
605 char *ret = NULL;
|
|
|
606
|
|
|
607 ret = mdns_read_name(data, datalen, offset);
|
|
|
608
|
|
|
609 return ret;
|
|
|
610 }
|
|
|
611
|
|
|
612 /*
|
|
|
613 *
|
|
|
614 *
|
|
|
615 */
|
|
|
616 static GHashTable *
|
|
8634
|
617 mdns_read_rr_rdata_txt(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
|
|
8487
|
618 {
|
|
|
619 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
|
620 int endoffset = offset + rdlength;
|
|
|
621 unsigned char tmp;
|
|
|
622 char buf[256], *key, *value;
|
|
|
623
|
|
|
624 while (offset < endoffset) {
|
|
|
625 /* Read in the length of the next name/value pair */
|
|
|
626 tmp = util_get8(&data[offset]);
|
|
|
627 offset++;
|
|
|
628
|
|
|
629 /* Ensure packet is valid */
|
|
|
630 if (offset + tmp > endoffset)
|
|
|
631 break;
|
|
|
632
|
|
|
633 /* Read in the next name/value pair */
|
|
|
634 strncpy(buf, &data[offset], tmp);
|
|
|
635 offset += tmp;
|
|
|
636
|
|
|
637 if (buf[0] == '=') {
|
|
|
638 /* Name/value pairs beginning with = are silently ignored */
|
|
|
639 continue;
|
|
|
640 }
|
|
|
641
|
|
|
642 /* The value is a substring of buf, starting just after the = */
|
|
|
643 buf[tmp] = '\0';
|
|
|
644 value = strchr(buf, '=');
|
|
|
645 if (value != NULL) {
|
|
|
646 value[0] = '\0';
|
|
|
647 value++;
|
|
|
648 }
|
|
|
649
|
|
|
650 /* Make the key all lowercase */
|
|
|
651 key = g_utf8_strdown(buf, -1);
|
|
|
652 if (!g_hash_table_lookup(ret, key))
|
|
|
653 g_hash_table_insert(ret, key, g_strdup(value));
|
|
|
654 else
|
|
|
655 g_free(key);
|
|
|
656 }
|
|
|
657
|
|
|
658 return ret;
|
|
|
659 }
|
|
|
660
|
|
|
661 /*
|
|
8594
|
662 *
|
|
|
663 *
|
|
|
664 */
|
|
|
665 static ResourceRecordSRV *
|
|
8634
|
666 mdns_read_rr_rdata_srv(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
|
|
8594
|
667 {
|
|
|
668 ResourceRecordSRV *ret = NULL;
|
|
|
669 int endoffset = offset + rdlength;
|
|
|
670
|
|
|
671 if (offset + 7 > endoffset)
|
|
|
672 return NULL;
|
|
|
673
|
|
|
674 ret = g_malloc(sizeof(ResourceRecordSRV));
|
|
|
675
|
|
|
676 /* Read in the priority */
|
|
|
677 ret->priority = util_get16(&data[offset]);
|
|
|
678 offset += 2;
|
|
|
679
|
|
|
680 /* Read in the weight */
|
|
|
681 ret->weight = util_get16(&data[offset]);
|
|
|
682 offset += 2;
|
|
|
683
|
|
|
684 /* Read in the port */
|
|
|
685 ret->port = util_get16(&data[offset]);
|
|
|
686 offset += 2;
|
|
|
687
|
|
|
688 /* Read in the target name */
|
|
|
689 /*
|
|
|
690 * XXX - RFC2782 says it's not supposed to be an alias...
|
|
|
691 * but it was in the packet capture I looked at from iChat.
|
|
|
692 */
|
|
|
693 ret->target = mdns_read_name(data, datalen, offset);
|
|
|
694
|
|
|
695 return ret;
|
|
|
696 }
|
|
|
697
|
|
|
698 /*
|
|
8487
|
699 * XXX - Needs bounds checking!
|
|
|
700 *
|
|
|
701 */
|
|
|
702 static ResourceRecord *
|
|
8634
|
703 mdns_read_rr(int numrecords, const char *data, unsigned int datalen, int *offset)
|
|
8487
|
704 {
|
|
|
705 ResourceRecord *ret;
|
|
|
706 int i;
|
|
|
707
|
|
|
708 ret = (ResourceRecord *)g_malloc0(numrecords * sizeof(ResourceRecord));
|
|
|
709 for (i = 0; i < numrecords; i++) {
|
|
8634
|
710 ret[i].name = mdns_read_name(data, datalen, *offset); /* NAME */
|
|
|
711 *offset += mdns_read_name_len(data, datalen, *offset);
|
|
8487
|
712 ret[i].type = util_get16(&data[*offset]); /* TYPE */
|
|
|
713 *offset += 2;
|
|
|
714 ret[i].class = util_get16(&data[*offset]); /* CLASS */
|
|
|
715 *offset += 2;
|
|
|
716 ret[i].ttl = util_get32(&data[*offset]); /* TTL */
|
|
|
717 *offset += 4;
|
|
|
718 ret[i].rdlength = util_get16(&data[*offset]); /* RDLENGTH */
|
|
|
719 *offset += 2;
|
|
|
720
|
|
|
721 /* RDATA */
|
|
|
722 switch (ret[i].type) {
|
|
|
723 case RENDEZVOUS_RRTYPE_NULL:
|
|
|
724 ret[i].rdata = mdns_read_rr_rdata_null(data, datalen, *offset, ret[i].rdlength);
|
|
|
725 break;
|
|
|
726
|
|
|
727 case RENDEZVOUS_RRTYPE_PTR:
|
|
|
728 ret[i].rdata = mdns_read_rr_rdata_ptr(data, datalen, *offset);
|
|
|
729 break;
|
|
|
730
|
|
|
731 case RENDEZVOUS_RRTYPE_TXT:
|
|
|
732 ret[i].rdata = mdns_read_rr_rdata_txt(data, datalen, *offset, ret[i].rdlength);
|
|
|
733 break;
|
|
|
734
|
|
8594
|
735 case RENDEZVOUS_RRTYPE_SRV:
|
|
|
736 ret[i].rdata = mdns_read_rr_rdata_srv(data, datalen, *offset, ret[i].rdlength);
|
|
|
737 break;
|
|
|
738
|
|
8487
|
739 default:
|
|
|
740 ret[i].rdata = NULL;
|
|
|
741 break;
|
|
|
742 }
|
|
|
743 *offset += ret[i].rdlength;
|
|
|
744 }
|
|
|
745
|
|
|
746 return ret;
|
|
|
747 }
|
|
|
748
|
|
|
749 /*
|
|
|
750 * XXX - Needs bounds checking!
|
|
|
751 *
|
|
|
752 */
|
|
|
753 DNSPacket *
|
|
|
754 mdns_read(int fd)
|
|
|
755 {
|
|
|
756 DNSPacket *ret = NULL;
|
|
|
757 int i; /* Current position in datagram */
|
|
8612
|
758 /* XXX - Find out what to use as a maximum incoming UDP packet size */
|
|
|
759 /* char data[512]; */
|
|
8487
|
760 char data[10096];
|
|
8634
|
761 unsigned int datalen;
|
|
8487
|
762 struct sockaddr_in addr;
|
|
|
763 socklen_t addrlen;
|
|
|
764
|
|
|
765 /* Read in an mDNS packet */
|
|
|
766 addrlen = sizeof(struct sockaddr_in);
|
|
|
767 if ((datalen = recvfrom(fd, data, sizeof(data), 0, (struct sockaddr *)&addr, &addrlen)) == -1) {
|
|
|
768 gaim_debug_error("mdns", "Error reading packet: %d\n", errno);
|
|
|
769 return NULL;
|
|
|
770 }
|
|
|
771
|
|
|
772 ret = (DNSPacket *)g_malloc0(sizeof(DNSPacket));
|
|
|
773
|
|
|
774 /* Parse the incoming packet, starting from 0 */
|
|
|
775 i = 0;
|
|
|
776
|
|
|
777 /* The header section */
|
|
|
778 ret->header.id = util_get16(&data[i]); /* ID */
|
|
|
779 i += 2;
|
|
|
780
|
|
|
781 /* For the flags, some bits must be 0 and some must be 1, the rest are ignored */
|
|
|
782 ret->header.flags = util_get16(&data[i]); /* Flags (QR, OPCODE, AA, TC, RD, RA, Z, AD, CD, and RCODE */
|
|
|
783 i += 2;
|
|
|
784 if ((ret->header.flags & 0x8000) == 0) {
|
|
|
785 /* QR should be 1 */
|
|
|
786 g_free(ret);
|
|
|
787 return NULL;
|
|
|
788 }
|
|
|
789 if ((ret->header.flags & 0x7800) != 0) {
|
|
|
790 /* OPCODE should be all 0's */
|
|
|
791 g_free(ret);
|
|
|
792 return NULL;
|
|
|
793 }
|
|
|
794
|
|
|
795 /* Read in the number of other things in the packet */
|
|
|
796 ret->header.numquestions = util_get16(&data[i]);
|
|
|
797 i += 2;
|
|
|
798 ret->header.numanswers = util_get16(&data[i]);
|
|
|
799 i += 2;
|
|
|
800 ret->header.numauthority = util_get16(&data[i]);
|
|
|
801 i += 2;
|
|
|
802 ret->header.numadditional = util_get16(&data[i]);
|
|
|
803 i += 2;
|
|
|
804
|
|
|
805 /* Read in all the questions */
|
|
|
806 ret->questions = mdns_read_questions(ret->header.numquestions, data, datalen, &i);
|
|
|
807
|
|
|
808 /* Read in all resource records */
|
|
|
809 ret->answers = mdns_read_rr(ret->header.numanswers, data, datalen, &i);
|
|
|
810
|
|
|
811 /* Read in all authority records */
|
|
|
812 ret->authority = mdns_read_rr(ret->header.numauthority, data, datalen, &i);
|
|
|
813
|
|
|
814 /* Read in all additional records */
|
|
|
815 ret->additional = mdns_read_rr(ret->header.numadditional, data, datalen, &i);
|
|
|
816
|
|
|
817 /* We should be at the end of the packet */
|
|
|
818 if (i != datalen) {
|
|
|
819 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", i, datalen);
|
|
|
820 g_free(ret);
|
|
|
821 return NULL;
|
|
|
822 }
|
|
|
823
|
|
|
824 return ret;
|
|
|
825 }
|
|
|
826
|
|
|
827 /**
|
|
|
828 * Free the rdata associated with a given resource record.
|
|
|
829 */
|
|
|
830 static void
|
|
|
831 mdns_free_rr_rdata(unsigned short type, void *rdata)
|
|
|
832 {
|
|
|
833 switch (type) {
|
|
|
834 case RENDEZVOUS_RRTYPE_NULL:
|
|
|
835 case RENDEZVOUS_RRTYPE_PTR:
|
|
|
836 g_free(rdata);
|
|
|
837 break;
|
|
|
838
|
|
|
839 case RENDEZVOUS_RRTYPE_TXT:
|
|
|
840 g_hash_table_destroy(rdata);
|
|
|
841 break;
|
|
8612
|
842
|
|
|
843 case RENDEZVOUS_RRTYPE_SRV:
|
|
8634
|
844 g_free(((ResourceRecordRDataSRV *)rdata)->target);
|
|
8612
|
845 g_free(rdata);
|
|
|
846 break;
|
|
8487
|
847 }
|
|
|
848 }
|
|
|
849
|
|
|
850 /**
|
|
|
851 * Free a given question
|
|
|
852 */
|
|
|
853 static void
|
|
|
854 mdns_free_q(Question *q)
|
|
|
855 {
|
|
|
856 g_free(q->name);
|
|
|
857 }
|
|
|
858
|
|
|
859 /**
|
|
|
860 * Free a given resource record.
|
|
|
861 */
|
|
|
862 static void
|
|
|
863 mdns_free_rr(ResourceRecord *rr)
|
|
|
864 {
|
|
|
865 g_free(rr->name);
|
|
8631
|
866 if (rr->rdata != NULL)
|
|
|
867 mdns_free_rr_rdata(rr->type, rr->rdata);
|
|
8487
|
868 }
|
|
|
869
|
|
|
870 void
|
|
|
871 mdns_free(DNSPacket *dns)
|
|
|
872 {
|
|
|
873 int i;
|
|
|
874
|
|
|
875 for (i = 0; i < dns->header.numquestions; i++)
|
|
|
876 mdns_free_q(&dns->questions[i]);
|
|
|
877 for (i = 0; i < dns->header.numanswers; i++)
|
|
|
878 mdns_free_rr(&dns->answers[i]);
|
|
|
879 for (i = 0; i < dns->header.numauthority; i++)
|
|
|
880 mdns_free_rr(&dns->authority[i]);
|
|
|
881 for (i = 0; i < dns->header.numadditional; i++)
|
|
|
882 mdns_free_rr(&dns->additional[i]);
|
|
|
883
|
|
|
884 g_free(dns->questions);
|
|
|
885 g_free(dns->answers);
|
|
|
886 g_free(dns->authority);
|
|
|
887 g_free(dns->additional);
|
|
|
888 g_free(dns);
|
|
|
889 }
|