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