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