diff options
| -rw-r--r-- | plugins/check_tcp.c | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index d2ebc16d..8cd86460 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
| @@ -43,20 +43,22 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 43 | #include <ctype.h> | 43 | #include <ctype.h> |
| 44 | #include <sys/select.h> | 44 | #include <sys/select.h> |
| 45 | 45 | ||
| 46 | ssize_t my_recv(char *buf, size_t len) { | 46 | ssize_t my_recv(int socket_descriptor, char *buf, size_t len, bool use_tls) { |
| 47 | #ifdef HAVE_SSL | 47 | #ifdef HAVE_SSL |
| 48 | return np_net_ssl_read(buf, (int)len); | 48 | if (use_tls) { |
| 49 | #else | 49 | return np_net_ssl_read(buf, (int)len); |
| 50 | } | ||
| 51 | #endif | ||
| 50 | return read(socket_descriptor, buf, len); | 52 | return read(socket_descriptor, buf, len); |
| 51 | #endif // HAVE_SSL | ||
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | ssize_t my_send(char *buf, size_t len) { | 55 | ssize_t my_send(int socket_descriptor, char *buf, size_t len, bool use_tls) { |
| 55 | #ifdef HAVE_SSL | 56 | #ifdef HAVE_SSL |
| 56 | return np_net_ssl_write(buf, (int)len); | 57 | if (use_tls) { |
| 57 | #else | 58 | return np_net_ssl_write(buf, (int)len); |
| 59 | } | ||
| 60 | #endif | ||
| 58 | return write(socket_descriptor, buf, len); | 61 | return write(socket_descriptor, buf, len); |
| 59 | #endif // HAVE_SSL | ||
| 60 | } | 62 | } |
| 61 | 63 | ||
| 62 | typedef struct { | 64 | typedef struct { |
| @@ -302,7 +304,7 @@ int main(int argc, char **argv) { | |||
| 302 | #endif /* HAVE_SSL */ | 304 | #endif /* HAVE_SSL */ |
| 303 | 305 | ||
| 304 | if (config.send != NULL) { /* Something to send? */ | 306 | if (config.send != NULL) { /* Something to send? */ |
| 305 | my_send(config.send, strlen(config.send)); | 307 | my_send(socket_descriptor, config.send, strlen(config.send), config.use_tls); |
| 306 | } | 308 | } |
| 307 | 309 | ||
| 308 | if (config.delay > 0) { | 310 | if (config.delay > 0) { |
| @@ -325,8 +327,8 @@ int main(int argc, char **argv) { | |||
| 325 | 327 | ||
| 326 | /* if(len) later on, we know we have a non-NULL response */ | 328 | /* if(len) later on, we know we have a non-NULL response */ |
| 327 | ssize_t len = 0; | 329 | ssize_t len = 0; |
| 328 | char *status = NULL; | 330 | char *received_buffer = NULL; |
| 329 | int match = -1; | 331 | enum np_match_result match = NP_MATCH_NONE; |
| 330 | mp_subcheck expected_data_result = mp_subcheck_init(); | 332 | mp_subcheck expected_data_result = mp_subcheck_init(); |
| 331 | 333 | ||
| 332 | if (config.server_expect_count) { | 334 | if (config.server_expect_count) { |
| @@ -334,23 +336,24 @@ int main(int argc, char **argv) { | |||
| 334 | char buffer[MAXBUF]; | 336 | char buffer[MAXBUF]; |
| 335 | 337 | ||
| 336 | /* watch for the expect string */ | 338 | /* watch for the expect string */ |
| 337 | while ((received = my_recv(buffer, sizeof(buffer))) > 0) { | 339 | while ((received = my_recv(socket_descriptor, buffer, sizeof(buffer), config.use_tls)) > 0) { |
| 338 | status = realloc(status, len + received + 1); | 340 | received_buffer = realloc(received_buffer, len + received + 1); |
| 339 | 341 | ||
| 340 | if (status == NULL) { | 342 | if (received_buffer == NULL) { |
| 341 | die(STATE_UNKNOWN, _("Allocation failed")); | 343 | die(STATE_UNKNOWN, _("Allocation failed")); |
| 342 | } | 344 | } |
| 343 | 345 | ||
| 344 | memcpy(&status[len], buffer, received); | 346 | memcpy(&received_buffer[len], buffer, received); |
| 345 | len += received; | 347 | len += received; |
| 346 | status[len] = '\0'; | 348 | received_buffer[len] = '\0'; |
| 347 | 349 | ||
| 348 | /* stop reading if user-forced */ | 350 | /* stop reading if user-forced */ |
| 349 | if (config.maxbytes && len >= config.maxbytes) { | 351 | if (config.maxbytes && len >= config.maxbytes) { |
| 350 | break; | 352 | break; |
| 351 | } | 353 | } |
| 352 | 354 | ||
| 353 | if ((match = np_expect_match(status, config.server_expect, config.server_expect_count, config.match_flags)) != NP_MATCH_RETRY) { | 355 | if ((match = np_expect_match(received_buffer, config.server_expect, config.server_expect_count, config.match_flags)) != |
| 356 | NP_MATCH_RETRY) { | ||
| 354 | break; | 357 | break; |
| 355 | } | 358 | } |
| 356 | 359 | ||
| @@ -382,16 +385,16 @@ int main(int argc, char **argv) { | |||
| 382 | 385 | ||
| 383 | /* print raw output if we're debugging */ | 386 | /* print raw output if we're debugging */ |
| 384 | if (verbosity > 0) { | 387 | if (verbosity > 0) { |
| 385 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", (int)len + 1, status); | 388 | printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", (int)len + 1, received_buffer); |
| 386 | } | 389 | } |
| 387 | /* strip whitespace from end of output */ | 390 | /* strip whitespace from end of output */ |
| 388 | while (--len > 0 && isspace(status[len])) { | 391 | while (--len > 0 && isspace(received_buffer[len])) { |
| 389 | status[len] = '\0'; | 392 | received_buffer[len] = '\0'; |
| 390 | } | 393 | } |
| 391 | } | 394 | } |
| 392 | 395 | ||
| 393 | if (config.quit != NULL) { | 396 | if (config.quit != NULL) { |
| 394 | my_send(config.quit, strlen(config.quit)); | 397 | my_send(socket_descriptor, config.quit, strlen(config.quit), config.use_tls); |
| 395 | } | 398 | } |
| 396 | 399 | ||
| 397 | if (socket_descriptor) { | 400 | if (socket_descriptor) { |
