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
|
|
79 /* Ensure loopback is enabled (it should be enabled by default, by let's be sure) */
|
|
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
|
|
138 mdns_getlength_RR(const ResourceRecord *rr)
|
|
139 {
|
|
140 int ret = 0;
|
|
141
|
|
142 ret += strlen(rr->name) + 2;
|
|
143 ret += 10;
|
|
144
|
|
145 switch (rr->type) {
|
|
146 case RENDEZVOUS_RRTYPE_PTR:
|
|
147 ret += strlen((const char *)rr->rdata) + 2;
|
|
148 break;
|
|
149 }
|
|
150
|
|
151 return ret;
|
|
152 }
|
|
153
|
|
154 static int
|
|
155 mdns_put_name(char *data, int datalen, int offset, const char *name)
|
|
156 {
|
|
157 int i = 0;
|
|
158 char *b, *c;
|
|
159
|
|
160 b = (char *)name;
|
|
161 while ((c = strchr(b, '.'))) {
|
|
162 i += util_put8(&data[offset + i], c - b); /* Length of domain-name segment */
|
|
163 memcpy(&data[offset + i], b, c - b); /* Domain-name segment */
|
|
164 i += c - b; /* Increment the destination pointer */
|
|
165 b = c + 1;
|
|
166 }
|
|
167 i += util_put8(&data[offset + i], strlen(b)); /* Length of domain-name segment */
|
|
168 strcpy(&data[offset + i], b); /* Domain-name segment */
|
|
169 i += strlen(b) + 1; /* Increment the destination pointer */
|
|
170
|
|
171 return i;
|
|
172 }
|
|
173
|
|
174 static int
|
|
175 mdns_put_RR(char *data, int datalen, int offset, const ResourceRecord *rr)
|
|
176 {
|
|
177 int i = 0;
|
|
178
|
|
179 i += mdns_put_name(data, datalen, offset + i, rr->name);
|
|
180 i += util_put16(&data[offset + i], rr->type);
|
|
181 i += util_put16(&data[offset + i], rr->class);
|
|
182 i += util_put32(&data[offset + i], rr->ttl);
|
|
183 i += util_put16(&data[offset + i], rr->rdlength);
|
|
184
|
|
185 switch (rr->type) {
|
|
186 case RENDEZVOUS_RRTYPE_PTR:
|
|
187 i += mdns_put_name(data, datalen, offset + i, (const char *)rr->rdata);
|
|
188 break;
|
|
189 }
|
|
190
|
|
191 return i;
|
|
192 }
|
|
193
|
|
194 int
|
|
195 mdns_send_dns(int fd, const DNSPacket *dns)
|
|
196 {
|
|
197 int ret;
|
|
198 unsigned int datalen;
|
|
199 unsigned char *data;
|
|
200 int offset;
|
|
201 int i;
|
|
202
|
|
203 /* Calculate the length of the buffer we'll need to hold the DNS packet */
|
|
204 datalen = 0;
|
|
205
|
|
206 /* Header */
|
|
207 datalen += 12;
|
|
208
|
|
209 /* Questions */
|
|
210 for (i = 0; i < dns->header.numquestions; i++)
|
|
211 datalen += strlen(dns->questions[i].name) + 2 + 4;
|
|
212
|
|
213 /* Resource records */
|
|
214 for (i = 0; i < dns->header.numanswers; i++)
|
|
215 datalen += mdns_getlength_RR(&dns->answers[i]);
|
|
216 for (i = 0; i < dns->header.numauthority; i++)
|
|
217 datalen += mdns_getlength_RR(&dns->authority[i]);
|
|
218 for (i = 0; i < dns->header.numadditional; i++)
|
|
219 datalen += mdns_getlength_RR(&dns->additional[i]);
|
|
220
|
|
221 /* Allocate a buffer */
|
|
222 if (!(data = (unsigned char *)g_malloc(datalen))) {
|
|
223 return -ENOMEM;
|
|
224 }
|
|
225
|
|
226 /* Construct the datagram */
|
|
227 /* Header */
|
|
228 offset = 0;
|
|
229 offset += util_put16(&data[offset], dns->header.id); /* ID */
|
|
230 offset += util_put16(&data[offset], dns->header.flags);
|
|
231 offset += util_put16(&data[offset], dns->header.numquestions); /* QDCOUNT */
|
|
232 offset += util_put16(&data[offset], dns->header.numanswers); /* ANCOUNT */
|
|
233 offset += util_put16(&data[offset], dns->header.numauthority); /* NSCOUNT */
|
|
234 offset += util_put16(&data[offset], dns->header.numadditional); /* ARCOUNT */
|
|
235
|
|
236 /* Questions */
|
|
237 for (i = 0; i < dns->header.numquestions; i++) {
|
|
238 offset += mdns_put_name(data, datalen, offset, dns->questions[i].name); /* QNAME */
|
|
239 offset += util_put16(&data[offset], dns->questions[i].type); /* QTYPE */
|
|
240 offset += util_put16(&data[offset], dns->questions[i].class); /* QCLASS */
|
|
241 }
|
|
242
|
|
243 /* Resource records */
|
|
244 for (i = 0; i < dns->header.numanswers; i++)
|
|
245 offset += mdns_put_RR(data, datalen, offset, &dns->answers[i]);
|
|
246 for (i = 0; i < dns->header.numauthority; i++)
|
|
247 offset += mdns_put_RR(data, datalen, offset, &dns->authority[i]);
|
|
248 for (i = 0; i < dns->header.numadditional; i++)
|
|
249 offset += mdns_put_RR(data, datalen, offset, &dns->additional[i]);
|
|
250
|
|
251 /* Send the datagram */
|
|
252 ret = mdns_send_raw(fd, datalen, data);
|
|
253
|
|
254 g_free(data);
|
|
255
|
|
256 return ret;
|
|
257 }
|
|
258
|
8487
|
259 int
|
|
260 mdns_query(int fd, const char *domain)
|
|
261 {
|
8612
|
262 int ret;
|
|
263 DNSPacket *dns;
|
8487
|
264
|
|
265 if (strlen(domain) > 255) {
|
|
266 return -EINVAL;
|
|
267 }
|
|
268
|
8612
|
269 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
270 dns->header.id = 0x0000;
|
|
271 dns->header.flags = 0x0000;
|
|
272 dns->header.numquestions = 0x0001;
|
|
273 dns->header.numanswers = 0x0000;
|
|
274 dns->header.numauthority = 0x0000;
|
|
275 dns->header.numadditional = 0x0000;
|
|
276
|
|
277 dns->questions = (Question *)g_malloc(1 * sizeof(Question));
|
|
278 dns->questions[0].name = g_strdup(domain);
|
|
279 dns->questions[0].type = RENDEZVOUS_RRTYPE_PTR;
|
|
280 dns->questions[0].class = 0x8001;
|
|
281
|
|
282 dns->answers = NULL;
|
|
283 dns->authority = NULL;
|
|
284 dns->additional = NULL;
|
|
285
|
|
286 mdns_send_dns(fd, dns);
|
|
287
|
|
288 mdns_free(dns);
|
|
289
|
|
290 return ret;
|
|
291 }
|
|
292
|
|
293 int
|
|
294 mdns_advertise_ptr(int fd, const char *name, const char *domain)
|
|
295 {
|
|
296 int ret;
|
|
297 DNSPacket *dns;
|
|
298
|
|
299 if ((strlen(name) > 255) || (strlen(domain) > 255)) {
|
|
300 return -EINVAL;
|
8487
|
301 }
|
|
302
|
8612
|
303 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
|
|
304 dns->header.id = 0x0000;
|
|
305 dns->header.flags = 0x8400;
|
|
306 dns->header.numquestions = 0x0000;
|
|
307 dns->header.numanswers = 0x0001;
|
|
308 dns->header.numauthority = 0x0000;
|
|
309 dns->header.numadditional = 0x0000;
|
|
310 dns->questions = NULL;
|
8487
|
311
|
8612
|
312 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
|
|
313 dns->answers[0].name = g_strdup(name);
|
|
314 dns->answers[0].type = RENDEZVOUS_RRTYPE_PTR;
|
|
315 dns->answers[0].class = 0x0001;
|
|
316 dns->answers[0].ttl = 0x00001c20;
|
|
317 dns->answers[0].rdlength = strlen(domain) + 2;
|
|
318 dns->answers[0].rdata = (void *)g_strdup(domain);
|
|
319
|
|
320 dns->authority = NULL;
|
|
321 dns->additional = NULL;
|
8487
|
322
|
8612
|
323 mdns_send_dns(fd, dns);
|
|
324
|
|
325 mdns_free(dns);
|
8487
|
326
|
8612
|
327 return ret;
|
|
328 }
|
8487
|
329
|
8612
|
330 /***************************************/
|
|
331 /* Functions for parsing mDNS messages */
|
|
332 /***************************************/
|
8487
|
333
|
|
334 /*
|
|
335 * XXX - Needs bounds checking!
|
|
336 *
|
|
337 * Read in a domain name from the given buffer starting at the given
|
|
338 * offset. This handles using domain name compression to jump around
|
|
339 * the data buffer, if needed.
|
|
340 *
|
|
341 * @return A null-terminated string representation of the domain name.
|
|
342 * This should be g_free'd when no longer needed.
|
|
343 */
|
|
344 static gchar *
|
|
345 mdns_read_name(const char *data, int datalen, int dataoffset)
|
|
346 {
|
|
347 GString *ret = g_string_new("");
|
|
348 unsigned char tmp;
|
|
349
|
|
350 while ((tmp = util_get8(&data[dataoffset])) != 0) {
|
|
351 dataoffset++;
|
|
352
|
|
353 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
|
|
354 if (*ret->str)
|
|
355 g_string_append_c(ret, '.');
|
|
356 g_string_append_len(ret, &data[dataoffset], tmp);
|
|
357 dataoffset += tmp;
|
|
358
|
|
359 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
|
|
360 /* Reserved for future use */
|
|
361
|
|
362 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
|
|
363 /* Reserved for future use */
|
|
364
|
|
365 } else { /* First two bits are 11 */
|
|
366 /* Jump to another position in the data */
|
|
367 dataoffset = util_get8(&data[dataoffset]);
|
|
368
|
|
369 }
|
|
370 }
|
|
371
|
|
372 return g_string_free(ret, FALSE);
|
|
373 }
|
|
374
|
|
375 /*
|
|
376 * XXX - Needs bounds checking!
|
|
377 *
|
|
378 * Determine how many bytes long a portion of the domain name is
|
|
379 * at the given offset. This does NOT jump around the data array
|
|
380 * in the case of domain name compression.
|
|
381 *
|
|
382 * @return The length of the portion of the domain name.
|
|
383 */
|
|
384 static int
|
|
385 mdns_read_name_len(const char *data, int datalen, int dataoffset)
|
|
386 {
|
|
387 int startoffset = dataoffset;
|
|
388 unsigned char tmp;
|
|
389
|
|
390 while ((tmp = util_get8(&data[dataoffset++])) != 0) {
|
|
391
|
|
392 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
|
|
393 dataoffset += tmp;
|
|
394
|
|
395 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
|
|
396 /* Reserved for future use */
|
|
397
|
|
398 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
|
|
399 /* Reserved for future use */
|
|
400
|
|
401 } else { /* First two bits are 11 */
|
|
402 /* End of this portion of the domain name */
|
|
403 dataoffset++;
|
|
404 break;
|
|
405
|
|
406 }
|
|
407 }
|
|
408
|
|
409 return dataoffset - startoffset;
|
|
410 }
|
|
411
|
|
412 /*
|
|
413 * XXX - Needs bounds checking!
|
|
414 *
|
|
415 */
|
|
416 static Question *
|
|
417 mdns_read_questions(int numquestions, const char *data, int datalen, int *offset)
|
|
418 {
|
|
419 Question *ret;
|
|
420 int i;
|
|
421
|
|
422 ret = (Question *)g_malloc0(numquestions * sizeof(Question));
|
|
423 for (i = 0; i < numquestions; i++) {
|
|
424 ret[i].name = mdns_read_name(data, 0, *offset);
|
|
425 *offset += mdns_read_name_len(data, 0, *offset);
|
|
426 ret[i].type = util_get16(&data[*offset]); /* QTYPE */
|
|
427 *offset += 2;
|
|
428 ret[i].class = util_get16(&data[*offset]); /* QCLASS */
|
|
429 *offset += 2;
|
|
430 }
|
|
431
|
|
432 return ret;
|
|
433 }
|
|
434
|
|
435 /*
|
|
436 * Read in a chunk of data, probably a buddy icon.
|
|
437 *
|
|
438 */
|
|
439 static unsigned char *
|
|
440 mdns_read_rr_rdata_null(const char *data, int datalen, int offset, unsigned short rdlength)
|
|
441 {
|
|
442 unsigned char *ret = NULL;
|
|
443
|
|
444 if (offset + rdlength > datalen)
|
|
445 return NULL;
|
|
446
|
|
447 ret = (unsigned char *)g_malloc(rdlength);
|
|
448 memcpy(ret, &data[offset], rdlength);
|
|
449
|
|
450 return ret;
|
|
451 }
|
|
452
|
|
453 /*
|
|
454 * XXX - Needs bounds checking!
|
|
455 *
|
|
456 */
|
|
457 static char *
|
|
458 mdns_read_rr_rdata_ptr(const char *data, int datalen, int offset)
|
|
459 {
|
|
460 char *ret = NULL;
|
|
461
|
|
462 ret = mdns_read_name(data, datalen, offset);
|
|
463
|
|
464 return ret;
|
|
465 }
|
|
466
|
|
467 /*
|
|
468 *
|
|
469 *
|
|
470 */
|
|
471 static GHashTable *
|
|
472 mdns_read_rr_rdata_txt(const char *data, int datalen, int offset, unsigned short rdlength)
|
|
473 {
|
|
474 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
475 int endoffset = offset + rdlength;
|
|
476 unsigned char tmp;
|
|
477 char buf[256], *key, *value;
|
|
478
|
|
479 while (offset < endoffset) {
|
|
480 /* Read in the length of the next name/value pair */
|
|
481 tmp = util_get8(&data[offset]);
|
|
482 offset++;
|
|
483
|
|
484 /* Ensure packet is valid */
|
|
485 if (offset + tmp > endoffset)
|
|
486 break;
|
|
487
|
|
488 /* Read in the next name/value pair */
|
|
489 strncpy(buf, &data[offset], tmp);
|
|
490 offset += tmp;
|
|
491
|
|
492 if (buf[0] == '=') {
|
|
493 /* Name/value pairs beginning with = are silently ignored */
|
|
494 continue;
|
|
495 }
|
|
496
|
|
497 /* The value is a substring of buf, starting just after the = */
|
|
498 buf[tmp] = '\0';
|
|
499 value = strchr(buf, '=');
|
|
500 if (value != NULL) {
|
|
501 value[0] = '\0';
|
|
502 value++;
|
|
503 }
|
|
504
|
|
505 /* Make the key all lowercase */
|
|
506 key = g_utf8_strdown(buf, -1);
|
|
507 if (!g_hash_table_lookup(ret, key))
|
|
508 g_hash_table_insert(ret, key, g_strdup(value));
|
|
509 else
|
|
510 g_free(key);
|
|
511 }
|
|
512
|
|
513 return ret;
|
|
514 }
|
|
515
|
|
516 /*
|
8594
|
517 *
|
|
518 *
|
|
519 */
|
|
520 static ResourceRecordSRV *
|
|
521 mdns_read_rr_rdata_srv(const char *data, int datalen, int offset, unsigned short rdlength)
|
|
522 {
|
|
523 ResourceRecordSRV *ret = NULL;
|
|
524 int endoffset = offset + rdlength;
|
|
525
|
|
526 if (offset + 7 > endoffset)
|
|
527 return NULL;
|
|
528
|
|
529 ret = g_malloc(sizeof(ResourceRecordSRV));
|
|
530
|
|
531 /* Read in the priority */
|
|
532 ret->priority = util_get16(&data[offset]);
|
|
533 offset += 2;
|
|
534
|
|
535 /* Read in the weight */
|
|
536 ret->weight = util_get16(&data[offset]);
|
|
537 offset += 2;
|
|
538
|
|
539 /* Read in the port */
|
|
540 ret->port = util_get16(&data[offset]);
|
|
541 offset += 2;
|
|
542
|
|
543 /* Read in the target name */
|
|
544 /*
|
|
545 * XXX - RFC2782 says it's not supposed to be an alias...
|
|
546 * but it was in the packet capture I looked at from iChat.
|
|
547 */
|
|
548 ret->target = mdns_read_name(data, datalen, offset);
|
|
549
|
|
550 return ret;
|
|
551 }
|
|
552
|
|
553 /*
|
8487
|
554 * XXX - Needs bounds checking!
|
|
555 *
|
|
556 */
|
|
557 static ResourceRecord *
|
|
558 mdns_read_rr(int numrecords, const char *data, int datalen, int *offset)
|
|
559 {
|
|
560 ResourceRecord *ret;
|
|
561 int i;
|
|
562
|
|
563 ret = (ResourceRecord *)g_malloc0(numrecords * sizeof(ResourceRecord));
|
|
564 for (i = 0; i < numrecords; i++) {
|
|
565 ret[i].name = mdns_read_name(data, 0, *offset); /* NAME */
|
|
566 *offset += mdns_read_name_len(data, 0, *offset);
|
|
567 ret[i].type = util_get16(&data[*offset]); /* TYPE */
|
|
568 *offset += 2;
|
|
569 ret[i].class = util_get16(&data[*offset]); /* CLASS */
|
|
570 *offset += 2;
|
|
571 ret[i].ttl = util_get32(&data[*offset]); /* TTL */
|
|
572 *offset += 4;
|
|
573 ret[i].rdlength = util_get16(&data[*offset]); /* RDLENGTH */
|
|
574 *offset += 2;
|
|
575
|
|
576 /* RDATA */
|
|
577 switch (ret[i].type) {
|
|
578 case RENDEZVOUS_RRTYPE_NULL:
|
|
579 ret[i].rdata = mdns_read_rr_rdata_null(data, datalen, *offset, ret[i].rdlength);
|
|
580 break;
|
|
581
|
|
582 case RENDEZVOUS_RRTYPE_PTR:
|
|
583 ret[i].rdata = mdns_read_rr_rdata_ptr(data, datalen, *offset);
|
|
584 break;
|
|
585
|
|
586 case RENDEZVOUS_RRTYPE_TXT:
|
|
587 ret[i].rdata = mdns_read_rr_rdata_txt(data, datalen, *offset, ret[i].rdlength);
|
|
588 break;
|
|
589
|
8594
|
590 case RENDEZVOUS_RRTYPE_SRV:
|
|
591 ret[i].rdata = mdns_read_rr_rdata_srv(data, datalen, *offset, ret[i].rdlength);
|
|
592 break;
|
|
593
|
8487
|
594 default:
|
|
595 ret[i].rdata = NULL;
|
|
596 break;
|
|
597 }
|
|
598 *offset += ret[i].rdlength;
|
|
599 }
|
|
600
|
|
601 return ret;
|
|
602 }
|
|
603
|
|
604 /*
|
|
605 * XXX - Needs bounds checking!
|
|
606 *
|
|
607 */
|
|
608 DNSPacket *
|
|
609 mdns_read(int fd)
|
|
610 {
|
|
611 DNSPacket *ret = NULL;
|
|
612 int i; /* Current position in datagram */
|
8612
|
613 /* XXX - Find out what to use as a maximum incoming UDP packet size */
|
|
614 /* char data[512]; */
|
8487
|
615 char data[10096];
|
|
616 int datalen;
|
|
617 struct sockaddr_in addr;
|
|
618 socklen_t addrlen;
|
|
619
|
|
620 /* Read in an mDNS packet */
|
|
621 addrlen = sizeof(struct sockaddr_in);
|
|
622 if ((datalen = recvfrom(fd, data, sizeof(data), 0, (struct sockaddr *)&addr, &addrlen)) == -1) {
|
|
623 gaim_debug_error("mdns", "Error reading packet: %d\n", errno);
|
|
624 return NULL;
|
|
625 }
|
|
626
|
|
627 ret = (DNSPacket *)g_malloc0(sizeof(DNSPacket));
|
|
628
|
|
629 /* Parse the incoming packet, starting from 0 */
|
|
630 i = 0;
|
|
631
|
|
632 /* The header section */
|
|
633 ret->header.id = util_get16(&data[i]); /* ID */
|
|
634 i += 2;
|
|
635
|
|
636 /* For the flags, some bits must be 0 and some must be 1, the rest are ignored */
|
|
637 ret->header.flags = util_get16(&data[i]); /* Flags (QR, OPCODE, AA, TC, RD, RA, Z, AD, CD, and RCODE */
|
|
638 i += 2;
|
|
639 if ((ret->header.flags & 0x8000) == 0) {
|
|
640 /* QR should be 1 */
|
|
641 g_free(ret);
|
|
642 return NULL;
|
|
643 }
|
|
644 if ((ret->header.flags & 0x7800) != 0) {
|
|
645 /* OPCODE should be all 0's */
|
|
646 g_free(ret);
|
|
647 return NULL;
|
|
648 }
|
|
649
|
|
650 /* Read in the number of other things in the packet */
|
|
651 ret->header.numquestions = util_get16(&data[i]);
|
|
652 i += 2;
|
|
653 ret->header.numanswers = util_get16(&data[i]);
|
|
654 i += 2;
|
|
655 ret->header.numauthority = util_get16(&data[i]);
|
|
656 i += 2;
|
|
657 ret->header.numadditional = util_get16(&data[i]);
|
|
658 i += 2;
|
|
659
|
|
660 /* Read in all the questions */
|
|
661 ret->questions = mdns_read_questions(ret->header.numquestions, data, datalen, &i);
|
|
662
|
|
663 /* Read in all resource records */
|
|
664 ret->answers = mdns_read_rr(ret->header.numanswers, data, datalen, &i);
|
|
665
|
|
666 /* Read in all authority records */
|
|
667 ret->authority = mdns_read_rr(ret->header.numauthority, data, datalen, &i);
|
|
668
|
|
669 /* Read in all additional records */
|
|
670 ret->additional = mdns_read_rr(ret->header.numadditional, data, datalen, &i);
|
|
671
|
|
672 /* We should be at the end of the packet */
|
|
673 if (i != datalen) {
|
|
674 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", i, datalen);
|
|
675 g_free(ret);
|
|
676 return NULL;
|
|
677 }
|
|
678
|
|
679 return ret;
|
|
680 }
|
|
681
|
|
682 /**
|
|
683 * Free the rdata associated with a given resource record.
|
|
684 */
|
|
685 static void
|
|
686 mdns_free_rr_rdata(unsigned short type, void *rdata)
|
|
687 {
|
|
688 switch (type) {
|
|
689 case RENDEZVOUS_RRTYPE_NULL:
|
|
690 case RENDEZVOUS_RRTYPE_PTR:
|
|
691 g_free(rdata);
|
|
692 break;
|
|
693
|
|
694 case RENDEZVOUS_RRTYPE_TXT:
|
|
695 g_hash_table_destroy(rdata);
|
|
696 break;
|
8612
|
697
|
|
698 case RENDEZVOUS_RRTYPE_SRV:
|
|
699 g_free(((ResourceRecordSRV *)rdata)->target);
|
|
700 g_free(rdata);
|
|
701 break;
|
8487
|
702 }
|
|
703 }
|
|
704
|
|
705 /**
|
|
706 * Free a given question
|
|
707 */
|
|
708 static void
|
|
709 mdns_free_q(Question *q)
|
|
710 {
|
|
711 g_free(q->name);
|
|
712 }
|
|
713
|
|
714 /**
|
|
715 * Free a given resource record.
|
|
716 */
|
|
717 static void
|
|
718 mdns_free_rr(ResourceRecord *rr)
|
|
719 {
|
|
720 g_free(rr->name);
|
|
721 mdns_free_rr_rdata(rr->type, rr->rdata);
|
|
722 }
|
|
723
|
|
724 void
|
|
725 mdns_free(DNSPacket *dns)
|
|
726 {
|
|
727 int i;
|
|
728
|
|
729 for (i = 0; i < dns->header.numquestions; i++)
|
|
730 mdns_free_q(&dns->questions[i]);
|
|
731 for (i = 0; i < dns->header.numanswers; i++)
|
|
732 mdns_free_rr(&dns->answers[i]);
|
|
733 for (i = 0; i < dns->header.numauthority; i++)
|
|
734 mdns_free_rr(&dns->authority[i]);
|
|
735 for (i = 0; i < dns->header.numadditional; i++)
|
|
736 mdns_free_rr(&dns->additional[i]);
|
|
737
|
|
738 g_free(dns->questions);
|
|
739 g_free(dns->answers);
|
|
740 g_free(dns->authority);
|
|
741 g_free(dns->additional);
|
|
742 g_free(dns);
|
|
743 }
|