Exemplo n.º 1
0
void QueryDNSListenUDPStart(ConfigFileInfo *ConfigInfo)
{
	int _ThreadCount;

	if(Inited == FALSE) return;

	_ThreadCount = ConfigGetInt32(ConfigInfo, "UDPThreads");
	if(_ThreadCount < 1)
	{
		return;
	}

	Threads = SafeMalloc(_ThreadCount * sizeof(ThreadHandle));

	for(; _ThreadCount != 0; --_ThreadCount)
	{
		CREATE_THREAD(QueryDNSListenUDP,
					  (void *)(long)_ThreadCount,
					  Threads[_ThreadCount - 1]
					  );
	}
	INFO("Starting UDP socket %s:%d successfully.\n",
		 ConfigGetRawString(ConfigInfo, "LocalInterface"),
		 ConfigGetInt32(ConfigInfo, "LocalPort")
		 );
}
Exemplo n.º 2
0
static int LoadGfwList_Thread(ConfigFileInfo *ConfigInfo)
{
	int		UpdateInterval	=	ConfigGetInt32(ConfigInfo, "GfwListUpdateInterval");
	int		RetryInterval	=	ConfigGetInt32(ConfigInfo, "GfwListRetryInterval");
	BOOL	NeedBase64Decode	=	ConfigGetBoolean(ConfigInfo, "GfwListBase64Decode");
	int		Count;

	if( RetryInterval < 0 )
	{
		RetryInterval = 0;
	}

	while( TRUE )
	{
		INFO("Loading GFW List From %s ...\n", GfwList);
		if( GetFromInternet_Base(GfwList, File, FALSE) != 0 )
		{
			ERRORMSG("Downloading GFW List failed. Waiting %d second(s) for retry.\n", RetryInterval);
			SLEEP(RetryInterval * 1000);
		} else {
			INFO("GFW List saved at %s.\n", File);

			Count = LoadGfwListFile(File, NeedBase64Decode);

			switch( Count )
			{
				case -1:
				case -2:
				case -3:
					break;

				case -4:
					ERRORMSG("Loading GFW List failed, cannot open file %s. Stop loading.\n", File);
					return -1;
					break;

				default:
					INFO("Loading GFW List completed. %d effective items.\n", Count);
					break;
			}

			if( UpdateInterval < 0 )
			{
				return 0;
			}

			SLEEP(UpdateInterval * 1000);

		}
	}

	return 0;
}
Exemplo n.º 3
0
int Debug_Init(ConfigFileInfo *ConfigInfo)
{
	if( ConfigGetBoolean(ConfigInfo, "LogOn") == FALSE )
	{
		return 1;
	}

	sprintf(FilePath, "%s%cdnsforwarder.log", ConfigGetRawString(ConfigInfo, "LogFileFolder"), PATH_SLASH_CH);

	Debug_File = fopen(FilePath, "r+");
	if( Debug_File == NULL )
	{
		Debug_File = fopen(FilePath, "w");
		CurrentLength = 0;
	} else {
		fseek(Debug_File, 0, SEEK_END);
		CurrentLength = ftell(Debug_File);
	}

	ThresholdLength = ConfigGetInt32(ConfigInfo, "LogFileThresholdLength");

	Debug_PrintFile("\n\n\n\n\nNew session\n");

	return 0;
}
Exemplo n.º 4
0
void QueryDNSListenTCPStart(void)
{
	static ThreadHandle	Unused;

	if(Inited == FALSE)
		return;

	INFO("Starting TCP socket %s:%d successfully.\n", ConfigGetString(&ConfigInfo, "LocalInterface"),
													   ConfigGetInt32(&ConfigInfo, "LocalPort")
													   );
	CREATE_THREAD(QueryDNSListenTCP, NULL, Unused);
#ifdef WIN32
	CloseHandle(Unused);
#endif /* WIN32 */
}
Exemplo n.º 5
0
/* Functions */
int QueryDNSListenUDPInit(ConfigFileInfo *ConfigInfo)
{
	CompatibleAddr ListenAddr;

	const char	*LocalAddr = ConfigGetRawString(ConfigInfo, "LocalInterface");

	int			LocalPort = ConfigGetInt32(ConfigInfo, "LocalPort");

	int			AddrLen;

	RefusingResponseCode = ConfigGetInt32(ConfigInfo, "RefusingResponseCode");

	Family = GetAddressFamily(LocalAddr);

	ListenSocketUDP = socket(Family, SOCK_DGRAM, IPPROTO_UDP);

	if(ListenSocketUDP == INVALID_SOCKET)
	{
		int		ErrorNum = GET_LAST_ERROR();
		char	ErrorMessage[320];
		ErrorMessage[0] = '\0';

		GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));

		ERRORMSG("Creating UDP socket failed. %d : %s\n",
				 ErrorNum,
				 ErrorMessage
				 );
		return -1;
	}

	memset(&ListenAddr, 0, sizeof(ListenAddr));

	if( Family == AF_INET )
	{
		FILL_ADDR4(ListenAddr.Addr4, AF_INET, LocalAddr, LocalPort);

		AddrLen = sizeof(struct sockaddr);
	} else {
		char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};

		sscanf(LocalAddr, "[%s]", Addr);

		ListenAddr.Addr6.sin6_family = Family;
		ListenAddr.Addr6.sin6_port = htons(LocalPort);
		IPv6AddressToNum(Addr, &(ListenAddr.Addr6.sin6_addr));

		AddrLen = sizeof(struct sockaddr_in6);
	}

	if(	bind(ListenSocketUDP, (struct sockaddr*)&(ListenAddr), AddrLen)
			!= 0
		)
	{
		int		ErrorNum = GET_LAST_ERROR();
		char	ErrorMessage[320];
		ErrorMessage[0] = '\0';

		GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));

		ERRORMSG("Opening UDP socket failed. %d : %s\n",
				 ErrorNum,
				 ErrorMessage
				 );
		return -2;
	}

	CREATE_MUTEX(ListenMutex);
	EFFECTIVE_LOCK_INIT(LockOfSendBack);

	MaximumMessageSize = GetMaximumMessageSize(ListenSocketUDP);
	if(MaximumMessageSize < 0)
	{
		MaximumMessageSize = 1000;
	}
	Inited = TRUE;

	return 0;
}
Exemplo n.º 6
0
/* Functions */
int QueryDNSListenTCPInit(void)
{
	static struct _Address	ListenAddr;

	const char	*LocalAddr = ConfigGetString(&ConfigInfo, "LocalInterface");
	int			LocalPort = ConfigGetInt32(&ConfigInfo, "LocalPort");

	int			AddrLen;

	Family = GetAddressFamily(LocalAddr);

	ListenSocketTCP = socket(Family, SOCK_STREAM, IPPROTO_TCP);
	if(ListenSocketTCP == INVALID_SOCKET)
	{
		int		ErrorNum = GET_LAST_ERROR();
		char	ErrorMessage[320];
		ErrorMessage[0] = '\0';

		GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));

		ERRORMSG("Creating TCP socket failed. %d : %s\n", ErrorNum, ErrorMessage);
		return -1;
	}

	memset(&ListenAddr, 0, sizeof(ListenAddr));

	if( Family == AF_INET )
	{
		FILL_ADDR4(ListenAddr.Addr.Addr4, AF_INET, LocalAddr, LocalPort);

		AddrLen = sizeof(struct sockaddr);
	} else {
		char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};

		sscanf(LocalAddr, "[%s]", Addr);

		ListenAddr.Addr.Addr6.sin6_family = Family;
		ListenAddr.Addr.Addr6.sin6_port = htons(LocalPort);
		IPv6AddressToNum(Addr, &(ListenAddr.Addr.Addr6.sin6_addr));

		AddrLen = sizeof(struct sockaddr_in6);
	}

	if(	bind(ListenSocketTCP, (struct sockaddr*)&(ListenAddr.Addr), AddrLen) != 0 )
	{
		int		ErrorNum = GET_LAST_ERROR();
		char	ErrorMessage[320];
		ErrorMessage[0] = '\0';

		GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));

		ERRORMSG("Opening TCP socket failed. %d : %s\n", ErrorNum, ErrorMessage);
		return -2;
	}

	if( listen(ListenSocketTCP, 16) == SOCKET_ERROR )
	{
		int		ErrorNum = GET_LAST_ERROR();
		char	ErrorMessage[320];
		ErrorMessage[0] = '\0';

		GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));

		ERRORMSG("Opening TCP socket failed. %d : %s\n", ErrorNum, ErrorMessage);
		return -3;
	}

	Inited = TRUE;

	return 0;
}