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