void InitContext(ThreadContext *Context, char *RequestEntity) { Context -> Head = Context; Context -> Previous = NULL; Context -> TCPSocket = INVALID_SOCKET; Context -> LastServer = NULL; Context -> UDPSocket = INVALID_SOCKET; Context -> LastFamily = AF_UNSPEC; Context -> Compress = TRUE; Context -> ResponseBuffer = &(Context -> ResponseBuffer_Entity); ExtendableBuffer_Init(Context -> ResponseBuffer, 512, 10240); Context -> RequestEntity = RequestEntity; /* Choose and fill default primary and secondary socket */ if( PrimaryProtocol == DNS_QUARY_PROTOCOL_TCP ) { Context -> PrimarySocket = &(Context -> TCPSocket); Context -> SecondarySocket = &(Context -> UDPSocket); } else { Context -> PrimarySocket = &(Context -> UDPSocket); Context -> SecondarySocket = &(Context -> TCPSocket); } }
int Hosts_InitContainer(HostsContainer *Container) { if( StringList_Init(&(Container -> Domains), NULL, ',') != 0 ) { return -1; } if( StringChunk_Init(&(Container -> Ipv4Hosts), &(Container -> Domains)) != 0 ) { return -2; } if( StringChunk_Init(&(Container -> Ipv6Hosts), &(Container -> Domains)) != 0 ) { return -3; } if( StringChunk_Init(&(Container -> CNameHosts), &(Container -> Domains)) != 0 ) { return -4; } if( StringChunk_Init(&(Container -> ExcludedDomains), &(Container -> Domains)) != 0 ) { return -4; } if( ExtendableBuffer_Init(&(Container ->IPs), 0, -1) != 0 ) { return -6; } return 0; }
int StringChunk_Init(StringChunk *dl, int InitialCount /* For no-wildcard domain */) { if( StringList_Init(&(dl -> List), NULL, 0) != 0 ) { return -1; } if( HashTable_Init(&(dl -> List_Pos), sizeof(EntryForString), InitialCount, NULL) != 0 ) { StringList_Free(&(dl -> List)); return -2; } if( StringList_Init(&(dl -> List_W), NULL, 0) != 0 ) { StringList_Free(&(dl -> List)); HashTable_Free(&(dl -> List_Pos)); return -3; } if( Array_Init(&(dl -> List_W_Pos), sizeof(EntryForString), 0, FALSE, NULL) != 0 ) { StringList_Free(&(dl -> List)); HashTable_Free(&(dl -> List_Pos)); StringList_Free(&(dl -> List_W)); return -4; } if( ExtendableBuffer_Init(&(dl -> AdditionalDataChunk), 0, -1) != 0 ) { StringList_Free(&(dl -> List)); HashTable_Free(&(dl -> List_Pos)); StringList_Free(&(dl -> List_W)); Array_Free(&(dl -> List_W_Pos)); return -5; } return 0; }
static int TCPRecv(RecvInfo *Info) { SOCKET Socket = Info -> Socket; CompatibleAddr Peer = Info -> Peer; int state; char ResultBuffer[1024]; ExtendableBuffer Buffer; /* Sockets to server */ SOCKET TCPSocket = INVALID_SOCKET; SOCKET UDPSocket = INVALID_SOCKET; SOCKET *PrimarySocketPtr; SOCKET *SecondarySocketPtr; DNSQuaryProtocol PrimaryProtocol; char ProtocolStr[8] = {0}; strncpy(ProtocolStr, ConfigGetString(&ConfigInfo, "PrimaryServer"), 3); StrToLower(ProtocolStr); if( strcmp(ProtocolStr, "tcp") == 0 ) { PrimaryProtocol = DNS_QUARY_PROTOCOL_TCP; PrimarySocketPtr = &TCPSocket; if( ConfigGetString(&ConfigInfo, "UDPServer") != NULL ) SecondarySocketPtr = &UDPSocket; else SecondarySocketPtr = NULL; } else { PrimaryProtocol = DNS_QUARY_PROTOCOL_UDP; PrimarySocketPtr = &UDPSocket; if( ConfigGetString(&ConfigInfo, "TCPServer") != NULL ) SecondarySocketPtr = &TCPSocket; else SecondarySocketPtr = NULL; } ExtendableBuffer_Init(&Buffer, 512, 10240); while(TRUE){ state = recv(Socket, ResultBuffer, sizeof(ResultBuffer), MSG_NOSIGNAL); if(GET_LAST_ERROR() == TCP_TIME_OUT) { break; } if( state < 1 ) { break; } Query(PrimarySocketPtr, SecondarySocketPtr, PrimaryProtocol, ResultBuffer, state, &Socket, &Peer, &Buffer); ExtendableBuffer_Reset(&Buffer); } CLOSE_SOCKET(TCPSocket); CLOSE_SOCKET(UDPSocket); CLOSE_SOCKET(Socket); if( Family == AF_INET ) { INFO("Closed TCP connection to %s:%d\n", inet_ntoa(Peer.Addr4.sin_addr), Peer.Addr4.sin_port); } else { char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0}; IPv6AddressToAsc(&(Peer.Addr6.sin6_addr), Addr); INFO("Closed TCP connection to %s:%d\n", Addr, Peer.Addr6.sin6_port); } SafeFree(Info); EXIT_THREAD(0); }