4
0

utils_tcp.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*****************************************************************************
  2. *
  3. * Library for check_tcp
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains utilities for check_tcp. These are tested by libtap
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *
  27. *****************************************************************************/
  28. #include "common.h"
  29. #include "utils_tcp.h"
  30. #define VERBOSE(message) \
  31. do { \
  32. if (flags & NP_MATCH_VERBOSE) \
  33. puts(message); \
  34. } while (0)
  35. enum np_match_result
  36. np_expect_match(char *status, char **server_expect, int expect_count, int flags)
  37. {
  38. int i, match = 0, partial = 0;
  39. for (i = 0; i < expect_count; i++) {
  40. if (flags & NP_MATCH_VERBOSE)
  41. printf("looking for [%s] %s [%s]\n", server_expect[i],
  42. (flags & NP_MATCH_EXACT) ?
  43. "in beginning of" : "anywhere in",
  44. status);
  45. if (flags & NP_MATCH_EXACT) {
  46. if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) {
  47. VERBOSE("found it");
  48. match++;
  49. continue;
  50. } else if (strncmp(status, server_expect[i], strlen(status)) == 0) {
  51. VERBOSE("found a substring");
  52. partial++;
  53. continue;
  54. }
  55. } else if (strstr(status, server_expect[i]) != NULL) {
  56. VERBOSE("found it");
  57. match++;
  58. continue;
  59. }
  60. VERBOSE("couldn't find it");
  61. }
  62. if ((flags & NP_MATCH_ALL && match == expect_count) ||
  63. (!(flags & NP_MATCH_ALL) && match >= 1))
  64. return NP_MATCH_SUCCESS;
  65. else if (partial > 0 || !(flags & NP_MATCH_EXACT))
  66. return NP_MATCH_RETRY;
  67. else
  68. return NP_MATCH_FAILURE;
  69. }