~samiam/MaraDNS

ref: 3.5.0002 MaraDNS/dns/Queries.c -rw-r--r-- 32.6 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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
/* Copyright (c) 2002-2006 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.
 */

/* Routines for converting varous query data specified in RFC1035 in to
   data to send over the network */

#include "../MaraDns.h"
#include "functions_dns.h"

/* Determine the length of a domain-name label (if it is a compressed
   label, determine the length of the compressed portion)
   Input: the js_string obejct with the domain label in question,
          the offset from the beginning of the js_string object
          with the domain label
   Output: The length of the label, JS_ERROR on error
*/

int dlabel_length(js_string *raw, unsigned int offset) {

    int length;
    unsigned char toread;

    /* Sanity checks */
    if(js_has_sanity(raw) == JS_ERROR)
        return JS_ERROR;
    if(raw->unit_size != 1)
        return JS_ERROR;

    length = 0;
    if(offset > raw->unit_count)
        return JS_ERROR;
    toread = *(raw->string + offset);
    while(length < 256 && toread > 0 && toread != '_') {
        if(toread >= 192) /* compressed label */ {
            if(length + 2 + offset <= raw->unit_count)
               return length + 2;
            else
               return JS_ERROR;
            }
        if(toread > 63)
            return JS_ERROR; /* No EDNS support */
        length += toread + 1;
        /* Go to the next jump */
        if(length + offset > raw->unit_count)
            return JS_ERROR;
        toread = *(raw->string + length + offset);
        }

    if(length + 1 + offset <= raw->unit_count)
        return length + 1;

    return JS_ERROR;

    }

/* Given a q_header structure, initialize the values of the structure
   to sane values */

void init_header(q_header *header) {
    header->id = 0;
    header->qr = 0;
    header->opcode = 0;
    header->aa = 0;
    header->tc = 0;
    header->rd = 0;
    header->ra = 0;
    header->z  = 0;
    header->rcode = 0;
    header->qdcount = 0;
    header->ancount = 0;
    header->nscount = 0;
    header->arcount = 0;
    }

/* Given a q_header structure, and a js object to put the raw UDP data
   in, convert a q_header structure to raw UDP data.
   input: pointer to q_header structure, pointer to js_string object
   output: JS_ERROR on error, JS_SUCCESS on success
*/

int make_hdr(q_header *header, js_string *js) {
    unsigned char *raw; /* Raw data to be made js_string */

    /* Some sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
       return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(js->max_count < 14)
        return JS_ERROR;

    /* Speed things up by making this zero-copy */
    raw = js->string;

    /* Conversion is fairly straightforward */
    raw[0] = (header->id & 0xff00) >> 8;
    raw[1] = (header->id & 0x00ff);
    /* All the flags and 4-bit fields are a little tricky */
    raw[2] = raw[3] = 0;
    if(header->qr == 1)
        raw[2] |= 0x80;
    raw[2] |= (header->opcode & 0x0f) << 3;
    if(header->aa == 1)
        raw[2] |= 0x04;
    if(header->tc == 1)
        raw[2] |= 0x02;
    if(header->rd == 1)
        raw[2] |= 0x01;
    if(header->ra == 1)
        raw[3] |= 0x80;
    raw[3] |= (header->z & 0x07) << 4;
    raw[3] |= (header->rcode & 0x0f);
    /* Now the four unsigned 16-bit fields */
    raw[4] = (header->qdcount & 0xff00) >> 8;
    raw[5] = (header->qdcount & 0x00ff);
    raw[6] = (header->ancount & 0xff00) >> 8;
    raw[7] = (header->ancount & 0x00ff);
    raw[8] = (header->nscount & 0xff00) >> 8;
    raw[9] = (header->nscount & 0x00ff);
    raw[10] = (header->arcount & 0xff00) >> 8;
    raw[11] = (header->arcount & 0x00ff);

    /* Finally, make all that a legit js string */
    if(js->max_count >= 12)
        js->unit_count = 12;

    return JS_SUCCESS;
    }


/* Given a q_header structure, and a js object with the raw UDP data,
   convert raw UDP data to a q_header structure.
   input: pointer to q_header structure, pointer to js_string object
   output: JS_ERROR on error, JS_SUCCESS on success
*/

int read_hdr(js_string *js, q_header *header) {
    unsigned char *raw; /* Raw data to be made js_string */

    /* Some sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
       return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(js->max_count < 14)
        return JS_ERROR;

    raw = js->string;

    /* Conversion is fairly straightforward */
    header->id = raw[0] << 8 | raw[1];
    /* All the flags and 4-bit fields are a little tricky */
    if(raw[2] & 0x80)
        header->qr = 1;
    else
        header->qr = 0;
    header->opcode = (raw[2] & 0x78) >> 3;
    if(raw[2] & 0x04)
        header->aa = 1;
    else
        header->aa = 0;
    if(raw[2] & 0x02)
        header->tc = 1;
    else
        header->tc = 0;
    if(raw[2] & 0x01)
        header->rd = 1;
    else
        header->rd = 0;
    if(raw[3] & 0x80)
        header->ra = 1;
    else
        header->ra = 0;
    header->z = (raw[3] & 0x70) >> 4;
    header->rcode = raw[3] & 0x0f;
    /* Now the four unsigned 16-bit fields */
    header->qdcount = raw[4] << 8 | raw[5];
    header->ancount = raw[6] << 8 | raw[7];
    header->nscount = raw[8] << 8 | raw[9];
    header->arcount = raw[10] << 8 | raw[11];

    return JS_SUCCESS;

    }

/* Given a js string object and a q_question structure, place the raw UDP
   format of the query at the end of the js_string object
   input: see above
   output: JS_ERROR on error, JS_SUCCESS on success */

int make_question(q_question *question, js_string *js) {
    int counter;

    unsigned char toread, read;

    int offset = js->unit_count;

    unsigned char *raw;

    counter = toread = 0;

    /* Some sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(js_has_sanity(question->qname) == JS_ERROR)
        return JS_ERROR;
    if(question->qname->unit_size != 1)
        return JS_ERROR;
    if(offset >= js->max_count)
        return JS_ERROR;

    raw = question->qname->string;

    /* Copy over the domain-name string in to the js_string object */
    while(counter < 256) {
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < question->qname->max_count &&
               counter + offset < js->max_count)
                /* Then copy the character over */
                *(js->string + offset + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }
        }

    /* Since counter is now sitting on the dot after .com, increment it */
    *(js->string + offset + counter) = 0;
    counter++;

    /* Add the QTYPE and QCLASS to the raw UDP data */
    if(counter + offset + 4 < js->max_count) {
        raw = js->string + counter + offset;
        raw[0] = (question->qtype & 0xff00) >> 8;
        raw[1] = question->qtype & 0x00ff;
        raw[2] = (question->qclass & 0xff00) >> 8;
        raw[3] = question->qclass & 0x00ff;
        }
    else
        return JS_ERROR;

    /* Resize the modified js string */
    if(js->max_count > counter + offset + 4)
        js->unit_count = counter + offset + 4;

    return JS_SUCCESS;

    }

/* Given a js string object and an offset (where we begin reading our
   question), in addition to a q_question structure, read the raw UDP
   format of the query in to the q_question structure
   input: see above
   output: JS_ERROR on error, number of bytes in question on success */

int read_question(js_string *js,q_question *question, int offset) {
    int counter;

    unsigned char toread, read;

    unsigned char *raw;

    counter = toread = 0;

    /* Some sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(js_has_sanity(question->qname) == JS_ERROR)
        return JS_ERROR;
    if(question->qname->unit_size != 1)
        return JS_ERROR;
    if(offset >= js->unit_count)
        return JS_ERROR;

    raw = js->string + offset;

    /* Copy over the domain-name string inside the js_string object */
    while(counter < 256) {
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < question->qname->max_count &&
               counter + offset < js->unit_count)
                /* Then copy the character over */
                *(question->qname->string + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }

        }

    /* Since counter is now sitting on the dot after .com, increment it */
    *(question->qname->string + counter) = 0;
    counter++;

    /* Get the QTYPE and QCLASS from the raw UDP data */
    if(counter + offset + 4 <= js->unit_count) {
        raw = js->string + counter + offset;
        question->qtype = (raw[0] << 8 & 0xff00) | (raw[1] & 0xff);
        question->qclass = (raw[2] << 8 & 0xff00) | (raw[3] & 0xff);
        }
    else
        return JS_ERROR;

    /* Resize the qname string in the question structure */
    if(counter < question->qname->max_count)
        question->qname->unit_count = counter;
    else
        return JS_ERROR;

    return counter + 4;

    }

/* Zero-copy implemention of converting the raw UDP data for a domain in
   to a human-readable host name.
   Input:  pointer to js_string object we modify "in place", query type
           (-2 if we wish to make it show a pipe)
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int hname_translate(js_string *hostname, int qtype) {
    int counter = 0, special_root_case = 0;
    unsigned char toread = 0;
    unsigned char *raw = 0;
    int dotseen = 0;

    /* Sanity checks */
    if(js_has_sanity(hostname) == JS_ERROR)
        return JS_ERROR;
    if(hostname->unit_size != 1)
        return JS_ERROR;
    if(hostname->unit_count < 1)
        return JS_ERROR;

    /* Store the length of the first part of the hostname */
    toread = *(hostname->string);

    /* Handle the special case of a zero-length zone name */
    if(hostname->unit_count == 1 && toread == 0)
        special_root_case = 1;

    /* Change the first letter based on the Query type */
    raw = hostname->string;
    switch(qtype) {
        case RR_A:
            *raw = 'A';
            break;
        case RR_NS:
            *raw = 'N';
            break;
        case RR_CNAME:
            *raw = 'C';
            break;
        case RR_SOA:
            *raw = 'S';
            break;
        case RR_PTR:
            *raw = 'P';
            break;
        case RR_MX:
            *raw = '@';
            break;
        case RR_TXT:
            *raw = 'T';
            break;
        case RR_ANY:
            *raw = 'Z';
            break;
        case -2: /* Special command to make it a pipe */
            *raw = '|';
            break;
        case RR_MAGIC_SPACE: /* Special command to make it a space */
        case RR_MAGIC_EMAIL: /* Same as magic space, but make the first
                              * seperator an @ instead of a dot */
            *raw = ' ';
            break;
        default:
            *raw = 'U';
        }

     /* Again, handle the special case of a zero-length host name
        (the "root" of the DNS tree) */
     if(special_root_case == 1) {
        if(hostname->max_count < 2)
            return JS_ERROR;
        hostname->unit_count = 2;
        raw++;
        *raw = '.';
        return JS_SUCCESS;
        }

     /* Convert the hostname delimters in to dots */
     counter = 0;
     dotseen = 0;
     while(counter < 256 && toread > 0) {
         if(toread > 63)
             return JS_ERROR; /* To do: compression support */
         counter += toread + 1;
         /* Get the next "jump" we need to do */
         if(counter > hostname->unit_count)
             return JS_ERROR;
         toread = *(raw + counter);
         /* Buffer overflow protection */
         if(counter <= hostname->unit_count) {
             if(qtype != RR_MAGIC_EMAIL || dotseen > 0) {
                 *(raw + counter) = '.';
             } else {
                 *(raw + counter) = '@';
                 dotseen = 1;
             }
         } else {
             return JS_ERROR;
             }
         }

     return JS_SUCCESS;
     }

/* Zero-copy implemention of converting the raw UDP data for a domain in
   to a human-readable email address
   Input:  pointer to js_string object we modify "in place"
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int email_translate(js_string *hostname) {
    int counter;
    unsigned char toread;
    int first = 1;
    unsigned char *raw;

    /* Sanity checks */
    if(js_has_sanity(hostname) == JS_ERROR)
        return JS_ERROR;
    if(hostname->unit_size != 1)
        return JS_ERROR;
    if(hostname->unit_count < 1)
        return JS_ERROR;

    /* Store the length of the first part of the hostname */
    toread = *(hostname->string);

    /* Change the first letter based on the Query type */
    raw = hostname->string;
    *raw = '|';

     /* Convert the hostname delimters in to dots */
     counter = 0;
     while(counter < 256 && toread > 0) {
         if(toread > 63)
             return JS_ERROR; /* To do: compression support */
         counter += toread + 1;
         /* Get the next "jump" we need to do */
         if(counter > hostname->unit_count)
             return JS_ERROR;
         toread = *(raw + counter);
         /* Buffer overflow protection */
         if(counter <= hostname->unit_count) {
             if(first) {
                 *(raw + counter) = '@';
                 first = 0;
                 }
             else {
                 *(raw + counter) = '.';
                 }
             }
         else
             return JS_ERROR;
         }

     return JS_SUCCESS;
     }

/* Zero-copy implemention of converting a human-readable host name in to the
   raw RFC1035 UDP data for a domain.
   Input:  pointer to js_string object we modify "in place"
   Output: -1 on error, numeric type of query on success, -2 on
           unsupported query type, -3 on 'U' query type (which
           then has to be specified by the user elsewhere)
*/

int hname_2rfc1035(js_string *hostname) {
    return hname_2rfc1035_starwhitis(hostname,0);
    }

/* Starwhitis: 0: Stars *not* allowed at end of host name
               1: Stars are allowed at host name ends */

int hname_2rfc1035_starwhitis(js_string *hostname, int starwhitis) {
    int counter,seen;
    unsigned char *raw, *towrite;
    int ret = -2;

    /* Sanity checks */
    if(js_has_sanity(hostname) == JS_ERROR)
        return JS_ERROR;
    if(hostname->unit_size != 1)
        return JS_ERROR;
    if(hostname->unit_count < 1)
        return JS_ERROR;

    /* Store the length of the first part of the hostname */
    raw = towrite = hostname->string;

    /* Get the query type from the first letter */
    switch(*raw) {
        case 'A':
            ret = RR_A;
            break;
        case 'N':
            ret = RR_NS;
            break;
        case 'C':
            ret = RR_CNAME;
            break;
        case 'S':
            ret = RR_SOA;
            break;
        case 'P':
            ret = RR_PTR;
            break;
        case '@':
            ret = RR_MX;
            break;
        case 'T':
            ret = RR_TXT;
            break;
        case 'U':
            ret = -3;
            break;
        case 'Z':
            ret = RR_ANY;
            break;
        default:
            return -2;
        }

     /* Handle the trivial case of an input in the form "A." */
     if(*(raw + 1) == '.' && hostname->unit_count == 2) {
         *raw = 0;
         hostname->unit_count = 1;
         return ret;
         }

     /* Also handle the case of the hostname being *just* a star */
     if(*(raw + 1) == '*' && hostname->unit_count == 2 &&
                     starwhitis == 1) {
         *raw = '_';
         hostname->unit_count = 1;
         return ret;
         }

     /* Convert the dots in to hostname delimiters */
     seen = counter = 0;
     while(counter < 256 && counter < hostname->unit_count) {
         counter++;
         if(counter > hostname->unit_count)
             return JS_ERROR;
         if(*(raw + counter) == '.') {
             if(seen < 1 || seen > 63)
                 return JS_ERROR;
             *towrite = seen;
             towrite = raw + counter;
             seen = 0;
             }
         else
             seen++;
         if(counter == hostname->unit_count - 1) /* The final '.' */
             break;
         }

     /* That last '0' for the dot after .com */
     if(counter < 256 && *(raw + counter) == '.') {
         *(raw + counter) = 0;
     /* We need to deal with trailing * characters */
     } else if(counter < 256 && counter > 1 && *(raw + counter) == '*' &&
                     hostname->unit_count > 1 && starwhitis == 1) {
         *(raw + counter - 1) = '_';
         hostname->unit_count--;
     }
     else {
         return JS_ERROR;
     }

     return ret;
     }

/* Zero-copy implemention of converting a human-readable email address in to
   the raw RFC1035 UDP data for a domain.
   Input:  pointer to js_string object we modify "in place" (needs a
           one-octet 'prefix' which can be be any character
   Output: -1 on error, JS_SUCCESS on success
*/

int email_2rfc1035(js_string *hostname) {
     int counter,seen;
     unsigned char *raw, *towrite;

     int firstat = 1;

     /* Sanity checks */
     if(js_has_sanity(hostname) == JS_ERROR)
         return JS_ERROR;
     if(hostname->unit_size != 1)
         return JS_ERROR;
     if(hostname->unit_count < 1)
         return JS_ERROR;

     /* Store the length of the first part of the hostname */
     raw = towrite = hostname->string;

     /* Convert the at/dots in to hostname delimiters */
     seen = counter = 0;
     /* Handle the special case of this being just a dot by itself */
     if(hostname->unit_count == 2 && *(raw + 1) == '.') {
         hostname->unit_count = 1;
         *(hostname->string) = '\0';
         return JS_SUCCESS;
         }

     while(counter < 256 && counter < hostname->unit_count) {
         counter++;
         if(counter > hostname->unit_count)
             return JS_ERROR;
         if((!firstat && *(raw + counter) == '.') ||
             (firstat && *(raw + counter) == '@')) {
             firstat = 0;
             if(seen < 1 || seen > 63)
                 return JS_ERROR;
             *towrite = seen;
             towrite = raw + counter;
             seen = 0;
             }
         else
             seen++;
         if(counter == hostname->unit_count - 1) /* The final '.' */
             break;
         }

     /* Handle the case of this string not having an @ in it by
      * processing the string as one with just dots */
     if(firstat) {
         return hname_2rfc1035_starwhitis(hostname,0);
         }

     /* That last '0' for the dot after .com */
     if(counter < 256 && *(raw + counter) == '.')
         *(raw + counter) = 0;
     else
         return JS_ERROR;

     return JS_SUCCESS;
     }

/* Process the header of a RR record as described in section 4.1.3 of
   RFC1035.  This converts the contents of a RFC1035 header in to an
   q_rr structure.
   input: js_string obejct with the raw UDP data, q_rr struct to put data
          in, offset form beginning of string to look at data for
   output: number of bytes in rr header on success, JS_ERROR on error
*/

int read_rr_h (js_string *js, q_rr *hdr, int offset) {
    int counter;
    unsigned char toread, read;

    unsigned char *raw;

    counter = toread = 0;

    /* Some sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(js_has_sanity(hdr->name) == JS_ERROR)
        return JS_ERROR;
    if(hdr->name->unit_size != 1)
        return JS_ERROR;
    if(offset >= js->max_count)
        return JS_ERROR;

    raw = js->string + offset;

    /* Copy over the domain-name string inside the js_string object */
    while(counter < 256) {
        if(offset + counter > js->unit_count)
            return JS_ERROR;
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < hdr->name->max_count &&
               counter + offset < js->unit_count)
                /* Then copy the character over */
                *(hdr->name->string + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }

        }

    /* Do not forget to copy over that dot after .com */
    *(hdr->name->string + counter) = *(raw + counter);
    /* Since counter is now sitting on the dot after .com, increment it */
    counter++;

    /* Get TYPE, CLASS, TTL, and RDLENGTH from the header */
    if(counter + offset + 10 <= js->unit_count) {
        raw = js->string + counter + offset;
        hdr->type = raw[0] << 8 | raw[1];
        hdr->class = raw[2] << 8 | raw[3];
        hdr->ttl = raw[4] << 24 | raw[5] << 16 | raw[6] << 8 | raw[7];
        hdr->rdlength = raw[8] << 8 | raw[9];
        }
    else
        return JS_ERROR;

    if(counter < hdr->name->max_count)
        hdr->name->unit_count = counter;
    else
        return JS_ERROR;

    return counter + 10;
    }

/* Process the data for various RR types */

/* read_soa: Read a SOA record.
   input: Pointer to js_string, pointer to rr_soa structure, offset
   output: JS_ERROR on error, bytes in SOA record on success
*/
int read_soa(js_string *js, rr_soa *soa, int offset) {
    int counter;
    unsigned char read,toread;

    unsigned char *raw;

    /* Sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(js_has_sanity(soa->mname) == JS_ERROR)
        return JS_ERROR;
    if(js_has_sanity(soa->rname) == JS_ERROR)
        return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    if(soa->mname->unit_size != 1)
        return JS_ERROR;
    if(soa->rname->unit_size != 1)
        return JS_ERROR;
    if(js->unit_count < offset)
        return JS_ERROR;

    raw = js->string + offset;

    /* Copy over the first domain-name string inside the js_string object */
    counter = 0;
    while(counter < 256) {
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < soa->mname->max_count &&
               counter + offset < js->unit_count)
                /* Then copy the character over */
                *(soa->mname->string + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }

        }

    *(soa->mname->string + counter) = 0;
    counter++; /* Do not forget the dot after .com */
    offset += counter;
    if(soa->mname->max_count < counter)
        return JS_ERROR;
    soa->mname->unit_count = counter;

    raw = js->string + offset;

    /* Copy over the 2nd domain-name string inside the js_string object */
    counter = 0;
    while(counter < 256) {
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < soa->rname->max_count &&
               counter + offset < js->unit_count)
                /* Then copy the character over */
                *(soa->rname->string + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }

        }

    *(soa->rname->string + counter) = 0;
    counter++; /* Do not forget the dot after .com */
    if(soa->rname->max_count < counter)
        return JS_ERROR;
    soa->rname->unit_count = counter;

    if(counter + offset + 20 > js->unit_count)
        return JS_ERROR;

    raw = js->string + counter + offset;
    soa->serial = raw[0] << 24 | raw[1] << 16 | raw[2] << 8 | raw[3];
    soa->refresh = raw[4] << 24 | raw[5] << 16 | raw[6] << 8 | raw[7];
    soa->retry = raw[8] << 24 | raw[9] << 16 | raw[10] << 8 | raw[11];
    soa->expire = raw[12] << 24 | raw[13] << 16 | raw[14] << 8 | raw[15];
    soa->minimum = raw[16] << 24 | raw[17] << 16 | raw[18] << 8 | raw[19];

    /* Return the number of bytes read */
    return counter + 20;

    }

/* read_ns: Read a NS (or any other <domain-name>) record
   input: js_string object with raw UDP data, js_string object to have just
          the NS record, offset from beginning of raw UDP data to get RR
   output: JS_ERROR on ERROR, bytes in <domain-name> on success
*/

int read_ns(js_string *in, js_string *out, int offset) {
    uint16 toread,read;
    int counter;
    unsigned char *raw;

    /* Sanity checks */
    if(js_has_sanity(in) == JS_ERROR)
        return JS_ERROR;
    if(js_has_sanity(out) == JS_ERROR)
        return JS_ERROR;
    if(in->unit_size != 1)
        return JS_ERROR;
    if(out->unit_size != 1)
        return JS_ERROR;
    if(in->unit_count < offset)
        return JS_ERROR;

    raw = in->string + offset;
    /* Copy over the domain-name string inside the js_string object */
    counter = 0;
    while(counter < 256) {
        toread = *(raw + counter);
        if(toread > 63) /* To do: compression support */
            return JS_ERROR;
        read = 0;
        if(toread == 0) /* 0-length query means we are at the dot after .com */
            break;
        while(read <= toread) {
            /* Overflow protection */
            if(counter < out->max_count &&
               counter + offset < in->unit_count)
                /* Then copy the character over */
                *(out->string + counter) = *(raw + counter);
            else
                return JS_ERROR;
            read++;
            counter++;
            }

        }

    *(out->string + counter) = 0;
    counter++; /* Do not forget the dot after .com */
    offset += counter;
    if(out->max_count < counter)
        return JS_ERROR;
    out->unit_count = counter;

    return counter;
    }

/* Process the RR portion of a TXT record.
   Input: pointer to string of uncompressed UDP data, pointer of string to
          put txt record in, offset from beginning of UDP data
   Output: JS_ERROR on error, byes in TXT record on success
*/

int read_txt(js_string *in, js_string *out, int offset) {
    int counter;
    unsigned char toread;

    counter = 0;

    /* Sanity checks */
    if(js_has_sanity(in) == JS_ERROR)
        return JS_ERROR;
    if(js_has_sanity(out) == JS_ERROR)
        return JS_ERROR;
    if(in->unit_size != 1)
        return JS_ERROR;
    if(out->unit_size != 1)
        return JS_ERROR;
    if(in->unit_count < offset)
        return JS_ERROR;

    /* TXT record: Number of bytes to read followed by the bytes */
    toread = *(in->string + offset);

    /* Security overflow protection */
    if(toread >= out->max_count || offset + toread >= in->unit_count)
        return JS_ERROR;

    /* Copy the TXT string over */
    offset++;
    for(counter = 0;counter < toread; counter++)
        *(out->string + counter) = *(in->string + offset + counter);

    out->unit_count = toread;
    return out->unit_count;
    }

/* This function is designed to make a packet which compresses to a size
 * greater than 512 bytes smaller.  The input is an uncompressed DNS packet;
 * the output is the same packet with the last DNS record removed from the
 * packet.  As per RFC2181 9, if we are removing a DNS record from the NS
 * or AN section of a reply, the TC bit is not set.  Otherwise (as a
 * reasonable interpretation of the wishes in RFC2181 9), we remove all DNS
 * information except the header and mark the DNS packet truncated.
 *
 * Input: A js_string object with a full uncompressed raw DNS packet.
 *
 * Output: A point to the above js_string object on success; 0 on error */

js_string *squeeze_to_fit(js_string *packet) {
        q_header hdr;
        int qd,an,ns,ar; /* counts of the various record types */
        int qc,count;
        int offset;

        if(read_hdr(packet,&hdr) == JS_ERROR) {
                return 0;
        }

        qd = hdr.qdcount;
        an = hdr.ancount;
        ns = hdr.nscount;
        ar = hdr.arcount;

        /* If the packet is already marked truncated, we remove all records
         * past the record and otherwise return it as is;
         * In the case of there being no ns nor ar records, we do the same
         * thing */

        if(hdr.tc == 1 || (ns == 0 && ar == 0)) {
                hdr.tc = 1;
                hdr.qdcount = hdr.ancount = hdr.nscount = hdr.arcount = 0;
                make_hdr(&hdr,packet);
                return packet;
        }

        qc = qd;
        count = an + ns + ar;
        offset = 12; /* Beginning of first question */
        /* Jump past all of the questions */
        while(qc > 0) {
                int len;
                len = dlabel_length(packet,offset);
                if(len == JS_ERROR) {
                        hdr.tc = 1;
                        hdr.qdcount = hdr.ancount = hdr.nscount =
                                hdr.arcount = 0;
                        make_hdr(&hdr,packet);
                        return packet;
                }
                len += 4; /* Type and class */
                offset += len;
                if(offset >= packet->unit_count) {
                        hdr.tc = 1;
                        hdr.qdcount = hdr.ancount = hdr.nscount =
                                hdr.arcount = 0;
                        make_hdr(&hdr,packet);
                        return packet;
                }
                qc--;
        }

        /* Jump past all of the answers except for the last one */
        while(count > 1) {
                int len;
                len = dlabel_length(packet,offset);
                if(len == JS_ERROR) {
                        hdr.tc = 1;
                        hdr.qdcount = hdr.ancount = hdr.nscount =
                                hdr.arcount = 0;
                        make_hdr(&hdr,packet);
                        return packet;
                }
                len += 8; /* type, class, TTL, and RDLENGTH */
                offset += len;
                if(offset + 2 >= packet->unit_count) {
                        hdr.tc = 1;
                        hdr.qdcount = hdr.ancount = hdr.nscount =
                                hdr.arcount = 0;
                        make_hdr(&hdr,packet);
                        return packet;
                }
                /* Get len from the rdlength of the data (this is why the
                 * packet *must* be uncompressed) */
                len = (((*(packet->string + offset ) & 0xff) << 8) +
                      *(packet->string + offset + 1));
                offset += len + 2;
                if(offset >= packet->unit_count) {
                        hdr.tc = 1;
                        hdr.qdcount = hdr.ancount = hdr.nscount =
                                hdr.arcount = 0;
                        make_hdr(&hdr,packet);
                        return packet;
                }
                count--;
        }

        /* Now that we've found the last DNS data, remove it from the
         * packet */
        if(ar > 0) {
                ar--;
                hdr.arcount = ar;
        } else if(ns > 0) {
                ns--;
                hdr.nscount = ns;
        } else {
                hdr.tc = 1;
                hdr.qdcount = hdr.ancount = hdr.nscount = hdr.arcount = 0;
                make_hdr(&hdr,packet);
                return packet;
        }

        hdr.tc = 0;
        make_hdr(&hdr,packet);
        packet->unit_count = offset;
        return packet;

}