summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxcomms/comms.c
blob: c58c524cc758dfb5a1c00334500ee0608c710f79 (plain)
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
/* 
** ZABBIX
** Copyright (C) 2000-2006 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <sys/wait.h>

#include <string.h>

#ifdef HAVE_NETDB_H
	#include <netdb.h>
#endif

/* Required for getpwuid */
#include <pwd.h>

#include <signal.h>
#include <errno.h>

#include <time.h>

#include "common.h"
#include "comms.h"
#include "zbxsock.h"

void	zbx_tcp_init(zbx_sock_t *s)
{
	memset(s, 0, sizeof(zbx_sock_t));
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_tcp_connect                                                  *
 *                                                                            *
 * Purpose: connect to external host                                          *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value: sockfd - open socket                                         * 
 *               FAIL - an error occured                                      *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int     zbx_tcp_connect(zbx_sock_t *s, char *ip, int port)
{
	struct	sockaddr_in myaddr_in;
	struct	sockaddr_in servaddr_in;
	struct	hostent *hp;

	memset(s, 0, sizeof(zbx_sock_t));

	servaddr_in.sin_family=AF_INET;

	if(NULL == (hp = zbx_gethost(ip)))
	{
/*		zabbix_log( LOG_LEVEL_WARNING, "Cannot resolve [%s]", ip); */
		return	FAIL;
	}

	servaddr_in.sin_addr.s_addr=((struct in_addr *)(hp->h_addr))->s_addr;

	servaddr_in.sin_port=htons(port);

	s->socket = socket(AF_INET,SOCK_STREAM,0);
	if(s->socket == -1)
	{
/*		zabbix_log( LOG_LEVEL_WARNING, "Cannot create socket [%s:%d] [%s]", ip, port ,strerror(errno)); */
		return	FAIL;
	}

	myaddr_in.sin_family = AF_INET;
	myaddr_in.sin_port=0;
	myaddr_in.sin_addr.s_addr=INADDR_ANY;

	if( connect(s->socket,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)) == -1 )
	{
/*		zabbix_log( LOG_LEVEL_WARNING, "Cannot connect to [%s:%d] [%s]", ip, port, strerror(errno)); */
		zbx_tcp_close(s);
		return	FAIL;
	}

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_tcp_send                                                     *
 *                                                                            *
 * Purpose: send data                                                         *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value: SUCCEED - success                                            * 
 *               FAIL - an error occured                                      *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int     zbx_tcp_send(zbx_sock_t *s, char *data)
{
	ssize_t	i;
	char	header[5]="ZBXD\1";
	zbx_uint64_t	len64;
	ssize_t	written = 0;

	/* Write header */
	i=write(s->socket, header, 5);
	if(i == -1)
	{
		zbx_tcp_close(s);
		return	FAIL;
	}
	len64 = (zbx_uint64_t)strlen(data);

	/* Write data length */
	i=write(s->socket, &len64, sizeof(len64));
	if(i == -1)
	{
		zbx_tcp_close(s);
		return	FAIL;
	}

	while(written<strlen(data))
	{
		i=write(s->socket, data+written,strlen(data)-written);
		if(i == -1)
		{
			zbx_tcp_close(s);
			return	FAIL;
		}
		written+=i;
	}

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_tcp_close                                                    *
 *                                                                            *
 * Purpose: close open socket                                                 *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void    zbx_tcp_close(zbx_sock_t *s)
{
	zbx_tcp_free(s);
	if(s->socket != 0)	close(s->socket);
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_tcp_close                                                    *
 *                                                                            *
 * Purpose: close open socket                                                 *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void    zbx_tcp_free(zbx_sock_t *s)
{
	if(s->buf_type == ZBX_BUF_TYPE_DYN && s->buf_dyn!=NULL)
		zbx_free(s->buf_dyn);

}

/******************************************************************************
 *                                                                            *
 * Function: zbx_tcp_recv                                                     *
 *                                                                            *
 * Purpose: receive data                                                      *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value: SUCCEED - success                                            * 
 *               FAIL - an error occured                                      *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int	zbx_tcp_recv(zbx_sock_t *s, char **data)
{
	char	*bufptr;
#define ZBX_STAT_BUF_LEN	2048
#define ZBX_BUF_LEN	ZBX_STAT_BUF_LEN*8
	ssize_t	nbytes;
	ssize_t	read_max;
	ssize_t	read_bytes;

	int	allocated, offset;
	zbx_uint64_t	expected_len;

	memset(s->buf_stat,0,sizeof(s->buf_stat));

	s->buf_type = ZBX_BUF_TYPE_STAT;
	nbytes=read(s->socket, s->buf_stat, 5);

	if(nbytes==5 && strncmp(s->buf_stat,"ZBXD",4)==0 && s->buf_stat[4] == 1)
	{
		nbytes=read(s->socket, (void *)&expected_len, sizeof(zbx_uint64_t));

/*		printf("Expected len: " ZBX_FS_UI64 "\n", expected_len);*/

		/* The rest was already cleared */
		memset(s->buf_stat,0,5);
		nbytes = read(s->socket, s->buf_stat, sizeof(s->buf_stat) - 1 );
		read_max = sizeof(s->buf_stat) - 1;

		*data = s->buf_stat;
	}
	else
	{
		bufptr = s->buf_stat+strlen(s->buf_stat);

		read_max = ZBX_STAT_BUF_LEN - strlen(s->buf_stat) -1;
		nbytes = read(s->socket, bufptr, read_max);
		expected_len = 16*1024*1024;

		*data = s->buf_stat;
	}

	read_bytes = nbytes;

	if(nbytes == read_max)
	{
/*		printf("Read max\n");*/
		s->buf_type = ZBX_BUF_TYPE_DYN;
		allocated = ZBX_BUF_LEN;
		s->buf_dyn=zbx_malloc(allocated);

		memset(s->buf_dyn,0,ZBX_BUF_LEN);
		strnscpy(s->buf_dyn, s->buf_stat, ZBX_BUF_LEN);
		offset = strlen(s->buf_dyn);
	
		while (read_bytes<expected_len && (nbytes = read(s->socket, s->buf_stat, sizeof(s->buf_stat)-2)) != -1 && nbytes != 0)
		{
			s->buf_stat[nbytes]='\0';
			zbx_snprintf_alloc(&s->buf_dyn, &allocated, &offset, ZBX_BUF_LEN-1, "%s", s->buf_stat);
			read_bytes+=nbytes;
/*			printf("Read bytes %d\n", read_bytes);*/
		}
		*data = s->buf_dyn;
	}

	if(nbytes < 0)
	{
/*		if(errno == EINTR)
		{
			zabbix_log( LOG_LEVEL_WARNING, "Read timeout");
		}
		else
		{
			zabbix_log( LOG_LEVEL_WARNING, "read() failed");
		}*/
		return	FAIL;
	}

	return	SUCCEED;
}