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