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