int IPv6AddressToAsc(const void *Address, void *Buffer) { sprintf((char *)Buffer, "%x:%x:%x:%x:%x:%x:%x:%x", GET_16_BIT_U_INT((const char *)Address), GET_16_BIT_U_INT((const char *)Address + 2), GET_16_BIT_U_INT((const char *)Address + 4), GET_16_BIT_U_INT((const char *)Address + 6), GET_16_BIT_U_INT((const char *)Address + 8), GET_16_BIT_U_INT((const char *)Address + 10), GET_16_BIT_U_INT((const char *)Address + 12), GET_16_BIT_U_INT((const char *)Address + 14) ); return 0; }
void DNSParser(const char *dns_over_tcp, char *buffer){ char *orig = buffer; char *dnsovertcp = dns_over_tcp; char InnerBuffer[128]; unsigned short qc, ac; buffer += sprintf(buffer, "TCPLength:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "QueryIdentifier:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "Flags:%x\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "QuestionCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); qc = GET_16_BIT_U_INT(dnsovertcp); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "AnswerCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); ac = GET_16_BIT_U_INT(dnsovertcp); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "NameServerCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "AdditionalCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ for(; qc != 0; --qc){ dnsovertcp += DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "QuestionName:%s\n", InnerBuffer); buffer += sprintf(buffer, "QuestionType:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "QuestionClass:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ } for(; ac != 0; --ac){ unsigned short rt, dl; dnsovertcp += DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "ResourceName:%s\n", InnerBuffer); buffer += sprintf(buffer, "ResourceType:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); rt = GET_16_BIT_U_INT(dnsovertcp); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "ResourceClass:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dnsovertcp += 2; /* sizeof(unsigned short) */ buffer += sprintf(buffer, "TimeToLive:%u\n", GET_32_BIT_U_INT(dnsovertcp)); dnsovertcp += 4; /* sizeof(unsigned int) */ buffer += sprintf(buffer, "ResourceDataLength:%hu\n", GET_16_BIT_U_INT(dnsovertcp)); dl = GET_16_BIT_U_INT(dnsovertcp); dnsovertcp += 2; /* sizeof(unsigned short) */ switch(rt){ case DNS_TYPE_A: /* A, IPv4 address */ buffer += sprintf(buffer, "IPv4Addres:%d.%d.%d.%d\n", GET_8_BIT_U_INT(dnsovertcp), GET_8_BIT_U_INT(dnsovertcp + 1), GET_8_BIT_U_INT(dnsovertcp + 2), GET_8_BIT_U_INT(dnsovertcp + 3)); break; case DNS_TYPE_AAAA: /* AAAA, IPv6 address */ buffer += sprintf(buffer, "IPv6Addres:%x:%x:%x:%x:%x:%x:%x:%x \n", GET_16_BIT_U_INT(dnsovertcp), GET_16_BIT_U_INT(dnsovertcp + 2), GET_16_BIT_U_INT(dnsovertcp + 4), GET_16_BIT_U_INT(dnsovertcp + 6), GET_16_BIT_U_INT(dnsovertcp + 8), GET_16_BIT_U_INT(dnsovertcp + 10), GET_16_BIT_U_INT(dnsovertcp + 12), GET_16_BIT_U_INT(dnsovertcp + 14) ); break; case DNS_TYPE_CNAME: /* CNAME */ DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "CName:%s\n", InnerBuffer); break; default: break; } dnsovertcp += dl; } }
void DNSParser(char *dns_over_tcp, char *buffer){ char *dnsovertcp = dns_over_tcp; char InnerBuffer[128] = {0}; unsigned short qc, ac; buffer += sprintf(buffer, "TCPLength:%hu\n", DNSGetTCPLength(DNSGetDNSBody(dnsovertcp))); buffer += sprintf(buffer, "QueryIdentifier:%hu\n", DNSGetQueryIdentifier(DNSGetDNSBody(dnsovertcp))); buffer += sprintf(buffer, "Flags:%x\n", DNSGetFlags(DNSGetDNSBody(dnsovertcp))); qc = DNSGetQuestionCount(DNSGetDNSBody(dnsovertcp)); buffer += sprintf(buffer, "QuestionCount:%hu\n", qc); ac = DNSGetAnswerCount(DNSGetDNSBody(dnsovertcp)); buffer += sprintf(buffer, "AnswerCount:%hu\n", ac); buffer += sprintf(buffer, "NameServerCount:%hu\n", DNSGetNameServerCount(DNSGetDNSBody(dnsovertcp))); buffer += sprintf(buffer, "AdditionalCount:%hu\n", DNSGetAdditionalCount(DNSGetDNSBody(dnsovertcp))); dnsovertcp = DNSJumpHeader(DNSGetDNSBody(dns_over_tcp)); for(; qc != 0; --qc){ DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "QuestionName:%s\n", InnerBuffer); buffer += sprintf(buffer, "QuestionType:%hu\n", DNSGetRecordType(dnsovertcp)); buffer += sprintf(buffer, "QuestionClass:%hu\n", DNSGetRecordClass(dnsovertcp)); } dnsovertcp = DNSJumpOverQuestionRecords(DNSGetDNSBody(dns_over_tcp)); while(ac != 0){ unsigned short rt, dl; dnsovertcp = DNSGetAnswerRecordPosition(DNSGetDNSBody(dns_over_tcp), DNSGetAnswerCount(DNSGetDNSBody(dns_over_tcp)) - ac + 1); DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "ResourceName:%s\n", InnerBuffer); rt = DNSGetRecordType(dnsovertcp); buffer += sprintf(buffer, "ResourceType:%hu\n", rt); buffer += sprintf(buffer, "ResourceClass:%hu\n", DNSGetRecordClass(dnsovertcp)); buffer += sprintf(buffer, "TimeToLive:%u\n", (unsigned int)DNSGetTTL(dnsovertcp)); dl = DNSGetResourceDataLength(dnsovertcp); buffer += sprintf(buffer, "ResourceDataLength:%hu\n", dl); dnsovertcp = DNSGetResourceDataPos(dnsovertcp); switch(rt){ case DNS_TYPE_A: /* A, IPv4 address */ buffer += sprintf(buffer, "IPv4Addres:%d.%d.%d.%d\n", GET_8_BIT_U_INT(dnsovertcp), GET_8_BIT_U_INT(dnsovertcp + 1), GET_8_BIT_U_INT(dnsovertcp + 2), GET_8_BIT_U_INT(dnsovertcp + 3)); break; case DNS_TYPE_AAAA: /* AAAA, IPv6 address */ buffer += sprintf(buffer, "IPv6Addres:%x:%x:%x:%x:%x:%x:%x:%x\n", GET_16_BIT_U_INT(dnsovertcp), GET_16_BIT_U_INT(dnsovertcp + 2), GET_16_BIT_U_INT(dnsovertcp + 4), GET_16_BIT_U_INT(dnsovertcp + 6), GET_16_BIT_U_INT(dnsovertcp + 8), GET_16_BIT_U_INT(dnsovertcp + 10), GET_16_BIT_U_INT(dnsovertcp + 12), GET_16_BIT_U_INT(dnsovertcp + 14) ); break; case DNS_TYPE_CNAME: /* CNAME */ DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer); buffer += sprintf(buffer, "CName:%s\n", InnerBuffer); break; default: break; } dnsovertcp = DNSGetAnswerRecordPosition(DNSGetDNSBody(dns_over_tcp), DNSGetAnswerCount(dns_over_tcp) - ac + 1); --ac; } }
DNSDataInfo DNSParseData(const char *DNSBody, const char *DataBody, int DataLength, void *Buffer, int BufferLength, const ElementDescriptor *Descriptor, int CountOfDescriptor, int Num) { DNSDataInfo Result = {DNS_DATA_TYPE_UNKNOWN, 0}; const char *PendingData = DataBody; if( Num > CountOfDescriptor || DataLength <= 0 ) return Result; while(Num != 1) { switch(Descriptor -> element) { case DNS_LABELED_NAME: PendingData = DNSJumpOverName(PendingData); break; case DNS_CHARACTER_STRING: PendingData += strlen(PendingData) + 1; break; case DNS_IPV6_ADDR: PendingData += 16; break; case DNS_IPV4_ADDR: case DNS_32BIT_UINT: PendingData += 4; break; case DNS_DNSKEY_FLAGS: case DNS_16BIT_UINT: PendingData += 2; break; case DNS_DNSKEY_PROTOCOL: case DNS_DNSKEY_ALGORITHM: case DNS_8BIT_UINT: PendingData += 1; break; default: return Result; break; } --Num; ++Descriptor; } switch(Descriptor -> element) { case DNS_LABELED_NAME: if(BufferLength < DNSGetHostNameLength(DNSBody, PendingData)) break; Result.DataLength = DNSGetHostNameLength(DNSBody, PendingData); DNSGetHostName(DNSBody, PendingData, (char *)Buffer); Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_CHARACTER_STRING: if( BufferLength < GET_8_BIT_U_INT(PendingData) + 1 ) { break; } memcpy(Buffer, PendingData + 1, GET_8_BIT_U_INT(PendingData)); ((char *)Buffer)[GET_8_BIT_U_INT(PendingData)] = '\0'; Result.DataLength = GET_8_BIT_U_INT(PendingData) + 1; Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_32BIT_UINT: { uint32_t Tmp = GET_32_BIT_U_INT(PendingData); if(BufferLength < 4) break; memcpy(Buffer, &Tmp, 4); Result.DataLength = 4; Result.DataType = DNS_DATA_TYPE_UINT; } break; case DNS_16BIT_UINT: { uint16_t Tmp = GET_16_BIT_U_INT(PendingData); if(BufferLength < 2) break; memcpy(Buffer, &Tmp, 2); Result.DataLength = 2; Result.DataType = DNS_DATA_TYPE_UINT; } break; case DNS_DNSKEY_PROTOCOL: case DNS_8BIT_UINT: if(BufferLength < 1) break; *(char *)Buffer = *PendingData; Result.DataLength = 1; Result.DataType = DNS_DATA_TYPE_UINT; break; case DNS_DNSKEY_FLAGS: if( BufferLength < 17 ) { break; } Result.DataLength = 17; BinaryOutput(PendingData, 2, Buffer); Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_DNSKEY_ALGORITHM: { const char *Name; Name = DNSSECGetAlgorithmName(*PendingData); if( BufferLength < strlen(Name) + 1 + 4 ) { break; } Result.DataLength = sprintf(Buffer, "%d %s", (unsigned char)*PendingData, Name) + 1; Result.DataType = DNS_DATA_TYPE_STRING; } break; case DNS_DNSKEY_PUBLIC_KEY: if( BufferLength < DataLength - 4 + 1 ) { break; } Result.DataLength = DataLength - 4 + 1; memcpy(Buffer, PendingData, DataLength - 4); ((char *)Buffer)[DataLength - 4] = '\0'; Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_DNSSIG_SIGNATURE: if( BufferLength < sizeof("( bytes binary object)") ) { break; } Result.DataLength = sprintf(Buffer, "(%d bytes binary object)", (int)(DataLength - (PendingData - DataBody))); Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_IPV4_ADDR: if(BufferLength < 16) break; Result.DataLength = sprintf((char *)Buffer, "%u.%u.%u.%u", GET_8_BIT_U_INT(PendingData), GET_8_BIT_U_INT(PendingData + 1), GET_8_BIT_U_INT(PendingData + 2), GET_8_BIT_U_INT(PendingData + 3) ); Result.DataType = DNS_DATA_TYPE_STRING; break; case DNS_IPV6_ADDR: if(BufferLength < 40) break; Result.DataLength = sprintf((char *)Buffer, "%x:%x:%x:%x:%x:%x:%x:%x", GET_16_BIT_U_INT(PendingData), GET_16_BIT_U_INT(PendingData + 2), GET_16_BIT_U_INT(PendingData + 4), GET_16_BIT_U_INT(PendingData + 6), GET_16_BIT_U_INT(PendingData + 8), GET_16_BIT_U_INT(PendingData + 10), GET_16_BIT_U_INT(PendingData + 12), GET_16_BIT_U_INT(PendingData + 14) ); Result.DataType = DNS_DATA_TYPE_STRING; break; default: break; } return Result; }