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