src/protocols/rendezvous/mdns.c

changeset 8738
0c6d12b1a014
parent 8735
01248ea222d3
child 8806
2dfc3541367e
equal deleted inserted replaced
8737:b9f8de95d6bb 8738:0c6d12b1a014
29 * me a doughnut. thx k bye. 29 * me a doughnut. thx k bye.
30 */ 30 */
31 31
32 /* 32 /*
33 * XXX - This entire file could use another pair of eyes to audit for 33 * XXX - This entire file could use another pair of eyes to audit for
34 * any possible buffer overflow exploits. 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.
35 */ 37 */
36 38
37 #include "internal.h" 39 #include "internal.h"
38 #include "debug.h" 40 #include "debug.h"
39 41
40 #include "mdns.h" 42 #include "mdns.h"
43 #include "mdns_cache.h"
41 #include "util.h" 44 #include "util.h"
42 45
43 /******************************************/ 46 /******************************************/
44 /* Functions for freeing a DNS structure */ 47 /* Functions for freeing a DNS structure */
45 /******************************************/ 48 /******************************************/
80 } 83 }
81 84
82 /** 85 /**
83 * Free a given resource record. 86 * Free a given resource record.
84 */ 87 */
85 static void 88 void
86 mdns_free_rr(ResourceRecord *rr) 89 mdns_free_rr(ResourceRecord *rr)
87 { 90 {
88 g_free(rr->name); 91 g_free(rr->name);
89 mdns_free_rr_rdata(rr->type, rr->rdata); 92 mdns_free_rr_rdata(rr->type, rr->rdata);
90 } 93 }
427 430
428 return ret; 431 return ret;
429 } 432 }
430 433
431 int 434 int
432 mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength) 435 mdns_send_rr(int fd, ResourceRecord *rr)
433 { 436 {
434 int ret; 437 int ret;
435 DNSPacket *dns; 438 DNSPacket *dns;
436
437 if ((strlen(name) > 255)) {
438 return -EINVAL;
439 }
440 439
441 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); 440 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
442 dns->header.id = 0x0000; 441 dns->header.id = 0x0000;
443 dns->header.flags = 0x8400; 442 dns->header.flags = 0x8400;
444 dns->header.numquestions = 0x0000; 443 dns->header.numquestions = 0x0000;
445 dns->header.numanswers = 0x0001; 444 dns->header.numanswers = 0x0001;
446 dns->header.numauthority = 0x0000; 445 dns->header.numauthority = 0x0000;
447 dns->header.numadditional = 0x0000; 446 dns->header.numadditional = 0x0000;
448 dns->questions = NULL; 447 dns->questions = NULL;
449 448 dns->answers = rr;
450 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
451 dns->answers[0].name = g_strdup(name);
452 dns->answers[0].type = RENDEZVOUS_RRTYPE_NULL;
453 dns->answers[0].class = 0x0001;
454 dns->answers[0].ttl = 0x00001c20;
455 dns->answers[0].rdlength = rdlength;
456 dns->answers[0].rdata = (void *)rdata;
457
458 dns->authority = NULL; 449 dns->authority = NULL;
459 dns->additional = NULL; 450 dns->additional = NULL;
460 451
461 mdns_send_dns(fd, dns); 452 mdns_send_dns(fd, dns);
462 453
454 /* The rr should be freed by the caller of this function */
455 dns->header.numanswers = 0x0000;
456 dns->answers = NULL;
457
458 mdns_free(dns);
459
460 return ret;
461 }
462
463 int
464 mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength)
465 {
466 int ret;
467 ResourceRecord *rr;
468
469 if ((strlen(name) > 255)) {
470 return -EINVAL;
471 }
472
473 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
474 rr->name = g_strdup(name);
475 rr->type = RENDEZVOUS_RRTYPE_NULL;
476 rr->class = 0x0001;
477 rr->ttl = 0x00001c20;
478 rr->rdlength = rdlength;
479 rr->rdata = (void *)rdata;
480
481 mdns_send_rr(fd, rr);
482
463 /* The rdata should be freed by the caller of this function */ 483 /* The rdata should be freed by the caller of this function */
464 dns->answers[0].rdata = NULL; 484 rr->rdata = NULL;
465 485
466 mdns_free(dns); 486 mdns_free_rr(rr);
467 487
468 return ret; 488 return ret;
469 } 489 }
470 490
471 int 491 int
472 mdns_advertise_ptr(int fd, const char *name, const char *domain) 492 mdns_advertise_ptr(int fd, const char *name, const char *domain)
473 { 493 {
474 int ret; 494 int ret;
475 DNSPacket *dns; 495 ResourceRecord *rr;
476 496
477 if ((strlen(name) > 255) || (strlen(domain) > 255)) { 497 if ((strlen(name) > 255) || (strlen(domain) > 255)) {
478 return -EINVAL; 498 return -EINVAL;
479 } 499 }
480 500
481 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); 501 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
482 dns->header.id = 0x0000; 502 rr->name = g_strdup(name);
483 dns->header.flags = 0x8400; 503 rr->type = RENDEZVOUS_RRTYPE_PTR;
484 dns->header.numquestions = 0x0000; 504 rr->class = 0x8001;
485 dns->header.numanswers = 0x0001; 505 rr->ttl = 0x00001c20;
486 dns->header.numauthority = 0x0000; 506 rr->rdata = (void *)g_strdup(domain);
487 dns->header.numadditional = 0x0000; 507 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
488 dns->questions = NULL; 508
489 509 mdns_send_rr(fd, rr);
490 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord)); 510
491 dns->answers[0].name = g_strdup(name); 511 mdns_free_rr(rr);
492 dns->answers[0].type = RENDEZVOUS_RRTYPE_PTR;
493 dns->answers[0].class = 0x8001;
494 dns->answers[0].ttl = 0x00001c20;
495 dns->answers[0].rdata = (void *)g_strdup(domain);
496 dns->answers[0].rdlength = mdns_getlength_RR_rdata(dns->answers[0].type, dns->answers[0].rdata);
497
498 dns->authority = NULL;
499 dns->additional = NULL;
500
501 mdns_send_dns(fd, dns);
502
503 mdns_free(dns);
504 512
505 return ret; 513 return ret;
506 } 514 }
507 515
508 int 516 int
509 mdns_advertise_txt(int fd, const char *name, const GSList *rdata) 517 mdns_advertise_txt(int fd, const char *name, const GSList *rdata)
510 { 518 {
511 int ret; 519 int ret;
512 DNSPacket *dns; 520 ResourceRecord *rr;
513 521
514 if ((strlen(name) > 255)) { 522 if ((strlen(name) > 255)) {
515 return -EINVAL; 523 return -EINVAL;
516 } 524 }
517 525
518 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); 526 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
519 dns->header.id = 0x0000; 527 rr->name = g_strdup(name);
520 dns->header.flags = 0x8400; 528 rr->type = RENDEZVOUS_RRTYPE_TXT;
521 dns->header.numquestions = 0x0000; 529 rr->class = 0x8001;
522 dns->header.numanswers = 0x0001; 530 rr->ttl = 0x00001c20;
523 dns->header.numauthority = 0x0000; 531 rr->rdata = (void *)rdata;
524 dns->header.numadditional = 0x0000; 532 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
525 dns->questions = NULL; 533
526 534 mdns_send_rr(fd, rr);
527 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
528 dns->answers[0].name = g_strdup(name);
529 dns->answers[0].type = RENDEZVOUS_RRTYPE_TXT;
530 dns->answers[0].class = 0x8001;
531 dns->answers[0].ttl = 0x00001c20;
532 dns->answers[0].rdata = (void *)rdata;
533 dns->answers[0].rdlength = mdns_getlength_RR_rdata(dns->answers[0].type, dns->answers[0].rdata);
534
535 dns->authority = NULL;
536 dns->additional = NULL;
537
538 mdns_send_dns(fd, dns);
539 535
540 /* The rdata should be freed by the caller of this function */ 536 /* The rdata should be freed by the caller of this function */
541 dns->answers[0].rdata = NULL; 537 rr->rdata = NULL;
542 538
543 mdns_free(dns); 539 mdns_free_rr(rr);
544 540
545 return ret; 541 return ret;
546 } 542 }
547 543
548 int 544 int
549 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target) 545 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target)
550 { 546 {
551 int ret; 547 int ret;
552 DNSPacket *dns; 548 ResourceRecord *rr;
553 ResourceRecordRDataSRV *rdata; 549 ResourceRecordRDataSRV *rdata;
554 550
555 if ((strlen(target) > 255)) { 551 if ((strlen(target) > 255)) {
556 return -EINVAL; 552 return -EINVAL;
557 } 553 }
558 554
559 rdata = g_malloc(sizeof(ResourceRecordRDataSRV)); 555 rdata = g_malloc(sizeof(ResourceRecordRDataSRV));
560 rdata->port = port; 556 rdata->port = port;
561 rdata->target = g_strdup(target); 557 rdata->target = g_strdup(target);
562 558
563 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket)); 559 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
564 dns->header.id = 0x0000; 560 rr->name = g_strdup(name);
565 dns->header.flags = 0x8400; 561 rr->type = RENDEZVOUS_RRTYPE_SRV;
566 dns->header.numquestions = 0x0000; 562 rr->class = 0x8001;
567 dns->header.numanswers = 0x0000; 563 rr->ttl = 0x00001c20;
568 dns->header.numauthority = 0x0001; 564 rr->rdata = rdata;
569 dns->header.numadditional = 0x0000; 565 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
570 dns->questions = NULL; 566
571 dns->answers = NULL; 567 mdns_send_rr(fd, rr);
572 568
573 dns->authority = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord)); 569 mdns_free_rr(rr);
574 dns->authority[0].name = g_strdup(name);
575 dns->authority[0].type = RENDEZVOUS_RRTYPE_SRV;
576 dns->authority[0].class = 0x8001;
577 dns->authority[0].ttl = 0x00001c20;
578 dns->authority[0].rdata = rdata;
579 dns->authority[0].rdlength = mdns_getlength_RR_rdata(dns->authority[0].type, dns->authority[0].rdata);
580
581 dns->additional = NULL;
582
583 mdns_send_dns(fd, dns);
584
585 mdns_free(dns);
586 570
587 return ret; 571 return ret;
588 } 572 }
589 573
590 /***************************************/ 574 /***************************************/
926 */ 910 */
927 DNSPacket * 911 DNSPacket *
928 mdns_read(int fd) 912 mdns_read(int fd)
929 { 913 {
930 DNSPacket *ret = NULL; 914 DNSPacket *ret = NULL;
915 int i;
931 int offset; /* Current position in datagram */ 916 int offset; /* Current position in datagram */
932 /* XXX - Find out what to use as a maximum incoming UDP packet size */ 917 /* XXX - Find out what to use as a maximum incoming UDP packet size */
933 /* char data[512]; */ 918 /* char data[512]; */
934 char data[10096]; 919 char data[10096];
935 unsigned int datalen; 920 unsigned int datalen;
1007 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", offset, datalen); 992 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", offset, datalen);
1008 g_free(ret); 993 g_free(ret);
1009 return NULL; 994 return NULL;
1010 } 995 }
1011 996
1012 return ret; 997 #if 0
1013 } 998 for (i = 0; i < ret->header.numanswers; i++)
999 mdns_cache_add(&ret->answers[i]);
1000 for (i = 0; i < ret->header.numauthority; i++)
1001 mdns_cache_add(&ret->authority[i]);
1002 for (i = 0; i < ret->header.numadditional; i++)
1003 mdns_cache_add(&ret->additional[i]);
1004 for (i = 0; i < ret->header.numquestions; i++)
1005 mdns_cache_respond(fd, &ret->questions[i]);
1006 #endif
1007
1008 return ret;
1009 }

mercurial