aboutsummaryrefslogtreecommitdiff
path: root/src/dns
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-01-05 12:29:26 +0000
committerChristian Grothoff <christian@grothoff.org>2012-01-05 12:29:26 +0000
commit1f971ba86a238c1046587889efd92f62cc9afb9f (patch)
treede2fa4b3fff6d5b040ed90cc4bf8f74ccad39ece /src/dns
parenta6790f928fdc3d0fff02fa28e5ad6c4561a87ecf (diff)
downloadgnunet-1f971ba86a238c1046587889efd92f62cc9afb9f.tar.gz
gnunet-1f971ba86a238c1046587889efd92f62cc9afb9f.zip
-finishing dns packet serialization -- albeit untested
Diffstat (limited to 'src/dns')
-rw-r--r--src/dns/dnsparser.c54
1 files changed, 47 insertions, 7 deletions
diff --git a/src/dns/dnsparser.c b/src/dns/dnsparser.c
index 10a9d6d9a..4149d102f 100644
--- a/src/dns/dnsparser.c
+++ b/src/dns/dnsparser.c
@@ -466,6 +466,7 @@ GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
466 * @param dst where to write the name 466 * @param dst where to write the name
467 * @param dst_len number of bytes in dst 467 * @param dst_len number of bytes in dst
468 * @param off pointer to offset where to write the name (increment by bytes used) 468 * @param off pointer to offset where to write the name (increment by bytes used)
469 * must not be changed if there is an error
469 * @param name name to write 470 * @param name name to write
470 * @return GNUNET_SYSERR if 'name' is invalid 471 * @return GNUNET_SYSERR if 'name' is invalid
471 * GNUNET_NO if 'name' did not fit 472 * GNUNET_NO if 'name' did not fit
@@ -515,6 +516,7 @@ add_name (char *dst,
515 * @param dst where to write the query 516 * @param dst where to write the query
516 * @param dst_len number of bytes in dst 517 * @param dst_len number of bytes in dst
517 * @param off pointer to offset where to write the query (increment by bytes used) 518 * @param off pointer to offset where to write the query (increment by bytes used)
519 * must not be changed if there is an error
518 * @param query query to write 520 * @param query query to write
519 * @return GNUNET_SYSERR if 'query' is invalid 521 * @return GNUNET_SYSERR if 'query' is invalid
520 * GNUNET_NO if 'query' did not fit 522 * GNUNET_NO if 'query' did not fit
@@ -545,7 +547,8 @@ add_query (char *dst,
545 * 547 *
546 * @param dst where to write the mx record 548 * @param dst where to write the mx record
547 * @param dst_len number of bytes in dst 549 * @param dst_len number of bytes in dst
548 * @param off pointer to offset where to write the mx information (increment by bytes used) 550 * @param off pointer to offset where to write the mx information (increment by bytes used);
551 * can also change if there was an error
549 * @param mx mx information to write 552 * @param mx mx information to write
550 * @return GNUNET_SYSERR if 'mx' is invalid 553 * @return GNUNET_SYSERR if 'mx' is invalid
551 * GNUNET_NO if 'mx' did not fit 554 * GNUNET_NO if 'mx' did not fit
@@ -557,7 +560,14 @@ add_mx (char *dst,
557 size_t *off, 560 size_t *off,
558 const struct GNUNET_DNSPARSER_MxRecord *mx) 561 const struct GNUNET_DNSPARSER_MxRecord *mx)
559{ 562{
560 return GNUNET_SYSERR; // not implemented 563 uint16_t mxpref;
564
565 if (*off + sizeof (uint16_t) > dst_len)
566 return GNUNET_NO;
567 mxpref = htons (mx->preference);
568 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
569 (*off) += sizeof (mxpref);
570 return add_name (dst, dst_len, off, mx->mxhost);
561} 571}
562 572
563 573
@@ -567,6 +577,7 @@ add_mx (char *dst,
567 * @param dst where to write the SOA record 577 * @param dst where to write the SOA record
568 * @param dst_len number of bytes in dst 578 * @param dst_len number of bytes in dst
569 * @param off pointer to offset where to write the SOA information (increment by bytes used) 579 * @param off pointer to offset where to write the SOA information (increment by bytes used)
580 * can also change if there was an error
570 * @param soa SOA information to write 581 * @param soa SOA information to write
571 * @return GNUNET_SYSERR if 'soa' is invalid 582 * @return GNUNET_SYSERR if 'soa' is invalid
572 * GNUNET_NO if 'soa' did not fit 583 * GNUNET_NO if 'soa' did not fit
@@ -578,7 +589,28 @@ add_soa (char *dst,
578 size_t *off, 589 size_t *off,
579 const struct GNUNET_DNSPARSER_SoaRecord *soa) 590 const struct GNUNET_DNSPARSER_SoaRecord *soa)
580{ 591{
581 return GNUNET_SYSERR; // not implemented 592 struct soa_data sd;
593 int ret;
594
595 if ( (GNUNET_OK != (ret = add_name (dst,
596 dst_len,
597 off,
598 soa->mname))) ||
599 (GNUNET_OK != (ret = add_name (dst,
600 dst_len,
601 off,
602 soa->rname)) ) )
603 return ret;
604 if (*off + sizeof (soa) > dst_len)
605 return GNUNET_NO;
606 sd.serial = htonl (soa->serial);
607 sd.refresh = htonl (soa->refresh);
608 sd.retry = htonl (soa->retry);
609 sd.expire = htonl (soa->expire);
610 sd.minimum = htonl (soa->minimum_ttl);
611 memcpy (&dst[*off], &sd, sizeof (sd));
612 (*off) += sizeof (sd);
613 return GNUNET_OK;
582} 614}
583 615
584 616
@@ -588,6 +620,7 @@ add_soa (char *dst,
588 * @param dst where to write the query 620 * @param dst where to write the query
589 * @param dst_len number of bytes in dst 621 * @param dst_len number of bytes in dst
590 * @param off pointer to offset where to write the query (increment by bytes used) 622 * @param off pointer to offset where to write the query (increment by bytes used)
623 * must not be changed if there is an error
591 * @param record record to write 624 * @param record record to write
592 * @return GNUNET_SYSERR if 'record' is invalid 625 * @return GNUNET_SYSERR if 'record' is invalid
593 * GNUNET_NO if 'record' did not fit 626 * GNUNET_NO if 'record' did not fit
@@ -632,8 +665,15 @@ add_record (char *dst,
632 } 665 }
633 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len); 666 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
634 pos += record->data.raw.data_len; 667 pos += record->data.raw.data_len;
668 ret = GNUNET_OK;
635 break; 669 break;
636 } 670 }
671 if (ret != GNUNET_OK)
672 {
673 *off = start;
674 return GNUNET_NO;
675 }
676
637 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX) 677 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
638 { 678 {
639 /* record data too long */ 679 /* record data too long */
@@ -650,7 +690,6 @@ add_record (char *dst,
650} 690}
651 691
652 692
653
654/** 693/**
655 * Given a DNS packet, generate the corresponding UDP payload. 694 * Given a DNS packet, generate the corresponding UDP payload.
656 * Note that we do not attempt to pack the strings with pointers 695 * Note that we do not attempt to pack the strings with pointers
@@ -689,6 +728,7 @@ GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
689 dns.answer_rcount = htons (p->num_answers); 728 dns.answer_rcount = htons (p->num_answers);
690 dns.authority_rcount = htons (p->num_authority_records); 729 dns.authority_rcount = htons (p->num_authority_records);
691 dns.additional_rcount = htons (p->num_additional_records); 730 dns.additional_rcount = htons (p->num_additional_records);
731
692 off = sizeof (struct dns_header); 732 off = sizeof (struct dns_header);
693 trc = GNUNET_NO; 733 trc = GNUNET_NO;
694 for (i=0;i<p->num_queries;i++) 734 for (i=0;i<p->num_queries;i++)
@@ -739,11 +779,11 @@ GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
739 break; 779 break;
740 } 780 }
741 } 781 }
742 if (GNUNET_YES == trc)
743 dns.flags.message_truncated = 1;
744
745 782
783 if (GNUNET_YES == trc)
784 dns.flags.message_truncated = 1;
746 memcpy (tmp, &dns, sizeof (struct dns_header)); 785 memcpy (tmp, &dns, sizeof (struct dns_header));
786
747 *buf = GNUNET_malloc (off); 787 *buf = GNUNET_malloc (off);
748 *buf_length = off; 788 *buf_length = off;
749 memcpy (*buf, tmp, off); 789 memcpy (*buf, tmp, off);