gnunet-dns-parser.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "platform.h"
  2. #include "gnunet-dns-parser.h"
  3. #include "gnunet-vpn-packet.h"
  4. /**
  5. * Parse a name from DNS to a normal .-delimited, 0-terminated string.
  6. *
  7. * @param d The destination of the name. Should have at least 255 bytes allocated.
  8. * @param src The DNS-Packet
  9. * @param idx The offset inside the Packet from which on the name should be read
  10. * @returns The offset of the first unparsed byte (the byte right behind the name)
  11. */
  12. static unsigned int
  13. parse_dns_name(char* d, const unsigned char* src, unsigned short idx) {/*{{{*/
  14. char* dest = d;
  15. int len = src[idx++];
  16. while (len != 0)
  17. {
  18. if (len & 0xC0)
  19. { /* Compressed name, offset in this and the next octet */
  20. unsigned short offset = ((len & 0x3F) << 8) | src[idx++];
  21. parse_dns_name(dest, src, offset - 12); /* 12 for the Header of the DNS-Packet, idx starts at 0 which is 12 bytes from the start of the packet */
  22. return idx;
  23. }
  24. memcpy(dest, src+idx, len);
  25. idx += len;
  26. dest += len;
  27. *dest = '.';
  28. dest++;
  29. len = src[idx++];
  30. };
  31. *dest = 0;
  32. return idx;
  33. }
  34. /*}}}*/
  35. /**
  36. * Parse a complete DNS-Record from raw DNS-data to a struct dns_record
  37. *
  38. * @param data The DNS-data
  39. * @param dst Pointer to count pointers; individual pointers will be allocated
  40. * @param count Number of records to parse
  41. * @param idx The offset inside the Packet from which on the name should be read
  42. * @returns The offset of the first unparsed byte (the byte right behind the last record)
  43. */
  44. static unsigned short
  45. parse_dns_record(unsigned char* data, /*{{{*/
  46. struct dns_record** dst,
  47. unsigned short count,
  48. unsigned short idx) {
  49. int i;
  50. unsigned short _idx;
  51. for (i = 0; i < count; i++) {
  52. dst[i] = GNUNET_malloc(sizeof(struct dns_record));
  53. dst[i]->name = alloca(255); // see RFC1035, no name can be longer than this.
  54. char* name = dst[i]->name;
  55. _idx = parse_dns_name(name, data, idx);
  56. dst[i]->namelen = _idx - idx;
  57. dst[i]->name = GNUNET_malloc(dst[i]->namelen);
  58. memcpy(dst[i]->name, name, dst[i]->namelen);
  59. idx = _idx;
  60. dst[i]->type = *((unsigned short*)(data+idx));
  61. idx += 2;
  62. dst[i]->class = *((unsigned short*)(data+idx));
  63. idx += 2;
  64. dst[i]->ttl = *((unsigned int*)(data+idx));
  65. idx += 4;
  66. dst[i]->data_len = *((unsigned short*)(data+idx));
  67. idx += 2;
  68. dst[i]->data = GNUNET_malloc(ntohs(dst[i]->data_len));
  69. memcpy(dst[i]->data, data+idx, ntohs(dst[i]->data_len));
  70. idx += ntohs(dst[i]->data_len);
  71. }
  72. return idx;
  73. }/*}}}*/
  74. /**
  75. * Parse a raw DNS-Packet into an usable struct
  76. */
  77. struct dns_pkt_parsed*
  78. parse_dns_packet(struct dns_pkt* pkt) {/*{{{*/
  79. struct dns_pkt_parsed* ppkt = GNUNET_malloc(sizeof(struct dns_pkt_parsed));
  80. memcpy(&ppkt->s, &pkt->s, sizeof pkt->s);
  81. unsigned short qdcount = ntohs(ppkt->s.qdcount);
  82. unsigned short ancount = ntohs(ppkt->s.ancount);
  83. unsigned short nscount = ntohs(ppkt->s.nscount);
  84. unsigned short arcount = ntohs(ppkt->s.arcount);
  85. ppkt->queries = GNUNET_malloc(qdcount*sizeof(struct dns_query*));
  86. ppkt->answers = GNUNET_malloc(ancount*sizeof(struct dns_record*));
  87. ppkt->nameservers = GNUNET_malloc(nscount*sizeof(struct dns_record*));
  88. ppkt->additional = GNUNET_malloc(arcount*sizeof(struct dns_record*));
  89. unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */
  90. /* Parse the Query */
  91. int i;
  92. for (i = 0; i < qdcount; i++)
  93. { /*{{{*/
  94. ppkt->queries[i] = GNUNET_malloc(sizeof(struct dns_query));
  95. char* name = alloca(255); /* see RFC1035, it can't be more than this. */
  96. _idx = parse_dns_name(name, pkt->data, idx);
  97. ppkt->queries[i]->namelen = _idx - idx;
  98. idx = _idx;
  99. ppkt->queries[i]->name = GNUNET_malloc(ppkt->queries[i]->namelen);
  100. memcpy(ppkt->queries[i]->name, name, ppkt->queries[i]->namelen);
  101. ppkt->queries[i]->qtype = *((unsigned short*)(pkt->data+idx));
  102. idx += 2;
  103. ppkt->queries[i]->qclass = *((unsigned short*)(pkt->data+idx));
  104. idx += 2;
  105. }
  106. /*}}}*/
  107. idx = parse_dns_record(pkt->data, ppkt->answers, ancount, idx);
  108. idx = parse_dns_record(pkt->data, ppkt->nameservers, nscount, idx);
  109. idx = parse_dns_record(pkt->data, ppkt->additional, arcount, idx);
  110. return ppkt;
  111. }/*}}}*/
  112. void
  113. free_parsed_dns_packet(struct dns_pkt_parsed* ppkt) {
  114. unsigned short qdcount = ntohs(ppkt->s.qdcount);
  115. unsigned short ancount = ntohs(ppkt->s.ancount);
  116. unsigned short nscount = ntohs(ppkt->s.nscount);
  117. unsigned short arcount = ntohs(ppkt->s.arcount);
  118. int i;
  119. for (i = 0; i < qdcount; i++) {
  120. GNUNET_free(ppkt->queries[i]->name);
  121. GNUNET_free(ppkt->queries[i]);
  122. }
  123. GNUNET_free(ppkt->queries);
  124. for (i = 0; i < ancount; i++) {
  125. GNUNET_free(ppkt->answers[i]->name);
  126. GNUNET_free(ppkt->answers[i]->data);
  127. GNUNET_free(ppkt->answers[i]);
  128. }
  129. GNUNET_free(ppkt->answers);
  130. for (i = 0; i < nscount; i++) {
  131. GNUNET_free(ppkt->nameservers[i]->name);
  132. GNUNET_free(ppkt->nameservers[i]->data);
  133. GNUNET_free(ppkt->nameservers[i]);
  134. }
  135. GNUNET_free(ppkt->nameservers);
  136. for (i = 0; i < arcount; i++) {
  137. GNUNET_free(ppkt->additional[i]->name);
  138. GNUNET_free(ppkt->additional[i]->data);
  139. GNUNET_free(ppkt->additional[i]);
  140. }
  141. GNUNET_free(ppkt->additional);
  142. GNUNET_free(ppkt);
  143. }