~samiam/MaraDNS

ref: 3.5.0002 MaraDNS/tools/askmara.c -rw-r--r-- 27.4 KiB View raw
db09084f — Sam Trenholme MaraDNS release 3.5.0002 3 months 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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/* 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.
*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#ifndef MINGW32
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else /* MINGW32 */
#include <winsock.h>
#include <wininet.h>
#endif /* MINGW32 */
#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_output(js_string *uindata);
int csv2_compatible_output(js_string *uindata, js_string *query);
int out_answer(js_string *uindata,int *place);
int csv2_answer(js_string *uindata, int *place, js_string *query);
/* escape_stdout_csv2: From parse/Csv2_esc_txt.c and defined in
   parse/Csv2_functions.h */
extern int escape_stdout_csv2(js_string *js);

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;

/* 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 */

uint16_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;
    struct sockaddr_in dns_udp;
    struct sockaddr_in *pdns_udp = &dns_udp;
#ifdef IPV6
    struct sockaddr_in6 dns_udp6;
#endif
    int len_inet; /* Length */
    int s = -1; /* 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;
    fd_set rx_set; /* Using select() because if its timeout option */
    int maxd;      /* select() */
    struct timeval tv;  /* select() */
    int n; /* Select() return value */
    int nrd = 0; /* whether recursion is desired or not 1: No recursion;
                    0: recursion desired */
    int id;
    int attempts = 10;
    int desired_rr = -1; /* This is used to determine what RR type the
                            user desires */
    char *temp;

#ifdef MINGW32
    int err;
    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(2,2);
    err = WSAStartup(wVersionRequested, &wsaData);
#endif /* MINGW32 */
    /* Determine what the query string is */
    verbose_mode = 0;
    timeout = 31;
    server_address = ASKMARA_DEFAULT_SERVER;
    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);
    if(argc>1)
        server_address = argv[1];

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

    /* Create a socket address to use with sendto() */
    memset(&dns_udp,0,sizeof(dns_udp));
    dns_udp.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) */
    dns_udp.sin_port = htons(dns_port);
    len_inet = sizeof(dns_udp);

    if((dns_udp.sin_addr.s_addr = inet_addr(server_address)) == INADDR_NONE) {
#ifdef IPV6
        memset(&dns_udp6,0,sizeof(dns_udp6));
        dns_udp6.sin6_family = AF_INET6;
        dns_udp6.sin6_port = htons(dns_port);
        if( inet_pton(AF_INET6, server_address, &dns_udp6.sin6_addr) < 1) {
#endif
            harderror(L_MAL_IP);
#ifdef IPV6
        } else {
            len_inet = sizeof(dns_udp6);
            pdns_udp = (struct sockaddr_in*)&dns_udp6;
            if((s = socket(AF_INET6,SOCK_DGRAM,0)) == -1)
                harderror(L_SOCKET);
            }
#endif
    } else {
         /* Create a UDP IPv4 client socket */
        if((s = socket(AF_INET,SOCK_DGRAM,0)) == -1)
            harderror(L_SOCKET);
        }


    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 */
    /* We now allow people to specify any numeric record in this format:
     * 40:t1.example.com., where 40 is the numeric rrtype */

    temp = argv[0];
    if(temp == 0) {
            printf("Wrong number of arguments to askmara\n");
            exit(43);
    }
    if(*temp >= '0' && *temp <= '9') {
        int td = 0;
        desired_rr = 0;
        for(td = 0; td < 7; td++) {
                if(*temp < '0' || *temp > '9') {
                        break;
                }
                desired_rr *= 10;
                desired_rr += *temp - '0';
                temp++;
        }
        if(*temp != ':') {
                printf("Invalid numeric rtype\n");
                exit(44);
        }
        *temp = 'A';
    }

    if(js_qstr2js(question.qname,temp) == 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;
    /* 40:name.example.com. style queries */
    if(desired_rr >= 0 && desired_rr <= 65535) {
            question.qtype = desired_rr;
    }

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

    do {
        /* Send out a DNS request */
        if(sendto(s,outdata->string,outdata->unit_count,0,
          (struct sockaddr *)pdns_udp,len_inet) < 0)
           harderror(L_UDP_NOSEND); /* Unable to send UDP packet */

        /* Wait for a reply from the DNS server */
        FD_ZERO(&rx_set);
        FD_SET(s,&rx_set);
        maxd = s + 1;
        tv.tv_sec = timeout;
        tv.tv_usec = 0;
        n = select(maxd,&rx_set,NULL,NULL,&tv);
        if(n == -1)  /* select error */
            harderror("Select() failed");
        if(n == 0) /* Timeout */
            harderror("Timeout");
        if((count = recvfrom(s,indata->string,indata->max_count,0,
                        (struct sockaddr *)pdns_udp,
                        (socklen_t *)&len_inet)) < 0)
            harderror(L_DNS_R_ERROR); /* Problem getting DNS server response */

        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
        csv2_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_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;
        }

/* csv2-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 csv2_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 */

    if(header.tc == 1) {
        printf("# Remote server said: TRUNCATED\n");
    }

    /* If not 0, show them the rcode from the remote server */
    switch(header.rcode) {
        case 1:
                printf("# Remote server said: FORMAT ERROR\n");
                break;
        case 2:
                printf("# Remote server said: SERVER FAILURE\n");
                break;
        case 3:
                printf("# Remote server said: NAME ERROR\n");
                break;
        case 4:
                printf("# Remote server said: NOT IMPLEMENTED\n");
                break;
        case 5:
                printf("# Remote server said: REFUSED\n");
                break;
        case 0:
                break;
        default:
                printf("# Non-RFC1035 RCODE %d sent from remote server\n",
                       header.rcode);
        }

    /* 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++)
        csv2_answer(uindata,&place,query);

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

    /* Get all of the ar replies */
    printf("# %s%s",L_ARREP,L_NEWLINE);
    for(counter = 0; counter < header.arcount; counter++)
        csv2_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 csv2_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;
    js_string *t;
    int rtype;

    /* 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("#");

    rtype = rr_hdr.type;
    hname_translate(rr_hdr.name,rr_hdr.type);
    t = js_create(256,1);
    if(js_copy(rr_hdr.name,t) == JS_ERROR) {
            harderror("Couldn't copy hostname");
    }
    t->string++;
    t->unit_count--;
    escape_stdout(t);
    t->string--;
    t->unit_count++;
    js_destroy(t);
    if(rr_hdr.type == RR_SOA) {
        if(read_soa(uindata,&soa,*place) == JS_ERROR)
            harderror(L_READSOA);
        hname_translate(soa.mname,RR_MAGIC_SPACE);
        hname_translate(soa.rname,RR_MAGIC_EMAIL);
        printf(" +%u",rr_hdr.ttl);
        printf(" soa"); /* SOA mname */
        escape_stdout(soa.mname);
        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(" +%u mx %d",rr_hdr.ttl,mx.preference); /* MX pref. */
        hname_translate(mx.exchange,RR_MAGIC_SPACE); /* Makes the char a
                                                      * space */
        escape_stdout(mx.exchange);
        printf("\n");
        }
    else if(rr_hdr.type == RR_NS || rr_hdr.type == RR_CNAME ||
            rr_hdr.type == RR_PTR) {
        printf(" +%u",rr_hdr.ttl);
        switch(rr_hdr.type) {
                case RR_NS:
                        printf(" ns");
                        break;
                case RR_CNAME:
                        printf(" cname");
                        break;
                case RR_PTR:
                        printf(" ptr");
                        break;
                }
        read_ns(uindata,mx.exchange,*place);
        hname_translate(mx.exchange,RR_MAGIC_SPACE); /* First char is space */
        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(" +%u a %d%s%d%s%d%s%d\n", rr_hdr.ttl,
                        *(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 */
        p = (unsigned short*)(uindata->string + *place);
        printf(" +%u aaaa %x:%x:%x:%x:%x:%x:%x:%x\n", rr_hdr.ttl,
                   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) {
        int len,p;
        len = rr_hdr.rdlength;
        p = *place;
        printf(" +%u txt ",rr_hdr.ttl);
        /* XXX: This doesn't correctly handle multi-chunked TXT data */
        while(len > 0) {
            int q;
            q = read_txt(uindata,mx.exchange,p);
            if(q < 0) {
                printf("Problem reading TXT record\n");
                exit(1);
                }
            q++;
            escape_stdout_csv2(mx.exchange);
            len -= q;
            p += q;
            if(len > 0) {
                printf(";");
                }
            else {
                printf("\n");
                }
            }
         }
    else {
        printf(" +%u raw %d ",rr_hdr.ttl,rr_hdr.type);
        if(js_substr(uindata,mx.exchange,*place,rr_hdr.rdlength) == JS_ERROR) {
                printf("Problem copying string over\n");
                exit(55);
        }
        escape_stdout_csv2(mx.exchange);
        printf(" \n");
        }

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