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";
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.
26 #include "port_before.h"
28 #include <sys/param.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <arpa/inet.h>
43 #include <isc/assertions.h>
44 #include <isc/eventlib.h>
45 #include <isc/logging.h>
46 #include <isc/memcluster.h>
51 #include "port_after.h"
55 const char * const ctl_sevnames
[] = {
56 "debug", "warning", "error"
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.
68 ctl_logger(enum ctl_severity severity
, const char *format
, ...) {
70 static const char me
[] = "ctl_logger";
72 fprintf(stderr
, "%s(%s): ", me
, ctl_sevnames
[severity
]);
74 vfprintf(stderr
, format
, ap
);
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
));
94 ctl_bufput(struct ctl_buf
*buf
) {
96 REQUIRE(allocated_p(*buf
));
97 memput(buf
->text
, MAX_LINELEN
);
103 ctl_sa_ntop(const struct sockaddr
*sa
,
104 char *buf
, size_t size
,
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
) {
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
)
118 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x): %s",
119 me
, in6
->sin6_family
,
120 in6
->sin6_port
, strerror(errno
));
123 if (strlen(tmp
) + sizeof "[].65535" > size
) {
124 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
127 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in6
->sin6_port
));
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
)
136 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x %08x): %s",
138 in
->sin_port
, in
->sin_addr
.s_addr
,
142 if (strlen(tmp
) + sizeof "[].65535" > size
) {
143 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
146 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in
->sin_port
));
149 #ifndef NO_SOCKADDR_UN
151 const struct sockaddr_un
*un
=
152 (const struct sockaddr_un
*) sa
;
153 unsigned int x
= sizeof un
->sun_path
;
157 strncpy(buf
, un
->sun_path
, x
- 1);
168 ctl_sa_copy(const struct sockaddr
*src
, struct sockaddr
*dst
) {
169 switch (src
->sa_family
) {
171 *((struct sockaddr_in6
*)dst
) =
172 *((const struct sockaddr_in6
*)src
);
175 *((struct sockaddr_in
*)dst
) =
176 *((const struct sockaddr_in
*)src
);
178 #ifndef NO_SOCKADDR_UN
180 *((struct sockaddr_un
*)dst
) =
181 *((const struct sockaddr_un
*)src
);