1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: ctl_clnt.c,v 1.4.2.1.4.3 2004/03/17 01:13:35 marka 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>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
42 #include <isc/assertions.h>
44 #include <isc/eventlib.h>
46 #include <isc/memcluster.h>
50 #include "port_after.h"
57 #define donefunc_p(ctx) ((ctx).donefunc != NULL)
58 #define arpacode_p(line) (isdigit((unsigned char)(line[0])) && \
59 isdigit((unsigned char)(line[1])) && \
60 isdigit((unsigned char)(line[2])))
61 #define arpacont_p(line) (line[3] == '-')
62 #define arpadone_p(line) (line[3] == ' ' || line[3] == '\t' || \
63 line[3] == '\r' || line[3] == '\0')
68 initializing = 0, connecting, connected, destroyed
72 LINK(struct ctl_tran) link;
73 LINK(struct ctl_tran) wlink;
74 struct ctl_cctx * ctx;
75 struct ctl_buf outbuf;
76 ctl_clntdone donefunc;
85 ctl_clntdone donefunc;
92 struct timespec timeout;
93 LIST(struct ctl_tran) tran;
94 LIST(struct ctl_tran) wtran;
99 static struct ctl_tran *new_tran(struct ctl_cctx *, ctl_clntdone, void *, int);
100 static void start_write(struct ctl_cctx *);
101 static void destroy(struct ctl_cctx *, int);
102 static void error(struct ctl_cctx *);
103 static void new_state(struct ctl_cctx *, enum state);
104 static void conn_done(evContext, void *, int,
107 static void write_done(evContext, void *, int, int);
108 static void start_read(struct ctl_cctx *);
109 static void stop_read(struct ctl_cctx *);
110 static void readable(evContext, void *, int, int);
111 static void start_timer(struct ctl_cctx *);
112 static void stop_timer(struct ctl_cctx *);
113 static void touch_timer(struct ctl_cctx *);
114 static void timer(evContext, void *,
115 struct timespec, struct timespec);
119 static const char * const state_names[] = {
120 "initializing", "connecting", "connected", "destroyed"
128 * create, condition, and connect to a listener on the control port.
131 ctl_client(evContext lev, const struct sockaddr *cap, size_t cap_len,
132 const struct sockaddr *sap, size_t sap_len,
133 ctl_clntdone donefunc, void *uap,
134 u_int timeout, ctl_logfunc logger)
136 static const char me[] = "ctl_client";
137 static const int on = 1;
138 struct ctl_cctx *ctx;
139 struct sockaddr *captmp;
143 ctx = memget(sizeof *ctx);
145 (*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
148 ctx->state = initializing;
150 ctx->logger = logger;
151 ctx->timeout = evConsTime(timeout, 0);
152 ctx->donefunc = donefunc;
154 ctx->coID.opaque = NULL;
155 ctx->tiID.opaque = NULL;
156 ctx->rdID.opaque = NULL;
157 ctx->wrID.opaque = NULL;
158 buffer_init(ctx->inbuf);
159 INIT_LIST(ctx->tran);
160 INIT_LIST(ctx->wtran);
161 ctx->sock = socket(sap->sa_family, SOCK_STREAM, PF_UNSPEC);
162 if (ctx->sock > evHighestFD(ctx->ev)) {
167 (*ctx->logger)(ctl_error, "%s: socket: %s",
168 me, strerror(errno));
172 if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR,
173 (const char *)&on, sizeof on) != 0) {
174 (*ctx->logger)(ctl_warning,
175 "%s: setsockopt(REUSEADDR): %s",
176 me, strerror(errno));
178 DE_CONST(cap, captmp);
179 if (bind(ctx->sock, captmp, cap_len) < 0) {
180 (*ctx->logger)(ctl_error, "%s: bind: %s", me,
185 if (evConnect(lev, ctx->sock, (const struct sockaddr *)sap, sap_len,
186 conn_done, ctx, &ctx->coID) < 0) {
187 (*ctx->logger)(ctl_error, "%s: evConnect(fd %d): %s",
188 me, ctx->sock, strerror(errno));
193 memput(ctx, sizeof *ctx);
197 new_state(ctx, connecting);
204 * close a client and release all of its resources.
207 ctl_endclient(struct ctl_cctx *ctx) {
208 if (ctx->state != destroyed)
210 memput(ctx, sizeof *ctx);
215 * ctl_command(ctx, cmd, len, donefunc, uap)
216 * Queue a transaction, which will begin with sending cmd
217 * and complete by calling donefunc with the answer.
220 ctl_command(struct ctl_cctx *ctx, const char *cmd, size_t len,
221 ctl_clntdone donefunc, void *uap)
223 struct ctl_tran *tran;
227 switch (ctx->state) {
237 if (len >= (size_t)MAX_LINELEN) {
241 tran = new_tran(ctx, donefunc, uap, 1);
244 if (ctl_bufget(&tran->outbuf, ctx->logger) < 0)
246 memcpy(tran->outbuf.text, cmd, len);
247 tran->outbuf.used = len;
248 for (pc = tran->outbuf.text, n = 0; n < tran->outbuf.used; pc++, n++)
249 if (!isascii((unsigned char)*pc) ||
250 !isprint((unsigned char)*pc))
258 static struct ctl_tran *
259 new_tran(struct ctl_cctx *ctx, ctl_clntdone donefunc, void *uap, int w) {
260 struct ctl_tran *new = memget(sizeof *new);
265 buffer_init(new->outbuf);
266 new->donefunc = donefunc;
268 INIT_LINK(new, link);
269 INIT_LINK(new, wlink);
270 APPEND(ctx->tran, new, link);
272 APPEND(ctx->wtran, new, wlink);
277 start_write(struct ctl_cctx *ctx) {
278 static const char me[] = "isc/ctl_clnt::start_write";
279 struct ctl_tran *tran;
280 struct iovec iov[2], *iovp = iov;
283 REQUIRE(ctx->state == connecting || ctx->state == connected);
284 /* If there is a write in progress, don't try to write more yet. */
285 if (ctx->wrID.opaque != NULL)
287 /* If there are no trans, make sure timer is off, and we're done. */
288 if (EMPTY(ctx->wtran)) {
289 if (ctx->tiID.opaque != NULL)
293 /* Pull it off the head of the write queue. */
294 tran = HEAD(ctx->wtran);
295 UNLINK(ctx->wtran, tran, wlink);
296 /* Since there are some trans, make sure timer is successfully "on". */
297 if (ctx->tiID.opaque != NULL)
301 if (ctx->state == destroyed)
303 /* Marshall a newline-terminated message and clock it out. */
304 *iovp++ = evConsIovec(tran->outbuf.text, tran->outbuf.used);
305 DE_CONST("\r\n", tmp);
306 *iovp++ = evConsIovec(tmp, 2);
307 if (evWrite(ctx->ev, ctx->sock, iov, iovp - iov,
308 write_done, tran, &ctx->wrID) < 0) {
309 (*ctx->logger)(ctl_error, "%s: evWrite: %s", me,
314 if (evTimeRW(ctx->ev, ctx->wrID, ctx->tiID) < 0) {
315 (*ctx->logger)(ctl_error, "%s: evTimeRW: %s", me,
323 destroy(struct ctl_cctx *ctx, int notify) {
324 struct ctl_tran *this, *next;
326 if (ctx->sock != -1) {
327 (void) close(ctx->sock);
330 switch (ctx->state) {
332 REQUIRE(ctx->wrID.opaque == NULL);
333 REQUIRE(EMPTY(ctx->tran));
335 * This test is nec'y since destroy() can be called from
336 * start_read() while the state is still "connecting".
338 if (ctx->coID.opaque != NULL) {
339 (void)evCancelConn(ctx->ev, ctx->coID);
340 ctx->coID.opaque = NULL;
344 REQUIRE(ctx->coID.opaque == NULL);
345 if (ctx->wrID.opaque != NULL) {
346 (void)evCancelRW(ctx->ev, ctx->wrID);
347 ctx->wrID.opaque = NULL;
349 if (ctx->rdID.opaque != NULL)
357 if (allocated_p(ctx->inbuf))
358 ctl_bufput(&ctx->inbuf);
359 for (this = HEAD(ctx->tran); this != NULL; this = next) {
360 next = NEXT(this, link);
361 if (allocated_p(this->outbuf))
362 ctl_bufput(&this->outbuf);
363 if (notify && this->donefunc != NULL)
364 (*this->donefunc)(ctx, this->uap, NULL, 0);
365 memput(this, sizeof *this);
367 if (ctx->tiID.opaque != NULL)
369 new_state(ctx, destroyed);
373 error(struct ctl_cctx *ctx) {
374 REQUIRE(ctx->state != destroyed);
379 new_state(struct ctl_cctx *ctx, enum state new_state) {
380 static const char me[] = "isc/ctl_clnt::new_state";
382 (*ctx->logger)(ctl_debug, "%s: %s -> %s", me,
383 state_names[ctx->state], state_names[new_state]);
384 ctx->state = new_state;
388 conn_done(evContext ev, void *uap, int fd,
389 const void *la, int lalen,
390 const void *ra, int ralen)
392 static const char me[] = "isc/ctl_clnt::conn_done";
393 struct ctl_cctx *ctx = uap;
394 struct ctl_tran *tran;
402 ctx->coID.opaque = NULL;
404 (*ctx->logger)(ctl_error, "%s: evConnect: %s", me,
409 new_state(ctx, connected);
410 tran = new_tran(ctx, ctx->donefunc, ctx->uap, 0);
412 (*ctx->logger)(ctl_error, "%s: new_tran failed: %s", me,
418 if (ctx->state == destroyed) {
419 (*ctx->logger)(ctl_error, "%s: start_read failed: %s",
420 me, strerror(errno));
427 write_done(evContext lev, void *uap, int fd, int bytes) {
428 struct ctl_tran *tran = (struct ctl_tran *)uap;
429 struct ctl_cctx *ctx = tran->ctx;
434 ctx->wrID.opaque = NULL;
435 if (ctx->tiID.opaque != NULL)
437 ctl_bufput(&tran->outbuf);
446 start_read(struct ctl_cctx *ctx) {
447 static const char me[] = "isc/ctl_clnt::start_read";
449 REQUIRE(ctx->state == connecting || ctx->state == connected);
450 REQUIRE(ctx->rdID.opaque == NULL);
451 if (evSelectFD(ctx->ev, ctx->sock, EV_READ, readable, ctx,
454 (*ctx->logger)(ctl_error, "%s: evSelect(fd %d): %s", me,
455 ctx->sock, strerror(errno));
462 stop_read(struct ctl_cctx *ctx) {
463 REQUIRE(ctx->coID.opaque == NULL);
464 REQUIRE(ctx->rdID.opaque != NULL);
465 (void)evDeselectFD(ctx->ev, ctx->rdID);
466 ctx->rdID.opaque = NULL;
470 readable(evContext ev, void *uap, int fd, int evmask) {
471 static const char me[] = "isc/ctl_clnt::readable";
472 struct ctl_cctx *ctx = uap;
473 struct ctl_tran *tran;
479 REQUIRE(ctx != NULL);
481 REQUIRE(evmask == EV_READ);
482 REQUIRE(ctx->state == connected);
483 REQUIRE(!EMPTY(ctx->tran));
484 tran = HEAD(ctx->tran);
485 if (!allocated_p(ctx->inbuf) &&
486 ctl_bufget(&ctx->inbuf, ctx->logger) < 0) {
487 (*ctx->logger)(ctl_error, "%s: can't get an input buffer", me);
491 n = read(ctx->sock, ctx->inbuf.text + ctx->inbuf.used,
492 MAX_LINELEN - ctx->inbuf.used);
494 (*ctx->logger)(ctl_warning, "%s: read: %s", me,
495 (n == 0) ? "Unexpected EOF" : strerror(errno));
499 if (ctx->tiID.opaque != NULL)
501 ctx->inbuf.used += n;
502 (*ctx->logger)(ctl_debug, "%s: read %d, used %d", me,
505 eos = memchr(ctx->inbuf.text, '\n', ctx->inbuf.used);
506 if (eos != NULL && eos != ctx->inbuf.text && eos[-1] == '\r') {
510 if (!arpacode_p(ctx->inbuf.text)) {
511 /* XXX Doesn't FTP do this sometimes? Is it legal? */
512 (*ctx->logger)(ctl_error, "%s: no arpa code (%s)", me,
517 if (arpadone_p(ctx->inbuf.text))
519 else if (arpacont_p(ctx->inbuf.text))
522 /* XXX Doesn't FTP do this sometimes? Is it legal? */
523 (*ctx->logger)(ctl_error, "%s: no arpa flag (%s)", me,
528 (*tran->donefunc)(ctx, tran->uap, ctx->inbuf.text,
529 (done ? 0 : CTL_MORE));
530 ctx->inbuf.used -= ((eos - ctx->inbuf.text) + 1);
531 if (ctx->inbuf.used == 0U)
532 ctl_bufput(&ctx->inbuf);
534 memmove(ctx->inbuf.text, eos + 1, ctx->inbuf.used);
536 UNLINK(ctx->tran, tran, link);
537 memput(tran, sizeof *tran);
542 if (allocated_p(ctx->inbuf))
546 if (ctx->inbuf.used == (size_t)MAX_LINELEN) {
547 (*ctx->logger)(ctl_error, "%s: line too long (%-10s...)", me,
553 /* Timer related stuff. */
556 start_timer(struct ctl_cctx *ctx) {
557 static const char me[] = "isc/ctl_clnt::start_timer";
559 REQUIRE(ctx->tiID.opaque == NULL);
560 if (evSetIdleTimer(ctx->ev, timer, ctx, ctx->timeout, &ctx->tiID) < 0){
561 (*ctx->logger)(ctl_error, "%s: evSetIdleTimer: %s", me,
569 stop_timer(struct ctl_cctx *ctx) {
570 static const char me[] = "isc/ctl_clnt::stop_timer";
572 REQUIRE(ctx->tiID.opaque != NULL);
573 if (evClearIdleTimer(ctx->ev, ctx->tiID) < 0) {
574 (*ctx->logger)(ctl_error, "%s: evClearIdleTimer: %s", me,
579 ctx->tiID.opaque = NULL;
583 touch_timer(struct ctl_cctx *ctx) {
584 REQUIRE(ctx->tiID.opaque != NULL);
586 evTouchIdleTimer(ctx->ev, ctx->tiID);
590 timer(evContext ev, void *uap, struct timespec due, struct timespec itv) {
591 static const char me[] = "isc/ctl_clnt::timer";
592 struct ctl_cctx *ctx = uap;
598 ctx->tiID.opaque = NULL;
599 (*ctx->logger)(ctl_error, "%s: timeout after %u seconds while %s", me,
600 ctx->timeout.tv_sec, state_names[ctx->state]);