Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / libbind / dist / isc / ctl_p.c
blob2d88043086de2f1d6ccf7266a945c19901e80cc1
1 /* $NetBSD$ */
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 */
7 /*
8 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1998,1999 by Internet Software Consortium.
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.
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.
24 /* Extern. */
26 #include "port_before.h"
28 #include <sys/param.h>
29 #include <sys/file.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <arpa/inet.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
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>
49 #include "ctl_p.h"
51 #include "port_after.h"
53 /* Constants. */
55 const char * const ctl_sevnames[] = {
56 "debug", "warning", "error"
59 /* Public. */
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.
67 void
68 ctl_logger(enum ctl_severity severity, const char *format, ...) {
69 va_list ap;
70 static const char me[] = "ctl_logger";
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);
79 int
80 ctl_bufget(struct ctl_buf *buf, ctl_logfunc logger) {
81 static const char me[] = "ctl_bufget";
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);
89 buf->used = 0;
90 return (0);
93 void
94 ctl_bufput(struct ctl_buf *buf) {
96 REQUIRE(allocated_p(*buf));
97 memput(buf->text, MAX_LINELEN);
98 buf->text = NULL;
99 buf->used = 0;
102 const char *
103 ctl_sa_ntop(const struct sockaddr *sa,
104 char *buf, size_t size,
105 ctl_logfunc logger)
107 static const char me[] = "ctl_sa_ntop";
108 static const char punt[] = "[0].-1";
109 char tmp[INET6_ADDRSTRLEN];
111 switch (sa->sa_family) {
112 case AF_INET6: {
113 const struct sockaddr_in6 *in6 =
114 (const struct sockaddr_in6 *) sa;
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);
123 if (strlen(tmp) + sizeof "[].65535" > size) {
124 (*logger)(ctl_error, "%s: buffer overflow", me);
125 return (punt);
127 (void) sprintf(buf, "[%s].%u", tmp, ntohs(in6->sin6_port));
128 return (buf);
130 case AF_INET: {
131 const struct sockaddr_in *in =
132 (const struct sockaddr_in *) sa;
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);
142 if (strlen(tmp) + sizeof "[].65535" > size) {
143 (*logger)(ctl_error, "%s: buffer overflow", me);
144 return (punt);
146 (void) sprintf(buf, "[%s].%u", tmp, ntohs(in->sin_port));
147 return (buf);
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;
155 if (x > size)
156 x = size;
157 strncpy(buf, un->sun_path, x - 1);
158 buf[x - 1] = '\0';
159 return (buf);
161 #endif
162 default:
163 return (punt);
167 void
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;
190 /*! \file */