int GetAnswersByName(ThreadContext *Context, const char *Name, DNSRecordType Type, const char *Agent) { ThreadContext RecursionContext; int StateOfReceiving; char RequestEntity[384] = { 00, 00, /* QueryIdentifier */ 01, 00, /* Flags */ 00, 01, /* QuestionCount */ 00, 00, /* AnswerCount */ 00, 00, /* NameServerCount */ 00, 00, /* AdditionalCount */ /* Header end */ }; char *NamePos = RequestEntity + 0x0C; if( DefinitionLoop(Context, Name) == TRUE ) { ERRORMSG("Cricular definition for %s.\n", Name); return -1; } if( DNSGenQuestionRecord(NamePos, sizeof(RequestEntity) - 12, Name, Type, DNS_CLASS_IN) == 0 ) { return -1; } *(uint16_t *)RequestEntity = rand(); memcpy(&RecursionContext, Context, sizeof(RecursionContext)); RecursionContext.Previous = Context; RecursionContext.RequestEntity = RequestEntity; RecursionContext.RequestLength = 12 + strlen(Name) + 2 + 4; RecursionContext.RequestingDomain = Name; RecursionContext.RequestingType = Type; RecursionContext.RequestingDomainHashValue = ELFHash(Name, 0); RecursionContext.ClientIP = Agent; StateOfReceiving = QueryBase(&RecursionContext); if( StateOfReceiving <= 0 ) { return -1; } return StateOfReceiving; }
static int Query(ThreadContext *Context, CompatibleAddr *ClientAddr) { int State; char RequestingDomain[256]; char ClientIP[LENGTH_OF_IPV6_ADDRESS_ASCII + 1]; if( Family == AF_INET ) { strcpy(ClientIP, inet_ntoa(ClientAddr -> Addr4.sin_addr)); } else { IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), ClientIP); } Context -> ClientIP = ClientIP; RequestingDomain[0] = '\0'; DNSGetHostName(Context -> RequestEntity, DNSJumpHeader(Context -> RequestEntity), RequestingDomain ); StrToLower(RequestingDomain); Context -> RequestingDomain = RequestingDomain; Context -> RequestingType = (DNSRecordType)DNSGetRecordType(DNSJumpHeader(Context -> RequestEntity)); Context -> RequestingDomainHashValue = ELFHash(RequestingDomain, 0); Context -> CurrentTime = time(NULL); State = QueryBase(Context); switch( State ) { case QUERY_RESULT_DISABLE: ((DNSHeader *)(Context -> RequestEntity)) -> Flags.Direction = 1; ((DNSHeader *)(Context -> RequestEntity)) -> Flags.RecursionAvailable = 1; ((DNSHeader *)(Context -> RequestEntity)) -> Flags.ResponseCode = RefusingResponseCode; if( Family == AF_INET ) { _SendTo(ListenSocketUDP, Context -> RequestEntity, Context -> RequestLength, 0, (struct sockaddr *)&(ClientAddr -> Addr4), sizeof(struct sockaddr) ); } else { _SendTo(ListenSocketUDP, Context -> RequestEntity, Context -> RequestLength, 0, (struct sockaddr *)&(ClientAddr -> Addr6), sizeof(struct sockaddr_in6) ); } return -1; break; case QUERY_RESULT_ERROR: ((DNSHeader *)(Context -> RequestEntity)) -> Flags.Direction = 1; ((DNSHeader *)(Context -> RequestEntity)) -> Flags.RecursionAvailable = 1; ((DNSHeader *)(Context -> RequestEntity)) -> Flags.ResponseCode = 2; if( Family == AF_INET ) { _SendTo(ListenSocketUDP, Context -> RequestEntity, Context -> RequestLength, 0, (struct sockaddr *)&(ClientAddr -> Addr4), sizeof(struct sockaddr) ); } else { _SendTo(ListenSocketUDP, Context -> RequestEntity, Context -> RequestLength, 0, (struct sockaddr *)&(ClientAddr -> Addr6), sizeof(struct sockaddr_in6) ); } return -1; break; default: /* Succeed */ if(State > MaximumMessageSize) { State = MaximumMessageSize; ((DNSHeader *)(ExtendableBuffer_GetData(Context -> ResponseBuffer))) -> Flags.TrunCation = 1; } if( Family == AF_INET ) { _SendTo(ListenSocketUDP, ExtendableBuffer_GetData(Context -> ResponseBuffer), State, 0, (struct sockaddr *)&(ClientAddr -> Addr4), sizeof(struct sockaddr) ); } else { _SendTo(ListenSocketUDP, ExtendableBuffer_GetData(Context -> ResponseBuffer), State, 0, (struct sockaddr *)&(ClientAddr -> Addr6), sizeof(struct sockaddr_in6) ); } return 0; } }
static int Query( SOCKET *PrimarySocket, SOCKET *SecondarySocket, DNSQuaryProtocol PrimaryProtocol, char *QueryContent, int QueryContentLength, SOCKET *ClientSocket, CompatibleAddr *ClientAddr, ExtendableBuffer *Buffer ) { int State; DNSRecordType SourceType; char *DNSBody = DNSGetDNSBody(QueryContent); char ProtocolCharacter = ' '; char QueryDomain[256]; char DateAndTime[32]; QueryContext Context; GetCurDateAndTime(DateAndTime, sizeof(DateAndTime)); QueryDomain[0] = '\0'; DNSGetHostName(DNSBody, DNSJumpHeader(DNSBody), QueryDomain); SourceType = (DNSRecordType)DNSGetRecordType(DNSJumpHeader(DNSBody)); Context.PrimarySocket = PrimarySocket; Context.SecondarySocket = SecondarySocket; Context.PrimaryProtocolToServer = PrimaryProtocol; Context.ProtocolToSrc = DNS_QUARY_PROTOCOL_TCP; Context.Compress = TRUE; State = QueryBase(&Context, QueryContent, QueryContentLength, Buffer, QueryDomain, SourceType, &ProtocolCharacter ); switch( State ) { case QUERY_RESULT_DISABLE: ((DNSHeader *)DNSBody) -> Flags.Direction = 1; ((DNSHeader *)DNSBody) -> Flags.ResponseCode = 5; send(*ClientSocket, QueryContent, QueryContentLength, 0); if( Family == AF_INET ) { PRINT("%s[R][%s:%d][%s][%s] Refused.\n", DateAndTime, inet_ntoa(ClientAddr -> Addr4.sin_addr), ClientAddr -> Addr4.sin_port, DNSGetTypeName(SourceType), QueryDomain); } else { char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0}; IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr); PRINT("%s[R][%s:%d][%s][%s] Refused.\n", DateAndTime, Addr, ClientAddr -> Addr6.sin6_port, DNSGetTypeName(SourceType), QueryDomain); } return -1; break; case QUERY_RESULT_ERROR: if( ErrorMessages == TRUE ) { int ErrorNum = GET_LAST_ERROR(); char ErrorMessage[320]; ErrorMessage[0] ='\0'; GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage)); if( Family == AF_INET ) { printf("%s[%c][%s][%s][%s] Error occured : %d : %s .\n", DateAndTime, ProtocolCharacter, inet_ntoa(ClientAddr -> Addr4.sin_addr), DNSGetTypeName(SourceType), QueryDomain, ErrorNum, ErrorMessage ); } else { char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0}; IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr); printf("%s[%c][%s][%s][%s] Error occured : %d : %s .\n", DateAndTime, ProtocolCharacter, Addr, DNSGetTypeName(SourceType), QueryDomain, ErrorNum, ErrorMessage ); } } return -1; break; default: /* Succeed */ send(*ClientSocket, ExtendableBuffer_GetData(Buffer), State, 0); if( ShowMassages == TRUE ) { char InfoBuffer[3072]; InfoBuffer[0] = '\0'; GetAllAnswers(DNSGetDNSBody(ExtendableBuffer_GetData(Buffer)), InfoBuffer); if( Family == AF_INET ) { PRINT("%s[%c][%s][%s][%s] :\n%s", DateAndTime, ProtocolCharacter, inet_ntoa(ClientAddr ->Addr4.sin_addr), DNSGetTypeName(SourceType), QueryDomain, InfoBuffer); } else { char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0}; IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr); PRINT("%s[%c][%s][%s][%s] :\n%s", DateAndTime, ProtocolCharacter, Addr, DNSGetTypeName(SourceType), QueryDomain, InfoBuffer); } } return 0; break; } }