diff -ruN nagiosplug-save/plugins/check_tcp.c nagiosplug/plugins/check_tcp.c
--- nagiosplug-save/plugins/check_tcp.c	2004-03-14 04:09:19.000000000 +0000
+++ nagiosplug/plugins/check_tcp.c	2004-03-28 20:20:57.000000000 +0100
@@ -44,6 +44,8 @@
 #  endif
 #endif
 
+char *progname = NULL;
+
 #ifdef HAVE_SSL
 SSL_CTX *ctx;
 SSL *ssl;
@@ -51,29 +53,48 @@
 #endif
 
 enum {
-	TCP_PROTOCOL = 1,
-	UDP_PROTOCOL = 2,
+	TCP_PROTOCOL = IPPROTO_TCP,
+	UDP_PROTOCOL = IPPROTO_UDP,
 	MAXBUF = 1024
 };
 
+typedef struct _sendexpect {
+  enum { NOOP= 0, SEND = 1, EXPECT = 2} operation;
+  char *data;
+} *sendexpect;
+
+typedef struct _sendexpectquit {
+  int operations;
+  char *quit;
+  struct _sendexpect **interchange;
+} *sendexpectquit;
+
+typedef struct _operation {
+  char *progname;
+  char *service;
+  sendexpectquit conversation;
+  int protocol;
+  int port;
+  int use_ssl;
+} *operation;
+
 int process_arguments (int, char **);
 int my_recv (void);
 void print_help (void);
 void print_usage (void);
+int check_expect(char *);
+char *strxdup(char *);
+char *strlower(char *);
+char *strupper(char *);
+int do_conversation(char **);
 
 char *SERVICE = NULL;
-char *SEND = NULL;
-char *EXPECT = NULL;
-char *QUIT = NULL;
+sendexpectquit CONVERSATION;
 int PROTOCOL = 0;
 int PORT = 0;
 
 int server_port = 0;
 char *server_address = NULL;
-char *server_send = NULL;
-char *server_quit = NULL;
-char **server_expect = NULL;
-size_t server_expect_count = 0;
 int maxbytes = 0;
 char **warn_codes = NULL;
 size_t warn_codes_count = 0;
@@ -91,8 +112,59 @@
 int sd = 0;
 char *buffer;
 
+static struct _sendexpect IMAPOPERATION = { EXPECT, "* OK" };
+static struct _sendexpect POPOPERATION = { EXPECT, "+OK" };
+static struct _sendexpect TWOHUNDREDOPERATION = { EXPECT, "200" };
+static struct _sendexpect TWOOHONEOPERATION = { EXPECT, "201" };
+static struct _sendexpect TWOTWENTYOPERATION = { EXPECT, "220" };
+
+static struct _sendexpect *IMAPOPERATIONS[] = { &IMAPOPERATION };
+static struct _sendexpect *NNTPOPERATIONS[] = { &TWOHUNDREDOPERATION, &TWOOHONEOPERATION };
+static struct _sendexpect *POPOPERATIONS[] = { &POPOPERATION };
+static struct _sendexpect *TWOTWENTYOPERATIONS[] = { &TWOTWENTYOPERATION };
+
+static struct _sendexpectquit NULLCONVERSATION = {
+  0,
+  NULL,
+  NULL
+};
+static struct _sendexpectquit TWOTWENTYCONVERSATION = {
+  1,
+  "QUIT\r\n",
+  TWOTWENTYOPERATIONS
+};
+static struct _sendexpectquit POPCONVERSATION = {
+  1,
+  "QUIT\r\n",
+  POPOPERATIONS
+};
+static struct _sendexpectquit IMAPCONVERSATION = {
+  1,
+  "QUIT\r\n",
+  IMAPOPERATIONS
+};
+static struct _sendexpectquit NNTPCONVERSATION = {
+  2,
+  "QUIT\r\n",
+  NNTPOPERATIONS
+};
 
-
+static struct _operation operations[] = {
+  { "check_udp", "UDP", &NULLCONVERSATION, UDP_PROTOCOL, 0, 0 },
+  { "check_tcp", "TCP", &NULLCONVERSATION, TCP_PROTOCOL, 0, 0 },
+  { "check_ftp", "FTP", &TWOTWENTYCONVERSATION, TCP_PROTOCOL, 21, 0 },
+  { "check_smtp", "SMTP", &TWOTWENTYCONVERSATION, TCP_PROTOCOL, 25, 0 },
+  { "check_pop", "POP", &POPCONVERSATION, TCP_PROTOCOL, 110, 0 },
+  { "check_imap", "IMAP", &IMAPCONVERSATION, TCP_PROTOCOL, 143, 0 },
+#ifdef HAVE_SSL
+  { "check_simap", "SIMAP", &IMAPCONVERSATION, TCP_PROTOCOL, 993, 1 },
+  { "check_imaps", "IMAPS", &IMAPCONVERSATION, TCP_PROTOCOL, 993, 1 },
+  { "check_spop", "SPOP", &POPCONVERSATION, TCP_PROTOCOL, 995, 1 },
+  { "check_pop3s", "POP3S", &POPCONVERSATION, TCP_PROTOCOL, 995, 1 },
+#endif
+  { "check_nntp", "NNTP", &NNTPCONVERSATION, TCP_PROTOCOL, 119, 0 },
+  { NULL, NULL, NULL, 0, 0, 0 }
+};
 
 
 
@@ -101,122 +173,38 @@
 {
 	int result;
 	int i;
-	char *status;
+	char *status = strdup("");
 	struct timeval tv;
 
 	setlocale (LC_ALL, "");
 	bindtextdomain (PACKAGE, LOCALEDIR);
 	textdomain (PACKAGE);
 
-	if (strstr (argv[0], "check_udp")) {
-		progname = strdup ("check_udp");
-		SERVICE = strdup ("UDP");
-		SEND = NULL;
-		EXPECT = NULL;
-		QUIT = NULL;
-		PROTOCOL = UDP_PROTOCOL;
-		PORT = 0;
-	}
-	else if (strstr (argv[0], "check_tcp")) {
-		progname = strdup ("check_tcp");
-		SERVICE = strdup ("TCP");
-		SEND = NULL;
-		EXPECT = NULL;
-		QUIT = NULL;
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 0;
-	}
-	else if (strstr (argv[0], "check_ftp")) {
-		progname = strdup ("check_ftp");
-		SERVICE = strdup ("FTP");
-		SEND = NULL;
-		EXPECT = strdup ("220");
-		QUIT = strdup ("QUIT\r\n");
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 21;
-	}
-	else if (strstr (argv[0], "check_smtp")) {
-		progname = strdup ("check_smtp");
-		SERVICE = strdup ("SMTP");
-		SEND = NULL;
-		EXPECT = strdup ("220");
-		QUIT = strdup ("QUIT\r\n");
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 25;
-	}
-	else if (strstr (argv[0], "check_pop")) {
-		progname = strdup ("check_pop");
-		SERVICE = strdup ("POP");
-		SEND = NULL;
-		EXPECT = strdup ("+OK");
-		QUIT = strdup ("QUIT\r\n");
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 110;
-	}
-	else if (strstr (argv[0], "check_imap")) {
-		progname = strdup ("check_imap");
-		SERVICE = strdup ("IMAP");
-		SEND = NULL;
-		EXPECT = strdup ("* OK");
-		QUIT = strdup ("a1 LOGOUT\r\n");
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 143;
-	}
+
+ 	for (i = 0; operations[i].progname; i++) {
+ 	        if (strstr (argv[0], operations[i].progname)) {
+ 		        progname = strxdup(operations[i].progname);
+ 			SERVICE = strxdup(operations[i].service);
+ 			CONVERSATION = operations[i].conversation;
+ 			PROTOCOL = operations[i].protocol;
+ 			PORT = operations[i].port;
 #ifdef HAVE_SSL
-	else if (strstr(argv[0],"check_simap")) {
-		progname = strdup ("check_simap");
-		SERVICE = strdup ("SIMAP");
-		SEND=NULL;
-		EXPECT = strdup ("* OK");
-		QUIT = strdup ("a1 LOGOUT\r\n");
-		PROTOCOL=TCP_PROTOCOL;
-		use_ssl=TRUE;
-		PORT=993;
-	}
-	else if (strstr(argv[0],"check_spop")) {
-		progname = strdup ("check_spop");
-		SERVICE = strdup ("SPOP");
-		SEND=NULL;
-		EXPECT = strdup ("+OK");
-		QUIT = strdup ("QUIT\r\n");
-		PROTOCOL=TCP_PROTOCOL;
-		use_ssl=TRUE;
-		PORT=995;
-	}
+ 			use_ssl = operations[i].use_ssl;
 #endif
-	else if (strstr (argv[0], "check_nntp")) {
-		progname = strdup ("check_nntp");
-		SERVICE = strdup ("NNTP");
-		SEND = NULL;
-		EXPECT = NULL;
-		server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
-		asprintf (&server_expect[server_expect_count - 1], "200");
-		server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
-		asprintf (&server_expect[server_expect_count - 1], "201");
-		asprintf (&QUIT, "QUIT\r\n");
-		PROTOCOL = TCP_PROTOCOL;
-		PORT = 119;
-	}
-	else {
-		progname = strdup ("check_tcp");
+ 			break;
+ 		}
+  	}
+ 	if (!progname) {
+ 		progname = strxdup ("check_tcp");
 		usage (_("ERROR: Generic check_tcp called with unknown service\n"));
 	}
 
 	server_address = strdup ("127.0.0.1");
 	server_port = PORT;
-	server_send = SEND;
-	server_quit = QUIT;
-	status = strdup ("");
 
 	if (process_arguments (argc, argv) == ERROR)
 		usage (_("Could not parse arguments\n"));
 
-	/* use default expect if none listed in process_arguments() */
-	if (EXPECT && server_expect_count == 0) {
-		server_expect = malloc (sizeof (char *) * (++server_expect_count));
-		server_expect[server_expect_count - 1] = EXPECT;
-	}
-
 	/* initialize alarm signal handling */
 	signal (SIGALRM, socket_timeout_alarm_handler);
 
@@ -231,80 +219,27 @@
 	else
 #endif
 		{
-			if (PROTOCOL == UDP_PROTOCOL)
+		        if (PROTOCOL == UDP_PROTOCOL) {
 				result = my_udp_connect (server_address, server_port, &sd);
-			else
+			} else if (PROTOCOL == TCP_PROTOCOL) {
 				/* default is TCP */
 				result = my_tcp_connect (server_address, server_port, &sd);
+			} else {
+			        die (STATE_UNKNOWN,
+				     _("Protocol not implemented (yet!)\n"));
+			}
 		}
 
 	if (result == STATE_CRITICAL)
 		return STATE_CRITICAL;
 
-	if (server_send != NULL) {		/* Something to send? */
-		asprintf (&server_send, "%s\r\n", server_send);
-#ifdef HAVE_SSL
-		if (use_ssl)
-			SSL_write(ssl, server_send, (int)strlen(server_send));
-		else
-#endif
-			send (sd, server_send, strlen(server_send), 0);
-	}
-
 	if (delay > 0) {
 		tv.tv_sec += delay;
 		sleep (delay);
 	}
 
-	if (server_send || server_expect_count > 0) {
-
-		buffer = malloc (MAXBUF);
-		memset (buffer, '\0', MAXBUF);
-		/* watch for the expect string */
-		while ((i = my_recv ()) > 0) {
-			buffer[i] = '\0';
-			asprintf (&status, "%s%s", status, buffer);
-			if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
-				break;
-			if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
-				break;
-		}
-
-		/* return a CRITICAL status if we couldn't read any data */
-		if (strlen(status) == 0)
-			die (STATE_CRITICAL, _("No data received from host\n"));
-
-		strip (status);
-
-		if (status && verbose)
-			printf ("%s\n", status);
-
-		if (server_expect_count > 0) {
-			for (i = 0;; i++) {
-				if (verbose)
-					printf ("%d %d\n", i, (int)server_expect_count);
-				if (i >= (int)server_expect_count)
-					die (STATE_WARNING, _("Invalid response from host\n"));
-				if (strstr (status, server_expect[i]))
-					break;
-			}
-		}
-	}
-
-	if (server_quit != NULL) {
-#ifdef HAVE_SSL
-		if (use_ssl) {
-			SSL_write (ssl, server_quit, (int)strlen(server_quit));
-			SSL_shutdown (ssl);
- 			SSL_free (ssl);
- 			SSL_CTX_free (ctx);
-		}
-		else {
-#endif
-			send (sd, server_quit, strlen (server_quit), 0);
-#ifdef HAVE_SSL
-		}
-#endif
+	if (result == STATE_OK && CONVERSATION->operations) {
+	        result = do_conversation(&status);
 	}
 
 	/* close the connection */
@@ -352,28 +287,32 @@
 
 	int option = 0;
 	static struct option longopts[] = {
-		{"hostname", required_argument, 0, 'H'},
-		{"critical-time", required_argument, 0, 'c'},
-		{"warning-time", required_argument, 0, 'w'},
 		{"critical-codes", required_argument, 0, 'C'},
-		{"warning-codes", required_argument, 0, 'W'},
-		{"timeout", required_argument, 0, 't'},
-		{"protocol", required_argument, 0, 'P'},
-		{"port", required_argument, 0, 'p'},
-		{"send", required_argument, 0, 's'},
+		{"critical-time", required_argument, 0, 'c'},
+		{"delay", required_argument, 0, 'd'},
 		{"expect", required_argument, 0, 'e'},
+		{"help", no_argument, 0, 'h'},
+		{"hostname", required_argument, 0, 'H'},
 		{"maxbytes", required_argument, 0, 'm'},
+		{"protocol", required_argument, 0, 'P'},
+		{"port", required_argument, 0, 'p'},
 		{"quit", required_argument, 0, 'q'},
-		{"delay", required_argument, 0, 'd'},
 		{"refuse", required_argument, 0, 'r'},
+		{"send", required_argument, 0, 's'},
+		{"ssl", no_argument, 0, 'S' },
+		{"service", required_argument, 0, 'X'},
+		{"timeout", required_argument, 0, 't'},
 		{"use-ipv4", no_argument, 0, '4'},
 		{"use-ipv6", no_argument, 0, '6'},
 		{"verbose", no_argument, 0, 'v'},
 		{"version", no_argument, 0, 'V'},
-		{"help", no_argument, 0, 'h'},
+		{"warning-time", required_argument, 0, 'w'},
+		{"warning-codes", required_argument, 0, 'W'},
 		{0, 0, 0, 0}
 	};
 
+	sendexpectquit conversation = NULL;
+
 	if (argc < 2)
 		usage ("No arguments found\n");
 
@@ -395,7 +334,7 @@
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
+		c = getopt_long (argc, argv, "+hVv46SH:s:e:q:m:c:w:t:p:C:W:d:r:P:X:",
 		                 longopts, &option);
 
 		if (c == -1 || c == EOF || c == 1)
@@ -464,16 +403,43 @@
 			else
 				server_port = atoi (optarg);
 			break;
-		case 's':
-			server_send = optarg;
+		case 's': /* send string (may be repated) */
+		        if (!conversation) {
+			        conversation = malloc(sizeof(*conversation));
+				conversation->operations = 0;
+				conversation->quit = NULL;
+				conversation->interchange = NULL;		    
+			}
+			conversation->interchange
+			  = realloc(conversation->interchange,
+				    ++conversation->operations
+				    * sizeof(struct _sendexpect*));
+			{
+			        sendexpect senditem = malloc(sizeof(struct _sendexpect));
+				senditem->operation = SEND;
+				senditem->data = optarg;
+				conversation->interchange[conversation->operations-1]
+				  = senditem;
+			}
 			break;
 		case 'e': /* expect string (may be repeated) */
-			EXPECT = NULL;
-			if (server_expect_count == 0)
-				server_expect = malloc (sizeof (char *) * (++server_expect_count));
-			else
-				server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
-			server_expect[server_expect_count - 1] = optarg;
+		        if (!conversation) {
+			        conversation = malloc(sizeof(*conversation));
+				conversation->operations = 0;
+				conversation->quit = NULL;
+				conversation->interchange = NULL;		    
+			}
+			conversation->interchange
+			  = realloc(conversation->interchange,
+				    ++conversation->operations
+				    * sizeof(struct _sendexpect*));
+			{
+			        sendexpect expectitem = malloc(sizeof(struct _sendexpect));
+				expectitem->operation = EXPECT;
+				expectitem->data = optarg;
+				conversation->interchange[conversation->operations-1]
+				  = expectitem;
+			}
 			break;
 		case 'm':
 			if (!is_intpos (optarg))
@@ -481,7 +447,16 @@
 			else
 				maxbytes = atoi (optarg);
 		case 'q':
-			asprintf(&server_quit, "%s\r\n", optarg);
+		        if (!conversation) {
+			        conversation = malloc(sizeof(*conversation));
+				conversation->operations = 0;
+				conversation->quit = NULL;
+				conversation->interchange = NULL;		    
+			}
+			if (conversation->quit)
+			        usage (_("Quit string can only be given once\n"));
+			else
+			        conversation->quit = optarg;
 			break;
 		case 'r':
 			if (!strncmp(optarg,"ok",2))
@@ -506,9 +481,92 @@
 #endif
 			use_ssl = TRUE;
 			break;
+		case 'P':
+		  {
+		    char *protocol = strdup(optarg);
+
+		    struct protoent *PE;
+
+		    PE = (is_intnonneg(protocol)
+			  ?getprotobynumber(atoi(protocol))
+			  :getprotobyname(protocol));
+
+		    if (!PE) {
+		      if (!is_intnonneg) {
+			die (STATE_UNKNOWN,
+			     _("Unknown protocol given to protocol flag P"));
+		      }
+		      PROTOCOL = atoi(protocol);
+		    } else {
+		      PROTOCOL = PE->p_proto;
+		    }
+		    free(protocol);
+		  }
+		  break;
+		case 'X':
+		  {
+		    char *service = strdup(optarg);
+		    char *service_name = service;
+		    char *protocol = strchr(service, '/');
+		    struct protoent *PE;
+		    struct servent *SE;
+
+		    /* Side effect be careful */
+		    strlower(service);
+
+		    if (protocol) {
+		      protocol[0] = '\0';
+		      protocol = &protocol[1];
+		    } else {
+		      die (STATE_UNKNOWN,
+			   _("Unknown protocol given for service flag X"));
+		    }
+		    
+		    PE = ((is_intnonneg(protocol))
+			  ?getprotobynumber(atoi(protocol))
+			  :getprotobyname(protocol));
+		    if (!PE) {
+			die (STATE_UNKNOWN,
+			     _("Unknown protocol given for service flag X"));
+		    }
+		    protocol = PE->p_name;
+		    PROTOCOL = PE->p_proto;
+		    SE = ((is_intnonneg(service_name))
+			  ?getservbyport(atoi(service_name),
+					 protocol)
+			  :getservbyname(service_name,
+					 protocol));
+		    if (!SE) {
+		      if (!is_intnonneg(service_name))
+			die (STATE_UNKNOWN,
+			     _("Unknown service for service flag X"));
+		      server_port = atoi(service_name);
+		    } else {
+		      server_port = ntohs(SE->s_port);
+		      service_name = SE->s_name; /* Ignore aliases */
+		    }
+		    SERVICE = strupper(strdup(service_name));
+		    free(service);
+		    {
+		      int i;
+		      for (i = 0; operations[i].progname; i++) {
+			if (strcmp(SERVICE, operations[i].service)) { continue; }
+			if (PROTOCOL != operations[i].protocol) { continue; }
+			if (server_port != operations[i].port) { continue; }
+			progname = operations[i].progname;
+#ifdef HAVE_SSL
+			use_ssl = operations[i].use_ssl;
+#endif
+			CONVERSATION = operations[i].conversation;
+		      }
+		    }
+		  }
+		  break;
 		}
 	}
 
+	if (conversation) { CONVERSATION = conversation; }
+
 	if (server_address == NULL)
 		usage (_("You must provide a server address\n"));
 
@@ -579,7 +637,7 @@
 	}
 	else {
 #endif
-		i = read (sd, buffer, MAXBUF - 1);
+		i = recv (sd, buffer, MAXBUF - 1, 0);
 #ifdef HAVE_SSL
 	}
 #endif
@@ -587,9 +645,146 @@
 	return i;
 }
 
+int do_conversation(char **servicestatus) {
+  int i;
+  char *status = strdup ("");
+
+  buffer = malloc (MAXBUF);
+  /* Run the conversation */
+  for (i = 0; i < CONVERSATION->operations; i++) {
+    switch (CONVERSATION->interchange[i]->operation) {
+    case NOOP: /* Should not get these so ignore for now */
+      break;
+    case SEND: /* If string is empty then just loop */
+      if (!CONVERSATION->interchange[i]->data
+	  && ! strlen(CONVERSATION->interchange[i]->data)) {
+	continue;
+      }
+      /* Send the string to the server */
+      memset (buffer, '\0', MAXBUF);
+      memcpy (buffer,
+	      CONVERSATION->interchange[i]->data,
+	      strlen(CONVERSATION->interchange[i]->data));
+      strcat (buffer, "\r\n");
+#ifdef HAVE_SSL
+      if (use_ssl)
+	SSL_write(ssl, buffer, (int)strlen(buffer));
+      else
+#endif
+	send (sd, buffer, strlen(buffer), 0);
+      break;
+    case EXPECT: /* Multiple expects in a row allow for alternatives,
+		    intersperse with NOOP's or SEND's for interchanges.
+		    An empty expect string says match anything
+		    - just throw it away! FUDGE */
+      {
+	/* Read in a record, which is data up to the next \r\n
+	   compare this with the expect string - substring allowed. */
+	/* If a match - then discard data and skip expect strings */
+	/* If expect is not empty string - then terminate with error */
+	/* If data received (in aggregate) exceeds maxbytes - then
+	   terminate search */
+	int j, k;
+
+	k = i;
+	memset(buffer, '\0', MAXBUF);
+	while ((j = my_recv()) > 0) {
+	  buffer[j] = '\0';
+	  /* Stash data for reporting purposes */
+	  /* Avoid memory leaks with long conversations */
+	  {
+	    char *ss = *servicestatus;
+	    asprintf (servicestatus, "%s%s", ss, buffer);
+	    if (ss != *servicestatus) free(ss);
+	  }
+	  {
+	    char *ss = status;
+	    asprintf (&status, "%s%s", status, buffer);
+	    if (ss != status) free(ss);
+	  }
+	  if (strstr (status, "\r\n")) { break; }
+	  if (strlen (status) >= (unsigned)maxbytes) { break; }
+	}
+	if (!strlen(status))
+	  die (STATE_CRITICAL, _("No data received from host\n"));
+	while (i < CONVERSATION->operations
+	       && CONVERSATION->interchange[i]->operation == EXPECT) {
+	  char *data = CONVERSATION->interchange[i]->data;
+	  i++;
+	  if (!strlen(data) || strstr (status, data)) { /* Got one to match */
+	    /* Shift status past the "\r\n" string */
+	    char *data = strstr(status, "\r\n");
+	    if (data) {
+	      data[0]= '\0';
+	      data = &data[2];
+	    } else {
+	      data = "";
+	    }
+	    maxbytes -= strlen(status);
+	    if (verbose)
+	      printf ("%s\n", status);
+	    {
+	      char *ss = status;
+	      asprintf (&status, "%s", data);
+	      /* if (ss != status) */ free(ss);
+	    }
+	    /* Skip past the remaining EXPECT operations */
+	    while (i < CONVERSATION->operations
+		   && CONVERSATION->interchange[i]->operation == EXPECT) {
+	      i++;
+	    }
+	    goto nextoperation;
+	  }
+	}
+	if (i >= CONVERSATION->operations)
+	  die (STATE_WARNING, _("invalid response from host\n"));
+      }
+      break;
+    }
+  nextoperation:
+    continue;
+  }
+
+  free (status);
+  free (buffer);
+
+  if (CONVERSATION->quit) {
+#ifdef HAVE_SSL
+    if (use_ssl) {
+      SSL_write (ssl, CONVERSATION->quit, (int)strlen(CONVERSATION->quit));
+      SSL_shutdown (ssl);
+      SSL_free (ssl);
+      SSL_CTX_free (ctx);
+    } else {
+#endif
+      send (sd, CONVERSATION->quit, strlen(CONVERSATION->quit), 0);
+#ifdef HAVE_SSL
+    }
+#endif
+  }
+  /* Conversation has succeeded */
+  return STATE_OK;
+}
 
+char *strxdup(char *str) {
+  return (str)?strdup(str):NULL;
+}
 
+char *strlower(char *str) {
+  int i;
 
+  for (i = 0; i < strlen(str); i++) { str[i] = tolower(str[i]); }
+
+  return str;
+}
+
+char *strupper(char *str) {
+  int i;
+
+  for (i = 0; i < strlen(str); i++) { str[i] = toupper(str[i]); }
+
+  return str;
+}
 
 
 void
@@ -598,6 +793,7 @@
 	print_revision (progname, revision);
 
 	printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
+	printf (_("Copyright (c) 2004 Howard Wilkinson <howard@cohtech.com>\n"));
 	printf (_(COPYRIGHT), copyright, email);
 
 	printf (_("This plugin tests %s connections with the specified host.\n\n"),
@@ -607,15 +803,31 @@
 
 	printf (_(UT_HELP_VRSN));
 
-	printf (_(UT_HOST_PORT), 'p', "none");
+#ifdef HAVE_SSL
+	printf (_("\
+ -S, --ssl\n\
+    Use SSL to wrap up the conversation with the server\n"));
+#endif
 
 	printf (_(UT_IPv46));
 
+	printf (_(UT_HOST_PORT), 'p', "none");
+
+	printf (_("\
+ -X, --service=STRING\n\
+   Set the service to be tested, alternative to changing the program name\n\
+   The STRING may be a service or a service name with protocol e.g. imap/tcp\n\
+   If protocol is not given here then the protocol switch must be used\n\
+ -P, --protocol=STRING\n\
+   Set the transport protocol to be used - currently only TCP and UDP\n"));
+
 	printf (_("\
  -s, --send=STRING\n\
-    String to send to the server\n\
+    String to send to the server - repeat as necessary (empty string to skip a send)\n\
  -e, --expect=STRING\n\
-    String to expect in server response\n\
+    String to expect in server response - repeat as necessary\n\
+      Each string is sequence not separated by a send is an alternative\n\
+      Each string sequence separated by a send is another response\n\
  -q, --quit=STRING\n\
     String to send server to initiate a clean close of the connection\n"));
 
@@ -643,10 +855,11 @@
 print_usage (void)
 {
 	printf (_("\
-Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
-  [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
-  [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
-  [-r <refuse state>] [-v] [-4|-6]\n"), progname);
+Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\
+ [[-s <send string>]* [-e <expect string>]*]* [-q <quit string>]\
+ [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\
+ [-r <refuse state>] [-v] [-4|-6] [-P <protocol>] [-X <service>]\n"),
+		progname);
 	printf ("       %s (-h|--help)\n", progname);
 	printf ("       %s (-V|--version)\n", progname);
 }
