xref: /onnv-gate/usr/src/lib/libresolv2/common/isc/ctl_clnt.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM  * Copyright (C) 1998-2003  Internet Software Consortium.
4*11038SRao.Shoaib@Sun.COM  *
5*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or distribute this software for any
6*11038SRao.Shoaib@Sun.COM  * purpose with or without fee is hereby granted, provided that the above
7*11038SRao.Shoaib@Sun.COM  * copyright notice and this permission notice appear in all copies.
8*11038SRao.Shoaib@Sun.COM  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM  * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ctl_clnt.c,v 1.11 2008/11/14 02:36:51 marka Exp $";
200Sstevel@tonic-gate #endif /* not lint */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Extern. */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/file.h>
280Sstevel@tonic-gate #include <sys/socket.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <arpa/nameser.h>
320Sstevel@tonic-gate #include <arpa/inet.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <time.h>
400Sstevel@tonic-gate #include <unistd.h>
41*11038SRao.Shoaib@Sun.COM #ifdef HAVE_MEMORY_H
42*11038SRao.Shoaib@Sun.COM #include <memory.h>
43*11038SRao.Shoaib@Sun.COM #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <isc/assertions.h>
460Sstevel@tonic-gate #include <isc/ctl.h>
470Sstevel@tonic-gate #include <isc/eventlib.h>
480Sstevel@tonic-gate #include <isc/list.h>
490Sstevel@tonic-gate #include <isc/memcluster.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include "ctl_p.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "port_after.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* Constants. */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* Macros. */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define donefunc_p(ctx) ((ctx).donefunc != NULL)
610Sstevel@tonic-gate #define arpacode_p(line) (isdigit((unsigned char)(line[0])) && \
620Sstevel@tonic-gate 			  isdigit((unsigned char)(line[1])) && \
630Sstevel@tonic-gate 			  isdigit((unsigned char)(line[2])))
640Sstevel@tonic-gate #define arpacont_p(line) (line[3] == '-')
650Sstevel@tonic-gate #define arpadone_p(line) (line[3] == ' ' || line[3] == '\t' || \
660Sstevel@tonic-gate 			  line[3] == '\r' || line[3] == '\0')
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* Types. */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate enum state {
710Sstevel@tonic-gate 	initializing = 0, connecting, connected, destroyed
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate struct ctl_tran {
750Sstevel@tonic-gate 	LINK(struct ctl_tran)	link;
760Sstevel@tonic-gate 	LINK(struct ctl_tran)	wlink;
770Sstevel@tonic-gate 	struct ctl_cctx *	ctx;
780Sstevel@tonic-gate 	struct ctl_buf		outbuf;
790Sstevel@tonic-gate 	ctl_clntdone		donefunc;
800Sstevel@tonic-gate 	void *			uap;
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate struct ctl_cctx {
840Sstevel@tonic-gate 	enum state		state;
850Sstevel@tonic-gate 	evContext		ev;
860Sstevel@tonic-gate 	int			sock;
870Sstevel@tonic-gate 	ctl_logfunc		logger;
880Sstevel@tonic-gate 	ctl_clntdone		donefunc;
890Sstevel@tonic-gate 	void *			uap;
900Sstevel@tonic-gate 	evConnID		coID;
910Sstevel@tonic-gate 	evTimerID		tiID;
920Sstevel@tonic-gate 	evFileID		rdID;
930Sstevel@tonic-gate 	evStreamID		wrID;
940Sstevel@tonic-gate 	struct ctl_buf		inbuf;
950Sstevel@tonic-gate 	struct timespec		timeout;
960Sstevel@tonic-gate 	LIST(struct ctl_tran)	tran;
970Sstevel@tonic-gate 	LIST(struct ctl_tran)	wtran;
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* Forward. */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static struct ctl_tran *new_tran(struct ctl_cctx *, ctl_clntdone, void *, int);
1030Sstevel@tonic-gate static void		start_write(struct ctl_cctx *);
1040Sstevel@tonic-gate static void		destroy(struct ctl_cctx *, int);
1050Sstevel@tonic-gate static void		error(struct ctl_cctx *);
1060Sstevel@tonic-gate static void		new_state(struct ctl_cctx *, enum state);
1070Sstevel@tonic-gate static void		conn_done(evContext, void *, int,
1080Sstevel@tonic-gate 				  const void *, int,
1090Sstevel@tonic-gate 				  const void *, int);
1100Sstevel@tonic-gate static void		write_done(evContext, void *, int, int);
1110Sstevel@tonic-gate static void		start_read(struct ctl_cctx *);
1120Sstevel@tonic-gate static void		stop_read(struct ctl_cctx *);
1130Sstevel@tonic-gate static void		readable(evContext, void *, int, int);
1140Sstevel@tonic-gate static void		start_timer(struct ctl_cctx *);
1150Sstevel@tonic-gate static void		stop_timer(struct ctl_cctx *);
1160Sstevel@tonic-gate static void		touch_timer(struct ctl_cctx *);
1170Sstevel@tonic-gate static void		timer(evContext, void *,
1180Sstevel@tonic-gate 			      struct timespec, struct timespec);
1190Sstevel@tonic-gate 
120*11038SRao.Shoaib@Sun.COM #ifndef HAVE_MEMCHR
121*11038SRao.Shoaib@Sun.COM static void *
memchr(const void * b,int c,size_t len)122*11038SRao.Shoaib@Sun.COM memchr(const void *b, int c, size_t len) {
123*11038SRao.Shoaib@Sun.COM 	const unsigned char *p = b;
124*11038SRao.Shoaib@Sun.COM 	size_t i;
125*11038SRao.Shoaib@Sun.COM 
126*11038SRao.Shoaib@Sun.COM 	for (i = 0; i < len; i++, p++)
127*11038SRao.Shoaib@Sun.COM 		if (*p == (unsigned char)c)
128*11038SRao.Shoaib@Sun.COM 			return ((void *)p);
129*11038SRao.Shoaib@Sun.COM 	return (NULL);
130*11038SRao.Shoaib@Sun.COM }
131*11038SRao.Shoaib@Sun.COM #endif
132*11038SRao.Shoaib@Sun.COM 
1330Sstevel@tonic-gate /* Private data. */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static const char * const state_names[] = {
1360Sstevel@tonic-gate 	"initializing", "connecting", "connected", "destroyed"
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* Public. */
1400Sstevel@tonic-gate 
141*11038SRao.Shoaib@Sun.COM /*%
1420Sstevel@tonic-gate  * void
1430Sstevel@tonic-gate  * ctl_client()
1440Sstevel@tonic-gate  *	create, condition, and connect to a listener on the control port.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate struct ctl_cctx *
ctl_client(evContext lev,const struct sockaddr * cap,size_t cap_len,const struct sockaddr * sap,size_t sap_len,ctl_clntdone donefunc,void * uap,u_int timeout,ctl_logfunc logger)1470Sstevel@tonic-gate ctl_client(evContext lev, const struct sockaddr *cap, size_t cap_len,
1480Sstevel@tonic-gate 	   const struct sockaddr *sap, size_t sap_len,
1490Sstevel@tonic-gate 	   ctl_clntdone donefunc, void *uap,
1500Sstevel@tonic-gate 	   u_int timeout, ctl_logfunc logger)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	static const char me[] = "ctl_client";
1530Sstevel@tonic-gate 	static const int on = 1;
1540Sstevel@tonic-gate 	struct ctl_cctx *ctx;
1550Sstevel@tonic-gate 	struct sockaddr *captmp;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (logger == NULL)
1580Sstevel@tonic-gate 		logger = ctl_logger;
1590Sstevel@tonic-gate 	ctx = memget(sizeof *ctx);
1600Sstevel@tonic-gate 	if (ctx == NULL) {
1610Sstevel@tonic-gate 		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
1620Sstevel@tonic-gate 		goto fatal;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	ctx->state = initializing;
1650Sstevel@tonic-gate 	ctx->ev = lev;
1660Sstevel@tonic-gate 	ctx->logger = logger;
1670Sstevel@tonic-gate 	ctx->timeout = evConsTime(timeout, 0);
1680Sstevel@tonic-gate 	ctx->donefunc = donefunc;
1690Sstevel@tonic-gate 	ctx->uap = uap;
1700Sstevel@tonic-gate 	ctx->coID.opaque = NULL;
1710Sstevel@tonic-gate 	ctx->tiID.opaque = NULL;
1720Sstevel@tonic-gate 	ctx->rdID.opaque = NULL;
1730Sstevel@tonic-gate 	ctx->wrID.opaque = NULL;
1740Sstevel@tonic-gate 	buffer_init(ctx->inbuf);
1750Sstevel@tonic-gate 	INIT_LIST(ctx->tran);
1760Sstevel@tonic-gate 	INIT_LIST(ctx->wtran);
1770Sstevel@tonic-gate 	ctx->sock = socket(sap->sa_family, SOCK_STREAM, PF_UNSPEC);
1780Sstevel@tonic-gate 	if (ctx->sock > evHighestFD(ctx->ev)) {
1790Sstevel@tonic-gate 		ctx->sock = -1;
1800Sstevel@tonic-gate 		errno = ENOTSOCK;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	if (ctx->sock < 0) {
1830Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: socket: %s",
1840Sstevel@tonic-gate 			       me, strerror(errno));
1850Sstevel@tonic-gate 		goto fatal;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	if (cap != NULL) {
1880Sstevel@tonic-gate 		if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR,
1890Sstevel@tonic-gate 			       (const char *)&on, sizeof on) != 0) {
1900Sstevel@tonic-gate 			(*ctx->logger)(ctl_warning,
1910Sstevel@tonic-gate 				       "%s: setsockopt(REUSEADDR): %s",
1920Sstevel@tonic-gate 				       me, strerror(errno));
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		DE_CONST(cap, captmp);
1950Sstevel@tonic-gate 		if (bind(ctx->sock, captmp, cap_len) < 0) {
1960Sstevel@tonic-gate 			(*ctx->logger)(ctl_error, "%s: bind: %s", me,
1970Sstevel@tonic-gate 				       strerror(errno));
1980Sstevel@tonic-gate 			goto fatal;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (evConnect(lev, ctx->sock, (const struct sockaddr *)sap, sap_len,
2020Sstevel@tonic-gate 		      conn_done, ctx, &ctx->coID) < 0) {
2030Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evConnect(fd %d): %s",
2040Sstevel@tonic-gate 			       me, ctx->sock, strerror(errno));
2050Sstevel@tonic-gate  fatal:
2060Sstevel@tonic-gate 		if (ctx != NULL) {
2070Sstevel@tonic-gate 			if (ctx->sock >= 0)
2080Sstevel@tonic-gate 				close(ctx->sock);
2090Sstevel@tonic-gate 			memput(ctx, sizeof *ctx);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 		return (NULL);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	new_state(ctx, connecting);
2140Sstevel@tonic-gate 	return (ctx);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
217*11038SRao.Shoaib@Sun.COM /*%
2180Sstevel@tonic-gate  * void
2190Sstevel@tonic-gate  * ctl_endclient(ctx)
2200Sstevel@tonic-gate  *	close a client and release all of its resources.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate void
ctl_endclient(struct ctl_cctx * ctx)2230Sstevel@tonic-gate ctl_endclient(struct ctl_cctx *ctx) {
2240Sstevel@tonic-gate 	if (ctx->state != destroyed)
2250Sstevel@tonic-gate 		destroy(ctx, 0);
2260Sstevel@tonic-gate 	memput(ctx, sizeof *ctx);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
229*11038SRao.Shoaib@Sun.COM /*%
2300Sstevel@tonic-gate  * int
2310Sstevel@tonic-gate  * ctl_command(ctx, cmd, len, donefunc, uap)
2320Sstevel@tonic-gate  *	Queue a transaction, which will begin with sending cmd
2330Sstevel@tonic-gate  *	and complete by calling donefunc with the answer.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate int
ctl_command(struct ctl_cctx * ctx,const char * cmd,size_t len,ctl_clntdone donefunc,void * uap)2360Sstevel@tonic-gate ctl_command(struct ctl_cctx *ctx, const char *cmd, size_t len,
2370Sstevel@tonic-gate 	    ctl_clntdone donefunc, void *uap)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	struct ctl_tran *tran;
2400Sstevel@tonic-gate 	char *pc;
2410Sstevel@tonic-gate 	unsigned int n;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	switch (ctx->state) {
2440Sstevel@tonic-gate 	case destroyed:
2450Sstevel@tonic-gate 		errno = ENOTCONN;
2460Sstevel@tonic-gate 		return (-1);
2470Sstevel@tonic-gate 	case connecting:
2480Sstevel@tonic-gate 	case connected:
2490Sstevel@tonic-gate 		break;
2500Sstevel@tonic-gate 	default:
2510Sstevel@tonic-gate 		abort();
2520Sstevel@tonic-gate 	}
253*11038SRao.Shoaib@Sun.COM 	if (len >= (size_t)MAX_LINELEN) {
2540Sstevel@tonic-gate 		errno = EMSGSIZE;
2550Sstevel@tonic-gate 		return (-1);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	tran = new_tran(ctx, donefunc, uap, 1);
2580Sstevel@tonic-gate 	if (tran == NULL)
2590Sstevel@tonic-gate 		return (-1);
2600Sstevel@tonic-gate 	if (ctl_bufget(&tran->outbuf, ctx->logger) < 0)
2610Sstevel@tonic-gate 		return (-1);
2620Sstevel@tonic-gate 	memcpy(tran->outbuf.text, cmd, len);
2630Sstevel@tonic-gate 	tran->outbuf.used = len;
2640Sstevel@tonic-gate 	for (pc = tran->outbuf.text, n = 0; n < tran->outbuf.used; pc++, n++)
2650Sstevel@tonic-gate 		if (!isascii((unsigned char)*pc) ||
2660Sstevel@tonic-gate 		    !isprint((unsigned char)*pc))
2670Sstevel@tonic-gate 			*pc = '\040';
2680Sstevel@tonic-gate 	start_write(ctx);
2690Sstevel@tonic-gate 	return (0);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /* Private. */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static struct ctl_tran *
new_tran(struct ctl_cctx * ctx,ctl_clntdone donefunc,void * uap,int w)2750Sstevel@tonic-gate new_tran(struct ctl_cctx *ctx, ctl_clntdone donefunc, void *uap, int w) {
2760Sstevel@tonic-gate 	struct ctl_tran *new = memget(sizeof *new);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (new == NULL)
2790Sstevel@tonic-gate 		return (NULL);
2800Sstevel@tonic-gate 	new->ctx = ctx;
2810Sstevel@tonic-gate 	buffer_init(new->outbuf);
2820Sstevel@tonic-gate 	new->donefunc = donefunc;
2830Sstevel@tonic-gate 	new->uap = uap;
2840Sstevel@tonic-gate 	INIT_LINK(new, link);
2850Sstevel@tonic-gate 	INIT_LINK(new, wlink);
2860Sstevel@tonic-gate 	APPEND(ctx->tran, new, link);
2870Sstevel@tonic-gate 	if (w)
2880Sstevel@tonic-gate 		APPEND(ctx->wtran, new, wlink);
2890Sstevel@tonic-gate 	return (new);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static void
start_write(struct ctl_cctx * ctx)2930Sstevel@tonic-gate start_write(struct ctl_cctx *ctx) {
2940Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::start_write";
2950Sstevel@tonic-gate 	struct ctl_tran *tran;
2960Sstevel@tonic-gate 	struct iovec iov[2], *iovp = iov;
2970Sstevel@tonic-gate 	char * tmp;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	REQUIRE(ctx->state == connecting || ctx->state == connected);
3000Sstevel@tonic-gate 	/* If there is a write in progress, don't try to write more yet. */
3010Sstevel@tonic-gate 	if (ctx->wrID.opaque != NULL)
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 	/* If there are no trans, make sure timer is off, and we're done. */
3040Sstevel@tonic-gate 	if (EMPTY(ctx->wtran)) {
3050Sstevel@tonic-gate 		if (ctx->tiID.opaque != NULL)
3060Sstevel@tonic-gate 			stop_timer(ctx);
3070Sstevel@tonic-gate 		return;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	/* Pull it off the head of the write queue. */
3100Sstevel@tonic-gate 	tran = HEAD(ctx->wtran);
3110Sstevel@tonic-gate 	UNLINK(ctx->wtran, tran, wlink);
3120Sstevel@tonic-gate 	/* Since there are some trans, make sure timer is successfully "on". */
3130Sstevel@tonic-gate 	if (ctx->tiID.opaque != NULL)
3140Sstevel@tonic-gate 		touch_timer(ctx);
3150Sstevel@tonic-gate 	else
3160Sstevel@tonic-gate 		start_timer(ctx);
3170Sstevel@tonic-gate 	if (ctx->state == destroyed)
3180Sstevel@tonic-gate 		return;
3190Sstevel@tonic-gate 	/* Marshall a newline-terminated message and clock it out. */
3200Sstevel@tonic-gate 	*iovp++ = evConsIovec(tran->outbuf.text, tran->outbuf.used);
3210Sstevel@tonic-gate 	DE_CONST("\r\n", tmp);
3220Sstevel@tonic-gate 	*iovp++ = evConsIovec(tmp, 2);
3230Sstevel@tonic-gate 	if (evWrite(ctx->ev, ctx->sock, iov, iovp - iov,
3240Sstevel@tonic-gate 		    write_done, tran, &ctx->wrID) < 0) {
3250Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evWrite: %s", me,
3260Sstevel@tonic-gate 			       strerror(errno));
3270Sstevel@tonic-gate 		error(ctx);
3280Sstevel@tonic-gate 		return;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	if (evTimeRW(ctx->ev, ctx->wrID, ctx->tiID) < 0) {
3310Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evTimeRW: %s", me,
3320Sstevel@tonic-gate 			       strerror(errno));
3330Sstevel@tonic-gate 		error(ctx);
3340Sstevel@tonic-gate 		return;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static void
destroy(struct ctl_cctx * ctx,int notify)3390Sstevel@tonic-gate destroy(struct ctl_cctx *ctx, int notify) {
3400Sstevel@tonic-gate 	struct ctl_tran *this, *next;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (ctx->sock != -1) {
3430Sstevel@tonic-gate 		(void) close(ctx->sock);
3440Sstevel@tonic-gate 		ctx->sock = -1;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	switch (ctx->state) {
3470Sstevel@tonic-gate 	case connecting:
3480Sstevel@tonic-gate 		REQUIRE(ctx->wrID.opaque == NULL);
3490Sstevel@tonic-gate 		REQUIRE(EMPTY(ctx->tran));
3500Sstevel@tonic-gate 		/*
3510Sstevel@tonic-gate 		 * This test is nec'y since destroy() can be called from
3520Sstevel@tonic-gate 		 * start_read() while the state is still "connecting".
3530Sstevel@tonic-gate 		 */
3540Sstevel@tonic-gate 		if (ctx->coID.opaque != NULL) {
3550Sstevel@tonic-gate 			(void)evCancelConn(ctx->ev, ctx->coID);
3560Sstevel@tonic-gate 			ctx->coID.opaque = NULL;
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 		break;
3590Sstevel@tonic-gate 	case connected:
3600Sstevel@tonic-gate 		REQUIRE(ctx->coID.opaque == NULL);
3610Sstevel@tonic-gate 		if (ctx->wrID.opaque != NULL) {
3620Sstevel@tonic-gate 			(void)evCancelRW(ctx->ev, ctx->wrID);
3630Sstevel@tonic-gate 			ctx->wrID.opaque = NULL;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		if (ctx->rdID.opaque != NULL)
3660Sstevel@tonic-gate 			stop_read(ctx);
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 	case destroyed:
3690Sstevel@tonic-gate 		break;
3700Sstevel@tonic-gate 	default:
3710Sstevel@tonic-gate 		abort();
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	if (allocated_p(ctx->inbuf))
3740Sstevel@tonic-gate 		ctl_bufput(&ctx->inbuf);
3750Sstevel@tonic-gate 	for (this = HEAD(ctx->tran); this != NULL; this = next) {
3760Sstevel@tonic-gate 		next = NEXT(this, link);
3770Sstevel@tonic-gate 		if (allocated_p(this->outbuf))
3780Sstevel@tonic-gate 			ctl_bufput(&this->outbuf);
3790Sstevel@tonic-gate 		if (notify && this->donefunc != NULL)
3800Sstevel@tonic-gate 			(*this->donefunc)(ctx, this->uap, NULL, 0);
3810Sstevel@tonic-gate 		memput(this, sizeof *this);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	if (ctx->tiID.opaque != NULL)
3840Sstevel@tonic-gate 		stop_timer(ctx);
3850Sstevel@tonic-gate 	new_state(ctx, destroyed);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static void
error(struct ctl_cctx * ctx)3890Sstevel@tonic-gate error(struct ctl_cctx *ctx) {
3900Sstevel@tonic-gate 	REQUIRE(ctx->state != destroyed);
3910Sstevel@tonic-gate 	destroy(ctx, 1);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static void
new_state(struct ctl_cctx * ctx,enum state new_state)3950Sstevel@tonic-gate new_state(struct ctl_cctx *ctx, enum state new_state) {
3960Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::new_state";
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: %s -> %s", me,
3990Sstevel@tonic-gate 		       state_names[ctx->state], state_names[new_state]);
4000Sstevel@tonic-gate 	ctx->state = new_state;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate static void
conn_done(evContext ev,void * uap,int fd,const void * la,int lalen,const void * ra,int ralen)4040Sstevel@tonic-gate conn_done(evContext ev, void *uap, int fd,
4050Sstevel@tonic-gate 	  const void *la, int lalen,
4060Sstevel@tonic-gate 	  const void *ra, int ralen)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::conn_done";
4090Sstevel@tonic-gate 	struct ctl_cctx *ctx = uap;
4100Sstevel@tonic-gate 	struct ctl_tran *tran;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	UNUSED(ev);
4130Sstevel@tonic-gate 	UNUSED(la);
4140Sstevel@tonic-gate 	UNUSED(lalen);
4150Sstevel@tonic-gate 	UNUSED(ra);
4160Sstevel@tonic-gate 	UNUSED(ralen);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ctx->coID.opaque = NULL;
4190Sstevel@tonic-gate 	if (fd < 0) {
4200Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evConnect: %s", me,
4210Sstevel@tonic-gate 			       strerror(errno));
4220Sstevel@tonic-gate 		error(ctx);
4230Sstevel@tonic-gate 		return;
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	new_state(ctx, connected);
4260Sstevel@tonic-gate 	tran = new_tran(ctx, ctx->donefunc, ctx->uap, 0);
4270Sstevel@tonic-gate 	if (tran == NULL) {
4280Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: new_tran failed: %s", me,
4290Sstevel@tonic-gate 			       strerror(errno));
4300Sstevel@tonic-gate 		error(ctx);
4310Sstevel@tonic-gate 		return;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	start_read(ctx);
4340Sstevel@tonic-gate 	if (ctx->state == destroyed) {
4350Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: start_read failed: %s",
4360Sstevel@tonic-gate 			       me, strerror(errno));
4370Sstevel@tonic-gate 		error(ctx);
4380Sstevel@tonic-gate 		return;
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate static void
write_done(evContext lev,void * uap,int fd,int bytes)4430Sstevel@tonic-gate write_done(evContext lev, void *uap, int fd, int bytes) {
4440Sstevel@tonic-gate 	struct ctl_tran *tran = (struct ctl_tran *)uap;
4450Sstevel@tonic-gate 	struct ctl_cctx *ctx = tran->ctx;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	UNUSED(lev);
4480Sstevel@tonic-gate 	UNUSED(fd);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	ctx->wrID.opaque = NULL;
4510Sstevel@tonic-gate 	if (ctx->tiID.opaque != NULL)
4520Sstevel@tonic-gate 		touch_timer(ctx);
4530Sstevel@tonic-gate 	ctl_bufput(&tran->outbuf);
4540Sstevel@tonic-gate 	start_write(ctx);
4550Sstevel@tonic-gate 	if (bytes < 0)
4560Sstevel@tonic-gate 		destroy(ctx, 1);
4570Sstevel@tonic-gate 	else
4580Sstevel@tonic-gate 		start_read(ctx);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate static void
start_read(struct ctl_cctx * ctx)4620Sstevel@tonic-gate start_read(struct ctl_cctx *ctx) {
4630Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::start_read";
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	REQUIRE(ctx->state == connecting || ctx->state == connected);
4660Sstevel@tonic-gate 	REQUIRE(ctx->rdID.opaque == NULL);
4670Sstevel@tonic-gate 	if (evSelectFD(ctx->ev, ctx->sock, EV_READ, readable, ctx,
4680Sstevel@tonic-gate 		       &ctx->rdID) < 0)
4690Sstevel@tonic-gate 	{
4700Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evSelect(fd %d): %s", me,
4710Sstevel@tonic-gate 			       ctx->sock, strerror(errno));
4720Sstevel@tonic-gate 		error(ctx);
4730Sstevel@tonic-gate 		return;
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
stop_read(struct ctl_cctx * ctx)4780Sstevel@tonic-gate stop_read(struct ctl_cctx *ctx) {
4790Sstevel@tonic-gate 	REQUIRE(ctx->coID.opaque == NULL);
4800Sstevel@tonic-gate 	REQUIRE(ctx->rdID.opaque != NULL);
4810Sstevel@tonic-gate 	(void)evDeselectFD(ctx->ev, ctx->rdID);
4820Sstevel@tonic-gate 	ctx->rdID.opaque = NULL;
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate static void
readable(evContext ev,void * uap,int fd,int evmask)4860Sstevel@tonic-gate readable(evContext ev, void *uap, int fd, int evmask) {
4870Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::readable";
4880Sstevel@tonic-gate 	struct ctl_cctx *ctx = uap;
4890Sstevel@tonic-gate 	struct ctl_tran *tran;
4900Sstevel@tonic-gate 	ssize_t n;
4910Sstevel@tonic-gate 	char *eos;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	UNUSED(ev);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	REQUIRE(ctx != NULL);
4960Sstevel@tonic-gate 	REQUIRE(fd >= 0);
4970Sstevel@tonic-gate 	REQUIRE(evmask == EV_READ);
4980Sstevel@tonic-gate 	REQUIRE(ctx->state == connected);
4990Sstevel@tonic-gate 	REQUIRE(!EMPTY(ctx->tran));
5000Sstevel@tonic-gate 	tran = HEAD(ctx->tran);
5010Sstevel@tonic-gate 	if (!allocated_p(ctx->inbuf) &&
5020Sstevel@tonic-gate 	    ctl_bufget(&ctx->inbuf, ctx->logger) < 0) {
5030Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: can't get an input buffer", me);
5040Sstevel@tonic-gate 		error(ctx);
5050Sstevel@tonic-gate 		return;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	n = read(ctx->sock, ctx->inbuf.text + ctx->inbuf.used,
5080Sstevel@tonic-gate 		 MAX_LINELEN - ctx->inbuf.used);
5090Sstevel@tonic-gate 	if (n <= 0) {
5100Sstevel@tonic-gate 		(*ctx->logger)(ctl_warning, "%s: read: %s", me,
5110Sstevel@tonic-gate 			       (n == 0) ? "Unexpected EOF" : strerror(errno));
5120Sstevel@tonic-gate 		error(ctx);
5130Sstevel@tonic-gate 		return;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	if (ctx->tiID.opaque != NULL)
5160Sstevel@tonic-gate 		touch_timer(ctx);
5170Sstevel@tonic-gate 	ctx->inbuf.used += n;
5180Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: read %d, used %d", me,
5190Sstevel@tonic-gate 		       n, ctx->inbuf.used);
5200Sstevel@tonic-gate  again:
5210Sstevel@tonic-gate 	eos = memchr(ctx->inbuf.text, '\n', ctx->inbuf.used);
5220Sstevel@tonic-gate 	if (eos != NULL && eos != ctx->inbuf.text && eos[-1] == '\r') {
5230Sstevel@tonic-gate 		int done = 0;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		eos[-1] = '\0';
5260Sstevel@tonic-gate 		if (!arpacode_p(ctx->inbuf.text)) {
5270Sstevel@tonic-gate 			/* XXX Doesn't FTP do this sometimes? Is it legal? */
5280Sstevel@tonic-gate 			(*ctx->logger)(ctl_error, "%s: no arpa code (%s)", me,
5290Sstevel@tonic-gate 				       ctx->inbuf.text);
5300Sstevel@tonic-gate 			error(ctx);
5310Sstevel@tonic-gate 			return;
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 		if (arpadone_p(ctx->inbuf.text))
5340Sstevel@tonic-gate 			done = 1;
5350Sstevel@tonic-gate 		else if (arpacont_p(ctx->inbuf.text))
5360Sstevel@tonic-gate 			done = 0;
5370Sstevel@tonic-gate 		else {
5380Sstevel@tonic-gate 			/* XXX Doesn't FTP do this sometimes? Is it legal? */
5390Sstevel@tonic-gate 			(*ctx->logger)(ctl_error, "%s: no arpa flag (%s)", me,
5400Sstevel@tonic-gate 				       ctx->inbuf.text);
5410Sstevel@tonic-gate 			error(ctx);
5420Sstevel@tonic-gate 			return;
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		(*tran->donefunc)(ctx, tran->uap, ctx->inbuf.text,
5450Sstevel@tonic-gate 				  (done ? 0 : CTL_MORE));
5460Sstevel@tonic-gate 		ctx->inbuf.used -= ((eos - ctx->inbuf.text) + 1);
547*11038SRao.Shoaib@Sun.COM 		if (ctx->inbuf.used == 0U)
5480Sstevel@tonic-gate 			ctl_bufput(&ctx->inbuf);
5490Sstevel@tonic-gate 		else
5500Sstevel@tonic-gate 			memmove(ctx->inbuf.text, eos + 1, ctx->inbuf.used);
5510Sstevel@tonic-gate 		if (done) {
5520Sstevel@tonic-gate 			UNLINK(ctx->tran, tran, link);
5530Sstevel@tonic-gate 			memput(tran, sizeof *tran);
5540Sstevel@tonic-gate 			stop_read(ctx);
5550Sstevel@tonic-gate 			start_write(ctx);
5560Sstevel@tonic-gate 			return;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		if (allocated_p(ctx->inbuf))
5590Sstevel@tonic-gate 			goto again;
5600Sstevel@tonic-gate 		return;
5610Sstevel@tonic-gate 	}
562*11038SRao.Shoaib@Sun.COM 	if (ctx->inbuf.used == (size_t)MAX_LINELEN) {
5630Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: line too long (%-10s...)", me,
5640Sstevel@tonic-gate 			       ctx->inbuf.text);
5650Sstevel@tonic-gate 		error(ctx);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /* Timer related stuff. */
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate static void
start_timer(struct ctl_cctx * ctx)5720Sstevel@tonic-gate start_timer(struct ctl_cctx *ctx) {
5730Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::start_timer";
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	REQUIRE(ctx->tiID.opaque == NULL);
5760Sstevel@tonic-gate 	if (evSetIdleTimer(ctx->ev, timer, ctx, ctx->timeout, &ctx->tiID) < 0){
5770Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evSetIdleTimer: %s", me,
5780Sstevel@tonic-gate 			       strerror(errno));
5790Sstevel@tonic-gate 		error(ctx);
5800Sstevel@tonic-gate 		return;
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate static void
stop_timer(struct ctl_cctx * ctx)5850Sstevel@tonic-gate stop_timer(struct ctl_cctx *ctx) {
5860Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::stop_timer";
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	REQUIRE(ctx->tiID.opaque != NULL);
5890Sstevel@tonic-gate 	if (evClearIdleTimer(ctx->ev, ctx->tiID) < 0) {
5900Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evClearIdleTimer: %s", me,
5910Sstevel@tonic-gate 			       strerror(errno));
5920Sstevel@tonic-gate 		error(ctx);
5930Sstevel@tonic-gate 		return;
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 	ctx->tiID.opaque = NULL;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate static void
touch_timer(struct ctl_cctx * ctx)5990Sstevel@tonic-gate touch_timer(struct ctl_cctx *ctx) {
6000Sstevel@tonic-gate 	REQUIRE(ctx->tiID.opaque != NULL);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	evTouchIdleTimer(ctx->ev, ctx->tiID);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate static void
timer(evContext ev,void * uap,struct timespec due,struct timespec itv)6060Sstevel@tonic-gate timer(evContext ev, void *uap, struct timespec due, struct timespec itv) {
6070Sstevel@tonic-gate 	static const char me[] = "isc/ctl_clnt::timer";
6080Sstevel@tonic-gate 	struct ctl_cctx *ctx = uap;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	UNUSED(ev);
6110Sstevel@tonic-gate 	UNUSED(due);
6120Sstevel@tonic-gate 	UNUSED(itv);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	ctx->tiID.opaque = NULL;
6150Sstevel@tonic-gate 	(*ctx->logger)(ctl_error, "%s: timeout after %u seconds while %s", me,
6160Sstevel@tonic-gate 		       ctx->timeout.tv_sec, state_names[ctx->state]);
6170Sstevel@tonic-gate 	error(ctx);
6180Sstevel@tonic-gate }
619*11038SRao.Shoaib@Sun.COM 
620*11038SRao.Shoaib@Sun.COM /*! \file */
621