File: netutils.c

package info (click to toggle)
icinga 1.0.2-2%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 33,952 kB
  • ctags: 13,294
  • sloc: xml: 154,821; ansic: 99,198; sh: 14,585; sql: 5,852; php: 5,126; perl: 2,838; makefile: 1,268
file content (289 lines) | stat: -rw-r--r-- 6,272 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************
 *
 * NETUTILS.C - Network connection  utility functions for Icinga
 *
 * Copyright (c) 1999,2008 Ethan Galstad (egalstad@nagios.org)
 * Portions Copyright (c) 1999-2008 Nagios Plugin development team
 * Copyright (c) 2009-2010 Icinga Development Team (http://www.icinga.org)
 *
 * License:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *****************************************************************************/

#include "../include/config.h"
#include "../include/common.h"
#include "../include/netutils.h"


/* connect to a TCP socket in nonblocking fashion */
int my_tcp_connect(char *host_name, int port, int *sd, int timeout){
	struct addrinfo hints;
	struct addrinfo *res;
	int result;
	char *port_str=NULL;
	int flags=0;
	fd_set rfds;
	fd_set wfds;
	struct timeval tv;
	int optval;
	socklen_t optlen;

	memset(&hints,0,sizeof(hints));
	hints.ai_family=PF_INET;
	hints.ai_socktype=SOCK_STREAM;

	asprintf(&port_str,"%d",port);
	result=getaddrinfo(host_name,port_str,&hints,&res);
	my_free(port_str);
	
	if(result!=0){
		/*printf("GETADDRINFO: %s (%s) = %s\n",host_name,port_str,gai_strerror(result));*/
		return ERROR;
		}

	/* create a socket */
	*sd=socket(res->ai_family,SOCK_STREAM,res->ai_protocol);
	if(*sd<0){
		freeaddrinfo(res);
		return ERROR;
		}

	/* make socket non-blocking */
	flags=fcntl(*sd,F_GETFL,0);
	fcntl(*sd,F_SETFL,flags|O_NONBLOCK);

	/* attempt to connect */
	result=connect(*sd,res->ai_addr,res->ai_addrlen);

	/* immediately successful connect */
	if(result==0){
		result=OK;
		/*printf("IMMEDIATE SUCCESS\n");*/
		}

	/* connection error */
	else if(result<0 && errno!=EINPROGRESS){
		result=ERROR;
		}

	/* connection in progress - wait for it... */
	else{

		do{
			/* set connection timeout */
			tv.tv_sec=timeout;
			tv.tv_usec=0;

			FD_ZERO(&wfds);
			FD_SET(*sd,&wfds);
			rfds=wfds;

			/* wait for readiness */
			result=select((*sd)+1,&rfds,&wfds,NULL,&tv);

			/*printf("SELECT RESULT: %d\n",result);*/

			/* timeout */
			if(result==0){
				/*printf("TIMEOUT\n");*/
				result=ERROR;
				break;
				}

			/* an error occurred */
			if(result<0 && errno!=EINTR){
				result=ERROR;
				break;
				}

			/* got something - check it */
			else if(result>0){

				/* get socket options to check for errors */
				optlen=sizeof(int);
				if(getsockopt(*sd,SOL_SOCKET,SO_ERROR,(void *)(&optval),&optlen) < 0){ 
					result=ERROR;
					break;
					}
				
				/* an error occurred in the connection */
				if(optval!=0){
					result=ERROR;
					break;
					}

				/* the connection was good! */
				/*
				printf("CONNECT SELECT: ERRNO=%s\n",strerror(errno));
				printf("CONNECT SELECT: OPTVAL=%s\n",strerror(optval));
				*/
				result=OK;
				break;
				}

			/* some other error occurred */
			else{
				result=ERROR;
				break;
				}
			
			}while(1);
		}


	freeaddrinfo(res);

	return result;
	}


/* based on Beej's sendall - thanks Beej! */
int my_sendall(int s, char *buf, int *len, int timeout){
	int total_sent=0;
	int bytes_left=0;
	int n;
	fd_set wfds;
	struct timeval tv;
	int result=OK;
	time_t start_time;
	time_t current_time;

	time(&start_time);

	bytes_left=*len;
	while(total_sent < *len){

		/* set send timeout */
		tv.tv_sec=timeout;
		tv.tv_usec=0;

		FD_ZERO(&wfds);
		FD_SET(s,&wfds);

		/* wait for readiness */
		result=select(s+1,NULL,&wfds,NULL,&tv);

		/* timeout */
		if(result==0){
			/*printf("RECV SELECT TIMEOUT\n");*/
			result=ERROR;
			break;
			}
		/* error */
		else if(result<0){
			/*printf("RECV SELECT ERROR: %s\n",strerror(errno));*/
			result=ERROR;
			break;
			}

		/* we're ready to write some data */
		result=OK;

		/* send the data */
		n=send(s,buf+total_sent,bytes_left,0);
		if(n==-1){
			/*printf("SEND ERROR: (%d) %s\n",s,strerror(errno));*/
			break;
			}

		total_sent+=n;
		bytes_left-=n;

                /* make sure we haven't overrun the timeout */
		time(&current_time);
		if(current_time-start_time>timeout){
			result=ERROR;
			break;
			}
		}

	*len=total_sent;

	return result;
	}


/* receives all data in non-blocking mode with a timeout  - modelled after sendall() */
int my_recvall(int s, char *buf, int *len, int timeout){
        int total_received=0;
        int bytes_left=*len;
        int n=0;
        time_t start_time;
        time_t current_time;
	fd_set rfds;
	struct timeval tv;
	int result=OK;

        /* clear the receive buffer */
        bzero(buf,*len);

        time(&start_time);

        /* receive all data */
        while(total_received<*len){

		/* set receive timeout */
		tv.tv_sec=timeout;
		tv.tv_usec=0;

		FD_ZERO(&rfds);
		FD_SET(s,&rfds);

		/* wait for readiness */
		result=select(s+1,&rfds,NULL,NULL,&tv);

		/* timeout */
		if(result==0){
			/*printf("RECV SELECT TIMEOUT\n");*/
			result=ERROR;
			break;
			}
		/* error */
		else if(result<0){
			/*printf("RECV SELECT ERROR: %s\n",strerror(errno));*/
			result=ERROR;
			break;
			}

		/* we're ready to read some data */
		result=OK;

                /* receive some data */
                n=recv(s,buf+total_received,bytes_left,0);

                /* server disconnected */
                if(n==0){
			/*printf("SERVER DISCONNECT\n");*/
                        break;
			}

                /* apply bytes we received */
                total_received+=n;
                bytes_left-=n;

                /* make sure we haven't overrun the timeout */
		time(&current_time);
		if(current_time-start_time>timeout){
			result=ERROR;
			break;
			}
                }

        /* return number of bytes actually received here */
        *len=total_received;

        return result;
        }