xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/ctl_p.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
7*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: ctl_p.c,v 8.8 2001/05/29 05:49:26 marka Exp $";
8*7c478bd9Sstevel@tonic-gate #endif /* not lint */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate /*
11*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998,1999 by Internet Software Consortium.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
14*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
15*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
18*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
19*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
20*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
21*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
22*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
23*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /* Extern. */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "port_before.h"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/un.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
39*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
40*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <errno.h>
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <time.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
49*7c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
50*7c478bd9Sstevel@tonic-gate #include <isc/logging.h>
51*7c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
52*7c478bd9Sstevel@tonic-gate #include <isc/ctl.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #include "ctl_p.h"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include "port_after.h"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /* Constants. */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate const char * const ctl_sevnames[] = {
61*7c478bd9Sstevel@tonic-gate 	"debug", "warning", "error"
62*7c478bd9Sstevel@tonic-gate };
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /* Public. */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * ctl_logger()
68*7c478bd9Sstevel@tonic-gate  *	if ctl_startup()'s caller didn't specify a logger, this one
69*7c478bd9Sstevel@tonic-gate  *	is used.  this pollutes stderr with all kinds of trash so it will
70*7c478bd9Sstevel@tonic-gate  *	probably never be used in real applications.
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate void
73*7c478bd9Sstevel@tonic-gate ctl_logger(enum ctl_severity severity, const char *format, ...) {
74*7c478bd9Sstevel@tonic-gate 	va_list ap;
75*7c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_logger";
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s(%s): ", me, ctl_sevnames[severity]);
78*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
79*7c478bd9Sstevel@tonic-gate 	vfprintf(stderr, format, ap);
80*7c478bd9Sstevel@tonic-gate 	va_end(ap);
81*7c478bd9Sstevel@tonic-gate 	fputc('\n', stderr);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate int
85*7c478bd9Sstevel@tonic-gate ctl_bufget(struct ctl_buf *buf, ctl_logfunc logger) {
86*7c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_bufget";
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	REQUIRE(!allocated_p(*buf) && buf->used == 0);
89*7c478bd9Sstevel@tonic-gate 	buf->text = memget(MAX_LINELEN);
90*7c478bd9Sstevel@tonic-gate 	if (!allocated_p(*buf)) {
91*7c478bd9Sstevel@tonic-gate 		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
92*7c478bd9Sstevel@tonic-gate 		return (-1);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 	buf->used = 0;
95*7c478bd9Sstevel@tonic-gate 	return (0);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate void
99*7c478bd9Sstevel@tonic-gate ctl_bufput(struct ctl_buf *buf) {
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	REQUIRE(allocated_p(*buf));
102*7c478bd9Sstevel@tonic-gate 	memput(buf->text, MAX_LINELEN);
103*7c478bd9Sstevel@tonic-gate 	buf->text = NULL;
104*7c478bd9Sstevel@tonic-gate 	buf->used = 0;
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate const char *
108*7c478bd9Sstevel@tonic-gate ctl_sa_ntop(const struct sockaddr *sa,
109*7c478bd9Sstevel@tonic-gate 	    char *buf, size_t size,
110*7c478bd9Sstevel@tonic-gate 	    ctl_logfunc logger)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_sa_ntop";
113*7c478bd9Sstevel@tonic-gate 	static const char punt[] = "[0].-1";
114*7c478bd9Sstevel@tonic-gate 	char tmp[INET6_ADDRSTRLEN];
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
117*7c478bd9Sstevel@tonic-gate 	case AF_INET6: {
118*7c478bd9Sstevel@tonic-gate 		const struct sockaddr_in6 *in6 =
119*7c478bd9Sstevel@tonic-gate 					(const struct sockaddr_in6 *) sa;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		if (inet_ntop(in6->sin6_family, &in6->sin6_addr, tmp, sizeof tmp)
122*7c478bd9Sstevel@tonic-gate 		    == NULL) {
123*7c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: inet_ntop(%u %04x): %s",
124*7c478bd9Sstevel@tonic-gate 				  me, in6->sin6_family,
125*7c478bd9Sstevel@tonic-gate 				  in6->sin6_port, strerror(errno));
126*7c478bd9Sstevel@tonic-gate 			return (punt);
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 		if (strlen(tmp) + sizeof "[].65535" > size) {
129*7c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: buffer overflow", me);
130*7c478bd9Sstevel@tonic-gate 			return (punt);
131*7c478bd9Sstevel@tonic-gate 		}
132*7c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "[%s].%u", tmp, ntohs(in6->sin6_port));
133*7c478bd9Sstevel@tonic-gate 		return (buf);
134*7c478bd9Sstevel@tonic-gate 	    }
135*7c478bd9Sstevel@tonic-gate 	case AF_INET: {
136*7c478bd9Sstevel@tonic-gate 		const struct sockaddr_in *in =
137*7c478bd9Sstevel@tonic-gate 					      (const struct sockaddr_in *) sa;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		if (inet_ntop(in->sin_family, &in->sin_addr, tmp, sizeof tmp)
140*7c478bd9Sstevel@tonic-gate 		    == NULL) {
141*7c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: inet_ntop(%u %04x %08x): %s",
142*7c478bd9Sstevel@tonic-gate 				  me, in->sin_family,
143*7c478bd9Sstevel@tonic-gate 				  in->sin_port, in->sin_addr.s_addr,
144*7c478bd9Sstevel@tonic-gate 				  strerror(errno));
145*7c478bd9Sstevel@tonic-gate 			return (punt);
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 		if (strlen(tmp) + sizeof "[].65535" > size) {
148*7c478bd9Sstevel@tonic-gate 			(*logger)(ctl_error, "%s: buffer overflow", me);
149*7c478bd9Sstevel@tonic-gate 			return (punt);
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "[%s].%u", tmp, ntohs(in->sin_port));
152*7c478bd9Sstevel@tonic-gate 		return (buf);
153*7c478bd9Sstevel@tonic-gate 	    }
154*7c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
155*7c478bd9Sstevel@tonic-gate 	case AF_UNIX: {
156*7c478bd9Sstevel@tonic-gate 		const struct sockaddr_un *un =
157*7c478bd9Sstevel@tonic-gate 					      (const struct sockaddr_un *) sa;
158*7c478bd9Sstevel@tonic-gate 		unsigned int x = sizeof un->sun_path;
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 		if (x > size)
161*7c478bd9Sstevel@tonic-gate 			x = size;
162*7c478bd9Sstevel@tonic-gate 		strncpy(buf, un->sun_path, x - 1);
163*7c478bd9Sstevel@tonic-gate 		buf[x - 1] = '\0';
164*7c478bd9Sstevel@tonic-gate 		return (buf);
165*7c478bd9Sstevel@tonic-gate 	    }
166*7c478bd9Sstevel@tonic-gate #endif
167*7c478bd9Sstevel@tonic-gate 	default:
168*7c478bd9Sstevel@tonic-gate 		return (punt);
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate void
173*7c478bd9Sstevel@tonic-gate ctl_sa_copy(const struct sockaddr *src, struct sockaddr *dst) {
174*7c478bd9Sstevel@tonic-gate 	switch (src->sa_family) {
175*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
176*7c478bd9Sstevel@tonic-gate 		*((struct sockaddr_in6 *)dst) =
177*7c478bd9Sstevel@tonic-gate 					 *((const struct sockaddr_in6 *)src);
178*7c478bd9Sstevel@tonic-gate 		break;
179*7c478bd9Sstevel@tonic-gate 	case AF_INET:
180*7c478bd9Sstevel@tonic-gate 		*((struct sockaddr_in *)dst) =
181*7c478bd9Sstevel@tonic-gate 					  *((const struct sockaddr_in *)src);
182*7c478bd9Sstevel@tonic-gate 		break;
183*7c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
184*7c478bd9Sstevel@tonic-gate 	case AF_UNIX:
185*7c478bd9Sstevel@tonic-gate 		*((struct sockaddr_un *)dst) =
186*7c478bd9Sstevel@tonic-gate 					  *((const struct sockaddr_un *)src);
187*7c478bd9Sstevel@tonic-gate 		break;
188*7c478bd9Sstevel@tonic-gate #endif
189*7c478bd9Sstevel@tonic-gate 	default:
190*7c478bd9Sstevel@tonic-gate 		*dst = *src;
191*7c478bd9Sstevel@tonic-gate 		break;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate }
194