1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid
[] = "$Id: ctl_p.c,v 1.4 2005/04/27 04:56:35 sra Exp $";
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1998,1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "port_before.h"
26 #include <sys/param.h>
28 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
41 #include <isc/assertions.h>
42 #include <isc/eventlib.h>
43 #include <isc/logging.h>
44 #include <isc/memcluster.h>
49 #include "port_after.h"
53 const char * const ctl_sevnames
[] = {
54 "debug", "warning", "error"
61 * if ctl_startup()'s caller didn't specify a logger, this one
62 * is used. this pollutes stderr with all kinds of trash so it will
63 * probably never be used in real applications.
66 ctl_logger(enum ctl_severity severity
, const char *format
, ...) {
68 static const char me
[] = "ctl_logger";
70 fprintf(stderr
, "%s(%s): ", me
, ctl_sevnames
[severity
]);
72 vfprintf(stderr
, format
, ap
);
78 ctl_bufget(struct ctl_buf
*buf
, ctl_logfunc logger
) {
79 static const char me
[] = "ctl_bufget";
81 REQUIRE(!allocated_p(*buf
) && buf
->used
== 0U);
82 buf
->text
= memget(MAX_LINELEN
);
83 if (!allocated_p(*buf
)) {
84 (*logger
)(ctl_error
, "%s: getmem: %s", me
, strerror(errno
));
92 ctl_bufput(struct ctl_buf
*buf
) {
94 REQUIRE(allocated_p(*buf
));
95 memput(buf
->text
, MAX_LINELEN
);
101 ctl_sa_ntop(const struct sockaddr
*sa
,
102 char *buf
, size_t size
,
105 static const char me
[] = "ctl_sa_ntop";
106 static const char punt
[] = "[0].-1";
107 char tmp
[INET6_ADDRSTRLEN
];
109 switch (sa
->sa_family
) {
111 const struct sockaddr_in6
*in6
=
112 (const struct sockaddr_in6
*) sa
;
114 if (inet_ntop(in6
->sin6_family
, &in6
->sin6_addr
, tmp
, sizeof tmp
)
116 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x): %s",
117 me
, in6
->sin6_family
,
118 in6
->sin6_port
, strerror(errno
));
121 if (strlen(tmp
) + sizeof "[].65535" > size
) {
122 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
125 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in6
->sin6_port
));
129 const struct sockaddr_in
*in
=
130 (const struct sockaddr_in
*) sa
;
132 if (inet_ntop(in
->sin_family
, &in
->sin_addr
, tmp
, sizeof tmp
)
134 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x %08x): %s",
136 in
->sin_port
, in
->sin_addr
.s_addr
,
140 if (strlen(tmp
) + sizeof "[].65535" > size
) {
141 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
144 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in
->sin_port
));
147 #ifndef NO_SOCKADDR_UN
149 const struct sockaddr_un
*un
=
150 (const struct sockaddr_un
*) sa
;
151 unsigned int x
= sizeof un
->sun_path
;
155 strncpy(buf
, un
->sun_path
, x
- 1);
166 ctl_sa_copy(const struct sockaddr
*src
, struct sockaddr
*dst
) {
167 switch (src
->sa_family
) {
169 *((struct sockaddr_in6
*)dst
) =
170 *((const struct sockaddr_in6
*)src
);
173 *((struct sockaddr_in
*)dst
) =
174 *((const struct sockaddr_in
*)src
);
176 #ifndef NO_SOCKADDR_UN
178 *((struct sockaddr_un
*)dst
) =
179 *((const struct sockaddr_un
*)src
);