1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
Index: netutils.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/netutils.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 netutils.c
--- netutils.c 28 Feb 2002 06:42:59 -0000 1.1.1.1
+++ netutils.c 11 Dec 2002 02:38:43 -0000
@@ -42,13 +42,11 @@
int process_tcp_request2 (char *, int, char *, char *, int);
int process_tcp_request (char *, int, char *, char *, int);
int process_udp_request (char *, int, char *, char *, int);
-int process_request (char *, int, char *, char *, char *, int);
+int process_request (char *, int, int, char *, char *, int);
int my_tcp_connect (char *, int, int *);
int my_udp_connect (char *, int, int *);
-int my_connect (char *, int, int *, char *);
-
-int my_inet_aton (register const char *, struct in_addr *);
+int my_connect (char *, int, int *, int);
/* handles socket timeouts */
void
@@ -64,47 +62,29 @@
/* connects to a host on a specified TCP port, sends a string,
and gets a response */
int
-process_tcp_request (char *server_address,
- int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_tcp_request (char *server_address, int server_port,
+ char *send_buffer, char *recv_buffer, int recv_size)
{
- int result;
- char proto[4] = "tcp";
-
- result = process_request (server_address,
- server_port,
- proto, send_buffer, recv_buffer, recv_size);
-
- return result;
+ return process_request (server_address, server_port, IPPROTO_TCP,
+ send_buffer, recv_buffer, recv_size);
}
-
/* connects to a host on a specified UDP port, sends a string, and gets a
response */
int
-process_udp_request (char *server_address,
- int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_udp_request (char *server_address, int server_port,
+ char *send_buffer, char *recv_buffer, int recv_size)
{
- int result;
- char proto[4] = "udp";
-
- result = process_request (server_address,
- server_port,
- proto, send_buffer, recv_buffer, recv_size);
-
- return result;
+ return process_request (server_address, server_port, IPPROTO_UDP,
+ send_buffer, recv_buffer, recv_size);
}
-
-
/* connects to a host on a specified tcp port, sends a string, and gets a
response. loops on select-recv until timeout or eof to get all of a
multi-packet answer */
int
-process_tcp_request2 (char *server_address,
- int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_tcp_request2 (char *server_address, int server_port,
+ char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
@@ -115,7 +95,7 @@
fd_set readfds;
int recv_length = 0;
- result = my_connect (server_address, server_port, &sd, "tcp");
+ result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
if (result != STATE_OK)
return STATE_CRITICAL;
@@ -149,24 +129,30 @@
else { /* it has */
recv_result =
recv (sd, recv_buffer + recv_length, recv_size - recv_length - 1, 0);
- if (recv_result == -1) { /* recv failed, bail out */
+ if (recv_result == -1) {
+ /* recv failed, bail out */
strcpy (recv_buffer + recv_length, "");
result = STATE_WARNING;
break;
}
- else if (recv_result == 0) { /* end of file ? */
+ else if (recv_result == 0) {
+ /* end of file ? */
recv_buffer[recv_length] = 0;
break;
}
- else { /* we got data! */
+ else {
+ /* we got data! */
recv_length += recv_result;
- if (recv_length >= recv_size - 1) { /* buffer full, we're done */
+ if (recv_length >= recv_size - 1) {
+ /* buffer full, we're done */
recv_buffer[recv_size - 1] = 0;
break;
}
}
- } /* end if(!FD_ISSET(sd,&readfds)) */
- } /* end while(1) */
+ }
+ /* end if(!FD_ISSET(sd,&readfds)) */
+ }
+ /* end while(1) */
close (sd);
return result;
@@ -175,10 +161,8 @@
/* connects to a host on a specified port, sends a string, and gets a
response */
int
-process_request (char *server_address,
- int server_port,
- char *proto,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_request (char *server_address, int server_port, int proto,
+ char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
int send_result;
@@ -218,7 +202,7 @@
recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
if (recv_result == -1) {
strcpy (recv_buffer, "");
- if (!strcmp (proto, "tcp"))
+ if (proto != IPPROTO_TCP)
printf ("recv() failed\n");
result = STATE_WARNING;
}
@@ -236,196 +220,74 @@
/* opens a connection to a remote host/tcp port */
+
int
my_tcp_connect (char *host_name, int port, int *sd)
{
- int result;
- char proto[4] = "tcp";
-
- result = my_connect (host_name, port, sd, proto);
-
- return result;
+ return my_connect (host_name, port, sd, IPPROTO_TCP);
}
-
/* opens a connection to a remote host/udp port */
int
my_udp_connect (char *host_name, int port, int *sd)
{
- int result;
- char proto[4] = "udp";
-
- result = my_connect (host_name, port, sd, proto);
-
- return result;
+ return my_connect (host_name, port, sd, IPPROTO_UDP);
}
/* opens a tcp or udp connection to a remote host */
int
-my_connect (char *host_name, int port, int *sd, char *proto)
-{
- struct sockaddr_in servaddr;
- struct hostent *hp;
- struct protoent *ptrp;
- int result;
-
- bzero ((char *) &servaddr, sizeof (servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons (port);
-
- /* try to bypass using a DNS lookup if this is just an IP address */
- if (!my_inet_aton (host_name, &servaddr.sin_addr)) {
-
- /* else do a DNS lookup */
- hp = gethostbyname ((const char *) host_name);
- if (hp == NULL) {
- printf ("Invalid host name '%s'\n", host_name);
- return STATE_UNKNOWN;
- }
-
- memcpy (&servaddr.sin_addr, hp->h_addr, hp->h_length);
- }
-
- /* map transport protocol name to protocol number */
- if ((ptrp = getprotobyname (proto)) == NULL) {
- printf ("Cannot map \"%s\" to protocol number\n", proto);
- return STATE_UNKNOWN;
- }
-
- /* create a socket */
- *sd =
- socket (PF_INET, (!strcmp (proto, "udp")) ? SOCK_DGRAM : SOCK_STREAM,
- ptrp->p_proto);
- if (*sd < 0) {
- printf ("Socket creation failed\n");
- return STATE_UNKNOWN;
- }
-
- /* open a connection */
- result = connect (*sd, (struct sockaddr *) &servaddr, sizeof (servaddr));
- if (result < 0) {
- switch (errno) {
- case ECONNREFUSED:
- printf ("Connection refused by host\n");
- break;
- case ETIMEDOUT:
- printf ("Timeout while attempting connection\n");
- break;
- case ENETUNREACH:
- printf ("Network is unreachable\n");
- break;
- default:
- printf ("Connection refused or timed out\n");
- }
-
- return STATE_CRITICAL;
- }
-
- return STATE_OK;
-}
-
-
-
-/* This code was taken from Fyodor's nmap utility, which was originally
- taken from the GLIBC 2.0.6 libraries because Solaris doesn't contain
- the inet_aton() funtion. */
-int
-my_inet_aton (register const char *cp, struct in_addr *addr)
+my_connect (char *host_name, int port, int *sd, int proto)
{
- register unsigned int val; /* changed from u_long --david */
- register int base, n;
- register char c;
- u_int parts[4];
- register u_int *pp = parts;
-
- c = *cp;
-
- for (;;) {
-
- /*
- * Collect number up to ``.''.
- * Values are specified as for C:
- * 0x=hex, 0=octal, isdigit=decimal.
- */
- if (!isdigit ((int) c))
- return (0);
- val = 0;
- base = 10;
-
- if (c == '0') {
- c = *++cp;
- if (c == 'x' || c == 'X')
- base = 16, c = *++cp;
- else
- base = 8;
- }
-
- for (;;) {
- if (isascii ((int) c) && isdigit ((int) c)) {
- val = (val * base) + (c - '0');
- c = *++cp;
- }
- else if (base == 16 && isascii ((int) c) && isxdigit ((int) c)) {
- val = (val << 4) | (c + 10 - (islower ((int) c) ? 'a' : 'A'));
- c = *++cp;
- }
- else
- break;
- }
-
- if (c == '.') {
-
- /*
- * Internet format:
- * a.b.c.d
- * a.b.c (with c treated as 16 bits)
- * a.b (with b treated as 24 bits)
- */
- if (pp >= parts + 3)
- return (0);
- *pp++ = val;
- c = *++cp;
- }
- else
- break;
- }
-
- /* Check for trailing characters */
- if (c != '\0' && (!isascii ((int) c) || !isspace ((int) c)))
- return (0);
-
- /* Concoct the address according to the number of parts specified */
- n = pp - parts + 1;
- switch (n) {
-
- case 0:
- return (0); /* initial nondigit */
-
- case 1: /* a -- 32 bits */
- break;
-
- case 2: /* a.b -- 8.24 bits */
- if (val > 0xffffff)
- return (0);
- val |= parts[0] << 24;
- break;
-
- case 3: /* a.b.c -- 8.8.16 bits */
- if (val > 0xffff)
- return (0);
- val |= (parts[0] << 24) | (parts[1] << 16);
- break;
-
- case 4: /* a.b.c.d -- 8.8.8.8 bits */
- if (val > 0xff)
- return (0);
- val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
- break;
- }
-
- if (addr)
- addr->s_addr = htonl (val);
-
- return (1);
+ struct addrinfo hints;
+ struct addrinfo *res;
+ struct protoent *ptrp;
+ char port_str[6];
+ int result;
+
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_protocol = proto;
+
+ snprintf(port_str, sizeof(port_str), "%d", port);
+ /* nice clean AF-independent code doesn't matter if passed hostname or IP address */
+ result = getaddrinfo (host_name, port_str, &hints, &res);
+
+ if (result != 0)
+ {
+ printf ("%s\n", gai_strerror (result));
+ return STATE_UNKNOWN;
+ }
+ else
+ {
+ while (res)
+ {
+ /* attempt to create a socket */
+ *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
+
+ if (*sd < 0) {
+ printf ("Socket creation failed\n");
+ freeaddrinfo (res);
+ return STATE_UNKNOWN;
+ }
+
+ /* attempt to open a connection */
+ result = connect (*sd, res->ai_addr, res->ai_addrlen);
+
+ if (result == 0)
+ break;
+
+ close(*sd);
+ res = res->ai_next;
+ }
+ freeaddrinfo (res);
+ }
+
+ if (result == 0)
+ return STATE_OK;
+ else
+ {
+ printf ("%s\n", strerror(errno));
+ return STATE_CRITICAL;
+ }
}
Index: netutils.h.in
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/netutils.h.in,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 netutils.h.in
--- netutils.h.in 28 Feb 2002 06:42:59 -0000 1.1.1.1
+++ netutils.h.in 11 Dec 2002 02:38:43 -0000
@@ -42,19 +42,17 @@
void socket_timeout_alarm_handler (int);
int process_tcp_request2 (char *address, int port, char *sbuffer,
- char *rbuffer, int rsize);
+ char *rbuffer, int rsize);
int process_tcp_request (char *address, int port, char *sbuffer,
- char *rbuffer, int rsize);
+ char *rbuffer, int rsize);
int process_udp_request (char *address, int port, char *sbuffer,
- char *rbuffer, int rsize);
-int process_request (char *address, int port, char *proto, char *sbuffer,
- char *rbuffer, int rsize);
+ char *rbuffer, int rsize);
+int process_request (char *address, int port, int proto, char *sbuffer,
+ char *rbuffer, int rsize);
int my_tcp_connect (char *address, int port, int *sd);
int my_udp_connect (char *address, int port, int *sd);
-int my_connect (char *address, int port, int *sd, char *proto);
-
-int my_inet_aton (register const char *cp, struct in_addr *addr);
+int my_connect (char *address, int port, int *sd, int proto);
#ifndef DEFAULT_SOCKET_TIMEOUT
#include "config.h"
|