示例#1
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;
}
示例#2
0
sa_family_t AddressList_ConvertToAddressFromString(struct _Address *Out, const char *Addr_Port, int DefaultPort)
{
	sa_family_t	Family;

	memset(Out, 0, sizeof(struct _Address));

	Family = GetAddressFamily(Addr_Port);
	Out -> family = Family;

	switch( Family )
	{
		case AF_INET6:
			{
				char		Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};
				in_port_t	Port;
				const char	*PortPos;

				memset(Addr, 0, sizeof(Addr));

				PortPos = strchr(Addr_Port, ']');
				if( PortPos == NULL )
				{
					return -1;
				}

				PortPos = strchr(PortPos, ':');
				if( PortPos == NULL )
				{
					sscanf(Addr_Port, "[%s]", Addr);
					Port = DefaultPort;
				} else {
					int	Port_warpper;

					sscanf(Addr_Port + 1, "%[^]]", Addr);
					sscanf(PortPos + 1, "%d", &Port_warpper);
					Port = Port_warpper;
				}

				Out -> Addr.Addr6.sin6_family = Family;
				Out -> Addr.Addr6.sin6_port = htons(Port);

				IPv6AddressToNum(Addr, &(Out -> Addr.Addr6.sin6_addr));

				return AF_INET6;
			}
			break;

		case AF_INET:
			{
				char		Addr[] = "xxx.xxx.xxx.xxx";
				in_port_t	Port;
				const char	*PortPos;

				memset(Addr, 0, sizeof(Addr));

				PortPos = strchr(Addr_Port, ':');
				if( PortPos == NULL )
				{
					sscanf(Addr_Port, "%s", Addr);
					Port = DefaultPort;
				} else {
					int Port_warpper;
					sscanf(Addr_Port, "%[^:]", Addr);
					sscanf(PortPos + 1, "%d", &Port_warpper);
					Port = Port_warpper;
				}
				FILL_ADDR4(Out -> Addr.Addr4, Family, Addr, Port);

				return AF_INET;
			}
			break;

		default:
			return AF_UNSPEC;
			break;
	}
}
示例#3
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;
}