summaryrefslogtreecommitdiffstats
path: root/contrib/bind/lib/isc/ctl_p.c
blob: 7d9058c6b6e76979876628c8f288126abba3b881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#if !defined(lint) && !defined(SABER)
static const char rcsid[] = "$Id: ctl_p.c,v 8.7 2000/02/04 08:28:33 vixie Exp $";
#endif /* not lint */

/*
 * Copyright (c) 1998,1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/* Extern. */

#include "port_before.h"

#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <isc/assertions.h>
#include <isc/eventlib.h>
#include <isc/logging.h>
#include <isc/memcluster.h>
#include <isc/ctl.h>

#include "ctl_p.h"

#include "port_after.h"

/* Constants. */

const char * const ctl_sevnames[] = {
	"debug", "warning", "error"
};

/* Public. */

/*
 * ctl_logger()
 *	if ctl_startup()'s caller didn't specify a logger, this one
 *	is used.  this pollutes stderr with all kinds of trash so it will
 *	probably never be used in real applications.
 */
void
ctl_logger(enum ctl_severity severity, const char *format, ...) {
	va_list ap;
	static const char me[] = "ctl_logger";

	fprintf(stderr, "%s(%s): ", me, ctl_sevnames[severity]);
	va_start(ap, format);
	vfprintf(stderr, format, ap);
	va_end(ap);
	fputc('\n', stderr);
}

int
ctl_bufget(struct ctl_buf *buf, ctl_logfunc logger) {
	static const char me[] = "ctl_bufget";

	REQUIRE(!allocated_p(*buf) && buf->used == 0);
	buf->text = memget(MAX_LINELEN);
	if (!allocated_p(*buf)) {
		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
		return (-1);
	}
	buf->used = 0;
	return (0);
}

void
ctl_bufput(struct ctl_buf *buf) {

	REQUIRE(allocated_p(*buf));
	memput(buf->text, MAX_LINELEN);
	buf->text = NULL;
	buf->used = 0;
}

const char *
ctl_sa_ntop(const struct sockaddr *sa,
	    char *buf, size_t size,
	    ctl_logfunc logger)
{
	static const char me[] = "ctl_sa_ntop";
	static const char punt[] = "[0].-1";
	char tmp[sizeof "255.255.255.255"];

	switch (sa->sa_family) {
	case AF_INET: {
		const struct sockaddr_in *in = (struct sockaddr_in *) sa;

		if (inet_ntop(in->sin_family, &in->sin_addr, tmp, sizeof tmp)
		    == NULL) {
			(*logger)(ctl_error, "%s: inet_ntop(%u %04x %08x): %s",
				  me, in->sin_family,
				  in->sin_port, in->sin_addr.s_addr,
				  strerror(errno));
			return (punt);
		}
		if (strlen(tmp) + sizeof "[].65535" > size) {
			(*logger)(ctl_error, "%s: buffer overflow", me);
			return (punt);
		}
		(void) sprintf(buf, "[%s].%u", tmp, ntohs(in->sin_port));
		return (buf);
	    }
#ifndef NO_SOCKADDR_UN
	case AF_UNIX: {
		const struct sockaddr_un *un = (struct sockaddr_un *) sa;
		int x = sizeof un->sun_path;

		if (x > size)
			x = size;
		strncpy(buf, un->sun_path, x - 1);
		buf[x - 1] = '\0';
		return (buf);
	    }
#endif
	default:
		return (punt);
	}
}

void
ctl_sa_copy(const struct sockaddr *src, struct sockaddr *dst) {
	switch (src->sa_family) {
	case AF_INET:
		*((struct sockaddr_in *)dst) =  *((struct sockaddr_in *)src);
		break;
#ifndef NO_SOCKADDR_UN
	case AF_UNIX:
		*((struct sockaddr_un *)dst) =  *((struct sockaddr_un *)src);
		break;
#endif
	default:
		*dst = *src;
		break;
	}
}
OpenPOWER on IntegriCloud