xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/ctl_p.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1998,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Extern. */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sys/param.h>
237c478bd9Sstevel@tonic-gate #include <sys/file.h>
247c478bd9Sstevel@tonic-gate #include <sys/socket.h>
257c478bd9Sstevel@tonic-gate #include <sys/un.h>
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <netinet/in.h>
287c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
297c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <time.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
387c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
397c478bd9Sstevel@tonic-gate #include <isc/logging.h>
407c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
417c478bd9Sstevel@tonic-gate #include <isc/ctl.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "ctl_p.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "port_after.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Constants. */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate const char * const ctl_sevnames[] = {
507c478bd9Sstevel@tonic-gate 	"debug", "warning", "error"
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* Public. */
547c478bd9Sstevel@tonic-gate 
55*9525b14bSRao Shoaib /*%
567c478bd9Sstevel@tonic-gate  * ctl_logger()
577c478bd9Sstevel@tonic-gate  *	if ctl_startup()'s caller didn't specify a logger, this one
587c478bd9Sstevel@tonic-gate  *	is used.  this pollutes stderr with all kinds of trash so it will
597c478bd9Sstevel@tonic-gate  *	probably never be used in real applications.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate void
ctl_logger(enum ctl_severity severity,const char * format,...)627c478bd9Sstevel@tonic-gate ctl_logger(enum ctl_severity severity, const char *format, ...) {
637c478bd9Sstevel@tonic-gate 	va_list ap;
647c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_logger";
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s(%s): ", me, ctl_sevnames[severity]);
677c478bd9Sstevel@tonic-gate 	va_start(ap, format);
687c478bd9Sstevel@tonic-gate 	vfprintf(stderr, format, ap);
697c478bd9Sstevel@tonic-gate 	va_end(ap);
707c478bd9Sstevel@tonic-gate 	fputc('\n', stderr);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
ctl_bufget(struct ctl_buf * buf,ctl_logfunc logger)747c478bd9Sstevel@tonic-gate ctl_bufget(struct ctl_buf *buf, ctl_logfunc logger) {
757c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_bufget";
767c478bd9Sstevel@tonic-gate 
77*9525b14bSRao Shoaib 	REQUIRE(!allocated_p(*buf) && buf->used == 0U);
787c478bd9Sstevel@tonic-gate 	buf->text = memget(MAX_LINELEN);
797c478bd9Sstevel@tonic-gate 	if (!allocated_p(*buf)) {
807c478bd9Sstevel@tonic-gate 		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
817c478bd9Sstevel@tonic-gate 		return (-1);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	buf->used = 0;
847c478bd9Sstevel@tonic-gate 	return (0);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate void
ctl_bufput(struct ctl_buf * buf)887c478bd9Sstevel@tonic-gate ctl_bufput(struct ctl_buf *buf) {
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	REQUIRE(allocated_p(*buf));
917c478bd9Sstevel@tonic-gate 	memput(buf->text, MAX_LINELEN);
927c478bd9Sstevel@tonic-gate 	buf->text = NULL;
937c478bd9Sstevel@tonic-gate 	buf->used = 0;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate const char *
ctl_sa_ntop(const struct sockaddr * sa,char * buf,size_t size,ctl_logfunc logger)977c478bd9Sstevel@tonic-gate ctl_sa_ntop(const struct sockaddr *sa,
987c478bd9Sstevel@tonic-gate 	    char *buf, size_t size,
997c478bd9Sstevel@tonic-gate 	    ctl_logfunc logger)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_sa_ntop";
1027c478bd9Sstevel@tonic-gate 	static const char punt[] = "[0].-1";
1037c478bd9Sstevel@tonic-gate 	char tmp[INET6_ADDRSTRLEN];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
1067c478bd9Sstevel@tonic-gate 	case AF_INET6: {
1077c478bd9Sstevel@tonic-gate 		const struct sockaddr_in6 *in6 =
1087c478bd9Sstevel@tonic-gate 					(const struct sockaddr_in6 *) sa;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		if (inet_ntop(in6->sin6_family, &in6->sin6_addr, tmp, sizeof tmp)
1117c478bd9Sstevel@tonic-gate 		    == NULL) {
1127c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: inet_ntop(%u %04x): %s",
1137c478bd9Sstevel@tonic-gate 				  me, in6->sin6_family,
1147c478bd9Sstevel@tonic-gate 				  in6->sin6_port, strerror(errno));
1157c478bd9Sstevel@tonic-gate 			return (punt);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		if (strlen(tmp) + sizeof "[].65535" > size) {
1187c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: buffer overflow", me);
1197c478bd9Sstevel@tonic-gate 			return (punt);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "[%s].%u", tmp, ntohs(in6->sin6_port));
1227c478bd9Sstevel@tonic-gate 		return (buf);
1237c478bd9Sstevel@tonic-gate 	    }
1247c478bd9Sstevel@tonic-gate 	case AF_INET: {
1257c478bd9Sstevel@tonic-gate 		const struct sockaddr_in *in =
1267c478bd9Sstevel@tonic-gate 					      (const struct sockaddr_in *) sa;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		if (inet_ntop(in->sin_family, &in->sin_addr, tmp, sizeof tmp)
1297c478bd9Sstevel@tonic-gate 		    == NULL) {
1307c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: inet_ntop(%u %04x %08x): %s",
1317c478bd9Sstevel@tonic-gate 				  me, in->sin_family,
1327c478bd9Sstevel@tonic-gate 				  in->sin_port, in->sin_addr.s_addr,
1337c478bd9Sstevel@tonic-gate 				  strerror(errno));
1347c478bd9Sstevel@tonic-gate 			return (punt);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		if (strlen(tmp) + sizeof "[].65535" > size) {
1377c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: buffer overflow", me);
1387c478bd9Sstevel@tonic-gate 			return (punt);
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "[%s].%u", tmp, ntohs(in->sin_port));
1417c478bd9Sstevel@tonic-gate 		return (buf);
1427c478bd9Sstevel@tonic-gate 	    }
1437c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1447c478bd9Sstevel@tonic-gate 	case AF_UNIX: {
1457c478bd9Sstevel@tonic-gate 		const struct sockaddr_un *un =
1467c478bd9Sstevel@tonic-gate 					      (const struct sockaddr_un *) sa;
1477c478bd9Sstevel@tonic-gate 		unsigned int x = sizeof un->sun_path;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		if (x > size)
1507c478bd9Sstevel@tonic-gate 			x = size;
1517c478bd9Sstevel@tonic-gate 		strncpy(buf, un->sun_path, x - 1);
1527c478bd9Sstevel@tonic-gate 		buf[x - 1] = '\0';
1537c478bd9Sstevel@tonic-gate 		return (buf);
1547c478bd9Sstevel@tonic-gate 	    }
1557c478bd9Sstevel@tonic-gate #endif
1567c478bd9Sstevel@tonic-gate 	default:
1577c478bd9Sstevel@tonic-gate 		return (punt);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
ctl_sa_copy(const struct sockaddr * src,struct sockaddr * dst)1627c478bd9Sstevel@tonic-gate ctl_sa_copy(const struct sockaddr *src, struct sockaddr *dst) {
1637c478bd9Sstevel@tonic-gate 	switch (src->sa_family) {
1647c478bd9Sstevel@tonic-gate 	case AF_INET6:
1657c478bd9Sstevel@tonic-gate 		*((struct sockaddr_in6 *)dst) =
1667c478bd9Sstevel@tonic-gate 					 *((const struct sockaddr_in6 *)src);
1677c478bd9Sstevel@tonic-gate 		break;
1687c478bd9Sstevel@tonic-gate 	case AF_INET:
1697c478bd9Sstevel@tonic-gate 		*((struct sockaddr_in *)dst) =
1707c478bd9Sstevel@tonic-gate 					  *((const struct sockaddr_in *)src);
1717c478bd9Sstevel@tonic-gate 		break;
1727c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1737c478bd9Sstevel@tonic-gate 	case AF_UNIX:
1747c478bd9Sstevel@tonic-gate 		*((struct sockaddr_un *)dst) =
1757c478bd9Sstevel@tonic-gate 					  *((const struct sockaddr_un *)src);
1767c478bd9Sstevel@tonic-gate 		break;
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 	default:
1797c478bd9Sstevel@tonic-gate 		*dst = *src;
1807c478bd9Sstevel@tonic-gate 		break;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate }
183*9525b14bSRao Shoaib 
184*9525b14bSRao Shoaib /*! \file */
185