1 static const char rcsid
[] = "$Id: ctl_p.c,v 1.4 2005/04/27 04:56:35 sra Exp $";
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.
22 #include "port_before.h"
24 #include <sys/param.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <arpa/inet.h>
39 #include <isc/assertions.h>
40 #include <isc/eventlib.h>
41 #include <isc/logging.h>
42 #include <isc/memcluster.h>
47 #include "port_after.h"
51 const char * const ctl_sevnames
[] = {
52 "debug", "warning", "error"
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.
64 ctl_logger(enum ctl_severity severity
, const char *format
, ...) {
66 static const char me
[] = "ctl_logger";
68 fprintf(stderr
, "%s(%s): ", me
, ctl_sevnames
[severity
]);
70 vfprintf(stderr
, format
, ap
);
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
));
90 ctl_bufput(struct ctl_buf
*buf
) {
92 REQUIRE(allocated_p(*buf
));
93 memput(buf
->text
, MAX_LINELEN
);
99 ctl_sa_ntop(const struct sockaddr
*sa
,
100 char *buf
, size_t size
,
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
) {
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
)
114 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x): %s",
115 me
, in6
->sin6_family
,
116 in6
->sin6_port
, strerror(errno
));
119 if (strlen(tmp
) + sizeof "[].65535" > size
) {
120 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
123 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in6
->sin6_port
));
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
)
132 (*logger
)(ctl_error
, "%s: inet_ntop(%u %04x %08x): %s",
134 in
->sin_port
, in
->sin_addr
.s_addr
,
138 if (strlen(tmp
) + sizeof "[].65535" > size
) {
139 (*logger
)(ctl_error
, "%s: buffer overflow", me
);
142 (void) sprintf(buf
, "[%s].%u", tmp
, ntohs(in
->sin_port
));
145 #ifndef NO_SOCKADDR_UN
147 const struct sockaddr_un
*un
=
148 (const struct sockaddr_un
*) sa
;
149 unsigned int x
= sizeof un
->sun_path
;
153 strncpy(buf
, un
->sun_path
, x
- 1);
164 ctl_sa_copy(const struct sockaddr
*src
, struct sockaddr
*dst
) {
165 switch (src
->sa_family
) {
167 *((struct sockaddr_in6
*)dst
) =
168 *((const struct sockaddr_in6
*)src
);
171 *((struct sockaddr_in
*)dst
) =
172 *((const struct sockaddr_in
*)src
);
174 #ifndef NO_SOCKADDR_UN
176 *((struct sockaddr_un
*)dst
) =
177 *((const struct sockaddr_un
*)src
);