DNS Relay Project is an assignment of BUPT Computer Network Course.
Original Binary Source (exe) archived in zip.
Hacked Code written in C language.
Sample Input (hosts table) in txt.
The original code is writtern on Windows, and this version is ported to UNIX-like systems.
On Windows
:
s_addr
is defined asS_un.S_addr
On UNIX-like systems:
InitWSA
is not implementedWSAGetLastError
is replaced aserrno
BYTE
,WORD
andDWORD
are defined asuint8_t
,uint16_t
,uint32_t
BYTE
,WORD
andDWORD
are 8-bit, 16-bit and 32-bitunsigned
respectivelypb*
,pw*
andpd*
are pointers toBYTE
,WORD
andDWORD
respectivelysz*
is the pointer to C-style string
debugLevel
is used to control debug information outputcurId
is ID used converting before forwarding to name serversock
is the socket shared among functionssaNameServer
is thesockaddr_in
of name serverpbTable
points to the hosts table
- Setup config and other global variables
- Initialize
sock
and Bind to port 53 - Call
LoadTable
- Call
Run
in a dead loop
- Load hosts table from file to memory of
pbTable
, if the file presents - Table format is
domain-name-string \0 dword-ip
domain-name
is converted to lower casedword-ip
is converted from IP string
- Memory of
pbTable
is allocated precisely
- Call
recvfrom
and Fill the buffer - Handle receiving error case (
length <= 0
) - Handle response message from name server (
QR
== 1)- Restore original ID and socket address from
records
- Send this packet to client
- Restore original ID and socket address from
- Handle query message to forward to name server
- In following cases:
- Not a simple Query (
QDCOUNT
!= 1) - Non-standard Query (
OPCODE
!= 0) || has-Authoritative-Answer (AA
== 1) - Anwser not found in
pbTable
(described below)
- Not a simple Query (
- Save the packet ID and socket address to
records
, and Replace the ID tocurId
- Send this packet to name server
- In following cases:
- (Fall through) If not above cases, then
- Retrieve domain name, type and class from query message
- Find the domain name in
pbTable
if it's IPv4 message when table is not empty- Not Found => Forward this packet to name server
- Found => Do as following
- Handle query message found in hosts table
- Call
SetAnswer
with found entry - Send to client directly
- Call
- Print ip addr, port and buffer length
- If
mode
is NOT 2- Print buffer raw view and packet content
- Set
QR
(Response) &AA
(Authoritative answer) &RA
(Recursion Available) - If the anwser is NOT
0.0.0.0
- Set
ANCOUNT
to 1 - Set name, type, class, ttl, rdlength and rddata
- Set
- Else
- Set error response (
RCODE
= 3)
- Set error response (
- Return the new size of buffer
Hacked by BOT-Man-JL
Original Binary Code by [email protected]
Special thanks: thiefuniverse - helping debug on Linux