xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/ctl_srvr.c (revision 458f44a49dc56cd17a39815122214e7a1b4793e3)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004-2006, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1998-2003  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sys/param.h>
217c478bd9Sstevel@tonic-gate #include <sys/file.h>
227c478bd9Sstevel@tonic-gate #include <sys/socket.h>
237c478bd9Sstevel@tonic-gate #include <sys/un.h>
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <netinet/in.h>
267c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
277c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <time.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*9525b14bSRao Shoaib #ifdef HAVE_MEMORY_H
38*9525b14bSRao Shoaib #include <memory.h>
39*9525b14bSRao Shoaib #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
427c478bd9Sstevel@tonic-gate #include <isc/ctl.h>
437c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
447c478bd9Sstevel@tonic-gate #include <isc/list.h>
457c478bd9Sstevel@tonic-gate #include <isc/logging.h>
467c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "ctl_p.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "port_after.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
537c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* Macros. */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	lastverb_p(verb)	(verb->name == NULL || verb->func == NULL)
617c478bd9Sstevel@tonic-gate #define	address_expr		ctl_sa_ntop((struct sockaddr *)&sess->sa, \
627c478bd9Sstevel@tonic-gate 					    tmp, sizeof tmp, ctx->logger)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* Types. */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate enum state {
677c478bd9Sstevel@tonic-gate 	available = 0, initializing, writing, reading, reading_data,
687c478bd9Sstevel@tonic-gate 	processing, idling, quitting, closing
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate union sa_un {
727c478bd9Sstevel@tonic-gate 	struct sockaddr_in in;
737c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
747c478bd9Sstevel@tonic-gate 	struct sockaddr_un un;
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct ctl_sess {
797c478bd9Sstevel@tonic-gate 	LINK(struct ctl_sess)	link;
807c478bd9Sstevel@tonic-gate 	struct ctl_sctx *	ctx;
817c478bd9Sstevel@tonic-gate 	enum state		state;
827c478bd9Sstevel@tonic-gate 	int			sock;
837c478bd9Sstevel@tonic-gate 	union sa_un		sa;
847c478bd9Sstevel@tonic-gate 	evFileID		rdID;
857c478bd9Sstevel@tonic-gate 	evStreamID		wrID;
867c478bd9Sstevel@tonic-gate 	evTimerID		rdtiID;
877c478bd9Sstevel@tonic-gate 	evTimerID		wrtiID;
887c478bd9Sstevel@tonic-gate 	struct ctl_buf		inbuf;
897c478bd9Sstevel@tonic-gate 	struct ctl_buf		outbuf;
907c478bd9Sstevel@tonic-gate 	const struct ctl_verb *	verb;
917c478bd9Sstevel@tonic-gate 	u_int			helpcode;
927c478bd9Sstevel@tonic-gate 	const void *		respctx;
937c478bd9Sstevel@tonic-gate 	u_int			respflags;
947c478bd9Sstevel@tonic-gate 	ctl_srvrdone		donefunc;
957c478bd9Sstevel@tonic-gate 	void *			uap;
967c478bd9Sstevel@tonic-gate 	void *			csctx;
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate struct ctl_sctx {
1007c478bd9Sstevel@tonic-gate 	evContext		ev;
1017c478bd9Sstevel@tonic-gate 	void *			uctx;
1027c478bd9Sstevel@tonic-gate 	u_int			unkncode;
1037c478bd9Sstevel@tonic-gate 	u_int			timeoutcode;
1047c478bd9Sstevel@tonic-gate 	const struct ctl_verb *	verbs;
1057c478bd9Sstevel@tonic-gate 	const struct ctl_verb *	connverb;
1067c478bd9Sstevel@tonic-gate 	int			sock;
1077c478bd9Sstevel@tonic-gate 	int			max_sess;
1087c478bd9Sstevel@tonic-gate 	int			cur_sess;
1097c478bd9Sstevel@tonic-gate 	struct timespec		timeout;
1107c478bd9Sstevel@tonic-gate 	ctl_logfunc		logger;
1117c478bd9Sstevel@tonic-gate 	evConnID		acID;
1127c478bd9Sstevel@tonic-gate 	LIST(struct ctl_sess)	sess;
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Forward. */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static void			ctl_accept(evContext, void *, int,
1187c478bd9Sstevel@tonic-gate 					   const void *, int,
1197c478bd9Sstevel@tonic-gate 					   const void *, int);
1207c478bd9Sstevel@tonic-gate static void			ctl_close(struct ctl_sess *);
1217c478bd9Sstevel@tonic-gate static void			ctl_new_state(struct ctl_sess *,
1227c478bd9Sstevel@tonic-gate 					      enum state,
1237c478bd9Sstevel@tonic-gate 					      const char *);
1247c478bd9Sstevel@tonic-gate static void			ctl_start_read(struct ctl_sess *);
1257c478bd9Sstevel@tonic-gate static void			ctl_stop_read(struct ctl_sess *);
1267c478bd9Sstevel@tonic-gate static void			ctl_readable(evContext, void *, int, int);
1277c478bd9Sstevel@tonic-gate static void			ctl_rdtimeout(evContext, void *,
1287c478bd9Sstevel@tonic-gate 					      struct timespec,
1297c478bd9Sstevel@tonic-gate 					      struct timespec);
1307c478bd9Sstevel@tonic-gate static void			ctl_wrtimeout(evContext, void *,
1317c478bd9Sstevel@tonic-gate 					      struct timespec,
1327c478bd9Sstevel@tonic-gate 					      struct timespec);
1337c478bd9Sstevel@tonic-gate static void			ctl_docommand(struct ctl_sess *);
1347c478bd9Sstevel@tonic-gate static void			ctl_writedone(evContext, void *, int, int);
1357c478bd9Sstevel@tonic-gate static void			ctl_morehelp(struct ctl_sctx *,
1367c478bd9Sstevel@tonic-gate 					     struct ctl_sess *,
1377c478bd9Sstevel@tonic-gate 					     const struct ctl_verb *,
1387c478bd9Sstevel@tonic-gate 					     const char *,
1397c478bd9Sstevel@tonic-gate 					     u_int, const void *, void *);
1407c478bd9Sstevel@tonic-gate static void			ctl_signal_done(struct ctl_sctx *,
1417c478bd9Sstevel@tonic-gate 						struct ctl_sess *);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* Private data. */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static const char *		state_names[] = {
1467c478bd9Sstevel@tonic-gate 	"available", "initializing", "writing", "reading",
1477c478bd9Sstevel@tonic-gate 	"reading_data", "processing", "idling", "quitting", "closing"
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static const char		space[] = " ";
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static const struct ctl_verb	fakehelpverb = {
1537c478bd9Sstevel@tonic-gate 	"fakehelp", ctl_morehelp , NULL
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /* Public. */
1577c478bd9Sstevel@tonic-gate 
158*9525b14bSRao Shoaib /*%
1597c478bd9Sstevel@tonic-gate  * void
1607c478bd9Sstevel@tonic-gate  * ctl_server()
1617c478bd9Sstevel@tonic-gate  *	create, condition, and start a listener on the control port.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate struct ctl_sctx *
ctl_server(evContext lev,const struct sockaddr * sap,size_t sap_len,const struct ctl_verb * verbs,u_int unkncode,u_int timeoutcode,u_int timeout,int backlog,int max_sess,ctl_logfunc logger,void * uctx)1647c478bd9Sstevel@tonic-gate ctl_server(evContext lev, const struct sockaddr *sap, size_t sap_len,
1657c478bd9Sstevel@tonic-gate 	   const struct ctl_verb *verbs,
1667c478bd9Sstevel@tonic-gate 	   u_int unkncode, u_int timeoutcode,
1677c478bd9Sstevel@tonic-gate 	   u_int timeout, int backlog, int max_sess,
1687c478bd9Sstevel@tonic-gate 	   ctl_logfunc logger, void *uctx)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_server";
1717c478bd9Sstevel@tonic-gate 	static const int on = 1;
1727c478bd9Sstevel@tonic-gate 	const struct ctl_verb *connverb;
1737c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx;
1747c478bd9Sstevel@tonic-gate 	int save_errno;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (logger == NULL)
1777c478bd9Sstevel@tonic-gate 		logger = ctl_logger;
1787c478bd9Sstevel@tonic-gate 	for (connverb = verbs;
1797c478bd9Sstevel@tonic-gate 	     connverb->name != NULL && connverb->func != NULL;
1807c478bd9Sstevel@tonic-gate 	     connverb++)
1817c478bd9Sstevel@tonic-gate 		if (connverb->name[0] == '\0')
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 	if (connverb->func == NULL) {
1847c478bd9Sstevel@tonic-gate 		(*logger)(ctl_error, "%s: no connection verb found", me);
1857c478bd9Sstevel@tonic-gate 		return (NULL);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	ctx = memget(sizeof *ctx);
1887c478bd9Sstevel@tonic-gate 	if (ctx == NULL) {
1897c478bd9Sstevel@tonic-gate 		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
1907c478bd9Sstevel@tonic-gate 		return (NULL);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	ctx->ev = lev;
1937c478bd9Sstevel@tonic-gate 	ctx->uctx = uctx;
1947c478bd9Sstevel@tonic-gate 	ctx->unkncode = unkncode;
1957c478bd9Sstevel@tonic-gate 	ctx->timeoutcode = timeoutcode;
1967c478bd9Sstevel@tonic-gate 	ctx->verbs = verbs;
1977c478bd9Sstevel@tonic-gate 	ctx->timeout = evConsTime(timeout, 0);
1987c478bd9Sstevel@tonic-gate 	ctx->logger = logger;
1997c478bd9Sstevel@tonic-gate 	ctx->connverb = connverb;
2007c478bd9Sstevel@tonic-gate 	ctx->max_sess = max_sess;
2017c478bd9Sstevel@tonic-gate 	ctx->cur_sess = 0;
2027c478bd9Sstevel@tonic-gate 	INIT_LIST(ctx->sess);
2037c478bd9Sstevel@tonic-gate 	ctx->sock = socket(sap->sa_family, SOCK_STREAM, PF_UNSPEC);
2047c478bd9Sstevel@tonic-gate 	if (ctx->sock > evHighestFD(ctx->ev)) {
2057c478bd9Sstevel@tonic-gate 		ctx->sock = -1;
2067c478bd9Sstevel@tonic-gate 		errno = ENOTSOCK;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	if (ctx->sock < 0) {
2097c478bd9Sstevel@tonic-gate 		save_errno = errno;
2107c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: socket: %s",
2117c478bd9Sstevel@tonic-gate 			       me, strerror(errno));
2127c478bd9Sstevel@tonic-gate 		memput(ctx, sizeof *ctx);
2137c478bd9Sstevel@tonic-gate 		errno = save_errno;
2147c478bd9Sstevel@tonic-gate 		return (NULL);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	if (ctx->sock > evHighestFD(lev)) {
2177c478bd9Sstevel@tonic-gate 		close(ctx->sock);
2187c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: file descriptor > evHighestFD");
2197c478bd9Sstevel@tonic-gate 		errno = ENFILE;
2207c478bd9Sstevel@tonic-gate 		memput(ctx, sizeof *ctx);
2217c478bd9Sstevel@tonic-gate 		return (NULL);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate #ifdef NO_UNIX_REUSEADDR
2247c478bd9Sstevel@tonic-gate 	if (sap->sa_family != AF_UNIX)
2257c478bd9Sstevel@tonic-gate #endif
2267c478bd9Sstevel@tonic-gate 		if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR,
2277c478bd9Sstevel@tonic-gate 			       (const char *)&on, sizeof on) != 0) {
2287c478bd9Sstevel@tonic-gate 			(*ctx->logger)(ctl_warning,
2297c478bd9Sstevel@tonic-gate 				       "%s: setsockopt(REUSEADDR): %s",
2307c478bd9Sstevel@tonic-gate 				       me, strerror(errno));
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	if (bind(ctx->sock, sap, sap_len) < 0) {
2337c478bd9Sstevel@tonic-gate 		char tmp[MAX_NTOP];
2347c478bd9Sstevel@tonic-gate 		save_errno = errno;
2357c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: bind: %s: %s",
2367c478bd9Sstevel@tonic-gate 			       me, ctl_sa_ntop((const struct sockaddr *)sap,
2377c478bd9Sstevel@tonic-gate 			       tmp, sizeof tmp, ctx->logger),
2387c478bd9Sstevel@tonic-gate 			       strerror(save_errno));
2397c478bd9Sstevel@tonic-gate 		close(ctx->sock);
2407c478bd9Sstevel@tonic-gate 		memput(ctx, sizeof *ctx);
2417c478bd9Sstevel@tonic-gate 		errno = save_errno;
2427c478bd9Sstevel@tonic-gate 		return (NULL);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (fcntl(ctx->sock, F_SETFD, 1) < 0) {
2457c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_warning, "%s: fcntl: %s", me,
2467c478bd9Sstevel@tonic-gate 			       strerror(errno));
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	if (evListen(lev, ctx->sock, backlog, ctl_accept, ctx,
2497c478bd9Sstevel@tonic-gate 		     &ctx->acID) < 0) {
2507c478bd9Sstevel@tonic-gate 		save_errno = errno;
2517c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: evListen(fd %d): %s",
2527c478bd9Sstevel@tonic-gate 			       me, ctx->sock, strerror(errno));
2537c478bd9Sstevel@tonic-gate 		close(ctx->sock);
2547c478bd9Sstevel@tonic-gate 		memput(ctx, sizeof *ctx);
2557c478bd9Sstevel@tonic-gate 		errno = save_errno;
2567c478bd9Sstevel@tonic-gate 		return (NULL);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: new ctx %p, sock %d",
2597c478bd9Sstevel@tonic-gate 		       me, ctx, ctx->sock);
2607c478bd9Sstevel@tonic-gate 	return (ctx);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
263*9525b14bSRao Shoaib /*%
2647c478bd9Sstevel@tonic-gate  * void
2657c478bd9Sstevel@tonic-gate  * ctl_endserver(ctx)
2667c478bd9Sstevel@tonic-gate  *	if the control listener is open, close it.  clean out all eventlib
2677c478bd9Sstevel@tonic-gate  *	stuff.  close all active sessions.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate void
ctl_endserver(struct ctl_sctx * ctx)2707c478bd9Sstevel@tonic-gate ctl_endserver(struct ctl_sctx *ctx) {
2717c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_endserver";
2727c478bd9Sstevel@tonic-gate 	struct ctl_sess *this, *next;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: ctx %p, sock %d, acID %p, sess %p",
2757c478bd9Sstevel@tonic-gate 		       me, ctx, ctx->sock, ctx->acID.opaque, ctx->sess);
2767c478bd9Sstevel@tonic-gate 	if (ctx->acID.opaque != NULL) {
2777c478bd9Sstevel@tonic-gate 		(void)evCancelConn(ctx->ev, ctx->acID);
2787c478bd9Sstevel@tonic-gate 		ctx->acID.opaque = NULL;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (ctx->sock != -1) {
2817c478bd9Sstevel@tonic-gate 		(void) close(ctx->sock);
2827c478bd9Sstevel@tonic-gate 		ctx->sock = -1;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	for (this = HEAD(ctx->sess); this != NULL; this = next) {
2857c478bd9Sstevel@tonic-gate 		next = NEXT(this, link);
2867c478bd9Sstevel@tonic-gate 		ctl_close(this);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	memput(ctx, sizeof *ctx);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
291*9525b14bSRao Shoaib /*%
2927c478bd9Sstevel@tonic-gate  * If body is non-NULL then it we add a "." line after it.
2937c478bd9Sstevel@tonic-gate  * Caller must have  escaped lines with leading ".".
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate void
ctl_response(struct ctl_sess * sess,u_int code,const char * text,u_int flags,const void * respctx,ctl_srvrdone donefunc,void * uap,const char * body,size_t bodylen)2967c478bd9Sstevel@tonic-gate ctl_response(struct ctl_sess *sess, u_int code, const char *text,
2977c478bd9Sstevel@tonic-gate 	     u_int flags, const void *respctx, ctl_srvrdone donefunc,
2987c478bd9Sstevel@tonic-gate 	     void *uap, const char *body, size_t bodylen)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_response";
3017c478bd9Sstevel@tonic-gate 	struct iovec iov[3], *iovp = iov;
3027c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
3037c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP], *pc;
3047c478bd9Sstevel@tonic-gate 	int n;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == initializing ||
3077c478bd9Sstevel@tonic-gate 		sess->state == processing ||
3087c478bd9Sstevel@tonic-gate 		sess->state == reading_data ||
3097c478bd9Sstevel@tonic-gate 		sess->state == writing);
3107c478bd9Sstevel@tonic-gate 	REQUIRE(sess->wrtiID.opaque == NULL);
3117c478bd9Sstevel@tonic-gate 	REQUIRE(sess->wrID.opaque == NULL);
3127c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, writing, me);
3137c478bd9Sstevel@tonic-gate 	sess->donefunc = donefunc;
3147c478bd9Sstevel@tonic-gate 	sess->uap = uap;
3157c478bd9Sstevel@tonic-gate 	if (!allocated_p(sess->outbuf) &&
3167c478bd9Sstevel@tonic-gate 	    ctl_bufget(&sess->outbuf, ctx->logger) < 0) {
3177c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: cant get an output buffer",
3187c478bd9Sstevel@tonic-gate 			       me, address_expr);
3197c478bd9Sstevel@tonic-gate 		goto untimely;
3207c478bd9Sstevel@tonic-gate 	}
321*9525b14bSRao Shoaib 	if (sizeof "000-\r\n" + strlen(text) > (size_t)MAX_LINELEN) {
3227c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: output buffer ovf, closing",
3237c478bd9Sstevel@tonic-gate 			       me, address_expr);
3247c478bd9Sstevel@tonic-gate 		goto untimely;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	sess->outbuf.used = SPRINTF((sess->outbuf.text, "%03d%c%s\r\n",
3277c478bd9Sstevel@tonic-gate 				     code, (flags & CTL_MORE) != 0 ? '-' : ' ',
3287c478bd9Sstevel@tonic-gate 				     text));
3297c478bd9Sstevel@tonic-gate 	for (pc = sess->outbuf.text, n = 0;
3307c478bd9Sstevel@tonic-gate 	     n < (int)sess->outbuf.used-2; pc++, n++)
3317c478bd9Sstevel@tonic-gate 		if (!isascii((unsigned char)*pc) ||
3327c478bd9Sstevel@tonic-gate 		    !isprint((unsigned char)*pc))
3337c478bd9Sstevel@tonic-gate 			*pc = '\040';
3347c478bd9Sstevel@tonic-gate 	*iovp++ = evConsIovec(sess->outbuf.text, sess->outbuf.used);
3357c478bd9Sstevel@tonic-gate 	if (body != NULL) {
3367c478bd9Sstevel@tonic-gate 		char *tmp;
3377c478bd9Sstevel@tonic-gate 		DE_CONST(body, tmp);
3387c478bd9Sstevel@tonic-gate 		*iovp++ = evConsIovec(tmp, bodylen);
3397c478bd9Sstevel@tonic-gate 		DE_CONST(".\r\n", tmp);
3407c478bd9Sstevel@tonic-gate 		*iovp++ = evConsIovec(tmp, 3);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: [%d] %s", me,
3437c478bd9Sstevel@tonic-gate 		       sess->outbuf.used, sess->outbuf.text);
3447c478bd9Sstevel@tonic-gate 	if (evWrite(ctx->ev, sess->sock, iov, iovp - iov,
3457c478bd9Sstevel@tonic-gate 		    ctl_writedone, sess, &sess->wrID) < 0) {
3467c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: evWrite: %s", me,
3477c478bd9Sstevel@tonic-gate 			       address_expr, strerror(errno));
3487c478bd9Sstevel@tonic-gate 		goto untimely;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	if (evSetIdleTimer(ctx->ev, ctl_wrtimeout, sess, ctx->timeout,
3517c478bd9Sstevel@tonic-gate 			   &sess->wrtiID) < 0)
3527c478bd9Sstevel@tonic-gate 	{
3537c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: evSetIdleTimer: %s", me,
3547c478bd9Sstevel@tonic-gate 			       address_expr, strerror(errno));
3557c478bd9Sstevel@tonic-gate 		goto untimely;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	if (evTimeRW(ctx->ev, sess->wrID, sess->wrtiID) < 0) {
3587c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: evTimeRW: %s", me,
3597c478bd9Sstevel@tonic-gate 			       address_expr, strerror(errno));
3607c478bd9Sstevel@tonic-gate  untimely:
3617c478bd9Sstevel@tonic-gate 		ctl_signal_done(ctx, sess);
3627c478bd9Sstevel@tonic-gate 		ctl_close(sess);
3637c478bd9Sstevel@tonic-gate 		return;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	sess->respctx = respctx;
3667c478bd9Sstevel@tonic-gate 	sess->respflags = flags;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate void
ctl_sendhelp(struct ctl_sess * sess,u_int code)3707c478bd9Sstevel@tonic-gate ctl_sendhelp(struct ctl_sess *sess, u_int code) {
3717c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_sendhelp";
3727c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	sess->helpcode = code;
3757c478bd9Sstevel@tonic-gate 	sess->verb = &fakehelpverb;
3767c478bd9Sstevel@tonic-gate 	ctl_morehelp(ctx, sess, NULL, me, CTL_MORE,
3777c478bd9Sstevel@tonic-gate 		     (const void *)ctx->verbs, NULL);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate void *
ctl_getcsctx(struct ctl_sess * sess)3817c478bd9Sstevel@tonic-gate ctl_getcsctx(struct ctl_sess *sess) {
3827c478bd9Sstevel@tonic-gate 	return (sess->csctx);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate void *
ctl_setcsctx(struct ctl_sess * sess,void * csctx)3867c478bd9Sstevel@tonic-gate ctl_setcsctx(struct ctl_sess *sess, void *csctx) {
3877c478bd9Sstevel@tonic-gate 	void *old = sess->csctx;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	sess->csctx = csctx;
3907c478bd9Sstevel@tonic-gate 	return (old);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /* Private functions. */
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate static void
ctl_accept(evContext lev,void * uap,int fd,const void * lav,int lalen,const void * rav,int ralen)3967c478bd9Sstevel@tonic-gate ctl_accept(evContext lev, void *uap, int fd,
3977c478bd9Sstevel@tonic-gate 	   const void *lav, int lalen,
3987c478bd9Sstevel@tonic-gate 	   const void *rav, int ralen)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_accept";
4017c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = uap;
4027c478bd9Sstevel@tonic-gate 	struct ctl_sess *sess = NULL;
4037c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	UNUSED(lev);
4067c478bd9Sstevel@tonic-gate 	UNUSED(lalen);
4077c478bd9Sstevel@tonic-gate 	UNUSED(ralen);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (fd < 0) {
4107c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: accept: %s",
4117c478bd9Sstevel@tonic-gate 			       me, strerror(errno));
4127c478bd9Sstevel@tonic-gate 		return;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	if (ctx->cur_sess == ctx->max_sess) {
4157c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: too many control sessions",
4167c478bd9Sstevel@tonic-gate 			       me, ctl_sa_ntop((const struct sockaddr *)rav,
4177c478bd9Sstevel@tonic-gate 					       tmp, sizeof tmp,
4187c478bd9Sstevel@tonic-gate 					       ctx->logger));
4197c478bd9Sstevel@tonic-gate 		(void) close(fd);
4207c478bd9Sstevel@tonic-gate 		return;
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	sess = memget(sizeof *sess);
4237c478bd9Sstevel@tonic-gate 	if (sess == NULL) {
4247c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: memget: %s", me,
4257c478bd9Sstevel@tonic-gate 			       strerror(errno));
4267c478bd9Sstevel@tonic-gate 		(void) close(fd);
4277c478bd9Sstevel@tonic-gate 		return;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (fcntl(fd, F_SETFD, 1) < 0) {
4307c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_warning, "%s: fcntl: %s", me,
4317c478bd9Sstevel@tonic-gate 			       strerror(errno));
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	ctx->cur_sess++;
4347c478bd9Sstevel@tonic-gate 	INIT_LINK(sess, link);
4357c478bd9Sstevel@tonic-gate 	APPEND(ctx->sess, sess, link);
4367c478bd9Sstevel@tonic-gate 	sess->ctx = ctx;
4377c478bd9Sstevel@tonic-gate 	sess->sock = fd;
4387c478bd9Sstevel@tonic-gate 	sess->wrID.opaque = NULL;
4397c478bd9Sstevel@tonic-gate 	sess->rdID.opaque = NULL;
4407c478bd9Sstevel@tonic-gate 	sess->wrtiID.opaque = NULL;
4417c478bd9Sstevel@tonic-gate 	sess->rdtiID.opaque = NULL;
4427c478bd9Sstevel@tonic-gate 	sess->respctx = NULL;
4437c478bd9Sstevel@tonic-gate 	sess->csctx = NULL;
4447c478bd9Sstevel@tonic-gate 	if (((const struct sockaddr *)rav)->sa_family == AF_UNIX)
4457c478bd9Sstevel@tonic-gate 		ctl_sa_copy((const struct sockaddr *)lav,
4467c478bd9Sstevel@tonic-gate 			    (struct sockaddr *)&sess->sa);
4477c478bd9Sstevel@tonic-gate 	else
4487c478bd9Sstevel@tonic-gate 		ctl_sa_copy((const struct sockaddr *)rav,
4497c478bd9Sstevel@tonic-gate 			    (struct sockaddr *)&sess->sa);
4507c478bd9Sstevel@tonic-gate 	sess->donefunc = NULL;
4517c478bd9Sstevel@tonic-gate 	buffer_init(sess->inbuf);
4527c478bd9Sstevel@tonic-gate 	buffer_init(sess->outbuf);
4537c478bd9Sstevel@tonic-gate 	sess->state = available;
4547c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, initializing, me);
4557c478bd9Sstevel@tonic-gate 	sess->verb = ctx->connverb;
4567c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: %s: accepting (fd %d)",
4577c478bd9Sstevel@tonic-gate 		       me, address_expr, sess->sock);
4587c478bd9Sstevel@tonic-gate 	(*ctx->connverb->func)(ctx, sess, ctx->connverb, "", 0,
4597c478bd9Sstevel@tonic-gate 			       (const struct sockaddr *)rav, ctx->uctx);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate static void
ctl_new_state(struct ctl_sess * sess,enum state new_state,const char * reason)4637c478bd9Sstevel@tonic-gate ctl_new_state(struct ctl_sess *sess, enum state new_state, const char *reason)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_new_state";
4667c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
4677c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: %s: %s -> %s (%s)",
4707c478bd9Sstevel@tonic-gate 		       me, address_expr,
4717c478bd9Sstevel@tonic-gate 		       state_names[sess->state],
4727c478bd9Sstevel@tonic-gate 		       state_names[new_state], reason);
4737c478bd9Sstevel@tonic-gate 	sess->state = new_state;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate static void
ctl_close(struct ctl_sess * sess)4777c478bd9Sstevel@tonic-gate ctl_close(struct ctl_sess *sess) {
4787c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_close";
4797c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
4807c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == initializing ||
4837c478bd9Sstevel@tonic-gate 		sess->state == writing ||
4847c478bd9Sstevel@tonic-gate 		sess->state == reading ||
4857c478bd9Sstevel@tonic-gate 		sess->state == processing ||
4867c478bd9Sstevel@tonic-gate 		sess->state == reading_data ||
4877c478bd9Sstevel@tonic-gate 		sess->state == idling);
4887c478bd9Sstevel@tonic-gate 	REQUIRE(sess->sock != -1);
4897c478bd9Sstevel@tonic-gate 	if (sess->state == reading || sess->state == reading_data)
4907c478bd9Sstevel@tonic-gate 		ctl_stop_read(sess);
4917c478bd9Sstevel@tonic-gate 	else if (sess->state == writing) {
4927c478bd9Sstevel@tonic-gate 		if (sess->wrID.opaque != NULL) {
4937c478bd9Sstevel@tonic-gate 			(void) evCancelRW(ctx->ev, sess->wrID);
4947c478bd9Sstevel@tonic-gate 			sess->wrID.opaque = NULL;
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		if (sess->wrtiID.opaque != NULL) {
4977c478bd9Sstevel@tonic-gate 			(void) evClearIdleTimer(ctx->ev, sess->wrtiID);
4987c478bd9Sstevel@tonic-gate 			sess->wrtiID.opaque = NULL;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, closing, me);
5027c478bd9Sstevel@tonic-gate 	(void) close(sess->sock);
5037c478bd9Sstevel@tonic-gate 	if (allocated_p(sess->inbuf))
5047c478bd9Sstevel@tonic-gate 		ctl_bufput(&sess->inbuf);
5057c478bd9Sstevel@tonic-gate 	if (allocated_p(sess->outbuf))
5067c478bd9Sstevel@tonic-gate 		ctl_bufput(&sess->outbuf);
5077c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: %s: closed (fd %d)",
5087c478bd9Sstevel@tonic-gate 		       me, address_expr, sess->sock);
5097c478bd9Sstevel@tonic-gate 	UNLINK(ctx->sess, sess, link);
5107c478bd9Sstevel@tonic-gate 	memput(sess, sizeof *sess);
5117c478bd9Sstevel@tonic-gate 	ctx->cur_sess--;
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate static void
ctl_start_read(struct ctl_sess * sess)5157c478bd9Sstevel@tonic-gate ctl_start_read(struct ctl_sess *sess) {
5167c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_start_read";
5177c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
5187c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == initializing ||
5217c478bd9Sstevel@tonic-gate 		sess->state == writing ||
5227c478bd9Sstevel@tonic-gate 		sess->state == processing ||
5237c478bd9Sstevel@tonic-gate 		sess->state == idling);
5247c478bd9Sstevel@tonic-gate 	REQUIRE(sess->rdtiID.opaque == NULL);
5257c478bd9Sstevel@tonic-gate 	REQUIRE(sess->rdID.opaque == NULL);
5267c478bd9Sstevel@tonic-gate 	sess->inbuf.used = 0;
5277c478bd9Sstevel@tonic-gate 	if (evSetIdleTimer(ctx->ev, ctl_rdtimeout, sess, ctx->timeout,
5287c478bd9Sstevel@tonic-gate 			   &sess->rdtiID) < 0)
5297c478bd9Sstevel@tonic-gate 	{
5307c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: evSetIdleTimer: %s", me,
5317c478bd9Sstevel@tonic-gate 			       address_expr, strerror(errno));
5327c478bd9Sstevel@tonic-gate 		ctl_close(sess);
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	if (evSelectFD(ctx->ev, sess->sock, EV_READ,
5367c478bd9Sstevel@tonic-gate 		       ctl_readable, sess, &sess->rdID) < 0) {
5377c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: evSelectFD: %s", me,
5387c478bd9Sstevel@tonic-gate 			       address_expr, strerror(errno));
5397c478bd9Sstevel@tonic-gate 		return;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, reading, me);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate static void
ctl_stop_read(struct ctl_sess * sess)5457c478bd9Sstevel@tonic-gate ctl_stop_read(struct ctl_sess *sess) {
5467c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_stop_read";
5477c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == reading || sess->state == reading_data);
5507c478bd9Sstevel@tonic-gate 	REQUIRE(sess->rdID.opaque != NULL);
5517c478bd9Sstevel@tonic-gate 	(void) evDeselectFD(ctx->ev, sess->rdID);
5527c478bd9Sstevel@tonic-gate 	sess->rdID.opaque = NULL;
5537c478bd9Sstevel@tonic-gate 	if (sess->rdtiID.opaque != NULL) {
5547c478bd9Sstevel@tonic-gate 		(void) evClearIdleTimer(ctx->ev, sess->rdtiID);
5557c478bd9Sstevel@tonic-gate 		sess->rdtiID.opaque = NULL;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, idling, me);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate static void
ctl_readable(evContext lev,void * uap,int fd,int evmask)5617c478bd9Sstevel@tonic-gate ctl_readable(evContext lev, void *uap, int fd, int evmask) {
5627c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_readable";
5637c478bd9Sstevel@tonic-gate 	struct ctl_sess *sess = uap;
564*9525b14bSRao Shoaib 	struct ctl_sctx *ctx;
5657c478bd9Sstevel@tonic-gate 	char *eos, tmp[MAX_NTOP];
5667c478bd9Sstevel@tonic-gate 	ssize_t n;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	REQUIRE(sess != NULL);
5697c478bd9Sstevel@tonic-gate 	REQUIRE(fd >= 0);
5707c478bd9Sstevel@tonic-gate 	REQUIRE(evmask == EV_READ);
5717c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == reading || sess->state == reading_data);
572*9525b14bSRao Shoaib 
573*9525b14bSRao Shoaib 	ctx = sess->ctx;
5747c478bd9Sstevel@tonic-gate 	evTouchIdleTimer(lev, sess->rdtiID);
5757c478bd9Sstevel@tonic-gate 	if (!allocated_p(sess->inbuf) &&
5767c478bd9Sstevel@tonic-gate 	    ctl_bufget(&sess->inbuf, ctx->logger) < 0) {
5777c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: cant get an input buffer",
5787c478bd9Sstevel@tonic-gate 			       me, address_expr);
5797c478bd9Sstevel@tonic-gate 		ctl_close(sess);
5807c478bd9Sstevel@tonic-gate 		return;
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	n = read(sess->sock, sess->inbuf.text + sess->inbuf.used,
5837c478bd9Sstevel@tonic-gate 		 MAX_LINELEN - sess->inbuf.used);
5847c478bd9Sstevel@tonic-gate 	if (n <= 0) {
5857c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_debug, "%s: %s: read: %s",
5867c478bd9Sstevel@tonic-gate 			       me, address_expr,
5877c478bd9Sstevel@tonic-gate 			       (n == 0) ? "Unexpected EOF" : strerror(errno));
5887c478bd9Sstevel@tonic-gate 		ctl_close(sess);
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	sess->inbuf.used += n;
5927c478bd9Sstevel@tonic-gate 	eos = memchr(sess->inbuf.text, '\n', sess->inbuf.used);
5937c478bd9Sstevel@tonic-gate 	if (eos != NULL && eos != sess->inbuf.text && eos[-1] == '\r') {
5947c478bd9Sstevel@tonic-gate 		eos[-1] = '\0';
5957c478bd9Sstevel@tonic-gate 		if ((sess->respflags & CTL_DATA) != 0) {
5967c478bd9Sstevel@tonic-gate 			INSIST(sess->verb != NULL);
5977c478bd9Sstevel@tonic-gate 			(*sess->verb->func)(sess->ctx, sess, sess->verb,
5987c478bd9Sstevel@tonic-gate 					    sess->inbuf.text,
5997c478bd9Sstevel@tonic-gate 					    CTL_DATA, sess->respctx,
6007c478bd9Sstevel@tonic-gate 					    sess->ctx->uctx);
6017c478bd9Sstevel@tonic-gate 		} else {
6027c478bd9Sstevel@tonic-gate 			ctl_stop_read(sess);
6037c478bd9Sstevel@tonic-gate 			ctl_docommand(sess);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		sess->inbuf.used -= ((eos - sess->inbuf.text) + 1);
606*9525b14bSRao Shoaib 		if (sess->inbuf.used == 0U)
6077c478bd9Sstevel@tonic-gate 			ctl_bufput(&sess->inbuf);
6087c478bd9Sstevel@tonic-gate 		else
6097c478bd9Sstevel@tonic-gate 			memmove(sess->inbuf.text, eos + 1, sess->inbuf.used);
6107c478bd9Sstevel@tonic-gate 		return;
6117c478bd9Sstevel@tonic-gate 	}
612*9525b14bSRao Shoaib 	if (sess->inbuf.used == (size_t)MAX_LINELEN) {
6137c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: line too long, closing",
6147c478bd9Sstevel@tonic-gate 			       me, address_expr);
6157c478bd9Sstevel@tonic-gate 		ctl_close(sess);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate static void
ctl_wrtimeout(evContext lev,void * uap,struct timespec due,struct timespec itv)6207c478bd9Sstevel@tonic-gate ctl_wrtimeout(evContext lev, void *uap,
6217c478bd9Sstevel@tonic-gate 	      struct timespec due,
6227c478bd9Sstevel@tonic-gate 	      struct timespec itv)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_wrtimeout";
6257c478bd9Sstevel@tonic-gate 	struct ctl_sess *sess = uap;
6267c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
6277c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	UNUSED(lev);
6307c478bd9Sstevel@tonic-gate 	UNUSED(due);
6317c478bd9Sstevel@tonic-gate 	UNUSED(itv);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == writing);
6347c478bd9Sstevel@tonic-gate 	sess->wrtiID.opaque = NULL;
6357c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_warning, "%s: %s: write timeout, closing",
6367c478bd9Sstevel@tonic-gate 		       me, address_expr);
6377c478bd9Sstevel@tonic-gate 	if (sess->wrID.opaque != NULL) {
6387c478bd9Sstevel@tonic-gate 		(void) evCancelRW(ctx->ev, sess->wrID);
6397c478bd9Sstevel@tonic-gate 		sess->wrID.opaque = NULL;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	ctl_signal_done(ctx, sess);
6427c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, processing, me);
6437c478bd9Sstevel@tonic-gate 	ctl_close(sess);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static void
ctl_rdtimeout(evContext lev,void * uap,struct timespec due,struct timespec itv)6477c478bd9Sstevel@tonic-gate ctl_rdtimeout(evContext lev, void *uap,
6487c478bd9Sstevel@tonic-gate 	      struct timespec due,
6497c478bd9Sstevel@tonic-gate 	      struct timespec itv)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_rdtimeout";
6527c478bd9Sstevel@tonic-gate 	struct ctl_sess *sess = uap;
6537c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
6547c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	UNUSED(lev);
6577c478bd9Sstevel@tonic-gate 	UNUSED(due);
6587c478bd9Sstevel@tonic-gate 	UNUSED(itv);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == reading);
6617c478bd9Sstevel@tonic-gate 	sess->rdtiID.opaque = NULL;
6627c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_warning, "%s: %s: timeout, closing",
6637c478bd9Sstevel@tonic-gate 		       me, address_expr);
6647c478bd9Sstevel@tonic-gate 	if (sess->state == reading || sess->state == reading_data)
6657c478bd9Sstevel@tonic-gate 		ctl_stop_read(sess);
6667c478bd9Sstevel@tonic-gate 	ctl_signal_done(ctx, sess);
6677c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, processing, me);
6687c478bd9Sstevel@tonic-gate 	ctl_response(sess, ctx->timeoutcode, "Timeout.", CTL_EXIT, NULL,
6697c478bd9Sstevel@tonic-gate 		     NULL, NULL, NULL, 0);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate static void
ctl_docommand(struct ctl_sess * sess)6737c478bd9Sstevel@tonic-gate ctl_docommand(struct ctl_sess *sess) {
6747c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_docommand";
6757c478bd9Sstevel@tonic-gate 	char *name, *rest, tmp[MAX_NTOP];
6767c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
6777c478bd9Sstevel@tonic-gate 	const struct ctl_verb *verb;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	REQUIRE(allocated_p(sess->inbuf));
6807c478bd9Sstevel@tonic-gate 	(*ctx->logger)(ctl_debug, "%s: %s: \"%s\" [%u]",
6817c478bd9Sstevel@tonic-gate 		       me, address_expr,
6827c478bd9Sstevel@tonic-gate 		       sess->inbuf.text, (u_int)sess->inbuf.used);
6837c478bd9Sstevel@tonic-gate 	ctl_new_state(sess, processing, me);
6847c478bd9Sstevel@tonic-gate 	name = sess->inbuf.text + strspn(sess->inbuf.text, space);
6857c478bd9Sstevel@tonic-gate 	rest = name + strcspn(name, space);
6867c478bd9Sstevel@tonic-gate 	if (*rest != '\0') {
6877c478bd9Sstevel@tonic-gate 		*rest++ = '\0';
6887c478bd9Sstevel@tonic-gate 		rest += strspn(rest, space);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	for (verb = ctx->verbs;
6917c478bd9Sstevel@tonic-gate 	     verb != NULL && verb->name != NULL && verb->func != NULL;
6927c478bd9Sstevel@tonic-gate 	     verb++)
6937c478bd9Sstevel@tonic-gate 		if (verb->name[0] != '\0' && strcasecmp(name, verb->name) == 0)
6947c478bd9Sstevel@tonic-gate 			break;
6957c478bd9Sstevel@tonic-gate 	if (verb != NULL && verb->name != NULL && verb->func != NULL) {
6967c478bd9Sstevel@tonic-gate 		sess->verb = verb;
6977c478bd9Sstevel@tonic-gate 		(*verb->func)(ctx, sess, verb, rest, 0, NULL, ctx->uctx);
6987c478bd9Sstevel@tonic-gate 	} else {
6997c478bd9Sstevel@tonic-gate 		char buf[1100];
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (sizeof "Unrecognized command \"\" (args \"\")" +
7027c478bd9Sstevel@tonic-gate 		    strlen(name) + strlen(rest) > sizeof buf)
7037c478bd9Sstevel@tonic-gate 			strcpy(buf, "Unrecognized command (buf ovf)");
7047c478bd9Sstevel@tonic-gate 		else
7057c478bd9Sstevel@tonic-gate 			sprintf(buf,
7067c478bd9Sstevel@tonic-gate 				"Unrecognized command \"%s\" (args \"%s\")",
7077c478bd9Sstevel@tonic-gate 				name, rest);
7087c478bd9Sstevel@tonic-gate 		ctl_response(sess, ctx->unkncode, buf, 0, NULL, NULL, NULL,
7097c478bd9Sstevel@tonic-gate 			     NULL, 0);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate static void
ctl_writedone(evContext lev,void * uap,int fd,int bytes)7147c478bd9Sstevel@tonic-gate ctl_writedone(evContext lev, void *uap, int fd, int bytes) {
7157c478bd9Sstevel@tonic-gate 	static const char me[] = "ctl_writedone";
7167c478bd9Sstevel@tonic-gate 	struct ctl_sess *sess = uap;
7177c478bd9Sstevel@tonic-gate 	struct ctl_sctx *ctx = sess->ctx;
7187c478bd9Sstevel@tonic-gate 	char tmp[MAX_NTOP];
7197c478bd9Sstevel@tonic-gate 	int save_errno = errno;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	UNUSED(lev);
7227c478bd9Sstevel@tonic-gate 	UNUSED(uap);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	REQUIRE(sess->state == writing);
7257c478bd9Sstevel@tonic-gate 	REQUIRE(fd == sess->sock);
7267c478bd9Sstevel@tonic-gate 	REQUIRE(sess->wrtiID.opaque != NULL);
7277c478bd9Sstevel@tonic-gate 	sess->wrID.opaque = NULL;
7287c478bd9Sstevel@tonic-gate 	(void) evClearIdleTimer(ctx->ev, sess->wrtiID);
7297c478bd9Sstevel@tonic-gate 	sess->wrtiID.opaque = NULL;
7307c478bd9Sstevel@tonic-gate 	if (bytes < 0) {
7317c478bd9Sstevel@tonic-gate 		(*ctx->logger)(ctl_error, "%s: %s: %s",
7327c478bd9Sstevel@tonic-gate 			       me, address_expr, strerror(save_errno));
7337c478bd9Sstevel@tonic-gate 		ctl_close(sess);
7347c478bd9Sstevel@tonic-gate 		return;
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	INSIST(allocated_p(sess->outbuf));
7387c478bd9Sstevel@tonic-gate 	ctl_bufput(&sess->outbuf);
7397c478bd9Sstevel@tonic-gate 	if ((sess->respflags & CTL_EXIT) != 0) {
7407c478bd9Sstevel@tonic-gate 		ctl_signal_done(ctx, sess);
7417c478bd9Sstevel@tonic-gate 		ctl_close(sess);
7427c478bd9Sstevel@tonic-gate 		return;
7437c478bd9Sstevel@tonic-gate 	} else if ((sess->respflags & CTL_MORE) != 0) {
7447c478bd9Sstevel@tonic-gate 		INSIST(sess->verb != NULL);
7457c478bd9Sstevel@tonic-gate 		(*sess->verb->func)(sess->ctx, sess, sess->verb, "",
7467c478bd9Sstevel@tonic-gate 				    CTL_MORE, sess->respctx, sess->ctx->uctx);
7477c478bd9Sstevel@tonic-gate 	} else {
7487c478bd9Sstevel@tonic-gate 		ctl_signal_done(ctx, sess);
7497c478bd9Sstevel@tonic-gate 		ctl_start_read(sess);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static void
ctl_morehelp(struct ctl_sctx * ctx,struct ctl_sess * sess,const struct ctl_verb * verb,const char * text,u_int respflags,const void * respctx,void * uctx)7547c478bd9Sstevel@tonic-gate ctl_morehelp(struct ctl_sctx *ctx, struct ctl_sess *sess,
7557c478bd9Sstevel@tonic-gate 	     const struct ctl_verb *verb, const char *text,
7567c478bd9Sstevel@tonic-gate 	     u_int respflags, const void *respctx, void *uctx)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	const struct ctl_verb *this = respctx, *next = this + 1;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	UNUSED(ctx);
7617c478bd9Sstevel@tonic-gate 	UNUSED(verb);
7627c478bd9Sstevel@tonic-gate 	UNUSED(text);
7637c478bd9Sstevel@tonic-gate 	UNUSED(uctx);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	REQUIRE(!lastverb_p(this));
7667c478bd9Sstevel@tonic-gate 	REQUIRE((respflags & CTL_MORE) != 0);
7677c478bd9Sstevel@tonic-gate 	if (lastverb_p(next))
7687c478bd9Sstevel@tonic-gate 		respflags &= ~CTL_MORE;
7697c478bd9Sstevel@tonic-gate 	ctl_response(sess, sess->helpcode, this->help, respflags, next,
7707c478bd9Sstevel@tonic-gate 		     NULL, NULL, NULL, 0);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate static void
ctl_signal_done(struct ctl_sctx * ctx,struct ctl_sess * sess)7747c478bd9Sstevel@tonic-gate ctl_signal_done(struct ctl_sctx *ctx, struct ctl_sess *sess) {
7757c478bd9Sstevel@tonic-gate 	if (sess->donefunc != NULL) {
7767c478bd9Sstevel@tonic-gate 		(*sess->donefunc)(ctx, sess, sess->uap);
7777c478bd9Sstevel@tonic-gate 		sess->donefunc = NULL;
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate }
780*9525b14bSRao Shoaib 
781*9525b14bSRao Shoaib /*! \file */
782