summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_tcp.c38
-rw-r--r--plugins/netutils.c98
-rw-r--r--plugins/netutils.h32
3 files changed, 37 insertions, 131 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index fd2fe66b..1bf1f4cb 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -57,11 +57,7 @@ int connect_SSL (void);
57int check_certificate (X509 **); 57int check_certificate (X509 **);
58#endif 58#endif
59 59
60enum { 60#define MAXBUF 1024
61 TCP_PROTOCOL = 1,
62 UDP_PROTOCOL = 2,
63 MAXBUF = 1024
64};
65 61
66int process_arguments (int, char **); 62int process_arguments (int, char **);
67int my_recv (void); 63int my_recv (void);
@@ -120,7 +116,7 @@ main (int argc, char **argv)
120 SEND = NULL; 116 SEND = NULL;
121 EXPECT = NULL; 117 EXPECT = NULL;
122 QUIT = NULL; 118 QUIT = NULL;
123 PROTOCOL = UDP_PROTOCOL; 119 PROTOCOL = IPPROTO_UDP;
124 PORT = 0; 120 PORT = 0;
125 } 121 }
126 else if (strstr (argv[0], "check_tcp")) { 122 else if (strstr (argv[0], "check_tcp")) {
@@ -129,7 +125,7 @@ main (int argc, char **argv)
129 SEND = NULL; 125 SEND = NULL;
130 EXPECT = NULL; 126 EXPECT = NULL;
131 QUIT = NULL; 127 QUIT = NULL;
132 PROTOCOL = TCP_PROTOCOL; 128 PROTOCOL = IPPROTO_TCP;
133 PORT = 0; 129 PORT = 0;
134 } 130 }
135 else if (strstr (argv[0], "check_ftp")) { 131 else if (strstr (argv[0], "check_ftp")) {
@@ -138,7 +134,7 @@ main (int argc, char **argv)
138 SEND = NULL; 134 SEND = NULL;
139 EXPECT = strdup ("220"); 135 EXPECT = strdup ("220");
140 QUIT = strdup ("QUIT\r\n"); 136 QUIT = strdup ("QUIT\r\n");
141 PROTOCOL = TCP_PROTOCOL; 137 PROTOCOL = IPPROTO_TCP;
142 PORT = 21; 138 PORT = 21;
143 } 139 }
144 else if (strstr (argv[0], "check_smtp")) { 140 else if (strstr (argv[0], "check_smtp")) {
@@ -147,7 +143,7 @@ main (int argc, char **argv)
147 SEND = NULL; 143 SEND = NULL;
148 EXPECT = strdup ("220"); 144 EXPECT = strdup ("220");
149 QUIT = strdup ("QUIT\r\n"); 145 QUIT = strdup ("QUIT\r\n");
150 PROTOCOL = TCP_PROTOCOL; 146 PROTOCOL = IPPROTO_TCP;
151 PORT = 25; 147 PORT = 25;
152 } 148 }
153 else if (strstr (argv[0], "check_pop")) { 149 else if (strstr (argv[0], "check_pop")) {
@@ -156,7 +152,7 @@ main (int argc, char **argv)
156 SEND = NULL; 152 SEND = NULL;
157 EXPECT = strdup ("+OK"); 153 EXPECT = strdup ("+OK");
158 QUIT = strdup ("QUIT\r\n"); 154 QUIT = strdup ("QUIT\r\n");
159 PROTOCOL = TCP_PROTOCOL; 155 PROTOCOL = IPPROTO_TCP;
160 PORT = 110; 156 PORT = 110;
161 } 157 }
162 else if (strstr (argv[0], "check_imap")) { 158 else if (strstr (argv[0], "check_imap")) {
@@ -165,7 +161,7 @@ main (int argc, char **argv)
165 SEND = NULL; 161 SEND = NULL;
166 EXPECT = strdup ("* OK"); 162 EXPECT = strdup ("* OK");
167 QUIT = strdup ("a1 LOGOUT\r\n"); 163 QUIT = strdup ("a1 LOGOUT\r\n");
168 PROTOCOL = TCP_PROTOCOL; 164 PROTOCOL = IPPROTO_TCP;
169 PORT = 143; 165 PORT = 143;
170 } 166 }
171#ifdef HAVE_SSL 167#ifdef HAVE_SSL
@@ -175,7 +171,7 @@ main (int argc, char **argv)
175 SEND=NULL; 171 SEND=NULL;
176 EXPECT = strdup ("* OK"); 172 EXPECT = strdup ("* OK");
177 QUIT = strdup ("a1 LOGOUT\r\n"); 173 QUIT = strdup ("a1 LOGOUT\r\n");
178 PROTOCOL=TCP_PROTOCOL; 174 PROTOCOL=IPPROTO_TCP;
179 use_ssl=TRUE; 175 use_ssl=TRUE;
180 PORT=993; 176 PORT=993;
181 } 177 }
@@ -185,7 +181,7 @@ main (int argc, char **argv)
185 SEND=NULL; 181 SEND=NULL;
186 EXPECT = strdup ("+OK"); 182 EXPECT = strdup ("+OK");
187 QUIT = strdup ("QUIT\r\n"); 183 QUIT = strdup ("QUIT\r\n");
188 PROTOCOL=TCP_PROTOCOL; 184 PROTOCOL=IPPROTO_TCP;
189 use_ssl=TRUE; 185 use_ssl=TRUE;
190 PORT=995; 186 PORT=995;
191 } 187 }
@@ -195,7 +191,7 @@ main (int argc, char **argv)
195 SEND=NULL; 191 SEND=NULL;
196 EXPECT = strdup ("220"); 192 EXPECT = strdup ("220");
197 QUIT = strdup ("QUIT\r\n"); 193 QUIT = strdup ("QUIT\r\n");
198 PROTOCOL=TCP_PROTOCOL; 194 PROTOCOL=IPPROTO_TCP;
199 use_ssl=TRUE; 195 use_ssl=TRUE;
200 PORT=465; 196 PORT=465;
201 } 197 }
@@ -205,7 +201,7 @@ main (int argc, char **argv)
205 SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n"); 201 SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n");
206 EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'"); 202 EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'");
207 QUIT = strdup("</stream:stream>\n"); 203 QUIT = strdup("</stream:stream>\n");
208 PROTOCOL=TCP_PROTOCOL; 204 PROTOCOL=IPPROTO_TCP;
209 use_ssl=TRUE; 205 use_ssl=TRUE;
210 PORT = 5222; 206 PORT = 5222;
211 } 207 }
@@ -219,7 +215,7 @@ main (int argc, char **argv)
219 server_expect = realloc (server_expect, ++server_expect_count); 215 server_expect = realloc (server_expect, ++server_expect_count);
220 asprintf (&server_expect[server_expect_count - 1], "201"); 216 asprintf (&server_expect[server_expect_count - 1], "201");
221 QUIT = strdup("QUIT\r\n"); 217 QUIT = strdup("QUIT\r\n");
222 PROTOCOL = TCP_PROTOCOL; 218 PROTOCOL = IPPROTO_TCP;
223 use_ssl=TRUE; 219 use_ssl=TRUE;
224 PORT = 563; 220 PORT = 563;
225} 221}
@@ -235,7 +231,7 @@ main (int argc, char **argv)
235 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count)); 231 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
236 asprintf (&server_expect[server_expect_count - 1], "201"); 232 asprintf (&server_expect[server_expect_count - 1], "201");
237 asprintf (&QUIT, "QUIT\r\n"); 233 asprintf (&QUIT, "QUIT\r\n");
238 PROTOCOL = TCP_PROTOCOL; 234 PROTOCOL = IPPROTO_TCP;
239 PORT = 119; 235 PORT = 119;
240 } 236 }
241 else { 237 else {
@@ -288,13 +284,7 @@ main (int argc, char **argv)
288 result = connect_SSL (); 284 result = connect_SSL ();
289 else 285 else
290#endif 286#endif
291 { 287 result = my_connect (server_address, server_port, &sd, PROTOCOL);
292 if (PROTOCOL == UDP_PROTOCOL)
293 result = my_udp_connect (server_address, server_port, &sd);
294 else
295 /* default is TCP */
296 result = my_tcp_connect (server_address, server_port, &sd);
297 }
298 288
299 if (result == STATE_CRITICAL) 289 if (result == STATE_CRITICAL)
300 return STATE_CRITICAL; 290 return STATE_CRITICAL;
diff --git a/plugins/netutils.c b/plugins/netutils.c
index 1231c1ef..e7d5ba78 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -39,7 +39,6 @@ int econn_refuse_state = STATE_CRITICAL;
39int was_refused = FALSE; 39int was_refused = FALSE;
40int address_family = AF_UNSPEC; 40int address_family = AF_UNSPEC;
41 41
42static int my_connect(const char *address, int port, int *sd, int proto);
43/* handles socket timeouts */ 42/* handles socket timeouts */
44void 43void
45socket_timeout_alarm_handler (int sig) 44socket_timeout_alarm_handler (int sig)
@@ -53,37 +52,6 @@ socket_timeout_alarm_handler (int sig)
53} 52}
54 53
55 54
56/* connects to a host on a specified TCP port, sends a string,
57 and gets a response */
58int
59process_tcp_request (const char *server_address, int server_port,
60 const char *send_buffer, char *recv_buffer, int recv_size)
61{
62 int result;
63
64 result = process_request (server_address, server_port,
65 IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
66
67 return result;
68}
69
70
71/* connects to a host on a specified UDP port, sends a string, and gets a
72 response */
73int
74process_udp_request (const char *server_address, int server_port,
75 const char *send_buffer, char *recv_buffer, int recv_size)
76{
77 int result;
78
79 result = process_request (server_address, server_port,
80 IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
81
82 return result;
83}
84
85
86
87/* connects to a host on a specified tcp port, sends a string, and gets a 55/* connects to a host on a specified tcp port, sends a string, and gets a
88 response. loops on select-recv until timeout or eof to get all of a 56 response. loops on select-recv until timeout or eof to get all of a
89 multi-packet answer */ 57 multi-packet answer */
@@ -163,6 +131,7 @@ process_tcp_request2 (const char *server_address, int server_port,
163 return result; 131 return result;
164} 132}
165 133
134
166/* connects to a host on a specified port, sends a string, and gets a 135/* connects to a host on a specified port, sends a string, and gets a
167 response */ 136 response */
168int 137int
@@ -186,32 +155,8 @@ process_request (const char *server_address, int server_port, int proto,
186} 155}
187 156
188 157
189/* opens a connection to a remote host/tcp port */
190int
191my_tcp_connect (const char *host_name, int port, int *sd)
192{
193 int result;
194
195 result = my_connect (host_name, port, sd, IPPROTO_TCP);
196
197 return result;
198}
199
200
201/* opens a connection to a remote host/udp port */
202int
203my_udp_connect (const char *host_name, int port, int *sd)
204{
205 int result;
206
207 result = my_connect (host_name, port, sd, IPPROTO_UDP);
208
209 return result;
210}
211
212
213/* opens a tcp or udp connection to a remote host */ 158/* opens a tcp or udp connection to a remote host */
214static int 159int
215my_connect (const char *host_name, int port, int *sd, int proto) 160my_connect (const char *host_name, int port, int *sd, int proto)
216{ 161{
217 struct addrinfo hints; 162 struct addrinfo hints;
@@ -291,20 +236,6 @@ my_connect (const char *host_name, int port, int *sd, int proto)
291 236
292 237
293int 238int
294send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
295{
296 return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
297}
298
299
300int
301send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
302{
303 return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
304}
305
306
307int
308send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) 239send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
309{ 240{
310 int result = STATE_OK; 241 int result = STATE_OK;
@@ -397,28 +328,3 @@ resolve_host_or_addr (const char *address, int family)
397 return TRUE; 328 return TRUE;
398 } 329 }
399} 330}
400
401int
402is_inet_addr (const char *address)
403{
404 return resolve_host_or_addr (address, AF_INET);
405}
406
407#ifdef USE_IPV6
408int
409is_inet6_addr (const char *address)
410{
411 return resolve_host_or_addr (address, AF_INET6);
412}
413#endif
414
415int
416is_hostname (const char *s1)
417{
418#ifdef USE_IPV6
419 return resolve_host_or_addr (s1, address_family);
420#else
421 return resolve_host_or_addr (s1, AF_INET);
422#endif
423}
424
diff --git a/plugins/netutils.h b/plugins/netutils.h
index 2f31bdc8..bd532011 100644
--- a/plugins/netutils.h
+++ b/plugins/netutils.h
@@ -38,30 +38,40 @@
38 38
39RETSIGTYPE socket_timeout_alarm_handler (int) __attribute__((noreturn)); 39RETSIGTYPE socket_timeout_alarm_handler (int) __attribute__((noreturn));
40 40
41/* process_request and wrapper macros */
42#define process_tcp_request(addr, port, sbuf, rbuf, rsize) \
43 process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize)
44#define process_udp_request(addr, port, sbuf, rbuf, rsize) \
45 process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize)
41int process_tcp_request2 (const char *address, int port, 46int process_tcp_request2 (const char *address, int port,
42 const char *sbuffer, char *rbuffer, int rsize); 47 const char *sbuffer, char *rbuffer, int rsize);
43int process_tcp_request (const char *address, int port,
44 const char *sbuffer, char *rbuffer, int rsize);
45int process_udp_request (const char *address, int port,
46 const char *sbuffer, char *rbuffer, int rsize);
47int process_request (const char *address, int port, int proto, 48int process_request (const char *address, int port, int proto,
48 const char *sbuffer, char *rbuffer, int rsize); 49 const char *sbuffer, char *rbuffer, int rsize);
49 50
50int my_tcp_connect (const char *address, int port, int *sd); 51/* my_connect and wrapper macros */
51int my_udp_connect (const char *address, int port, int *sd); 52#define my_tcp_connect(addr, port, s) my_connect(addr, port, s, IPPROTO_TCP)
53#define my_udp_connect(addr, port, s) my_connect(addr, port, s, IPPROTO_UDP)
54int my_connect(const char *address, int port, int *sd, int proto);
52 55
53int send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size); 56/* send_request and wrapper macros */
54int send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size); 57#define send_tcp_request(s, sbuf, rbuf, rsize) \
58 send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize)
59#define send_udp_request(s, sbuf, rbuf, rsize) \
60 send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize)
55int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size); 61int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size);
56 62
63
64/* "is_*" wrapper macros and functions */
57int is_host (const char *); 65int is_host (const char *);
58int is_addr (const char *); 66int is_addr (const char *);
59int resolve_host_or_addr (const char *, int); 67int resolve_host_or_addr (const char *, int);
60int is_inet_addr (const char *); 68#define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET)
61#ifdef USE_IPV6 69#ifdef USE_IPV6
62int is_inet6_addr (const char *); 70# define is_inet6_addr(addr) resolve_host_or_addr(addr, AF_INET6)
71# define is_hostname(addr) resolve_host_or_addr(addr, address_family)
72#else
73# define is_hostname(addr) resolve_host_or_addr(addr, AF_INET)
63#endif 74#endif
64int is_hostname (const char *);
65 75
66extern unsigned int socket_timeout; 76extern unsigned int socket_timeout;
67extern int econn_refuse_state; 77extern int econn_refuse_state;