Mercurial > pidgin
annotate src/protocols/rendezvous/mdns.c @ 8834:beb7be215db3
[gaim-migrate @ 9598]
I removed account->ip because it isn't used anywhere and I think it's dumb.
Also added handling for a and aaaa records to rendezvous. Gaim peeps
show up in iChat rendezvous lists now. There are still problems.
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Wed, 28 Apr 2004 00:48:21 +0000 |
| parents | 8212661dc3cc |
| children | 518455386538 |
| rev | line source |
|---|---|
| 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 |
| 8738 | 34 * any possible buffer overflow exploits. It doesn't even HAVE to be |
| 35 * a pair, neither--one eye would suffice. Oh, snap, somebody call | |
| 36 * One Eyed Willie. | |
| 8487 | 37 */ |
| 38 | |
| 8546 | 39 #include "internal.h" |
| 8487 | 40 #include "debug.h" |
| 41 | |
| 42 #include "mdns.h" | |
| 8738 | 43 #include "mdns_cache.h" |
| 8487 | 44 #include "util.h" |
| 45 | |
| 8612 | 46 /******************************************/ |
| 8636 | 47 /* Functions for freeing a DNS structure */ |
| 48 /******************************************/ | |
| 49 | |
| 50 /** | |
| 8806 | 51 * Free a given question |
| 52 * | |
| 53 * @param q The question to free. | |
| 54 */ | |
| 55 static void | |
| 56 mdns_free_q(Question *q) | |
| 57 { | |
| 58 g_free(q->name); | |
| 59 g_free(q); | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Free a given list of questions. | |
| 64 * | |
| 65 * @param qs The list of questions to free. | |
| 66 */ | |
| 67 static void | |
| 68 mdns_free_qs(GSList *qs) | |
| 69 { | |
| 70 Question *q; | |
| 71 | |
| 72 while (qs != NULL) { | |
| 73 q = qs->data; | |
| 74 mdns_free_q(q); | |
| 75 qs = g_slist_remove(qs, q); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 /** | |
| 8636 | 80 * Free the rdata associated with a given resource record. |
| 8806 | 81 * |
| 82 * @param type The type of the resource record you are freeing. | |
| 83 * @param rdata The rdata of the resource record you are freeing. | |
| 8636 | 84 */ |
| 85 static void | |
| 86 mdns_free_rr_rdata(unsigned short type, void *rdata) | |
| 87 { | |
| 88 if (rdata == NULL) | |
| 89 return; | |
| 90 | |
| 91 switch (type) { | |
| 8834 | 92 case RENDEZVOUS_RRTYPE_A: |
| 8636 | 93 case RENDEZVOUS_RRTYPE_NULL: |
| 94 case RENDEZVOUS_RRTYPE_PTR: | |
| 8834 | 95 case RENDEZVOUS_RRTYPE_AAAA: |
| 8636 | 96 g_free(rdata); |
| 97 break; | |
| 98 | |
| 8806 | 99 case RENDEZVOUS_RRTYPE_TXT: { |
| 100 GSList *cur = rdata; | |
| 101 ResourceRecordRDataTXTNode *node; | |
| 102 while (cur != NULL) { | |
| 103 node = cur->data; | |
| 104 cur = g_slist_remove(cur, node); | |
| 105 g_free(node->name); | |
| 106 g_free(node->value); | |
| 107 g_free(node); | |
| 108 } | |
| 109 } break; | |
| 8636 | 110 |
| 111 case RENDEZVOUS_RRTYPE_SRV: | |
| 112 g_free(((ResourceRecordRDataSRV *)rdata)->target); | |
| 113 g_free(rdata); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Free a given resource record. | |
| 8806 | 120 * |
| 121 * @param rr The resource record to free. | |
| 8636 | 122 */ |
| 8738 | 123 void |
| 8636 | 124 mdns_free_rr(ResourceRecord *rr) |
| 125 { | |
| 126 g_free(rr->name); | |
| 127 mdns_free_rr_rdata(rr->type, rr->rdata); | |
| 8806 | 128 g_free(rr); |
| 8636 | 129 } |
| 130 | |
| 8806 | 131 /** |
| 132 * Free a given list of resource records. | |
| 133 * | |
| 134 * @param rrs The list of resource records to free. | |
| 135 */ | |
| 136 void | |
| 137 mdns_free_rrs(GSList *rrs) | |
| 138 { | |
| 139 ResourceRecord *rr; | |
| 140 | |
| 141 while (rrs != NULL) { | |
| 142 rr = rrs->data; | |
| 143 mdns_free_rr(rr); | |
| 144 rrs = g_slist_remove(rrs, rr); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 /** | |
| 149 * Free a given DNS packet. This frees all the questions and all | |
| 150 * the resource records. | |
| 151 * | |
| 152 * @param dns The DNS packet to free. | |
| 153 */ | |
| 8636 | 154 void |
| 155 mdns_free(DNSPacket *dns) | |
| 156 { | |
| 8806 | 157 mdns_free_qs(dns->questions); |
| 158 mdns_free_rrs(dns->answers); | |
| 159 mdns_free_rrs(dns->authority); | |
| 160 mdns_free_rrs(dns->additional); | |
| 161 | |
| 162 g_free(dns); | |
| 163 } | |
| 164 | |
| 165 /**********************************************/ | |
| 166 /* Functions for duplicating a DNS structure */ | |
| 167 /**********************************************/ | |
| 168 | |
| 169 static Question * | |
| 170 mdns_copy_q(const Question *q) | |
| 171 { | |
| 172 Question *ret; | |
| 173 | |
| 174 if (q == NULL) | |
| 175 return NULL; | |
| 176 | |
| 177 ret = g_malloc(sizeof(Question)); | |
| 178 ret->name = g_strdup(q->name); | |
| 179 ret->type = q->type; | |
| 180 ret->class = q->class; | |
| 181 | |
| 182 return ret; | |
| 183 } | |
| 184 | |
| 185 static GSList * | |
| 186 mdns_copy_qs(const GSList *qs) | |
| 187 { | |
| 188 GSList *ret = NULL; | |
| 189 GSList *cur; | |
| 190 Question *new; | |
| 191 | |
| 192 for (cur = (GSList *)qs; cur != NULL; cur = cur->next) { | |
| 193 new = mdns_copy_q(cur->data); | |
| 194 ret = g_slist_append(ret, new); | |
| 195 } | |
| 196 | |
| 197 return ret; | |
| 198 } | |
| 199 | |
| 200 static void * | |
| 201 mdns_copy_rr_rdata_txt(const ResourceRecordRDataTXT *rdata) | |
| 202 { | |
| 203 GSList *ret = NULL; | |
| 204 GSList *cur; | |
| 205 ResourceRecordRDataTXTNode *node, *copy; | |
| 206 | |
| 207 for (cur = (GSList *)rdata; cur != NULL; cur = cur->next) { | |
| 208 node = cur->data; | |
| 209 copy = g_malloc0(sizeof(ResourceRecordRDataTXTNode)); | |
| 210 copy->name = g_strdup(node->name); | |
| 211 if (node->value != NULL) | |
| 212 copy->value = g_strdup(node->value); | |
| 213 ret = g_slist_append(ret, copy); | |
| 214 } | |
| 215 | |
| 216 return ret; | |
| 217 } | |
| 218 | |
| 219 static void * | |
| 220 mdns_copy_rr_rdata_srv(const ResourceRecordRDataSRV *rdata) | |
| 221 { | |
| 222 ResourceRecordRDataSRV *ret; | |
| 223 | |
| 224 ret = g_malloc(sizeof(ResourceRecordRDataSRV)); | |
| 225 ret->priority = rdata->priority; | |
| 226 ret->weight = rdata->weight; | |
| 227 ret->port = rdata->port; | |
| 228 ret->target = g_strdup(rdata->target); | |
| 229 | |
| 230 return ret; | |
| 231 } | |
| 8636 | 232 |
| 8806 | 233 void * |
| 234 mdns_copy_rr_rdata(unsigned short type, const void *rdata, unsigned int rdlength) | |
| 235 { | |
| 236 void *ret; | |
| 237 | |
| 238 if (rdata == NULL) | |
| 239 return NULL; | |
| 240 | |
| 241 switch (type) { | |
| 8834 | 242 case RENDEZVOUS_RRTYPE_A: |
| 243 ret = g_memdup(rdata, rdlength); | |
| 244 break; | |
| 245 | |
| 8806 | 246 case RENDEZVOUS_RRTYPE_NULL: |
| 247 ret = g_memdup(rdata, rdlength); | |
| 248 break; | |
| 249 | |
| 250 case RENDEZVOUS_RRTYPE_PTR: | |
| 8834 | 251 ret = g_strdup(rdata); |
| 8806 | 252 break; |
| 253 | |
| 254 case RENDEZVOUS_RRTYPE_TXT: | |
| 255 ret = mdns_copy_rr_rdata_txt(rdata); | |
| 256 break; | |
| 257 | |
| 8834 | 258 case RENDEZVOUS_RRTYPE_AAAA: |
| 259 ret = g_memdup(rdata, rdlength); | |
| 260 break; | |
| 261 | |
| 8806 | 262 case RENDEZVOUS_RRTYPE_SRV: |
| 263 ret = mdns_copy_rr_rdata_srv(rdata); | |
| 264 break; | |
| 265 } | |
| 266 | |
| 267 return ret; | |
| 268 } | |
| 269 | |
| 270 ResourceRecord * | |
| 271 mdns_copy_rr(const ResourceRecord *rr) | |
| 272 { | |
| 273 ResourceRecord *ret; | |
| 274 | |
| 275 if (rr == NULL) | |
| 276 return NULL; | |
| 8636 | 277 |
| 8806 | 278 ret = g_malloc(sizeof(ResourceRecord)); |
| 279 ret->name = g_strdup(rr->name); | |
| 280 ret->type = rr->type; | |
| 281 ret->class = rr->class; | |
| 282 ret->ttl = rr->ttl; | |
| 283 ret->rdlength = rr->rdlength; | |
| 284 ret->rdata = mdns_copy_rr_rdata(rr->type, rr->rdata, rr->rdlength); | |
| 285 | |
| 286 return ret; | |
| 287 } | |
| 288 | |
| 289 static GSList * | |
| 290 mdns_copy_rrs(const GSList *rrs) | |
| 291 { | |
| 292 GSList *ret = NULL; | |
| 293 GSList *cur; | |
| 294 ResourceRecord *new; | |
| 295 | |
| 296 for (cur = (GSList *)rrs; cur != NULL; cur = cur->next) { | |
| 297 new = mdns_copy_rr(cur->data); | |
| 298 ret = g_slist_append(ret, new); | |
| 299 } | |
| 300 | |
| 301 return ret; | |
| 302 } | |
| 303 | |
| 304 static DNSPacket * | |
| 305 mdns_copy(const DNSPacket *dns) | |
| 306 { | |
| 307 DNSPacket *ret; | |
| 308 | |
| 309 if (dns == NULL) | |
| 310 return NULL; | |
| 311 | |
| 312 ret = g_malloc0(sizeof(DNSPacket)); | |
| 313 ret->header.id = dns->header.id; | |
| 314 ret->header.flags = dns->header.flags; | |
| 315 ret->header.numquestions = dns->header.numquestions; | |
| 316 ret->header.numanswers = dns->header.numanswers; | |
| 317 ret->header.numauthority = dns->header.numauthority; | |
| 318 ret->header.numadditional = dns->header.numadditional; | |
| 319 ret->questions = mdns_copy_qs(dns->questions); | |
| 320 ret->answers = mdns_copy_rrs(dns->answers); | |
| 321 ret->authority = mdns_copy_rrs(dns->authority); | |
| 322 ret->additional = mdns_copy_rrs(dns->additional); | |
| 323 | |
| 324 return ret; | |
| 8636 | 325 } |
| 326 | |
| 327 /******************************************/ | |
| 8612 | 328 /* Functions for connection establishment */ |
| 329 /******************************************/ | |
| 330 | |
| 8487 | 331 int |
| 8834 | 332 mdns_socket_establish() |
| 8487 | 333 { |
| 334 int fd = -1; | |
| 335 struct sockaddr_in addr; | |
| 336 struct ip_mreq mreq; | |
| 337 unsigned char loop; | |
| 338 unsigned char ttl; | |
| 339 int reuseaddr; | |
| 340 | |
| 341 gaim_debug_info("mdns", "Establishing multicast socket\n"); | |
| 342 | |
| 343 /* What's the difference between AF_INET and PF_INET? */ | |
| 344 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | |
| 345 gaim_debug_error("mdns", "Unable to create socket: %s\n", strerror(errno)); | |
| 346 return -1; | |
| 347 } | |
| 348 | |
| 349 /* Make the socket non-blocking (although it shouldn't matter) */ | |
| 350 fcntl(fd, F_SETFL, O_NONBLOCK); | |
| 351 | |
| 352 /* Bind the socket to a local IP and port */ | |
| 353 addr.sin_family = AF_INET; | |
| 354 addr.sin_port = htons(5353); | |
| 355 addr.sin_addr.s_addr = INADDR_ANY; | |
| 356 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { | |
| 357 gaim_debug_error("mdns", "Unable to bind socket to interface.\n"); | |
| 358 close(fd); | |
| 359 return -1; | |
| 360 } | |
| 361 | |
| 8631 | 362 /* Ensure loopback is enabled (it should be enabled by default, but let's be sure) */ |
| 8487 | 363 loop = 1; |
| 364 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(unsigned char)) == -1) { | |
| 365 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_LOOP\n"); | |
| 366 } | |
| 367 | |
| 368 /* Set TTL to 255--required by mDNS */ | |
| 369 ttl = 255; | |
| 370 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(unsigned char)) == -1) { | |
| 371 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_TTL\n"); | |
| 372 close(fd); | |
| 373 return -1; | |
| 374 } | |
| 375 | |
| 376 /* Join the .local multicast group */ | |
| 377 mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.251"); | |
| 378 mreq.imr_interface.s_addr = htonl(INADDR_ANY); | |
| 379 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq)) == -1) { | |
| 380 gaim_debug_error("mdns", "Error calling setsockopt for IP_ADD_MEMBERSHIP\n"); | |
| 381 close(fd); | |
| 382 return -1; | |
| 383 } | |
| 384 | |
| 385 /* Make the local IP re-usable */ | |
| 386 reuseaddr = 1; | |
| 387 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) { | |
| 388 gaim_debug_error("mdns", "Error calling setsockopt for SO_REUSEADDR: %s\n", strerror(errno)); | |
| 389 } | |
| 390 | |
| 391 return fd; | |
| 392 } | |
| 393 | |
| 8834 | 394 void |
| 395 mdns_socket_close(int fd) | |
| 396 { | |
| 397 if (fd >= 0) | |
| 398 close(fd); | |
| 399 | |
| 400 mdns_cache_remove_all(); | |
| 401 } | |
| 402 | |
| 8636 | 403 /** |
| 404 * Multicast raw data over a file descriptor. | |
| 405 * | |
| 406 * @param fd A file descriptor that is a socket bound to a UDP port. | |
| 407 * @param datalen The length of the data you wish to send. | |
| 408 * @param data The data you wish to send. | |
| 409 * @return 0 on success, otherwise return -1. | |
| 410 */ | |
| 8612 | 411 static int |
| 412 mdns_send_raw(int fd, unsigned int datalen, unsigned char *data) | |
| 413 { | |
| 414 struct sockaddr_in addr; | |
| 415 int n; | |
| 416 | |
| 417 addr.sin_family = AF_INET; | |
| 418 addr.sin_port = htons(5353); | |
| 419 addr.sin_addr.s_addr = inet_addr("224.0.0.251"); | |
| 420 n = sendto(fd, data, datalen, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); | |
| 421 | |
| 422 if (n == -1) { | |
| 423 gaim_debug_error("mdns", "Error sending packet: %d\n", errno); | |
| 424 return -1; | |
| 425 } else if (n != datalen) { | |
| 426 gaim_debug_error("mdns", "Only sent %d of %d bytes of data.\n", n, datalen); | |
| 427 return -1; | |
| 428 } | |
| 429 | |
| 430 return 0; | |
| 431 } | |
| 432 | |
| 433 /***************************************/ | |
| 434 /* Functions for sending mDNS messages */ | |
| 435 /***************************************/ | |
| 436 | |
| 8806 | 437 /** |
| 438 * | |
| 439 */ | |
| 8612 | 440 static int |
| 8631 | 441 mdns_getlength_name(const void *name) |
| 8629 | 442 { |
| 8631 | 443 return strlen((const char *)name) + 2; |
| 444 } | |
| 445 | |
| 8806 | 446 /** |
| 447 * | |
| 448 */ | |
| 8631 | 449 static int |
| 8806 | 450 mdns_getlength_q(const Question *q) |
| 451 { | |
| 452 return mdns_getlength_name(q->name) + 4; | |
| 453 } | |
| 454 | |
| 455 /** | |
| 456 * | |
| 457 */ | |
| 458 static int | |
| 459 mdns_getlength_qs(const GSList *qs) | |
| 460 { | |
| 461 int length = 0; | |
| 462 GSList *cur; | |
| 463 | |
| 464 for (cur = (GSList *)qs; cur != NULL; cur = cur->next) | |
| 465 length += mdns_getlength_q(cur->data); | |
| 466 | |
| 467 return length; | |
| 468 } | |
| 469 | |
| 470 /** | |
| 471 * | |
| 472 */ | |
| 473 static int | |
| 474 mdns_getlength_rr_rdata(unsigned short type, const void *rdata) | |
| 8631 | 475 { |
| 8629 | 476 int rdlength = 0; |
| 477 | |
| 8631 | 478 switch (type) { |
| 8834 | 479 case RENDEZVOUS_RRTYPE_A: |
| 480 rdlength = 4; | |
| 481 break; | |
| 482 | |
| 8631 | 483 case RENDEZVOUS_RRTYPE_PTR: |
| 484 rdlength = mdns_getlength_name(rdata); | |
| 485 break; | |
| 486 | |
| 487 case RENDEZVOUS_RRTYPE_TXT: { | |
| 488 GSList *cur; | |
| 489 ResourceRecordRDataTXTNode *node; | |
| 490 | |
| 491 for (cur = (GSList *)rdata; cur != NULL; cur = cur->next) { | |
| 492 node = (ResourceRecordRDataTXTNode *)cur->data; | |
| 493 rdlength += 1 + strlen(node->name); | |
| 494 if (node->value != NULL) | |
| 495 rdlength += 1 + strlen(node->value); | |
| 496 } | |
| 497 } break; | |
| 498 | |
| 8834 | 499 case RENDEZVOUS_RRTYPE_AAAA: |
| 500 rdlength = 16; | |
| 501 break; | |
| 502 | |
| 8631 | 503 case RENDEZVOUS_RRTYPE_SRV: |
| 504 rdlength = 6 + mdns_getlength_name(((const ResourceRecordRDataSRV *)rdata)->target); | |
| 505 break; | |
| 8629 | 506 } |
| 507 | |
| 508 return rdlength; | |
| 509 } | |
| 510 | |
| 8806 | 511 /** |
| 512 * | |
| 513 */ | |
| 8629 | 514 static int |
| 8806 | 515 mdns_getlength_rr(const ResourceRecord *rr) |
| 8612 | 516 { |
| 8634 | 517 return mdns_getlength_name(rr->name) + 10 + rr->rdlength; |
| 8612 | 518 } |
| 519 | |
| 8806 | 520 /** |
| 521 * | |
| 522 */ | |
| 523 static int | |
| 524 mdns_getlength_rrs(const GSList *rrs) | |
| 525 { | |
| 526 int length = 0; | |
| 527 GSList *cur; | |
| 528 | |
| 529 for (cur = (GSList *)rrs; cur != NULL; cur = cur->next) | |
| 530 length += mdns_getlength_rr(cur->data); | |
| 531 | |
| 532 return length; | |
| 533 } | |
| 534 | |
| 535 /** | |
| 536 * | |
| 537 */ | |
| 538 static int | |
| 539 mdns_getlength_dns(const DNSPacket *dns) | |
| 540 { | |
| 541 int length = 0; | |
| 542 | |
| 543 /* Header */ | |
| 544 length += 12; | |
| 545 | |
| 546 /* Questions */ | |
| 547 length += mdns_getlength_qs(dns->questions); | |
| 548 | |
| 549 /* Resource records */ | |
| 550 length += mdns_getlength_rrs(dns->answers); | |
| 551 length += mdns_getlength_rrs(dns->authority); | |
| 552 length += mdns_getlength_rrs(dns->additional); | |
| 553 | |
| 554 return length; | |
| 555 } | |
| 556 | |
| 557 /** | |
| 558 * | |
| 559 */ | |
| 8612 | 560 static int |
| 8634 | 561 mdns_put_name(char *data, unsigned int datalen, unsigned int offset, const char *name) |
| 8612 | 562 { |
| 563 int i = 0; | |
| 564 char *b, *c; | |
| 565 | |
| 566 b = (char *)name; | |
| 567 while ((c = strchr(b, '.'))) { | |
| 568 i += util_put8(&data[offset + i], c - b); /* Length of domain-name segment */ | |
| 569 memcpy(&data[offset + i], b, c - b); /* Domain-name segment */ | |
| 570 i += c - b; /* Increment the destination pointer */ | |
| 571 b = c + 1; | |
| 572 } | |
| 573 i += util_put8(&data[offset + i], strlen(b)); /* Length of domain-name segment */ | |
| 574 strcpy(&data[offset + i], b); /* Domain-name segment */ | |
| 575 i += strlen(b) + 1; /* Increment the destination pointer */ | |
| 576 | |
| 577 return i; | |
| 578 } | |
| 579 | |
| 8806 | 580 /** |
| 581 * | |
| 582 */ | |
| 8612 | 583 static int |
| 8806 | 584 mdns_put_q(char *data, unsigned int datalen, unsigned int offset, const Question *q) |
| 585 { | |
| 586 int i = 0; | |
| 587 | |
| 588 i += mdns_put_name(data, datalen, offset + i, q->name); /* QNAME */ | |
| 589 i += util_put16(&data[offset + i], q->type); /* QTYPE */ | |
| 590 i += util_put16(&data[offset + i], q->class); /* QCLASS */ | |
| 591 | |
| 592 return i; | |
| 593 } | |
| 594 | |
| 595 /** | |
| 596 * | |
| 597 */ | |
| 598 static int | |
| 599 mdns_put_rr(char *data, unsigned int datalen, unsigned int offset, const ResourceRecord *rr) | |
| 8612 | 600 { |
| 601 int i = 0; | |
| 602 | |
| 603 i += mdns_put_name(data, datalen, offset + i, rr->name); | |
| 604 i += util_put16(&data[offset + i], rr->type); | |
| 605 i += util_put16(&data[offset + i], rr->class); | |
| 606 i += util_put32(&data[offset + i], rr->ttl); | |
| 8631 | 607 i += util_put16(&data[offset + i], rr->rdlength); |
| 8612 | 608 |
| 609 switch (rr->type) { | |
| 8834 | 610 case RENDEZVOUS_RRTYPE_A: |
| 611 memcpy(&data[offset + i], rr->rdata, rr->rdlength); | |
| 612 i += rr->rdlength; | |
| 613 break; | |
| 614 | |
| 8636 | 615 case RENDEZVOUS_RRTYPE_NULL: |
| 616 memcpy(&data[offset + i], rr->rdata, rr->rdlength); | |
| 617 i += rr->rdlength; | |
| 618 break; | |
| 619 | |
| 8612 | 620 case RENDEZVOUS_RRTYPE_PTR: |
| 621 i += mdns_put_name(data, datalen, offset + i, (const char *)rr->rdata); | |
| 622 break; | |
| 8629 | 623 |
| 624 case RENDEZVOUS_RRTYPE_TXT: { | |
| 625 GSList *cur; | |
| 8631 | 626 ResourceRecordRDataTXTNode *node; |
| 8629 | 627 int mylength; |
| 628 | |
| 629 for (cur = (GSList *)rr->rdata; cur != NULL; cur = cur->next) { | |
| 8631 | 630 node = (ResourceRecordRDataTXTNode *)cur->data; |
| 8629 | 631 mylength = 1 + strlen(node->name); |
| 8806 | 632 if (node->value != NULL) |
| 8629 | 633 mylength += 1 + strlen(node->value); |
| 634 i += util_put8(&data[offset + i], mylength - 1); | |
| 635 memcpy(&data[offset + i], node->name, strlen(node->name)); | |
| 636 i += strlen(node->name); | |
| 8806 | 637 if (node->value != NULL) { |
| 8629 | 638 data[offset + i] = '='; |
| 639 i++; | |
| 640 memcpy(&data[offset + i], node->value, strlen(node->value)); | |
| 641 i += strlen(node->value); | |
| 642 } | |
| 643 } | |
| 644 } break; | |
| 8631 | 645 |
| 8834 | 646 case RENDEZVOUS_RRTYPE_AAAA: |
| 647 memcpy(&data[offset + i], rr->rdata, rr->rdlength); | |
| 648 i += rr->rdlength; | |
| 649 break; | |
| 650 | |
| 8631 | 651 case RENDEZVOUS_RRTYPE_SRV: { |
| 652 ResourceRecordRDataSRV *srv = rr->rdata; | |
| 653 i += util_put16(&data[offset + i], 0); | |
| 654 i += util_put16(&data[offset + i], 0); | |
| 655 i += util_put16(&data[offset + i], srv->port); | |
| 656 i += mdns_put_name(data, datalen, offset + i, srv->target); | |
| 657 } break; | |
| 8612 | 658 } |
| 659 | |
| 660 return i; | |
| 661 } | |
| 662 | |
| 663 int | |
| 664 mdns_send_dns(int fd, const DNSPacket *dns) | |
| 665 { | |
| 666 int ret; | |
| 667 unsigned int datalen; | |
| 668 unsigned char *data; | |
| 8634 | 669 unsigned int offset; |
| 8806 | 670 GSList *cur; |
| 8612 | 671 |
| 672 /* Calculate the length of the buffer we'll need to hold the DNS packet */ | |
| 8806 | 673 datalen = mdns_getlength_dns(dns); |
| 8612 | 674 |
| 675 /* Allocate a buffer */ | |
| 676 if (!(data = (unsigned char *)g_malloc(datalen))) { | |
| 677 return -ENOMEM; | |
| 678 } | |
| 679 | |
| 680 /* Construct the datagram */ | |
| 681 /* Header */ | |
| 682 offset = 0; | |
| 683 offset += util_put16(&data[offset], dns->header.id); /* ID */ | |
| 684 offset += util_put16(&data[offset], dns->header.flags); | |
| 685 offset += util_put16(&data[offset], dns->header.numquestions); /* QDCOUNT */ | |
| 686 offset += util_put16(&data[offset], dns->header.numanswers); /* ANCOUNT */ | |
| 687 offset += util_put16(&data[offset], dns->header.numauthority); /* NSCOUNT */ | |
| 688 offset += util_put16(&data[offset], dns->header.numadditional); /* ARCOUNT */ | |
| 689 | |
| 690 /* Questions */ | |
| 8806 | 691 for (cur = dns->questions; cur != NULL; cur = cur->next) |
| 692 offset += mdns_put_q(data, datalen, offset, cur->data); | |
| 8612 | 693 |
| 694 /* Resource records */ | |
| 8806 | 695 for (cur = dns->answers; cur != NULL; cur = cur->next) |
| 696 offset += mdns_put_rr(data, datalen, offset, cur->data); | |
| 697 for (cur = dns->authority; cur != NULL; cur = cur->next) | |
| 698 offset += mdns_put_rr(data, datalen, offset, cur->data); | |
| 699 for (cur = dns->additional; cur != NULL; cur = cur->next) | |
| 700 offset += mdns_put_rr(data, datalen, offset, cur->data); | |
| 8612 | 701 |
| 702 /* Send the datagram */ | |
| 8634 | 703 /* Offset can be shorter than datalen because of name compression */ |
| 704 ret = mdns_send_raw(fd, offset, data); | |
| 8612 | 705 |
| 706 g_free(data); | |
| 707 | |
| 708 return ret; | |
| 709 } | |
| 710 | |
| 8487 | 711 int |
| 8636 | 712 mdns_query(int fd, const char *domain, unsigned short type) |
| 8487 | 713 { |
| 8612 | 714 int ret; |
| 715 DNSPacket *dns; | |
| 8806 | 716 Question *q; |
| 8487 | 717 |
| 8806 | 718 if ((domain == NULL) || strlen(domain) > 255) { |
| 8487 | 719 return -EINVAL; |
| 720 } | |
| 721 | |
| 8612 | 722 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); |
| 723 dns->header.id = 0x0000; | |
| 724 dns->header.flags = 0x0000; | |
| 725 dns->header.numquestions = 0x0001; | |
| 726 dns->header.numanswers = 0x0000; | |
| 727 dns->header.numauthority = 0x0000; | |
| 728 dns->header.numadditional = 0x0000; | |
| 729 | |
| 8806 | 730 q = (Question *)g_malloc(sizeof(Question)); |
| 731 q->name = g_strdup(domain); | |
| 732 q->type = type; | |
| 733 q->class = 0x0001; | |
| 734 dns->questions = g_slist_append(NULL, q); | |
| 8612 | 735 |
| 736 dns->answers = NULL; | |
| 737 dns->authority = NULL; | |
| 738 dns->additional = NULL; | |
| 739 | |
| 8806 | 740 ret = mdns_send_dns(fd, dns); |
| 8612 | 741 |
| 742 mdns_free(dns); | |
| 743 | |
| 744 return ret; | |
| 745 } | |
| 746 | |
| 747 int | |
| 8738 | 748 mdns_send_rr(int fd, ResourceRecord *rr) |
| 8636 | 749 { |
| 750 int ret; | |
| 751 DNSPacket *dns; | |
| 752 | |
| 8806 | 753 g_return_val_if_fail(rr != NULL, -1); |
| 754 | |
| 8612 | 755 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); |
| 756 dns->header.id = 0x0000; | |
| 757 dns->header.flags = 0x8400; | |
| 758 dns->header.numquestions = 0x0000; | |
| 759 dns->header.numanswers = 0x0001; | |
| 760 dns->header.numauthority = 0x0000; | |
| 761 dns->header.numadditional = 0x0000; | |
| 762 dns->questions = NULL; | |
| 8806 | 763 dns->answers = g_slist_append(NULL, mdns_copy_rr(rr)); |
| 8612 | 764 dns->authority = NULL; |
| 765 dns->additional = NULL; | |
| 8487 | 766 |
| 8806 | 767 ret = mdns_send_dns(fd, dns); |
| 8738 | 768 |
| 8612 | 769 mdns_free(dns); |
| 8487 | 770 |
| 8612 | 771 return ret; |
| 772 } | |
| 8487 | 773 |
| 8629 | 774 int |
| 8834 | 775 mdns_advertise_a(int fd, const char *name, unsigned char *ip) |
| 776 { | |
| 777 int ret; | |
| 778 ResourceRecord *rr; | |
| 779 ResourceRecordRDataA *rdata; | |
| 780 int i; | |
| 781 | |
| 782 if ((name == NULL) || (strlen(name) > 255)) { | |
| 783 return -EINVAL; | |
| 784 } | |
| 785 | |
| 786 rdata = g_malloc(4); | |
| 787 for (i = 0; i < 4; i++) | |
| 788 util_put8(&rdata[i], ip[i]); | |
| 789 | |
| 790 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); | |
| 791 rr->name = g_strdup(name); | |
| 792 rr->type = RENDEZVOUS_RRTYPE_A; | |
| 793 rr->class = 0x0001; | |
| 794 rr->ttl = 0x000000f0; | |
| 795 rr->rdlength = 4; | |
| 796 rr->rdata = mdns_copy_rr_rdata(rr->type, rdata, rr->rdlength); | |
| 797 | |
| 798 ret = mdns_send_rr(fd, rr); | |
| 799 | |
| 800 mdns_free_rr(rr); | |
| 801 | |
| 802 return ret; | |
| 803 } | |
| 804 | |
| 805 int | |
| 8738 | 806 mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength) |
| 807 { | |
| 808 int ret; | |
| 809 ResourceRecord *rr; | |
| 810 | |
| 8806 | 811 if ((name == NULL) || (strlen(name) > 255)) { |
| 8738 | 812 return -EINVAL; |
| 813 } | |
| 814 | |
| 815 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); | |
| 816 rr->name = g_strdup(name); | |
| 817 rr->type = RENDEZVOUS_RRTYPE_NULL; | |
| 818 rr->class = 0x0001; | |
| 819 rr->ttl = 0x00001c20; | |
| 820 rr->rdlength = rdlength; | |
| 8806 | 821 rr->rdata = mdns_copy_rr_rdata(rr->type, rdata, rr->rdlength); |
| 8738 | 822 |
| 8806 | 823 ret = mdns_send_rr(fd, rr); |
| 8738 | 824 |
| 825 mdns_free_rr(rr); | |
| 826 | |
| 827 return ret; | |
| 828 } | |
| 829 | |
| 830 int | |
| 831 mdns_advertise_ptr(int fd, const char *name, const char *domain) | |
| 832 { | |
| 833 int ret; | |
| 834 ResourceRecord *rr; | |
| 835 | |
| 8806 | 836 if ((name == NULL) || (strlen(name) > 255) || (domain == NULL) || (strlen(domain) > 255)) { |
| 8738 | 837 return -EINVAL; |
| 838 } | |
| 839 | |
| 840 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); | |
| 841 rr->name = g_strdup(name); | |
| 842 rr->type = RENDEZVOUS_RRTYPE_PTR; | |
| 843 rr->class = 0x8001; | |
| 844 rr->ttl = 0x00001c20; | |
| 845 rr->rdata = (void *)g_strdup(domain); | |
| 8806 | 846 rr->rdlength = mdns_getlength_rr_rdata(rr->type, rr->rdata); |
| 8738 | 847 |
| 8806 | 848 ret = mdns_send_rr(fd, rr); |
| 8738 | 849 |
| 850 mdns_free_rr(rr); | |
| 851 | |
| 852 return ret; | |
| 853 } | |
| 854 | |
| 855 int | |
| 8629 | 856 mdns_advertise_txt(int fd, const char *name, const GSList *rdata) |
| 857 { | |
| 858 int ret; | |
| 8738 | 859 ResourceRecord *rr; |
| 8629 | 860 |
| 8806 | 861 if ((name == NULL) || (strlen(name) > 255)) { |
| 8629 | 862 return -EINVAL; |
| 863 } | |
| 864 | |
| 8738 | 865 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); |
| 866 rr->name = g_strdup(name); | |
| 867 rr->type = RENDEZVOUS_RRTYPE_TXT; | |
| 868 rr->class = 0x8001; | |
| 869 rr->ttl = 0x00001c20; | |
| 8806 | 870 rr->rdlength = mdns_getlength_rr_rdata(rr->type, rdata); |
| 871 rr->rdata = mdns_copy_rr_rdata(rr->type, rdata, rr->rdlength); | |
| 8629 | 872 |
| 8806 | 873 ret = mdns_send_rr(fd, rr); |
| 8631 | 874 |
| 8738 | 875 mdns_free_rr(rr); |
| 8631 | 876 |
| 877 return ret; | |
| 878 } | |
| 879 | |
| 880 int | |
| 8834 | 881 mdns_advertise_aaaa(int fd, const char *name, unsigned char *ip) |
| 882 { | |
| 883 int ret; | |
| 884 ResourceRecord *rr; | |
| 885 ResourceRecordRDataA *rdata; | |
| 886 int i; | |
| 887 | |
| 888 if ((name == NULL) || (strlen(name) > 255)) { | |
| 889 return -EINVAL; | |
| 890 } | |
| 891 | |
| 892 rdata = g_malloc(16); | |
| 893 for (i = 0; i < 16; i++) | |
| 894 util_put8(&rdata[i], ip[i]); | |
| 895 | |
| 896 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); | |
| 897 rr->name = g_strdup(name); | |
| 898 rr->type = RENDEZVOUS_RRTYPE_A; | |
| 899 rr->class = 0x0001; | |
| 900 rr->ttl = 0x000000f0; | |
| 901 rr->rdlength = 16; | |
| 902 rr->rdata = mdns_copy_rr_rdata(rr->type, rdata, rr->rdlength); | |
| 903 | |
| 904 ret = mdns_send_rr(fd, rr); | |
| 905 | |
| 906 mdns_free_rr(rr); | |
| 907 | |
| 908 return ret; | |
| 909 } | |
| 910 | |
| 911 int | |
| 8631 | 912 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target) |
| 913 { | |
| 914 int ret; | |
| 8738 | 915 ResourceRecord *rr; |
| 8631 | 916 ResourceRecordRDataSRV *rdata; |
| 917 | |
| 8806 | 918 if ((target == NULL) || (strlen(target) > 255)) { |
| 8631 | 919 return -EINVAL; |
| 920 } | |
| 921 | |
| 922 rdata = g_malloc(sizeof(ResourceRecordRDataSRV)); | |
| 923 rdata->port = port; | |
| 8634 | 924 rdata->target = g_strdup(target); |
| 8631 | 925 |
| 8738 | 926 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord)); |
| 927 rr->name = g_strdup(name); | |
| 928 rr->type = RENDEZVOUS_RRTYPE_SRV; | |
| 929 rr->class = 0x8001; | |
| 930 rr->ttl = 0x00001c20; | |
| 931 rr->rdata = rdata; | |
| 8806 | 932 rr->rdlength = mdns_getlength_rr_rdata(rr->type, rr->rdata); |
| 8631 | 933 |
| 8806 | 934 ret = mdns_send_rr(fd, rr); |
| 8631 | 935 |
| 8738 | 936 mdns_free_rr(rr); |
| 8629 | 937 |
| 938 return ret; | |
| 939 } | |
| 940 | |
| 8612 | 941 /***************************************/ |
| 942 /* Functions for parsing mDNS messages */ | |
| 943 /***************************************/ | |
| 8487 | 944 |
| 8806 | 945 /** |
| 8487 | 946 * Read in a domain name from the given buffer starting at the given |
| 947 * offset. This handles using domain name compression to jump around | |
| 948 * the data buffer, if needed. | |
| 949 * | |
| 950 * @return A null-terminated string representation of the domain name. | |
| 951 * This should be g_free'd when no longer needed. | |
| 952 */ | |
| 953 static gchar * | |
| 8634 | 954 mdns_read_name(const char *data, int datalen, unsigned int offset) |
| 8487 | 955 { |
| 956 GString *ret = g_string_new(""); | |
| 8634 | 957 unsigned char tmp, newoffset; |
| 8487 | 958 |
| 8634 | 959 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) { |
| 960 offset++; | |
| 8487 | 961 |
| 962 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */ | |
| 8634 | 963 if (offset + tmp > datalen) |
| 964 /* Attempt to read past end of data! Bailing! */ | |
| 965 return g_string_free(ret, TRUE); | |
| 966 if (*ret->str != '\0') | |
| 8487 | 967 g_string_append_c(ret, '.'); |
| 8634 | 968 g_string_append_len(ret, &data[offset], tmp); |
| 969 offset += tmp; | |
| 8487 | 970 |
| 971 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */ | |
| 972 /* Reserved for future use */ | |
| 973 | |
| 974 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */ | |
| 975 /* Reserved for future use */ | |
| 976 | |
| 977 } else { /* First two bits are 11 */ | |
| 978 /* Jump to another position in the data */ | |
| 8634 | 979 newoffset = util_get8(&data[offset]); |
| 980 if (newoffset >= offset) | |
| 981 /* Invalid pointer! Could lead to infinite recursion! Bailing! */ | |
| 8636 | 982 return g_string_free(ret, TRUE); |
| 8634 | 983 offset = newoffset; |
| 8487 | 984 } |
| 985 } | |
| 986 | |
| 8636 | 987 if (offset > datalen) |
| 988 return g_string_free(ret, TRUE); | |
| 989 | |
| 8487 | 990 return g_string_free(ret, FALSE); |
| 991 } | |
| 992 | |
| 8806 | 993 /** |
| 8487 | 994 * Determine how many bytes long a portion of the domain name is |
| 995 * at the given offset. This does NOT jump around the data array | |
| 996 * in the case of domain name compression. | |
| 997 * | |
| 998 * @return The length of the portion of the domain name. | |
| 999 */ | |
| 1000 static int | |
| 8634 | 1001 mdns_read_name_len(const char *data, unsigned int datalen, unsigned int offset) |
| 8487 | 1002 { |
| 8634 | 1003 int startoffset = offset; |
| 8487 | 1004 unsigned char tmp; |
| 1005 | |
| 8634 | 1006 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) { |
| 1007 offset++; | |
| 8487 | 1008 |
| 1009 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */ | |
| 8634 | 1010 if (offset + tmp > datalen) |
| 1011 /* Attempt to read past end of data! Bailing! */ | |
| 1012 return 0; | |
| 1013 offset += tmp; | |
| 8487 | 1014 |
| 1015 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */ | |
| 1016 /* Reserved for future use */ | |
| 1017 | |
| 1018 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */ | |
| 1019 /* Reserved for future use */ | |
| 1020 | |
| 1021 } else { /* First two bits are 11 */ | |
| 1022 /* End of this portion of the domain name */ | |
| 1023 break; | |
| 1024 | |
| 1025 } | |
| 1026 } | |
| 1027 | |
| 8634 | 1028 return offset - startoffset + 1; |
| 8487 | 1029 } |
| 1030 | |
| 8806 | 1031 /** |
| 8636 | 1032 * |
| 8487 | 1033 * |
| 1034 */ | |
| 1035 static Question * | |
| 8806 | 1036 mdns_read_q(const char *data, unsigned int datalen, int *offset) |
| 1037 { | |
| 1038 Question *q; | |
| 1039 | |
| 1040 q = (Question *)g_malloc0(sizeof(Question)); | |
| 1041 q->name = mdns_read_name(data, datalen, *offset); | |
| 1042 *offset += mdns_read_name_len(data, datalen, *offset); | |
| 1043 if (*offset + 4 > datalen) { | |
| 1044 mdns_free_q(q); | |
| 1045 return NULL; | |
| 1046 } | |
| 1047 | |
| 1048 q->type = util_get16(&data[*offset]); /* QTYPE */ | |
| 1049 *offset += 2; | |
| 1050 q->class = util_get16(&data[*offset]); /* QCLASS */ | |
| 1051 *offset += 2; | |
| 1052 if (*offset > datalen) { | |
| 1053 mdns_free_q(q); | |
| 1054 return NULL; | |
| 1055 } | |
| 1056 | |
| 1057 return q; | |
| 1058 } | |
| 1059 | |
| 1060 /** | |
| 1061 * | |
| 1062 * | |
| 1063 */ | |
| 1064 static GSList * | |
| 8634 | 1065 mdns_read_questions(int numquestions, const char *data, unsigned int datalen, int *offset) |
| 8487 | 1066 { |
| 8806 | 1067 GSList *ret = NULL; |
| 1068 Question *q; | |
| 8487 | 1069 int i; |
| 1070 | |
| 1071 for (i = 0; i < numquestions; i++) { | |
| 8806 | 1072 q = mdns_read_q(data, datalen, offset); |
| 1073 if (q == NULL) | |
| 8636 | 1074 break; |
| 8806 | 1075 ret = g_slist_append(ret, q); |
| 8636 | 1076 } |
| 1077 | |
| 1078 /* Malformed packet check */ | |
| 1079 if (i < numquestions) { | |
| 8806 | 1080 mdns_free_qs(ret); |
| 8636 | 1081 return NULL; |
| 8487 | 1082 } |
| 1083 | |
| 1084 return ret; | |
| 1085 } | |
| 1086 | |
| 8806 | 1087 /** |
| 8487 | 1088 * Read in a chunk of data, probably a buddy icon. |
| 1089 * | |
| 1090 */ | |
| 1091 static unsigned char * | |
| 8634 | 1092 mdns_read_rr_rdata_null(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength) |
| 8487 | 1093 { |
| 1094 unsigned char *ret = NULL; | |
| 1095 | |
| 1096 if (offset + rdlength > datalen) | |
| 1097 return NULL; | |
| 1098 | |
| 1099 ret = (unsigned char *)g_malloc(rdlength); | |
| 1100 memcpy(ret, &data[offset], rdlength); | |
| 1101 | |
| 1102 return ret; | |
| 1103 } | |
| 1104 | |
| 8806 | 1105 /** |
| 8636 | 1106 * Read in a compressed name. |
| 8487 | 1107 * |
| 1108 */ | |
| 1109 static char * | |
| 8634 | 1110 mdns_read_rr_rdata_ptr(const char *data, unsigned int datalen, unsigned int offset) |
| 8487 | 1111 { |
| 8636 | 1112 return mdns_read_name(data, datalen, offset); |
| 8487 | 1113 } |
| 1114 | |
| 8806 | 1115 ResourceRecordRDataTXTNode * |
| 1116 mdns_txt_find(const GSList *ret, const char *name) | |
| 1117 { | |
| 1118 ResourceRecordRDataTXTNode *node; | |
| 1119 GSList *cur; | |
| 1120 | |
| 1121 for (cur = (GSList *)ret; cur != NULL; cur = cur->next) { | |
| 1122 node = cur->data; | |
| 1123 if (!strcasecmp(node->name, name)) | |
| 1124 return node; | |
| 1125 } | |
| 1126 | |
| 1127 return NULL; | |
| 1128 } | |
| 1129 | |
| 1130 /** | |
| 1131 * | |
| 8487 | 1132 * |
| 1133 */ | |
| 8806 | 1134 GSList * |
| 1135 mdns_txt_add(GSList *ret, const char *name, const char *value, gboolean replace) | |
| 1136 { | |
| 1137 ResourceRecordRDataTXTNode *node; | |
| 1138 | |
| 1139 node = mdns_txt_find(ret, name); | |
| 1140 | |
| 1141 if (node == NULL) { | |
| 1142 /* Add a new node */ | |
| 1143 node = g_malloc(sizeof(ResourceRecordRDataTXTNode)); | |
| 1144 node->name = g_strdup(name); | |
| 1145 node->value = value != NULL ? g_strdup(value) : NULL; | |
| 1146 ret = g_slist_append(ret, node); | |
| 1147 } else { | |
| 1148 /* Replace the value of the old node, or do nothing */ | |
| 1149 if (replace) { | |
| 1150 g_free(node->value); | |
| 1151 node->value = value != NULL ? g_strdup(value) : NULL; | |
| 1152 } | |
| 1153 } | |
| 1154 | |
| 1155 return ret; | |
| 1156 } | |
| 1157 | |
| 1158 /** | |
| 1159 * Read in a list of name=value pairs as a GSList of | |
| 1160 * ResourceRecordRDataTXTNodes. | |
| 1161 * | |
| 1162 */ | |
| 1163 static GSList * | |
| 8634 | 1164 mdns_read_rr_rdata_txt(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength) |
| 8487 | 1165 { |
| 8806 | 1166 GSList *ret = NULL; |
| 8487 | 1167 int endoffset = offset + rdlength; |
| 1168 unsigned char tmp; | |
| 8806 | 1169 char buf[256], *name, *value; |
| 8487 | 1170 |
| 1171 while (offset < endoffset) { | |
| 1172 /* Read in the length of the next name/value pair */ | |
| 1173 tmp = util_get8(&data[offset]); | |
| 1174 offset++; | |
| 1175 | |
| 8636 | 1176 /* Malformed packet check */ |
| 8487 | 1177 if (offset + tmp > endoffset) |
| 1178 break; | |
| 1179 | |
| 1180 /* Read in the next name/value pair */ | |
| 1181 strncpy(buf, &data[offset], tmp); | |
| 1182 offset += tmp; | |
| 1183 | |
| 1184 if (buf[0] == '=') { | |
| 1185 /* Name/value pairs beginning with = are silently ignored */ | |
| 1186 continue; | |
| 1187 } | |
| 1188 | |
| 1189 /* The value is a substring of buf, starting just after the = */ | |
| 1190 buf[tmp] = '\0'; | |
| 1191 value = strchr(buf, '='); | |
| 1192 if (value != NULL) { | |
| 1193 value[0] = '\0'; | |
| 1194 value++; | |
| 1195 } | |
| 1196 | |
| 8806 | 1197 /* Make the name all lowercase */ |
| 1198 name = g_utf8_strdown(buf, -1); | |
| 1199 ret = mdns_txt_add(ret, name, value, FALSE); | |
| 1200 g_free(name); | |
| 8487 | 1201 } |
| 1202 | |
| 8636 | 1203 /* Malformed packet check */ |
| 1204 if ((offset > datalen) || (offset != endoffset)) { | |
| 8806 | 1205 mdns_free_rr_rdata(RENDEZVOUS_RRTYPE_TXT, ret); |
| 8636 | 1206 return NULL; |
| 1207 } | |
| 1208 | |
| 8487 | 1209 return ret; |
| 1210 } | |
| 1211 | |
| 8806 | 1212 /** |
| 8594 | 1213 * |
| 1214 * | |
| 1215 */ | |
| 8806 | 1216 static ResourceRecordRDataSRV * |
| 8634 | 1217 mdns_read_rr_rdata_srv(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength) |
| 8594 | 1218 { |
| 8806 | 1219 ResourceRecordRDataSRV *ret = NULL; |
| 8594 | 1220 int endoffset = offset + rdlength; |
| 1221 | |
| 8636 | 1222 /* Malformed packet check */ |
| 8594 | 1223 if (offset + 7 > endoffset) |
| 1224 return NULL; | |
| 1225 | |
| 8806 | 1226 ret = g_malloc(sizeof(ResourceRecordRDataSRV)); |
| 8594 | 1227 |
| 1228 /* Read in the priority */ | |
| 1229 ret->priority = util_get16(&data[offset]); | |
| 1230 offset += 2; | |
| 1231 | |
| 1232 /* Read in the weight */ | |
| 1233 ret->weight = util_get16(&data[offset]); | |
| 1234 offset += 2; | |
| 1235 | |
| 1236 /* Read in the port */ | |
| 1237 ret->port = util_get16(&data[offset]); | |
| 1238 offset += 2; | |
| 1239 | |
| 1240 /* Read in the target name */ | |
| 1241 /* | |
| 1242 * XXX - RFC2782 says it's not supposed to be an alias... | |
| 1243 * but it was in the packet capture I looked at from iChat. | |
| 1244 */ | |
| 1245 ret->target = mdns_read_name(data, datalen, offset); | |
| 8636 | 1246 offset += mdns_read_name_len(data, datalen, offset); |
| 1247 | |
| 1248 /* Malformed packet check */ | |
| 1249 if ((offset > endoffset) || (ret->target == NULL)) { | |
| 1250 g_free(ret->target); | |
| 1251 g_free(ret); | |
| 1252 return NULL; | |
| 1253 } | |
| 8594 | 1254 |
| 1255 return ret; | |
| 1256 } | |
| 1257 | |
| 8806 | 1258 /** |
| 8636 | 1259 * |
| 8487 | 1260 * |
| 1261 */ | |
| 1262 static ResourceRecord * | |
| 8806 | 1263 mdns_read_rr(const char *data, unsigned int datalen, int *offset) |
| 8487 | 1264 { |
| 8806 | 1265 ResourceRecord *rr; |
| 1266 | |
| 1267 rr = (ResourceRecord *)g_malloc0(sizeof(ResourceRecord)); | |
| 1268 | |
| 1269 /* NAME */ | |
| 1270 rr->name = mdns_read_name(data, datalen, *offset); | |
| 1271 *offset += mdns_read_name_len(data, datalen, *offset); | |
| 1272 | |
| 1273 /* Malformed packet check */ | |
| 1274 if (*offset + 10 > datalen) { | |
| 1275 mdns_free_rr(rr); | |
| 1276 return NULL; | |
| 1277 } | |
| 1278 | |
| 1279 /* TYPE */ | |
| 1280 rr->type = util_get16(&data[*offset]); | |
| 1281 *offset += 2; | |
| 1282 | |
| 1283 /* CLASS */ | |
| 1284 rr->class = util_get16(&data[*offset]); | |
| 1285 *offset += 2; | |
| 1286 | |
| 1287 /* TTL */ | |
| 1288 rr->ttl = util_get32(&data[*offset]); | |
| 1289 *offset += 4; | |
| 1290 | |
| 1291 /* RDLENGTH */ | |
| 1292 rr->rdlength = util_get16(&data[*offset]); | |
| 1293 *offset += 2; | |
| 1294 | |
| 1295 /* RDATA */ | |
| 8834 | 1296 if (rr->type == RENDEZVOUS_RRTYPE_A) { |
| 1297 rr->rdata = mdns_read_rr_rdata_null(data, datalen, *offset, rr->rdlength); | |
| 1298 if (rr->rdata == NULL) { | |
| 1299 mdns_free_rr(rr); | |
| 1300 return NULL; | |
| 1301 } | |
| 1302 | |
| 1303 } else if (rr->type == RENDEZVOUS_RRTYPE_NULL) { | |
| 8806 | 1304 rr->rdata = mdns_read_rr_rdata_null(data, datalen, *offset, rr->rdlength); |
| 1305 if (rr->rdata == NULL) { | |
| 1306 mdns_free_rr(rr); | |
| 1307 return NULL; | |
| 1308 } | |
| 1309 | |
| 1310 } else if (rr->type == RENDEZVOUS_RRTYPE_PTR) { | |
| 1311 rr->rdata = mdns_read_rr_rdata_ptr(data, datalen, *offset); | |
| 1312 if (rr->rdata == NULL) { | |
| 1313 mdns_free_rr(rr); | |
| 1314 return NULL; | |
| 1315 } | |
| 1316 | |
| 1317 } else if (rr->type == RENDEZVOUS_RRTYPE_TXT) { | |
| 1318 rr->rdata = mdns_read_rr_rdata_txt(data, datalen, *offset, rr->rdlength); | |
| 1319 if (rr->rdata == NULL) { | |
| 1320 mdns_free_rr(rr); | |
| 1321 return NULL; | |
| 1322 } | |
| 1323 | |
| 8834 | 1324 } else if (rr->type == RENDEZVOUS_RRTYPE_AAAA) { |
| 1325 rr->rdata = mdns_read_rr_rdata_null(data, datalen, *offset, rr->rdlength); | |
| 1326 if (rr->rdata == NULL) { | |
| 1327 mdns_free_rr(rr); | |
| 1328 return NULL; | |
| 1329 } | |
| 1330 | |
| 8806 | 1331 } else if (rr->type == RENDEZVOUS_RRTYPE_SRV) { |
| 1332 rr->rdata = mdns_read_rr_rdata_srv(data, datalen, *offset, rr->rdlength); | |
| 1333 if (rr->rdata == NULL) { | |
| 1334 mdns_free_rr(rr); | |
| 1335 return NULL; | |
| 1336 } | |
| 1337 | |
| 1338 } | |
| 1339 | |
| 1340 /* Malformed packet check */ | |
| 1341 *offset += rr->rdlength; | |
| 1342 if (*offset > datalen) { | |
| 1343 mdns_free_rr(rr); | |
| 1344 return NULL; | |
| 1345 } | |
| 1346 | |
| 1347 return rr; | |
| 1348 } | |
| 1349 | |
| 1350 static GSList * | |
| 1351 mdns_read_rrs(int numrecords, const char *data, unsigned int datalen, int *offset) | |
| 1352 { | |
| 1353 GSList *ret = NULL; | |
| 1354 ResourceRecord *rr; | |
| 8487 | 1355 int i; |
| 1356 | |
| 1357 for (i = 0; i < numrecords; i++) { | |
| 8806 | 1358 rr = mdns_read_rr(data, datalen, offset); |
| 1359 if (rr == NULL) | |
| 8636 | 1360 break; |
| 8806 | 1361 ret = g_slist_append(ret, rr); |
| 8636 | 1362 } |
| 8594 | 1363 |
| 8636 | 1364 /* Malformed packet check */ |
| 1365 if (i < numrecords) { | |
| 8806 | 1366 mdns_free_rrs(ret); |
| 8636 | 1367 return NULL; |
| 8487 | 1368 } |
| 1369 | |
| 1370 return ret; | |
| 1371 } | |
| 1372 | |
| 8806 | 1373 /** |
| 8679 | 1374 * If invalid data is encountered at any point when parsing data |
|
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8679
diff
changeset
|
1375 * then the entire packet is discarded and NULL is returned. |
| 8487 | 1376 */ |
| 1377 DNSPacket * | |
| 1378 mdns_read(int fd) | |
| 1379 { | |
| 8806 | 1380 DNSPacket *dns = NULL; |
| 8636 | 1381 int offset; /* Current position in datagram */ |
| 8612 | 1382 /* XXX - Find out what to use as a maximum incoming UDP packet size */ |
| 1383 /* char data[512]; */ | |
| 8487 | 1384 char data[10096]; |
| 8634 | 1385 unsigned int datalen; |
| 8487 | 1386 struct sockaddr_in addr; |
| 1387 socklen_t addrlen; | |
| 8806 | 1388 GSList *cur; |
| 8487 | 1389 |
| 1390 /* Read in an mDNS packet */ | |
| 1391 addrlen = sizeof(struct sockaddr_in); | |
| 1392 if ((datalen = recvfrom(fd, data, sizeof(data), 0, (struct sockaddr *)&addr, &addrlen)) == -1) { | |
| 1393 gaim_debug_error("mdns", "Error reading packet: %d\n", errno); | |
| 1394 return NULL; | |
| 1395 } | |
| 1396 | |
| 8806 | 1397 dns = (DNSPacket *)g_malloc0(sizeof(DNSPacket)); |
| 8487 | 1398 |
| 1399 /* Parse the incoming packet, starting from 0 */ | |
| 8636 | 1400 offset = 0; |
| 1401 | |
| 1402 if (offset + 12 > datalen) { | |
| 8806 | 1403 g_free(dns); |
| 8636 | 1404 return NULL; |
| 1405 } | |
| 8487 | 1406 |
| 1407 /* The header section */ | |
| 8806 | 1408 dns->header.id = util_get16(&data[offset]); /* ID */ |
| 8636 | 1409 offset += 2; |
| 8487 | 1410 |
| 1411 /* For the flags, some bits must be 0 and some must be 1, the rest are ignored */ | |
| 8806 | 1412 dns->header.flags = util_get16(&data[offset]); /* Flags (QR, OPCODE, AA, TC, RD, RA, Z, AD, CD, and RCODE */ |
| 8636 | 1413 offset += 2; |
| 8806 | 1414 if ((dns->header.flags & 0x7800) != 0) { |
| 8487 | 1415 /* OPCODE should be all 0's */ |
| 8806 | 1416 g_free(dns); |
| 8487 | 1417 return NULL; |
| 1418 } | |
| 1419 | |
| 1420 /* Read in the number of other things in the packet */ | |
| 8806 | 1421 dns->header.numquestions = util_get16(&data[offset]); |
| 8636 | 1422 offset += 2; |
| 8806 | 1423 dns->header.numanswers = util_get16(&data[offset]); |
| 8636 | 1424 offset += 2; |
| 8806 | 1425 dns->header.numauthority = util_get16(&data[offset]); |
| 8636 | 1426 offset += 2; |
| 8806 | 1427 dns->header.numadditional = util_get16(&data[offset]); |
| 8636 | 1428 offset += 2; |
| 8487 | 1429 |
| 1430 /* Read in all the questions */ | |
| 8806 | 1431 dns->questions = mdns_read_questions(dns->header.numquestions, data, datalen, &offset); |
| 8487 | 1432 |
| 1433 /* Read in all resource records */ | |
| 8806 | 1434 dns->answers = mdns_read_rrs(dns->header.numanswers, data, datalen, &offset); |
| 8487 | 1435 |
| 1436 /* Read in all authority records */ | |
| 8806 | 1437 dns->authority = mdns_read_rrs(dns->header.numauthority, data, datalen, &offset); |
| 8487 | 1438 |
| 1439 /* Read in all additional records */ | |
| 8806 | 1440 dns->additional = mdns_read_rrs(dns->header.numadditional, data, datalen, &offset); |
| 8636 | 1441 |
| 1442 /* Malformed packet check */ | |
| 8806 | 1443 if ((dns->header.numquestions > 0 && dns->questions == NULL) || |
| 1444 (dns->header.numanswers > 0 && dns->answers == NULL) || | |
| 1445 (dns->header.numauthority > 0 && dns->authority == NULL) || | |
| 1446 (dns->header.numadditional > 0 && dns->additional == NULL)) { | |
| 8636 | 1447 gaim_debug_error("mdns", "Received an invalid DNS packet.\n"); |
| 1448 return NULL; | |
| 1449 } | |
| 8487 | 1450 |
| 1451 /* We should be at the end of the packet */ | |
| 8636 | 1452 if (offset != datalen) { |
| 1453 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", offset, datalen); | |
| 8806 | 1454 g_free(dns); |
| 8487 | 1455 return NULL; |
| 1456 } | |
| 1457 | |
| 8806 | 1458 for (cur = dns->answers; cur != NULL; cur = cur->next) |
| 1459 mdns_cache_add((ResourceRecord *)cur->data); | |
| 1460 for (cur = dns->authority; cur != NULL; cur = cur->next) | |
| 1461 mdns_cache_add((ResourceRecord *)cur->data); | |
| 1462 for (cur = dns->additional; cur != NULL; cur = cur->next) | |
| 1463 mdns_cache_add((ResourceRecord *)cur->data); | |
| 1464 for (cur = dns->questions; cur != NULL; cur = cur->next) | |
| 1465 mdns_cache_respond(fd, (Question *)cur->data); | |
| 8738 | 1466 |
| 8806 | 1467 return dns; |
| 8487 | 1468 } |
