~samiam/MaraDNS

MaraDNS/tools/askmara-tcp.c -rw-r--r-- 25.4 KiB View raw
ccb94b01Sam Trenholme Dockerfile now installs MaraDNS in Docker image. 23 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/* Copyright (c) 2002-2010 Sam Trenholme
 *
 * TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This software is provided 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

/* Simple testing DNS resolving client.  This sends out simple
   queries to DNS servers and prints out to stdout the replies
   it receives.  This version creates TCP queries instead of
   UDP queries.
*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#ifndef MINGW32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include "../MaraDns.h"
/* All of the labels */
#include "askmara_labels_en.h"
/* BEGIN RNG USING CODE */
/* Yes, we use the RNG to make the psudo-random number */
#include "../rng/rng-api-fst.h"
/* END RNG USING CODE */

#include "../libs/MaraHash.h"
#include "../dns/functions_dns.h"
#include "../parse/functions_parse.h"

int verbose_mode = 0; /* Whether to have short or verbose output;
                         1: verbose output; any other value: short output */
int timeout = 21;

unsigned short dns_port = DNS_PORT;

/* So -Wall doesn't gag */
extern int decomp_init();

/* Defining finctions which are part of askmara-tcp.c */
int verbose_output(js_string *uindata);
int csv1_compatible_output(js_string *uindata, js_string *query);
int out_answer(js_string *uindata,int *place);
int csv1_answer(js_string *uindata, int *place, js_string *query);

/* Octal escape any non-ASCII sequences in the askmara output; almost idential
   to show_esc_stdout */
int escape_stdout(js_string *js) {
    int counter = 0;
    unsigned char this;

    /* Sanity checks */
    if(js_has_sanity(js) < 0)
        return -1;
    if(js->unit_size != 1)
        return -1;

    while(counter < js->unit_count) {
        this = *(js->string + counter);
        if(this < 32 || this > 126)
            printf("\\%03o",this);
        else if(this == '\\')
            printf("\\%c",this);
        else
            printf("%c",this);
        counter++;
        }
    return 1;
    }

/* Generate a psudo-random query-id based on the hostname we give it.
   This helps us test the server against a large number of query IDs */

u_int16_t gen_id(char *hostname) {
    /* BEGIN RNG USING CODE */
    /* May as well bring out the bug guns (e.g. our secure RNG) */
    MARA_BYTE r_inBlock[17],r_outBlock[17],r_binKey[17];
    keyInstance r_keyInst;
    cipherInstance r_cipherInst;
    unsigned char crypto_key[34];
    int counter;
    char *mc1, *mc2;
    /* END RNG USING CODE */
    int ret = 4321;

    /* BEGIN RNG USING CODE */
    if(hostname == 0)
        return JS_ERROR;

    /* Initialize the keys, including the "binKey" (is this used?) */
    memset(r_binKey,'0',16);
    mc1 = "";
    mc2 = mc1;
    /* Set the crypto key to '' (UTF-8 encoding) */
    for(counter = 0; counter < 16; counter++) {
        *(crypto_key + counter) = *mc2;
        mc2++;
        if(*mc2 == 0)
           mc2 = mc1;
        }
    /*memset(crypto_key,'',16);*/
    /* Make the first 16 characters of the hostname the key (padded by the
        character--do you see a n~ there?) */
    strncpy((char *)crypto_key,hostname,16);
    /* Set the plaintext block to '' (UTF-8 encoding) */
    mc2 = mc1;
    for(counter = 0; counter < 16; counter++) {
        *(r_inBlock + counter) = *mc2;
        mc2++;
        if(*mc2 == 0)
           mc2 = mc1;
        }
    /*memset(r_inBlock,'',16);*/
    /* If the hostname is longer than 16 characters, make the plaintext
       the longer part of the hostname */
    if(strlen(hostname) > 16)
    strncpy((char *)r_inBlock,hostname+16,16);

    if(makeKey(&r_keyInst, DIR_ENCRYPT, 128, (char *)crypto_key) != 1)
        return JS_ERROR;
    if(cipherInit(&r_cipherInst, MODE_ECB, NULL) != 1)
        return JS_ERROR;
    if(blockEncrypt(&r_cipherInst,&r_keyInst,r_inBlock,128,r_outBlock) != 128)
        return JS_ERROR;

    ret = ((r_outBlock[0] << 8) & 0xff00) | (r_outBlock[1] & 0x00ff);
    /* END RNG USING CODE */
    return ret;
    }

int harderror(char *msg) {
    if(verbose_mode != 1)
        printf("# ");
    printf("%s%s%s",L_HARD_ERROR,msg,L_NEWLINE);
    exit(1);
    }

int main(int argc, char **argv) {
    char *server_address = NULL;
#if defined IPV6 && ! defined __CYGWIN__
    struct sockaddr_in6 server;
#else
    struct sockaddr_in server;
#endif
    int len_inet; /* Length */
    int s; /* Socket */
    js_string *outdata; /* Outgoing data */
    js_string *indata, *uindata; /* Incoming data (uncompressed version) */
    q_header header; /* header data */
    q_question question;
    int place,count;
    int nrd = 0; /* whether recursion is desired or not 1: No recursion;
                   0: recursion desired */
    int id;
    int attempts = 10;
    unsigned char len[2];
    int length;

    /* Determine what the query string is */
    verbose_mode = 0;
    timeout = 31;
    server_address = "127.0.0.3";
    argc--;
    argv++;
    while(argc > 0) {
        if(argv[0] [0]== '-' && argv[0][1] == 'v')
            verbose_mode = 1;
        else if(argv[0][0] == '-' && argv[0][1] == 'n')
            nrd = 1;
        else if(argv[0][0] == '-' && argv[0][1] == 'p') {
            if(argc < 2) {
                harderror(L_USAGE);
                }
            argc--;
            argv++;
            dns_port = atoi(*argv);
            if(dns_port < 1 || dns_port > 32000) {
                    harderror("Wrong port number\n");
                }
            }
        else if(argv[0][0] == '-' && argv[0][1] == 't') {
            if(argc < 2) {
                harderror(L_USAGE);
                }
            argc--;
            argv++;
            timeout = atoi(*argv);
            if(timeout < 1)
               harderror( L_INVALID_TIMEOUT);
            verbose_mode = 2;
            }
        else if (argv[0][0] != '-')
            break;
        else
            harderror(L_USAGE);
        argc--;
        argv++;
        }

    /* we expect now the query string and possibly the server */
    if(argc<1)
        harderror(L_USAGE);

    /* Determine what IP address to connect to */
    if(argc>1)
        server_address = argv[1];

    if((indata = js_create(512,1)) == 0)
       harderror(L_JS_CREATE_INDATA);
    if((uindata = js_create(2048,1)) == 0)
       harderror(L_JS_CREATE_UINDATA);
    if((outdata = js_create(512,1)) == 0)
       harderror(L_JS_CREATE_OUTDATA);
    if((question.qname = js_create(512,1)) == 0)
       harderror(L_JS_CREATE_QNAME);

    /* Create a socket address */
    memset(&server,0,sizeof(server));
#if defined IPV6 && ! defined __CYGWIN__
    if ( strchr(server_address,':') != NULL) {
        /* assume that we have an IPv6 address */

        /* Create a TCP socket */
        if((s = socket(PF_INET6,SOCK_STREAM,0)) == -1)
            harderror(L_SOCKET);

        server.sin6_family = AF_INET6;
        server.sin6_port = htons(dns_port);
        if( inet_pton(AF_INET6, server_address, &server.sin6_addr) < 1) {
            harderror(L_MAL_IP);
            }
        len_inet = sizeof(struct sockaddr_in6);
        }
    else {
        if((s = socket(PF_INET,SOCK_STREAM,0)) == -1)
            harderror(L_SOCKET);
        ((struct sockaddr_in*)&server)->sin_family = AF_INET;
        ((struct sockaddr_in*)&server)->sin_port = htons(dns_port);
        if((((struct sockaddr_in*)&server)->sin_addr.s_addr = inet_addr(server_address)) == INADDR_NONE)
            harderror(L_MAL_IP);
        len_inet = sizeof(struct sockaddr_in);
        }
#else
    if((s = socket(PF_INET,SOCK_STREAM,0)) == -1)
        harderror(L_SOCKET);

    server.sin_family = AF_INET;
    /* When debugging MaraDNS on systems where I do not have root, it is
       useful to be able to contact a MaraDNS server running on a high
       port number.  However, DNS_PORT should be 53 otherwise (defined
       in MaraDNS.h) */
    server.sin_port = htons(dns_port);
    if((server.sin_addr.s_addr = inet_addr(server_address)) == INADDR_NONE)
        harderror(L_MAL_IP);
    len_inet = sizeof(struct sockaddr_in);
#endif

    id = gen_id(argv[0]);
    /* Format a DNS request */
    /* DNS header */
    header.id = id;
    header.qr = 0;
    header.opcode = 0;
    header.aa = 0;
    header.tc = 0;
    header.rd = 1 - nrd; /* Recursion desired */
    header.ra = 0;
    header.z = 0;
    header.rcode = 0;
    header.qdcount = 1;
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    /* Create a DNS question , and put it in raw UDP format */
    /* DNS question -> looking up an A record... */
    question.qclass = 1; /* ...on the internet */
    if(js_qstr2js(question.qname,argv[0]) == JS_ERROR)
        harderror(L_INVALID_Q); /* Invalid query */

    /* Make sure that the csv1-compatible output hashes all non-RR
       output */
    if(verbose_mode != 1)
        printf("# ");
    if(dns_port==DNS_PORT)
        printf("%s%s%s",L_QUERYING,server_address,L_NEWLINE); /* Querying the server with the IP... */
    else
        printf("%s%s#%d%s",L_QUERYING,server_address, dns_port,L_NEWLINE); /* Querying the server with the IP... */

    /* Make 'Aexample.com.' raw UDP data */

    place = hname_2rfc1035(question.qname);
    if(place == JS_ERROR)
        harderror(L_INVALID_DQ); /* Invalid form of domain query */
    question.qtype = place;

    /* Make a string containing the RAW UDP query */
    make_hdr(&header,outdata);
    make_question(&question,outdata);

    do {
        /* Connect to the zone server */
        if(connect(s,(struct sockaddr *)&server,len_inet) == -1)
        /* Determine how long it is */
        printf("DEBUG: outdata->unit_count %d\n",outdata->unit_count);
        len[0] = (outdata->unit_count & 0xff00) >> 8;
        len[1] = outdata->unit_count & 0xff;

        /* Send out how long the request is */
        if(write(s,len,2) == -1) {
            harderror("XXX No error message yet 271");
            }

        /* Send out a DNS request */
        if(write(s,outdata->string,outdata->unit_count) == -1)
           harderror("XXX No error message yet 276");

        /* Wait for a reply from the DNS server */
        recv(s,len,2,MSG_WAITALL);
        length = ((len[0] << 8) & 0xff00) | (len[1] & 0xff);
        if(length < 12)
            harderror("XXX No error message yet 282");
        if((count = recv(s,indata->string,length,MSG_WAITALL)) < 0)
            harderror(L_DNS_R_ERROR); /* Problem getting DNS server response */

        if(count != length)
             harderror("Length of reply not same as stated length");

        indata->unit_count = count;

        /* Get the ID from the remote response */
        if(count > 2) {
           id = (*(indata->string) << 8) | *(indata->string + 1);
           }
        else {
           id = header.id + 1;
           }

        attempts--;

        /* If the id doesn't match, get a reply again */
        } while(id != header.id && attempts > 0);

    decomp_init(0);
    decompress_data(indata,uindata);

    /*
    escape_stdout(indata);
    printf("%s%s",L_NEWLINE,L_NEWLINE);
    escape_stdout(uindata);
    */

    if(verbose_mode == 1)
        verbose_output(uindata);
    else
        csv1_compatible_output(uindata,question.qname);

    return 0;
    }

/* Verbose form of askmara output
   input: Pointer to js_string object with uncompressed UDP data
   output: None, shows data on standard output
*/

int verbose_output(js_string *uindata) {

    int place,counter,count;
    q_question question;
    q_header header;

    /* Allocate memory for the question.qname string */
    if((question.qname = js_create(512,1)) == 0)
       harderror(L_JS_CREATE_QNAME);

    /* Print out the reply -- cut 'n paste from rtest.c */
    if(read_hdr(uindata,&header) == JS_ERROR)
        harderror(L_INHEADER_CONV); /* Problem converting inheader */

    printf("%s%s",L_SERVER_REPLY,L_NEWLINE); /* Server reply */
    printf("%s%d%s",L_QUERY_ID,header.id,L_NEWLINE); /* Query ID */
    printf("%s%d%s",L_QUERY_TYPE,header.qr,L_NEWLINE); /* Query type */
    printf("%s%d%s",L_OPCODE,header.opcode,L_NEWLINE); /* Opcode */
    printf("%s%d%s",L_AUTHORITATIVE,header.aa,L_NEWLINE); /* Auth. */
    printf("%s%d%s",L_TRUNCATED,header.tc,L_NEWLINE); /* Truncated */
    printf("%s%d%s",L_RD,header.rd,L_NEWLINE); /* Recursion desired */
    printf("%s%d%s",L_RA,header.ra,L_NEWLINE); /* Recursion available */
    printf("%s%d%s",L_Z_DATA,header.z,L_NEWLINE); /* Z-Data */
    printf("%s%d%s",L_RC,header.rcode,L_NEWLINE); /* Result code */
    printf("%s%d%s",L_QDCOUNT,header.qdcount,L_NEWLINE); /* Num. questions */
    printf("%s%d%s",L_ANCOUNT,header.ancount,L_NEWLINE); /* Num. answers */
    printf("%s%d%s",L_NSCOUNT,header.nscount,L_NEWLINE); /* Num. NS RRs */
    printf("%s%d%s",L_ARCOUNT,header.arcount,L_NEWLINE); /* Num. Additional */
    printf("%s",L_NEWLINE);

    /* Get all of the questions in the reply */
    place = 12;
    for(counter = 0;counter < header.qdcount;counter++) {
        count = read_question(uindata,&question,place);
        if(count < 0) /* Error handling */
            harderror(L_QERROR); /* Error reading question */
        place += count;
        printf("%s",L_QNAME); /* Question name */
        hname_translate(question.qname,question.qtype);
        escape_stdout(question.qname);
        printf("%s%s%d%s",L_NEWLINE,L_QTYPE,question.qtype,L_NEWLINE);
        printf("%s%d%s",L_QCLASS,question.qclass,L_NEWLINE); /* Ques. Class */
        }

    /* Get all of the answers in the reply */
    /* Get all of the an replies */
    printf("%s%s%s",L_NEWLINE,L_ANREP,L_NEWLINE);
    for(counter = 0; counter < header.ancount; counter++)
        out_answer(uindata,&place);

    /* Get all of the ns replies */
    printf("%s%s%s",L_NEWLINE,L_NSREP,L_NEWLINE);
    for(counter = 0; counter < header.nscount; counter++)
        out_answer(uindata,&place);

    /* Get all of the ar replies */
    printf("%s%s%s",L_NEWLINE,L_ARREP,L_NEWLINE);
    for(counter = 0; counter < header.arcount; counter++)
        out_answer(uindata,&place);

    return 1;
    }


/* Parse the answer part of a DNS query.
   input: pointer to js_string with uncompressed UDP data
          pointer to where we are in that string
   output: outputs to stdout the data in that string
*/

int out_answer(js_string *uindata,int *place) {

        q_rr rr_hdr;
        rr_soa soa;
        rr_mx mx;
        int count;

    /* Create the needed strings */
    if((rr_hdr.name = js_create(512,1)) == 0)
       harderror(L_HNAME_OA); /* js_create with hname in oa */
    if((soa.mname = js_create(512,1)) == 0)
       harderror(L_C_MNAME); /* js_create with soa.mname in oa */
    if((soa.rname = js_create(512,1)) == 0)
       harderror(L_C_RNAME); /* js_create with soa.rname in oa */
    if((mx.exchange = js_create(512,1)) == 0)
       harderror(L_C_MXEXC); /* js_create with mx.exchange in oa */

        /* Start printing out the data header */
        count = read_rr_h(uindata,&rr_hdr,*place);
        if(count < 0)
            harderror(L_RR_ERR); /* Error reading rr in AN section */
        *place += count;
        hname_translate(rr_hdr.name,rr_hdr.type);
        printf("%s",L_RNAME); /* Record name */
        escape_stdout(rr_hdr.name);
        printf("\nRecord type: %d\n",rr_hdr.type);
        printf("Record class: %d\n",rr_hdr.class);
        printf("Record TTL: %u\n",rr_hdr.ttl);
        printf("Record length: %d\n",rr_hdr.rdlength);
        printf("%s",L_NEWLINE);
        if(rr_hdr.type == RR_SOA) {
            if(read_soa(uindata,&soa,*place) == JS_ERROR)
                harderror(L_READSOA);
            hname_translate(soa.mname,RR_SOA);
            hname_translate(soa.rname,RR_MX);
            printf("%s",L_S_MNAME); /* SOA mname */
            escape_stdout(soa.mname);
            printf("%s%s",L_NEWLINE,L_S_RNAME); /* SOA rname */
            escape_stdout(soa.rname);
            printf("%s%s%u%s",L_NEWLINE,L_S_SERIAL,soa.serial,L_NEWLINE);
            printf("%s%d%s",L_S_REFRESH,(int)soa.refresh,L_NEWLINE);
            printf("%s%d%s",L_S_RETRY,(int)soa.retry,L_NEWLINE);
            printf("%s%d%s",L_S_EXPIRE,(int)soa.expire,L_NEWLINE);
            printf("%s%u%s",L_S_MINIMUM,soa.minimum,L_NEWLINE);
            }
        else if(rr_hdr.type == RR_MX) {
            if(uindata->unit_count < *place + 2)
                return JS_ERROR;
            mx.preference = ((*(uindata->string + *place) & 0xff) << 8) |
                             (*(uindata->string + *place + 1) & 0xff);
            read_ns(uindata,mx.exchange,*place + 2);
            printf("%s%d%s",L_M_PREF,mx.preference,L_NEWLINE); /* MX pref. */
            printf("%s",L_M_EX); /* MX exchange */
            hname_translate(mx.exchange,RR_MX);
            escape_stdout(mx.exchange);
            printf("%s%s",L_NEWLINE,L_NEWLINE);
            }
        else if(rr_hdr.type == RR_NS || rr_hdr.type == RR_CNAME ||
                rr_hdr.type == RR_PTR) {
            printf("%s",L_DATA); /* Data: */
            read_ns(uindata,mx.exchange,*place);
            hname_translate(mx.exchange,rr_hdr.type);
            escape_stdout(mx.exchange);
            printf("%s%s",L_NEWLINE,L_NEWLINE);
            }
        else if(rr_hdr.type == RR_A) {
            if(uindata->unit_count < *place + 4)
                return JS_ERROR;
            /* Display the IP of the data */
            printf("%s%d%s%d%s%d%s%d%s%s",L_IP,*(uindata->string + *place),
                   L_DOT,*(uindata->string + *place + 1),
                   L_DOT,*(uindata->string + *place + 2),
                   L_DOT,*(uindata->string + *place + 3),L_NEWLINE,L_NEWLINE);
            }
        else if(rr_hdr.type == RR_AAAA) {
            unsigned short *p;
            if(uindata->unit_count < *place + 16)
                return JS_ERROR;
            /* Display the IP of the data */
            /* Display the IP of the data */
            p = (unsigned short*)(uindata->string + *place);
            printf("%s%d%x:%x:%x:%x:%x:%x:%x:%x%s%s",L_IP,*(uindata->string + *place),
                       htons(*(p + 0 )),
                       htons(*(p + 1)),
                       htons(*(p + 2)),
                       htons(*(p + 3)),
                       htons(*(p + 4)),
                       htons(*(p + 5)),
                       htons(*(p + 6)),
                       htons(*(p + 7)),L_NEWLINE,L_NEWLINE);
            }
        else if(rr_hdr.type == RR_TXT) {
            printf("%s",L_TXT); /* Text String */
            read_txt(uindata,mx.exchange,*place);
            escape_stdout(mx.exchange);
            printf("%s%s",L_NEWLINE,L_NEWLINE);
            }
        else
            printf("%s%d%s%s",L_UNSUP,rr_hdr.type,L_NEWLINE,L_NEWLINE);

        *place += rr_hdr.rdlength; /* To do: read the RD data itself for
                                      all MaraDNS supported RRs */

        return 0;
        }

/* csv1-compatible form of askmara output
   input: Pointer to js_string object with uncompressed UDP data,
          Raw UDP for of the question
   output: None, shows data on standard output
*/

int csv1_compatible_output(js_string *uindata, js_string *query) {

    int place,counter,count;
    q_question question;
    q_header header;

    /* Allocate memory for the question.qname string */
    if((question.qname = js_create(512,1)) == 0)
       harderror(L_JS_CREATE_QNAME);

    /* Get the header of the reply */
    if(read_hdr(uindata,&header) == JS_ERROR)
        harderror(L_INHEADER_CONV); /* Problem converting inheader */

    /* Move past the questions */
    place = 12;
    for(counter = 0;counter < header.qdcount;counter++) {
        count = read_question(uindata,&question,place);
        if(count < 0) /* Error handling */
            harderror(L_QERROR); /* Error reading question */
        printf("# Question: ");
        if(hname_translate(question.qname,question.qtype) == JS_ERROR)
            harderror(L_QERROR);
        show_esc_stdout(question.qname);
        printf("\n");
        place += count;
        }

    /* Get all of the answers in the reply */

    /* Get all of the an replies */
    for(counter = 0; counter < header.ancount; counter++)
        csv1_answer(uindata,&place,query);

    /* Get all of the ns replies */
    printf("# %s%s",L_NSREP,L_NEWLINE);
    for(counter = 0; counter < header.nscount; counter++)
        csv1_answer(uindata,&place,query);

    /* Get all of the ar replies */
    printf("# %s%s",L_ARREP,L_NEWLINE);
    for(counter = 0; counter < header.arcount; counter++)
        csv1_answer(uindata,&place,query);

    return 1;
    }


/* Parse the answer part of a DNS query.
   input: pointer to js_string with uncompressed UDP data,
          pointer to where we are in that string,
          raw UDP form of the question asked
   output: outputs to stdout the data in that string
*/

int csv1_answer(js_string *uindata, int *place, js_string *query) {

    q_rr rr_hdr;
    rr_soa soa;
    rr_mx mx;
    int count;
    js_string *lower1, *lower2;

    /* Create the needed strings */
    if((rr_hdr.name = js_create(512,1)) == 0)
       harderror(L_HNAME_OA); /* js_create with hname in oa */
    if((soa.mname = js_create(512,1)) == 0)
       harderror(L_C_MNAME); /* js_create with soa.mname in oa */
    if((soa.rname = js_create(512,1)) == 0)
       harderror(L_C_RNAME); /* js_create with soa.rname in oa */
    if((mx.exchange = js_create(512,1)) == 0)
       harderror(L_C_MXEXC); /* js_create with mx.exchange in oa */
    if((lower1 = js_create(512,1)) == 0)
       harderror(L_C_MXEXC); /* js_create with mx.exchange in oa */
    if((lower2 = js_create(512,1)) == 0)
       harderror(L_C_MXEXC); /* js_create with mx.exchange in oa */

    /* Start printing out the data header */
    count = read_rr_h(uindata,&rr_hdr,*place);
    if(count < 0)
        harderror(L_RR_ERR); /* Error reading rr in AN section */
    *place += count;

    /* Display a hash if the record name is not the same as the
       query (case_insensitive matching) */
    js_copy(query,lower1);
    js_copy(rr_hdr.name,lower2);
    lower1->encoding = JS_US_ASCII;
    lower2->encoding = JS_US_ASCII;
    js_tolower(lower1);
    js_tolower(lower2);
    if(js_issame(lower1,lower2) != 1)
        printf("#");

    hname_translate(rr_hdr.name,rr_hdr.type);
    escape_stdout(rr_hdr.name);
    /* We also show the type if this is an unsupported record type */
    if(rr_hdr.type != RR_A &&
       rr_hdr.type != RR_AAAA &&
       rr_hdr.type != RR_NS &&
       rr_hdr.type != RR_CNAME &&
       rr_hdr.type != RR_SOA &&
       rr_hdr.type != RR_PTR &&
       rr_hdr.type != RR_MX &&
       rr_hdr.type != RR_TXT)
        printf("|%d",rr_hdr.type);
    printf("|%u",rr_hdr.ttl);
    if(rr_hdr.type == RR_SOA) {
        if(read_soa(uindata,&soa,*place) == JS_ERROR)
            harderror(L_READSOA);
        hname_translate(soa.mname,RR_SOA);
        hname_translate(soa.rname,RR_MX);
        printf("|"); /* SOA mname */
        escape_stdout(soa.mname);
        printf("|"); /* SOA rname */
        escape_stdout(soa.rname);
        printf("|%u",soa.serial);
        printf("|%d",(int)soa.refresh);
        printf("|%d",(int)soa.retry);
        printf("|%d",(int)soa.expire);
        printf("|%u\n",soa.minimum);
        }
    else if(rr_hdr.type == RR_MX) {
        if(uindata->unit_count < *place + 2)
            return JS_ERROR;
        mx.preference = ((*(uindata->string + *place) & 0xff) << 8) |
                         (*(uindata->string + *place + 1) & 0xff);
        read_ns(uindata,mx.exchange,*place + 2);
        printf("|%d",mx.preference); /* MX pref. */
        hname_translate(mx.exchange,-2); /* Makes the char a pipe */
        escape_stdout(mx.exchange);
        printf("\n");
        }
    else if(rr_hdr.type == RR_NS || rr_hdr.type == RR_CNAME ||
            rr_hdr.type == RR_PTR) {
        read_ns(uindata,mx.exchange,*place);
        hname_translate(mx.exchange,-2); /* First char is a pipe */
        escape_stdout(mx.exchange);
        printf("\n");
        }
    else if(rr_hdr.type == RR_A) {
        if(uindata->unit_count < *place + 4)
            return JS_ERROR;
        /* Display the IP of the data */
        printf("|%d%s%d%s%d%s%d\n",*(uindata->string + *place),
               L_DOT,*(uindata->string + *place + 1),
               L_DOT,*(uindata->string + *place + 2),
               L_DOT,*(uindata->string + *place + 3));
        }
    else if(rr_hdr.type == RR_AAAA) {
        unsigned short *p;
        if(uindata->unit_count < *place + 16)
            return JS_ERROR;
        /* Display the IP of the data */
        /* Display the IP of the data */
        p = (unsigned short*)(uindata->string + *place);
        printf("|%x:%x:%x:%x:%x:%x:%x:%x\n",
                   htons(*(p + 0 )),
                   htons(*(p + 1)),
                   htons(*(p + 2)),
                   htons(*(p + 3)),
                   htons(*(p + 4)),
                   htons(*(p + 5)),
                   htons(*(p + 6)),
                   htons(*(p + 7)));
        }
    else if(rr_hdr.type == RR_TXT) {
        printf("|");
        read_txt(uindata,mx.exchange,*place);
        escape_stdout(mx.exchange);
        printf("\n");
        }
    else {
        printf("|\n");
        }

    *place += rr_hdr.rdlength; /* To do: read the RD data itself for
                                  all MaraDNS supported RRs */

    return 0;
    }