listen.c

来自「此dns服务器是在mydns基础上改写」· C语言 代码 · 共 724 行 · 第 1/2 页

C
724 行
字号
#endif	register ADDRLIST *A;	char *cc, *oc, *a, *c;	FirstAddr = LastAddr = NULL;	if (!opt || !opt[0])		return (NULL);	if (!(oc = strdup((char *)opt)))							/* Make copy of 'opt' for mangling */		Err("strdup");	for (c = oc; *c; c++)										/* Replace commas with CONF_FS */		if (*c == ',')			*c = CONF_FS_CHAR;	for (cc = oc; (a = strsep(&cc, CONF_FS_STR)); )		/* Add each address */	{		char	addr[256];											/* Address/port part of address */		int	port = default_port;								/* Port number */		strncpy(addr, a, sizeof(addr)-1);#if HAVE_IPV6		if (is_ipv6(addr))		/* IPv6 - treat '+' as port separator */		{			family = AF_INET6;			if ((c = strchr(addr, '+')))			{				*c++ = '\0';				if (!(port = atoi(c)))					port = default_port;			}		}		else							/* IPv4 - treat '+' or ':' as port separator  */#endif		{			family = AF_INET;			if ((c = strchr(addr, '+')) || (c = strchr(addr, ':')))			{				*c++ = '\0';				if (!(port = atoi(c)))					port = default_port;			}		}		/* If the address specifies the wildcard '*', add a record for each address */		if (addr[0] == '*')		{			for (A = Addresses; A; A = A->next)				if (A->family == AF_INET)					addrlist_add(A->family, &A->addr4, port);#if HAVE_IPV6				else if (A->family == AF_INET6)					addrlist_add(A->family, &A->addr6, port);#endif			continue;		}		/* Handle "localhost" */		if (!strcasecmp(addr, "localhost"))		{			/* Only add the IPv4 localhost if there are SOME IPv4 addresses */			for (A = Addresses; A; A = A->next)				if (A->family == AF_INET)				{					if (inet_pton(AF_INET, "127.0.0.1", &addr4) > 0)						addrlist_add(AF_INET, &addr4, port);					break;				}#if HAVE_IPV6			/* Only add the IPv6 localhost if there are SOME IPv6 addresses */			for (A = Addresses; A; A = A->next)				if (A->family == AF_INET6)				{					memcpy(&addr6, &in6addr_loopback, sizeof(struct in6_addr));					addrlist_add(AF_INET6, &addr6, port);					break;				}#endif			continue;		}		if (family == AF_INET)									/* Add regular IPv4 address */		{			if (inet_pton(AF_INET, addr, &addr4) <= 0)				Warnx("%s: `%s' %s: %s", addr, desc, _("address"), _("invalid IPv4 address format"));			else				addrlist_add(AF_INET, &addr4, port);		}#if HAVE_IPV6		else if (family == AF_INET6)							/* Add regular IPv6 address */		{			if (inet_pton(AF_INET6, addr, &addr6) <= 0)				Warnx("%s: `%s' %s: %s", addr, desc, _("address"), _("invalid IPv6 address format"));			else				addrlist_add(AF_INET6, &addr4, port);		}#endif	}	Free(oc);	return (FirstAddr);}/*--- get_opt_addrlist() ------------------------------------------------------------------------*//**************************************************************************************************	IPV4_LISTENER	Create IPv4 listening socket.  Returns the new file descriptor.  Errors are fatal.**************************************************************************************************/static intipv4_listener(struct sockaddr_in *sa, int protocol){	int fd, opt = 1;	if ((fd = socket(AF_INET, protocol, (protocol == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP)) < 0)		Err("socket");	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)		Warn("setsockopt");	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);	if (bind(fd, (struct sockaddr *)sa, sizeof(struct sockaddr_in)) < 0)		Err("bind (%s): %s:%d",			 (protocol == SOCK_STREAM) ? "TCP" : "UDP", inet_ntoa(sa->sin_addr), ntohs(sa->sin_port));	if (protocol == SOCK_STREAM)	{		if (listen(fd, SOMAXCONN) < 0)			Err("listen");	}	else	{		int n, size;		for (n = 1; n < 1024; n++)		{			size = n * 1024;			if ((setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))) < 0)				break;		}	}#if DEBUG_ENABLED && DEBUG_LISTEN	Debug("listening on %s, %s port %d", ipaddr(AF_INET, &sa->sin_addr),			protocol == SOCK_STREAM ? "TCP" : "UDP", ntohs(sa->sin_port));#endif	return (fd);}/*--- ipv4_listener() ---------------------------------------------------------------------------*/#if HAVE_IPV6/**************************************************************************************************	IPV6_LISTENER	Create IPv6 listening socket.  Returns the new file descriptor.  Errors are fatal.**************************************************************************************************/static intipv6_listener(struct sockaddr_in6 *sa, int protocol){	int fd, opt = 1;	if ((fd = socket(AF_INET6, protocol, (protocol == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP)) < 0)		Err("socket");	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)		Warn("setsockopt");	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);	if (bind(fd, (struct sockaddr *)sa, sizeof(struct sockaddr_in6)) < 0)		Err("bind (%s): %s+%d", (protocol == SOCK_STREAM) ? "TCP" : "UDP",			 ipaddr(AF_INET6, &sa->sin6_addr), ntohs(sa->sin6_port));	if (protocol == SOCK_STREAM)	{		if (listen(fd, 5) < 0)			Err("listen");	}	else	{		int n, size;		for (n = 1; n < 1024; n++)		{			size = n * 1024;			if ((setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))) < 0)				break;		}	}#if DEBUG_ENABLED && DEBUG_LISTEN	Debug("listening on %s, %s port %d", ipaddr(AF_INET6, &sa->sin6_addr), 			protocol == SOCK_STREAM ? "TCP" : "UDP", ntohs(sa->sin6_port));#endif	return (fd);}/*--- ipv6_listener() ---------------------------------------------------------------------------*/#endif/**************************************************************************************************	CREATE_LISTENERS**************************************************************************************************/voidcreate_listeners(void){	ADDRLIST	*Addresses;											/* List of available addresses */	ADDRLIST	*Listen;												/* Listen on these addresses */	ADDRLIST	*NoListen;											/* Don't listen on these addresses */	ADDRLIST	*L, *N;												/* Current address */	char		*port_opt = conf_get(&Conf, "port", 0);	/* "port" config option */	int		port = 53;											/* Listen on this port number */	/* Set default port number */	if (port_opt && atoi(port_opt))		port = atoi(port_opt);	Addresses = addrlist_load(port);							/* Get available addresses */#if DEBUG_ENABLED && DEBUG_LISTEN	Debug(" ");	for (L = Addresses; L; L = L->next)		if (L->family == AF_INET)			Debug("Address: %s (port %d)", ipaddr(AF_INET, &L->addr4), L->port);#if HAVE_IPV6		else if (L->family == AF_INET6)			Debug("Address: %s (port %d)", ipaddr(AF_INET6, &L->addr6), L->port);#endif	Debug(" ");#endif	/* Get addresses specified in 'listen' and 'no-listen' configuration variables */	Listen = get_opt_addrlist(Addresses, conf_get(&Conf, "listen", NULL), port, "listen");	NoListen = get_opt_addrlist(Addresses, conf_get(&Conf, "no-listen", NULL), port, "no-listen");#if DEBUG_ENABLED && DEBUG_LISTEN	for (L = NoListen; L; L = L->next)		if (L->family == AF_INET)			Debug("NoListen: %s (port %d)", ipaddr(AF_INET, &L->addr4), L->port);#if HAVE_IPV6		else if (L->family == AF_INET6)			Debug("NoListen: %s (port %d)", ipaddr(AF_INET6, &L->addr6), L->port);#endif	Debug(" ");#endif#if DEBUG_ENABLED && DEBUG_LISTEN	for (L = Listen; L; L = L->next)		if (L->family == AF_INET)			Debug("Listen: %s (port %d)", ipaddr(AF_INET, &L->addr4), L->port);#if HAVE_IPV6		else if (L->family == AF_INET6)			Debug("Listen: %s (port %d)", ipaddr(AF_INET6, &L->addr6), L->port);#endif	Debug(" ");#endif	/* Deactivate any 'NoListen' addresses found in 'Listen' */	for (N = NoListen; N; N = N->next)		for (L = Listen; L; L = L->next)			if (L->ok && (N->family == L->family) && (N->port == L->port)#if HAVE_IPV6				 && !memcmp(&N->addr6, &L->addr6, sizeof(struct in6_addr))#endif				 && !memcmp(&N->addr4, &L->addr4, sizeof(struct in_addr)))				L->ok = 0;	/* Free 'Addresses' and 'NoListen'; we're done with them */	addrlist_free(Addresses);	addrlist_free(NoListen);	/* Create listening socket for each address in 'Listen' */	for (L = Listen; L; L = L->next)	{		if (!L->ok)			continue;		if (L->family == AF_INET)		{			struct sockaddr_in sa;			memset(&sa, 0, sizeof(struct sockaddr_in));			sa.sin_family = AF_INET;			sa.sin_port = htons(L->port);			memcpy(&sa.sin_addr, &L->addr4, sizeof(struct in_addr));			/* Expand FD lists as appropriate and create listening sockets */			if (!(udp4_fd = realloc(udp4_fd, (1 + num_udp4_fd) * sizeof(int))))				Err(_("out of memory"));			udp4_fd[num_udp4_fd++] = ipv4_listener(&sa, SOCK_DGRAM);			if (axfr_enabled || tcp_enabled)			{				if (!(tcp4_fd = realloc(tcp4_fd, (1 + num_tcp4_fd) * sizeof(int))))					Err(_("out of memory"));				tcp4_fd[num_tcp4_fd++] = ipv4_listener(&sa, SOCK_STREAM);			}		}#if HAVE_IPV6		else if (L->family == AF_INET6)		{			struct sockaddr_in6 sa;			memset(&sa, 0, sizeof(struct sockaddr_in6));			sa.sin6_family = AF_INET6;			sa.sin6_port = htons(L->port);			memcpy(&sa.sin6_addr, &L->addr6, sizeof(struct in6_addr));#if 0			/* These two vars are part of struct sockaddr_in6, but I don't know what they are: */			uint32_t sin6_flowinfo;   /* IPv6 flow information */			uint32_t sin6_scope_id;   /* IPv6 scope-id */#endif			/* Expand FD lists as appropriate and create listening sockets */			if (!(udp6_fd = realloc(udp6_fd, (1 + num_udp6_fd) * sizeof(int))))				Err(_("out of memory"));			udp6_fd[num_udp6_fd++] = ipv6_listener(&sa, SOCK_DGRAM);			if (axfr_enabled || tcp_enabled)			{				if (!(tcp6_fd = realloc(tcp6_fd, (1 + num_tcp6_fd) * sizeof(int))))					Err(_("out of memory"));				tcp6_fd[num_tcp6_fd++] = ipv6_listener(&sa, SOCK_STREAM);			}		}#endif	}	server_greeting();	addrlist_free(Listen);}/*--- create_listeners() ------------------------------------------------------------------------*//**************************************************************************************************	SERVER_GREETING**************************************************************************************************/static voidserver_greeting(void){	char	greeting[512], *g = greeting;						/* Server startup message */	int	total;													/* Total listening sockets */	time_t now = time(NULL);	g += sprintf(g, "%s %s %s %.24s", progname, PACKAGE_VERSION, _("started"), ctime(&now));	total = num_udp4_fd;#if HAVE_IPV6	total += num_udp6_fd;#endif	g += sprintf(g, " (%s %d %s)", _("listening on"),					 total, total == 1 ? _("address") : _("addresses"));#if DEBUG_ENABLED	g += sprintf(g, "%s", _(" (compiled with debug)"));#endif	if (answer_then_quit)		g += sprintf(g, _(" (quit after %u requests)"), answer_then_quit);	Notice("%s", greeting);}/*--- server_greeting() -------------------------------------------------------------------------*//* vi:set ts=3: *//* NEED_PO */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?