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