11 #include "gnunet-vpn-packet.h"
12 #include "gnunet-dns-parser.h"
14 static char* pretty = /*{{{*/
16 0123456789012345678901234567890123456789012345678901234567890123456789 */
17 "IPv6-Paket from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //60
18 " to xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //120
20 0123456789012345678901234567890123456789012345678901234567890123456789 */
21 " flow 0xXXX ( ) \n" //180
22 " length 0xXX ( ) \n" //240
23 " nexthdr 0xXX ( \n" //300
24 " hoplmt 0xXX ( ) \n" //360
25 "first 128 bytes of payload: \n" //420
27 0123456789012345678901234567890123456789012345678901234567890123456789 */
28 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //490
29 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //560
30 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //630
31 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //700
32 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //770
33 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //840
34 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //910
35 "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n";//980
38 static void pp_ip6adr(unsigned char* adr, char* dest) {{{
41 sprintf(tmp, "%02X", adr[0]);
42 memcpy(dest+0, tmp, 2);
43 sprintf(tmp, "%02X", adr[1]);
44 memcpy(dest+2, tmp, 2);
46 sprintf(tmp, "%02X", adr[2]);
47 memcpy(dest+5, tmp, 2);
48 sprintf(tmp, "%02X", adr[3]);
49 memcpy(dest+7, tmp, 2);
51 sprintf(tmp, "%02X", adr[4]);
52 memcpy(dest+10, tmp, 2);
53 sprintf(tmp, "%02X", adr[5]);
54 memcpy(dest+12, tmp, 2);
56 sprintf(tmp, "%02X", adr[6]);
57 memcpy(dest+15, tmp, 2);
58 sprintf(tmp, "%02X", adr[7]);
59 memcpy(dest+17, tmp, 2);
61 sprintf(tmp, "%02X", adr[8]);
62 memcpy(dest+20, tmp, 2);
63 sprintf(tmp, "%02X", adr[9]);
64 memcpy(dest+22, tmp, 2);
66 sprintf(tmp, "%02X", adr[10]);
67 memcpy(dest+25, tmp, 2);
68 sprintf(tmp, "%02X", adr[11]);
69 memcpy(dest+27, tmp, 2);
71 sprintf(tmp, "%02X", adr[12]);
72 memcpy(dest+30, tmp, 2);
73 sprintf(tmp, "%02X", adr[13]);
74 memcpy(dest+32, tmp, 2);
76 sprintf(tmp, "%02X", adr[14]);
77 memcpy(dest+35, tmp, 2);
78 sprintf(tmp, "%02X", adr[15]);
79 memcpy(dest+37, tmp, 2);
82 void pp_hexdump(unsigned char* data, char* dest, int max) {{{
87 int to = max > 16 ? 16 : max;
88 for (i = 0; i < to; i++) {
90 sprintf(tmp, "%02x", data[i]);
91 memcpy(dest+(3*i)+off, tmp, 2);
92 if (isprint(data[i])) {
93 sprintf(tmp2, "%c", data[i]);
94 memcpy(dest+51+i, tmp2, 1);
99 void pp_write_header(char* dest, struct ip6_pkt* pkt) {{{
100 switch (pkt->ip6_hdr.nxthdr) {
102 memcpy(dest, "ICMPv6)", 7);
105 memcpy(dest, "TCP)", 4);
108 memcpy(dest, "UDP)", 4);
111 memcpy(dest, "unknown)", 8);
116 void pkt_printf(struct ip6_pkt* pkt) {{{
117 char* buf = alloca(strlen(pretty)+1);
120 memcpy(buf, pretty, strlen(pretty)+1);
122 pp_ip6adr(pkt->ip6_hdr.sadr, buf+16);
123 pp_ip6adr(pkt->ip6_hdr.dadr, buf+76);
125 int flow = (ntohl(pkt->ip6_hdr.flowlbl));
126 sprintf(tmp, "%03x", flow);
127 memcpy(buf+138, tmp, 3);
128 sprintf(tmp, "%-8d", flow);
129 memcpy(buf+143, tmp, 8);
131 int length = ntohs(pkt->ip6_hdr.paylgth);
132 sprintf(tmp, "%02x", length);
133 memcpy(buf+198, tmp, 2);
134 sprintf(tmp, "%-3d", length);
135 memcpy(buf+203, tmp, 3);
137 sprintf(tmp, "%02x", pkt->ip6_hdr.nxthdr);
138 memcpy(buf+258, tmp, 2);
139 pp_write_header(buf+263, pkt);
141 sprintf(tmp, "%02x", pkt->ip6_hdr.hoplmt);
142 memcpy(buf+318, tmp, 2);
143 sprintf(tmp, "%-3d", pkt->ip6_hdr.hoplmt);
144 memcpy(buf+323, tmp, 3);
146 int size = ntohs(pkt->ip6_hdr.paylgth);
148 for(i = 0; i < 8; i++) {
149 if (16*i > size) break;
150 pp_hexdump(pkt->data + (16*i), buf + 420 + (i*70), size - 16*i);
156 void pkt_printf_ip6tcp(struct ip6_tcp* pkt) {{{
157 printf("spt: %u\n", ntohs(pkt->tcp_hdr.spt));
158 printf("dpt: %u\n", ntohs(pkt->tcp_hdr.dpt));
159 printf("seq: %u\n", ntohs(pkt->tcp_hdr.seq));
160 printf("ack: %u\n", ntohs(pkt->tcp_hdr.ack));
161 printf("off: %u\n", ntohs(pkt->tcp_hdr.off));
162 printf("wsz: %u\n", ntohs(pkt->tcp_hdr.wsz));
163 printf("crc: 0x%x\n", ntohs(pkt->tcp_hdr.crc));
164 printf("urg: %u\n", ntohs(pkt->tcp_hdr.urg));
165 printf("flags: %c%c%c%c%c%c%c%c\n",
166 pkt->tcp_hdr.flg & 0x80 ? 'C' : '.',
167 pkt->tcp_hdr.flg & 0x40 ? 'E' : '.',
168 pkt->tcp_hdr.flg & 0x20 ? 'U' : '.',
169 pkt->tcp_hdr.flg & 0x10 ? 'A' : '.',
170 pkt->tcp_hdr.flg & 0x08 ? 'P' : '.',
171 pkt->tcp_hdr.flg & 0x04 ? 'R' : '.',
172 pkt->tcp_hdr.flg & 0x02 ? 'S' : '.',
173 pkt->tcp_hdr.flg & 0x01 ? 'F' : '.'
177 void pkt_printf_ip6udp(struct ip6_udp* pkt) {{{
178 printf("spt: %u\n", ntohs(pkt->udp_hdr.spt));
179 printf("dpt: %u\n", ntohs(pkt->udp_hdr.dpt));
180 printf("len: %u\n", ntohs(pkt->udp_hdr.len));
181 printf("crc: 0x%x\n", ntohs(pkt->udp_hdr.crc));
184 static char* dns_types(unsigned short type) {{{
185 static char* types[] = { /*{{{*/
187 "A", // 1 a host address
188 "NS", // 2 an authoritative name server
189 "MD", // 3 a mail destination (Obsolete - use MX)
190 "MF", // 4 a mail forwarder (Obsolete - use MX)
191 "CNAME", // 5 the canonical name for an alias
192 "SOA", // 6 marks the start of a zone of authority
193 "MB", // 7 a mailbox domain name (EXPERIMENTAL)
194 "MG", // 8 a mail group member (EXPERIMENTAL)
195 "MR", // 9 a mail rename domain name (EXPERIMENTAL)
196 "NULL", // 10 a null RR (EXPERIMENTAL)
197 "WKS", // 11 a well known service description
198 "PTR", // 12 a domain name pointer
199 "HINFO", // 13 host information
200 "MINFO", // 14 mailbox or mail list information
201 "MX", // 15 mail exchange
202 "TXT", // 16 text strings
207 static char* qtypes[] = { /* + 252! {{{ */
208 "AXFR", // 252 A request for a transfer of an entire zone
209 "MAILB", // 253 A request for mailbox-related records (MB, MG or MR)
210 "MAILA", // 254 A request for mail agent RRs (Obsolete - see MX)
211 "*", // 255 A request for all records
214 if (type <= 18) return types[type];
215 if (type >= 252 && type <= 255) return qtypes[type-252];
218 case 24: return "SIG";
219 case 25: return "KEY";
220 case 28: return "AAAA";
221 case 29: return "LOC";
222 case 33: return "SRV";
223 case 35: return "NAPTR";
224 case 36: return "KX";
225 case 37: return "CERT";
226 case 39: return "DNAME";
227 case 42: return "APL";
228 case 43: return "DS";
229 case 44: return "SSHFP";
230 case 45: return "IPSECKEY";
231 case 46: return "RRSIG";
232 case 47: return "NSEC";
233 case 48: return "DNSKEY";
234 case 49: return "DHCID";
235 case 50: return "NSEC3";
236 case 51: return "NSEC3PARAM";
237 case 55: return "HIP";
238 case 99: return "SPF";
239 case 249: return "TKEY";
240 case 250: return "TSIG";
241 case 32768: return "TA";
242 case 32769: return "DLV";
249 static char* dns_classes(short class) { /* {{{ */
250 static char* classes[] = { /*{{{*/
252 "IN", // 1 the Internet
253 "CS", // 2 the CSNET class (Obsolete - used only for examples in some obsolete RFCs)
254 "CH", // 3 the CHAOS class
255 "HS", // 4 Hesiod [Dyer 87]
258 if (class <= 4) return classes[class];
263 void pkt_printf_dns(struct dns_pkt* upkt) {{{
264 struct dns_pkt_parsed* pkt = parse_dns_packet(upkt);
266 printf("\nDNS-Packet: ");
267 printf("\tid: %5d ", ntohs(pkt->s.id));
268 printf("\t%d: %s ", pkt->s.qr, pkt->s.qr == 0 ? "query " : "response");
269 printf("\top: %s ", (char*[]){ "query ",
272 "inval "}[pkt->s.op]);
273 printf("\trecursion is%s desired ", pkt->s.rd == 0 ? " not" : " ");
274 unsigned short qdcount = ntohs(pkt->s.qdcount);
275 unsigned short ancount = ntohs(pkt->s.ancount);
276 unsigned short nscount = ntohs(pkt->s.nscount);
277 unsigned short arcount = ntohs(pkt->s.arcount);
278 printf("\t#qd: %5d ", qdcount);
279 printf("\t#an: %5d ", ancount);
280 printf("\t#ns: %5d ", nscount);
281 printf("\t#ar: %5d\n", arcount);
284 for (i = 0; i < qdcount; i++) { /*{{{*/
285 printf("query for %s type=%d (%s) class=%d (%s)\n", pkt->queries[i]->name, ntohs(pkt->queries[i]->qtype), dns_types(ntohs(pkt->queries[i]->qtype)), ntohs(pkt->queries[i]->qclass), dns_classes(ntohs(pkt->queries[i]->qclass)));
288 for (i = 0; i < ancount; i++) { /*{{{*/
289 printf("answer for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->answers[i]->name, ntohs(pkt->answers[i]->type), dns_types(ntohs(pkt->answers[i]->type)), ntohs(pkt->answers[i]->class), dns_classes(ntohs(pkt->answers[i]->class)), ntohl(pkt->answers[i]->ttl), ntohs(pkt->answers[i]->data_len));
292 for (i = 0; i < nscount; i++) { /*{{{*/
293 printf("nameservers for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->nameservers[i]->name, ntohs(pkt->nameservers[i]->type), dns_types(ntohs(pkt->nameservers[i]->type)), ntohs(pkt->nameservers[i]->class), dns_classes(ntohs(pkt->nameservers[i]->class)), ntohl(pkt->nameservers[i]->ttl), ntohs(pkt->nameservers[i]->data_len));
296 for (i = 0; i < arcount; i++) { /*{{{*/
297 printf("additional record for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->additional[i]->name, ntohs(pkt->additional[i]->type), dns_types(ntohs(pkt->additional[i]->type)), ntohs(pkt->additional[i]->class), dns_classes(ntohs(pkt->additional[i]->class)), ntohl(pkt->additional[i]->ttl), ntohs(pkt->additional[i]->data_len));
303 void pkt_printf_udp_dns(struct udp_dns* pkt) {{{
304 pkt_printf_dns(&pkt->data);
307 void pkt_printf_ip6dns(struct ip6_udp_dns* pkt) {{{
308 pkt_printf_udp_dns(&pkt->udp_dns);
311 void pkt_printf_ipdns(struct ip_udp_dns* pkt) {{{
312 pkt_printf_udp_dns(&pkt->udp_dns);