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