From 662997251d4fc43f4155784f9e7df827f193305e Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Thu, 12 Sep 2013 17:42:10 +0200 Subject: Improve interface of np_expect_match() function Replace the three boolean parameters of lib/utils_tcp.c's np_expect_match() function with a single "flags" parameter. diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 6cf9394..8e9d43c 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c @@ -33,21 +33,21 @@ main (int argc, char **argv) server_expect[1] = strdup("bb"); server_expect[2] = strdup("CC"); - ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, + ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, "Test matching any string at the beginning (first expect string)"); - ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, + ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, "Test matching any string at the beginning (second expect string)"); - ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, + ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, "Test with strings not matching at the beginning"); - ok(np_expect_match("XX CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, + ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, "Test matching any string"); - ok(np_expect_match("XX", server_expect, server_expect_count, FALSE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE, "Test not matching any string"); - ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == TRUE, + ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE, "Test matching all strings"); - ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, "Test not matching all strings"); - ok(np_expect_match("XX XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, "Test not matching any string (testing all)"); diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index 8589ce6..cf67b11 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c @@ -30,26 +30,27 @@ #include "utils_tcp.h" int -np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose) +np_expect_match(char* status, char** server_expect, int expect_count, int flags) { int match = 0; int i; for (i = 0; i < expect_count; i++) { - if (verbose) + if (flags & NP_MATCH_VERBOSE) printf ("looking for [%s] %s [%s]\n", server_expect[i], - (exact_match) ? "in beginning of" : "anywhere in", + (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); - if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || - (! exact_match && strstr(status, server_expect[i]))) + if ((flags & NP_MATCH_EXACT && + !strncmp(status, server_expect[i], strlen(server_expect[i]))) || + (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) { - if(verbose) puts("found it"); + if(flags & NP_MATCH_VERBOSE) puts("found it"); match += 1; } else - if(verbose) puts("couldn't find it"); + if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); } - if ((all == TRUE && match == expect_count) || - (! all && match >= 1)) { + if ((flags & NP_MATCH_ALL && match == expect_count) || + (!(flags & NP_MATCH_ALL) && match >= 1)) { return TRUE; } else return FALSE; diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h index b0eb8be..34b771d 100644 --- a/lib/utils_tcp.h +++ b/lib/utils_tcp.h @@ -1,4 +1,8 @@ /* Header file for utils_tcp */ +#define NP_MATCH_ALL 0x1 +#define NP_MATCH_EXACT 0x2 +#define NP_MATCH_VERBOSE 0x4 + int np_expect_match(char* status, char** server_expect, int server_expect_count, - int all, int exact_match, int verbose); + int flags); diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 9e62638..e8d7ec6 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c @@ -82,15 +82,14 @@ static int sd = 0; #define MAXBUF 1024 static char buffer[MAXBUF]; static int expect_mismatch_state = STATE_WARNING; +static int match_flags = NP_MATCH_EXACT; #define FLAG_SSL 0x01 #define FLAG_VERBOSE 0x02 -#define FLAG_EXACT_MATCH 0x04 -#define FLAG_TIME_WARN 0x08 -#define FLAG_TIME_CRIT 0x10 -#define FLAG_HIDE_OUTPUT 0x20 -#define FLAG_MATCH_ALL 0x40 -static size_t flags = FLAG_EXACT_MATCH; +#define FLAG_TIME_WARN 0x04 +#define FLAG_TIME_CRIT 0x08 +#define FLAG_HIDE_OUTPUT 0x10 +static size_t flags; int main (int argc, char **argv) @@ -296,12 +295,7 @@ main (int argc, char **argv) (int)len + 1, status); while(isspace(status[len])) status[len--] = '\0'; - match = np_expect_match(status, - server_expect, - server_expect_count, - (flags & FLAG_MATCH_ALL ? TRUE : FALSE), - (flags & FLAG_EXACT_MATCH ? TRUE : FALSE), - (flags & FLAG_VERBOSE ? TRUE : FALSE)); + match = np_expect_match(status, server_expect, server_expect_count, match_flags); } if (server_quit != NULL) { @@ -450,6 +444,7 @@ process_arguments (int argc, char **argv) exit (STATE_OK); case 'v': /* verbose mode */ flags |= FLAG_VERBOSE; + match_flags |= NP_MATCH_VERBOSE; break; case '4': address_family = AF_INET; @@ -506,7 +501,7 @@ process_arguments (int argc, char **argv) xasprintf(&server_send, "%s", optarg); break; case 'e': /* expect string (may be repeated) */ - flags &= ~FLAG_EXACT_MATCH; + match_flags &= ~NP_MATCH_EXACT; if (server_expect_count == 0) server_expect = malloc (sizeof (char *) * (++server_expect_count)); else @@ -584,7 +579,7 @@ process_arguments (int argc, char **argv) #endif break; case 'A': - flags |= FLAG_MATCH_ALL; + match_flags |= NP_MATCH_ALL; break; } } -- cgit v0.10-9-g596f