Skip to content

Commit

Permalink
Refactor context models
Browse files Browse the repository at this point in the history
* Introduce MsgContext concept.
* Link TCP connection oriented context to ModuleContext.
* Merge HostsContext into ModuleContext.
* Use MsgContext as Module Context Item. Less memcpy.
  • Loading branch information
lifenjoiner committed Sep 17, 2022
1 parent 31a294c commit a0d864a
Show file tree
Hide file tree
Showing 32 changed files with 342 additions and 518 deletions.
8 changes: 5 additions & 3 deletions dnscache.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ static int DNSCache_AddAItemToCache(DnsSimpleParserIterator *i,
return 0;
}

int DNSCache_AddItemsToCache(IHeader *Header, BOOL IsFirst)
int DNSCache_AddItemsToCache(MsgContext *MsgCtx, BOOL IsFirst)
{
IHeader *Header = (IHeader *)MsgCtx;
char *DnsEntity = IHEADER_TAIL(Header);
const CtrlContent *TtlContent = NULL;

Expand Down Expand Up @@ -844,8 +845,9 @@ static int DNSCache_GetByQuestion(__inout DnsGenerator *g,
}

/* Content length returned */
int DNSCache_FetchFromCache(IHeader *h /* Entity followed */, int BufferLength)
int DNSCache_FetchFromCache(MsgContext *MsgCtx, int BufferLength)
{
IHeader *h = (IHeader *)MsgCtx;
char *RequestContent = (char *)(h + 1);

DnsSimpleParser p;
Expand Down Expand Up @@ -913,7 +915,7 @@ int DNSCache_FetchFromCache(IHeader *h /* Entity followed */, int BufferLength)
memmove(RequestContent, HereToGenerate, ResultLength);

h->EntityLength = ResultLength;
if( IHeader_SendBack(h) < 0 )
if( MsgContext_SendBack(MsgCtx) < 0 )
{
/** TODO: Error handling */
return -861;
Expand Down
4 changes: 2 additions & 2 deletions dnscache.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ int DNSCache_Init(ConfigFileInfo *ConfigInfo);

BOOL Cache_IsInited(void);

int DNSCache_AddItemsToCache(IHeader *Header, BOOL IsFirst);
int DNSCache_AddItemsToCache(MsgContext *MsgCtx, BOOL IsFirst);

int DNSCache_FetchFromCache(IHeader *h /* Entity followed */, int BufferLength);
int DNSCache_FetchFromCache(MsgContext *MsgCtx, int BufferLength);

#endif /* _DNS_CACHE_ */
2 changes: 1 addition & 1 deletion dnsgenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ static int StripedLength(const char *Origin, int OriginLength)
}

int DnsGenerator_Init(DnsGenerator *g,
char *Buffer,
char *Buffer, /* generate to here */
int BufferLength,
const char *CopyFrom,
int SourceLength,
Expand Down
3 changes: 3 additions & 0 deletions dnsgenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

/* Handle DNS header*/
#define DNSSetTcpLength(dns_start, Len) SET_16_BIT_U_INT((char *)(dns_start), Len)

#define DNSSetQueryIdentifier(dns_start, QId) SET_16_BIT_U_INT((char *)(dns_start), QId)

#define DNSCopyQueryIdentifier(dst, src) (*(uint16_t *)(dst) = *(uint16_t *)(src))

#define DNSSetFlags(dns_start, Flags) SET_16_BIT_U_INT((char *)(dns_start) + 2, Flags)

#define DNSSetQuestionCount(dns_start, QC) SET_16_BIT_U_INT((char *)(dns_start) + 4, QC)
Expand Down
4 changes: 2 additions & 2 deletions dynamichosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ BOOL DynamicHosts_TypeExisting(const char *Domain, HostsRecordType Type)
return ret;
}

HostsUtilsTryResult DynamicHosts_Try(IHeader *Header, int BufferLength)
HostsUtilsTryResult DynamicHosts_Try(MsgContext *MsgCtx, int BufferLength)
{
HostsUtilsTryResult ret;

Expand All @@ -299,7 +299,7 @@ HostsUtilsTryResult DynamicHosts_Try(IHeader *Header, int BufferLength)

RWLock_RdLock(HostsLock);

ret = HostsUtils_Try(Header,
ret = HostsUtils_Try(MsgCtx,
BufferLength,
(HostsContainer *)MainDynamicContainer
);
Expand Down
2 changes: 1 addition & 1 deletion dynamichosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ int DynamicHosts_GetCName(const char *Domain, char *Buffer);

BOOL DynamicHosts_TypeExisting(const char *Domain, HostsRecordType Type);

HostsUtilsTryResult DynamicHosts_Try(IHeader *Header, int BufferLength);
HostsUtilsTryResult DynamicHosts_Try(MsgContext *MsgCtx, int BufferLength);

#endif // DYNAMICHOSTS_H_INCLUDED
6 changes: 4 additions & 2 deletions filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ static BOOL IsDisabledDomain(const char *Domain, uint32_t HashValue)
return ret;
}

BOOL Filter_Out(IHeader *h)
BOOL Filter_Out(MsgContext *MsgCtx)
{
IHeader *h = (IHeader *)MsgCtx;

if(IsDisabledType(h->Type) || IsDisabledDomain(h->Domain, h->HashValue) )
{
IHeader_SendBackRefusedMessage(h);
MsgContext_SendBackRefusedMessage(MsgCtx);
ShowRefusingMessage(h, "Disabled type or domain");
DomainStatistic_Add(h, STATISTIC_TYPE_REFUSED);

Expand Down
2 changes: 1 addition & 1 deletion filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ int Filter_Init(ConfigFileInfo *ConfigInfo);

int Filter_Update(void);

BOOL Filter_Out(IHeader *h);
BOOL Filter_Out(MsgContext *MsgCtx);

#endif // EXCLUDEDLIST_H_INCLUDED
165 changes: 0 additions & 165 deletions hcontext.c

This file was deleted.

37 changes: 0 additions & 37 deletions hcontext.h

This file was deleted.

Loading

0 comments on commit a0d864a

Please sign in to comment.