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