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