forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnscache.c
executable file
·804 lines (612 loc) · 17.8 KB
/
dnscache.c
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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "dnscache.h"
#include "dnsparser.h"
#include "dnsgenerator.h"
#include "utils.h"
#include "querydnsbase.h"
#include "rwlock.h"
#include "cacheht.h"
#define CACHE_VERSION 22
#define CACHE_END '\x0A'
#define CACHE_START '\xFF'
static BOOL Inited = FALSE;
static RWLock CacheLock;
static FileHandle CacheFileHandle;
static MappingHandle CacheMappingHandle;
static char *MapStart;
static ThreadHandle TTLCountdown_Thread;
static int32_t CacheSize;
static int OverrideTTL;
static int TTLMultiple;
static BOOL IgnoreTTL;
static int32_t *CacheCount;
static volatile int32_t *CacheEnd; /* Offset */
static CacheHT *CacheInfo;
struct _Header{
uint32_t Ver;
int32_t CacheSize;
int32_t End;
int32_t CacheCount;
CacheHT ht;
char Comment[128 - sizeof(uint32_t) - sizeof(int32_t) - sizeof(int32_t) - sizeof(int32_t) - sizeof(CacheHT)];
};
static void DNSCacheTTLCountdown_Thread(void)
{
register int loop;
register BOOL GotMutex = FALSE;
register Cht_Node *Node = NULL;
register Array *ChunkList = &(CacheInfo -> NodeChunk);
register time_t CurrentTime;
while( Inited )
{
CurrentTime = time(NULL);
loop = ChunkList -> Used - 1;
Node = (Cht_Node *)Array_GetBySubscript(ChunkList, loop);
while( Node != NULL )
{
if( Node -> TTL > 0 )
{
if( CurrentTime - Node -> TimeAdded >= Node -> TTL )
{
if(GotMutex == FALSE)
{
RWLock_WrLock(CacheLock);
GotMutex = TRUE;
}
Node -> TTL = 0;
*(char *)(MapStart + Node -> Offset) = 0xFD;
CacheHT_RemoveFromSlot(CacheInfo, loop, Node);
--(*CacheCount);
}
}
Node = (Cht_Node *)Array_GetBySubscript(ChunkList, --loop);
}
if(GotMutex == TRUE)
{
if( ChunkList -> Used == 0 )
{
(*CacheEnd) = sizeof(struct _Header);
} else {
Node = (Cht_Node *)(Cht_Node *)Array_GetBySubscript(ChunkList, ChunkList -> Used - 1);
(*CacheEnd) = Node -> Offset + Node -> Length;
}
RWLock_UnWLock(CacheLock);
GotMutex = FALSE;
}
SLEEP(59000);
}
TTLCountdown_Thread = INVALID_THREAD;
}
static BOOL IsReloadable(void)
{
struct _Header *Header = (struct _Header *)MapStart;
if( Header -> Ver != CACHE_VERSION )
{
ERRORMSG("The existing cache is not compatible with this version of program.\n");
return FALSE;
}
if( Header -> CacheSize != CacheSize )
{
ERRORMSG("The size of the existing cache and the value of `CacheSize' should be equal.\n");
return FALSE;
}
return TRUE;
}
static void ReloadCache(void)
{
struct _Header *Header = (struct _Header *)MapStart;
INFO("Reloading the cache ...\n");
CacheInfo = &(Header -> ht);
CacheHT_ReInit(CacheInfo, MapStart, CacheSize);
CacheEnd = &(Header -> End);
CacheCount = &(Header -> CacheCount);
INFO("Cache reloaded, containing %d entries for %d items.\n", CacheInfo -> NodeChunk.Used, (*CacheCount));
}
static void CreateNewCache(void)
{
struct _Header *Header = (struct _Header *)MapStart;
memset(MapStart, 0, CacheSize);
Header -> Ver = CACHE_VERSION;
Header -> CacheSize = CacheSize;
Header -> CacheCount = 0;
CacheEnd = &(Header -> End);
*CacheEnd = sizeof(struct _Header);
memset(Header -> Comment, 0, sizeof(Header -> Comment));
strcpy(Header -> Comment, "\nDo not edit this file.\n");
CacheInfo = &(Header -> ht);
CacheCount = &(Header -> CacheCount);
CacheHT_Init(CacheInfo, MapStart, CacheSize);
}
static int InitCacheInfo(ConfigFileInfo *ConfigInfo, BOOL Reload)
{
if( Reload == TRUE )
{
if( IsReloadable() )
{
ReloadCache();
} else {
if( ConfigGetBoolean(ConfigInfo, "OverwriteCache") == FALSE )
{
return -1;
} else {
CreateNewCache();
INFO("The existing cache has been overwritten.\n");
}
}
} else {
CreateNewCache();
}
return 0;
}
int DNSCache_Init(ConfigFileInfo *ConfigInfo)
{
int _CacheSize = ConfigGetInt32(ConfigInfo, "CacheSize");
const char *CacheFile = ConfigGetRawString(ConfigInfo, "CacheFile");
int InitCacheInfoState;
if( ConfigGetBoolean(ConfigInfo, "UseCache") == FALSE )
{
return 0;
}
IgnoreTTL = ConfigGetBoolean(ConfigInfo, "IgnoreTTL");
OverrideTTL = ConfigGetInt32(ConfigInfo, "OverrideTTL");
if( OverrideTTL > -1 )
{
TTLMultiple = 1;
} else {
TTLMultiple = ConfigGetInt32(ConfigInfo, "MultipleTTL");
if( TTLMultiple < 1 )
{
ERRORMSG("Invalid `MultipleTTL'.\n");
TTLMultiple = 1;
}
}
if( _CacheSize % 8 != 0 )
{
CacheSize = ROUND_UP(_CacheSize, 8);
} else {
CacheSize = _CacheSize;
}
if( CacheSize < 102400 )
{
ERRORMSG("Cache size must not less than 102400 bytes.\n");
return 1;
}
if( ConfigGetBoolean(ConfigInfo, "MemoryCache") == TRUE )
{
MapStart = SafeMalloc(CacheSize);
if( MapStart == NULL )
{
ERRORMSG("Cache initializing failed.\n");
return 2;
}
InitCacheInfoState = InitCacheInfo(ConfigInfo, FALSE);
} else {
BOOL FileExists;
INFO("Cache File : %s\n", CacheFile);
FileExists = FileIsReadable(CacheFile);
CacheFileHandle = OPEN_FILE(CacheFile);
if(CacheFileHandle == INVALID_FILE)
{
int ErrorNum = GET_LAST_ERROR();
char ErrorMessage[320];
GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
ERRORMSG("Cache initializing failed : %d : %s.\n", ErrorNum, ErrorMessage);
return 3;
}
CacheMappingHandle = CREATE_FILE_MAPPING(CacheFileHandle, CacheSize);
if(CacheMappingHandle == INVALID_MAP)
{
int ErrorNum = GET_LAST_ERROR();
char ErrorMessage[320];
GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
ERRORMSG("Cache initializing failed : %d : %s.\n", ErrorNum, ErrorMessage);
return 4;
}
MapStart = (char *)MPA_FILE(CacheMappingHandle, CacheSize);
if(MapStart == INVALID_MAPPING_FILE)
{
int ErrorNum = GET_LAST_ERROR();
char ErrorMessage[320];
GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
ERRORMSG("Cache initializing failed : %d : %s.\n", ErrorNum, ErrorMessage);
return 5;
}
if( FileExists == FALSE )
{
InitCacheInfoState = InitCacheInfo(ConfigInfo, FALSE);
} else {
InitCacheInfoState = InitCacheInfo(ConfigInfo, ConfigGetBoolean(ConfigInfo, "ReloadCache"));
}
}
if( InitCacheInfoState != 0 )
{
return 6;
}
RWLock_Init(CacheLock);
Inited = TRUE;
if(IgnoreTTL == FALSE)
CREATE_THREAD(DNSCacheTTLCountdown_Thread, NULL, TTLCountdown_Thread);
return 0;
}
BOOL Cache_IsInited(void)
{
return Inited;
}
static int32_t DNSCache_GetAviliableChunk(uint32_t Length, Cht_Node **Out)
{
int32_t NodeNumber;
Cht_Node *Node;
uint32_t RoundedLength = ROUND_UP(Length, 8);
BOOL NewCreated;
NodeNumber = CacheHT_FindUnusedNode(CacheInfo, RoundedLength, &Node, MapStart + (*CacheEnd) + RoundedLength, &NewCreated);
if( NodeNumber >= 0 )
{
if( NewCreated == TRUE )
{
Node -> Offset = (*CacheEnd);
(*CacheEnd) += RoundedLength;
}
memset(MapStart + Node -> Offset + Length, 0xFE, RoundedLength - Length);
*Out = Node;
return NodeNumber;
} else {
*Out = NULL;
return -1;
}
}
static Cht_Node *DNSCache_FindFromCache(char *Content, size_t Length, Cht_Node *Start, time_t CurrentTime)
{
Cht_Node *Node = Start;
do{
Node = CacheHT_Get(CacheInfo, Content, Node, NULL);
if( Node == NULL )
{
return NULL;
}
if( IgnoreTTL == TRUE || (CurrentTime - Node -> TimeAdded < Node -> TTL) )
{
if( memcmp(Content, MapStart + Node -> Offset + 1, Length) == 0 )
{
return Node;
}
}
} while( TRUE );
}
static char *DNSCache_GenerateTextFromRawRecord(const char *DNSBody, const char *DataBody, int DataLangth, char *Buffer, DNSRecordType ResourceType)
{
int loop2;
const ElementDescriptor *Descriptor;
int DescriptorCount;
if(Buffer == NULL) return NULL;
DescriptorCount = DNSGetDescriptor(ResourceType, TRUE, &Descriptor);
if(DescriptorCount != 0)
{
char InnerBuffer[512];
DNSDataInfo Data;
for(loop2 = 0; loop2 != DescriptorCount; ++loop2)
{
Data = DNSParseData(DNSBody, DataBody, DataLangth, InnerBuffer, sizeof(InnerBuffer), Descriptor, DescriptorCount, loop2 + 1);
switch(Data.DataType)
{
case DNS_DATA_TYPE_INT:
if(Data.DataLength == 1)Buffer += sprintf(Buffer, "%d", (int)*(char *)InnerBuffer);
if(Data.DataLength == 2)Buffer += sprintf(Buffer, "%d", (int)*(int16_t *)InnerBuffer);
if(Data.DataLength == 4)Buffer += sprintf(Buffer, "%u", *(int32_t *)InnerBuffer);
break;
case DNS_DATA_TYPE_UINT:
if(Data.DataLength == 1)Buffer += sprintf(Buffer, "%d", (int)*(unsigned char *)InnerBuffer);
if(Data.DataLength == 2)Buffer += sprintf(Buffer, "%d", (int)*(uint16_t *)InnerBuffer);
if(Data.DataLength == 4)Buffer += sprintf(Buffer, "%u", *(uint32_t *)InnerBuffer);
break;
case DNS_DATA_TYPE_STRING:
Buffer += sprintf(Buffer, "%s", InnerBuffer);
break;
default:
break;
}
*Buffer++ = '\0';
}
return Buffer;
} else {
return NULL;
}
}
static int DNSCache_AddAItemToCache(const char *DNSBody, const char *RecordBody, time_t CurrentTime)
{
/* used to store cache data temporarily, no bounds checking here */
char Buffer[512];
/* Iterator of `Buffer' */
char *BufferItr = Buffer;
/* Assign start byte of the cache */
Buffer[0] = CACHE_START;
/* Assign the name of the cache */
DNSGetHostName(DNSBody, RecordBody, Buffer + 1, sizeof(Buffer) -1);
/* Jump just over the name */
BufferItr += strlen(Buffer);
/* Set record type and class */
BufferItr += sprintf(BufferItr, "\1%d\1%d", (int)DNSGetRecordType(RecordBody), (int)DNSGetRecordClass(RecordBody));
/* End of class */
*BufferItr++ = '\0';
/* Generate data and store them */
BufferItr = DNSCache_GenerateTextFromRawRecord(DNSBody, DNSGetResourceDataPos(RecordBody), DNSGetResourceDataLength(RecordBody), BufferItr, (DNSRecordType)DNSGetRecordType(RecordBody));
/* If it failed in generating data, stop everything else */
if(BufferItr == NULL) return 0;
/* Mark the end */
*BufferItr = CACHE_END;
/* The whole cache data generating completed */
/* Add the cache item to the main cache zone below */
/* Determine whether the cache item has existed in the main cache zone */
if(DNSCache_FindFromCache(Buffer + 1, BufferItr - Buffer, NULL, CurrentTime) == NULL)
{
/* If not, add it */
/* Subscript of a chunk in the main cache zone */
int32_t Subscript;
uint32_t RecordTTL;
/* Node with subscript `Subscript' */
Cht_Node *Node;
if(OverrideTTL < 0)
{
RecordTTL = DNSGetTTL(RecordBody) * TTLMultiple;
} else {
RecordTTL = OverrideTTL;
}
if( RecordTTL == 0 )
{
return 0;
}
/* Get a usable chunk and its subscript */
Subscript = DNSCache_GetAviliableChunk(BufferItr - Buffer + 1, &Node);
/* If there is a usable chunk */
if(Subscript >= 0)
{
/* Copy the cache to this entry */
memcpy(MapStart + Node -> Offset, Buffer, BufferItr - Buffer + 1);
/* Assign TTL */
Node -> TTL = RecordTTL;
Node -> TimeAdded = CurrentTime;
/* Index this entry on the hash table */
CacheHT_InsertToSlot(CacheInfo, Buffer + 1, Subscript, Node, NULL);
++(*CacheCount);
} else {
return -1;
}
}
return 0;
}
int DNSCache_AddItemsToCache(char *DNSBody, time_t CurrentTime)
{
int loop;
int AnswerCount;
if(Inited == FALSE) return 0;
if(TTLMultiple < 1) return 0;
AnswerCount = DNSGetAnswerCount(DNSBody);
RWLock_WrLock(CacheLock);
for(loop = 1; loop != AnswerCount + 1; ++loop)
{
if( DNSCache_AddAItemToCache(DNSBody, DNSGetAnswerRecordPosition(DNSBody, loop), CurrentTime) != 0 )
{
RWLock_UnWLock(CacheLock);
return -1;
}
}
RWLock_UnWLock(CacheLock);
return 0;
}
static int DNSCache_GenerateDatasFromCache( __in char *Datas,
__inout char *Buffer,
__in int BufferLength,
__in const ElementDescriptor *Descriptor,
__in int CountOfDescriptor
)
{
int TotleLength = 0;
int SingleLength;
int loop;
for(loop = 0; loop != CountOfDescriptor && *Datas != CACHE_END; ++loop)
{
SingleLength = DNSGenerateData(Datas, NULL, 0, Descriptor + loop);
if( BufferLength < SingleLength )
{
break;
}
DNSGenerateData(Datas, Buffer, SingleLength, Descriptor + loop);
TotleLength += SingleLength;
BufferLength -= SingleLength;
Buffer += SingleLength;
/* move to next record */
for(;*Datas != '\0'; ++Datas);
++Datas;
}
return TotleLength;
}
static int DNSCache_GetRawRecordsFromCache( __in char *Name,
__in DNSRecordType Type,
__in DNSRecordClass Class,
__inout char *Buffer,
__in int BufferLength,
__out int *RecordsLength,
__in time_t CurrentTime
)
{
int SingleLength;
char *CacheItr;
Cht_Node *Node = NULL;
int DatasLen;
uint32_t NewTTL;
int RecordCount = 0;
const ElementDescriptor *Descriptor;
int CountOfDescriptor;
char Name_Type_Class[256];
*RecordsLength = 0;
sprintf(Name_Type_Class, "%s\1%d\1%d", Name, Type, Class);
do
{
Node = DNSCache_FindFromCache(Name_Type_Class, strlen(Name_Type_Class) + 1, Node, CurrentTime);
if( Node == NULL )
{
break;
}
if( Node -> TTL != 0 )
{
CountOfDescriptor = DNSGetDescriptor((DNSRecordType)Type, TRUE, &Descriptor);
if(CountOfDescriptor != 0)
{
++RecordCount;
CacheItr = MapStart + Node -> Offset + 1;
SingleLength = DNSGenResourceRecord(NULL, 0, "a", Type, Class, 0, NULL, 0, FALSE);
if( BufferLength < SingleLength )
{
break;
}
if( IgnoreTTL == TRUE )
{
NewTTL = Node -> TTL;
} else {
NewTTL = Node -> TTL - (CurrentTime - Node -> TimeAdded);
}
DNSGenResourceRecord(Buffer, SingleLength, "a", Type, Class, NewTTL, NULL, 0, FALSE);
for(; *CacheItr != '\0'; ++CacheItr);
/* Then *CacheItr == '\0' */
++CacheItr;
/* Then the data position */
Buffer += SingleLength;
BufferLength -= SingleLength;
DatasLen = DNSCache_GenerateDatasFromCache(CacheItr, Buffer, BufferLength, Descriptor, CountOfDescriptor);
SET_16_BIT_U_INT(Buffer - 2, DatasLen);
Buffer += DatasLen;
(*RecordsLength) += (SingleLength + DatasLen);
}
}
} while ( TRUE );
return RecordCount;
}
static Cht_Node *DNSCache_GetCNameFromCache(__in char *Name, __out char *Buffer, __in time_t CurrentTime)
{
char Name_Type_Class[256];
Cht_Node *Node = NULL;
sprintf(Name_Type_Class, "%s\1%d\1%d", Name, DNS_TYPE_CNAME, 1);
do
{
Node = DNSCache_FindFromCache(Name_Type_Class, strlen(Name_Type_Class) + 1, Node, CurrentTime);
if( Node == NULL )
{
return NULL;
}
strcpy(Buffer, MapStart + Node -> Offset + 1 + strlen(Name_Type_Class) + 1);
return Node;
} while( TRUE );
}
static int DNSCache_GetByQuestion(__in const char *Question, __inout char *Buffer, __in int BufferLength, __out int *RecordsLength, __in time_t CurrentTime)
{
int SingleLength = 0;
char Name[260];
char CName[260];
Cht_Node *Node;
uint32_t NewTTL;
int RecordsCount = 0;
DNSRecordType Type;
DNSRecordClass Class;
if(Inited == FALSE) return -1;
if(TTLMultiple < 1) return -2;
*RecordsLength = 0;
DNSGetHostName(Question, DNSJumpHeader(Question), Name, sizeof(Name));
Type = (DNSRecordType)DNSGetRecordType(DNSJumpHeader(Question));
Class = (DNSRecordClass)DNSGetRecordClass(DNSJumpHeader(Question));
RWLock_RdLock(CacheLock);
/* If the intended type is not DNS_TYPE_CNAME, then first find its cname */
if(Type != DNS_TYPE_CNAME)
{
while( (Node = DNSCache_GetCNameFromCache(Name, CName, CurrentTime)) != NULL )
{
SingleLength = DNSGenResourceRecord(NULL, 0, "a", DNS_TYPE_CNAME, 1, 0, CName, strlen(CName) + 1, TRUE);
if( BufferLength < SingleLength )
{
return RecordsCount;
}
++RecordsCount;
if( IgnoreTTL == TRUE )
{
NewTTL = Node -> TTL;
} else {
NewTTL = Node -> TTL - (CurrentTime - Node -> TimeAdded);
}
DNSGenResourceRecord(Buffer, SingleLength, "a", DNS_TYPE_CNAME, 1, NewTTL, CName, strlen(CName) + 1, TRUE);
BufferLength -= SingleLength;
Buffer += SingleLength;
(*RecordsLength) += SingleLength;
strcpy(Name, CName);
}
}
RecordsCount += DNSCache_GetRawRecordsFromCache(Name, Type, Class, Buffer, BufferLength, &SingleLength, CurrentTime);
RWLock_UnRLock(CacheLock);
if( RecordsCount == 0 || SingleLength == 0 )
{
return 0;
}
(*RecordsLength) += SingleLength;
return RecordsCount;
}
int DNSCache_FetchFromCache(char *RequestContent, int RequestLength, int BufferLength)
{
BOOL EDNSEnabled;
int RecordsCount, RecordsLength;
if( Inited == FALSE )
{
return -1;
}
switch( DNSRemoveEDNSPseudoRecord(RequestContent, &RequestLength) )
{
case EDNS_REMOVED:
EDNSEnabled = TRUE;
break;
case EDNS_NO_AR:
EDNSEnabled = FALSE;
break;
default:
return -1;
}
RecordsCount = DNSCache_GetByQuestion(RequestContent, RequestContent + RequestLength, BufferLength - RequestLength, &RecordsLength, time(NULL));
if( RecordsCount > 0 )
{
int UnCompressedLength = RequestLength + RecordsLength;
int CompressedLength;
((DNSHeader *)RequestContent) -> AnswerCount = htons(RecordsCount);
CompressedLength = DNSCompress(RequestContent, UnCompressedLength);
((DNSHeader *)RequestContent) -> Flags.Direction = 1;
((DNSHeader *)RequestContent) -> Flags.AuthoritativeAnswer = 0;
((DNSHeader *)RequestContent) -> Flags.RecursionAvailable = 1;
((DNSHeader *)RequestContent) -> Flags.ResponseCode = 0;
((DNSHeader *)RequestContent) -> Flags.Type = 0;
if( EDNSEnabled == TRUE )
{
DNSAppendEDNSPseudoRecord(RequestContent, &CompressedLength);
}
return CompressedLength;
} else {
if( EDNSEnabled == TRUE )
{
DNSAppendEDNSPseudoRecord(RequestContent, &RequestLength);
}
return -1;
}
}
void DNSCacheClose(ConfigFileInfo *ConfigInfo)
{
if(Inited == TRUE)
{
Inited = FALSE;
RWLock_WrLock(CacheLock);
if( ConfigGetBoolean(ConfigInfo, "MemoryCache") == FALSE )
{
UNMAP_FILE(MapStart, CacheSize);
DESTROY_MAPPING(CacheMappingHandle);
CLOSE_FILE(CacheFileHandle);
} else {
SafeFree(MapStart);
}
RWLock_UnWLock(CacheLock);
RWLock_Destroy(CacheLock);
}
}