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