ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
(Generate patch)

Comparing projects/cms/source/ihost/ihost.c (file contents):
Revision 1.16 by pajs, Sun May 19 19:42:41 2002 UTC vs.
Revision 1.33 by pajs, Sat Mar 8 20:41:46 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4   * Copyright (C) 2000-2002 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
# Line 17 | Line 18
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   */
20  
21 + #ifdef HAVE_CONFIG_H
22 + #include "config.h"
23 + #endif
24 +
25   #include <stdio.h>
26   #include <stdlib.h>
27   #include <unistd.h>
23 #include <syslog.h>
24 #include <netinet/in.h>
25 #include "ukcprog.h"
26 #include <netdb.h>
28   #include <string.h>
29 < #include "statgrab.h"
29 < #include <time.h>
29 > #include <sys/types.h>
30   #include <sys/socket.h>
31 < #include <netinet/in.h>
32 < #include <arpa/inet.h>
31 > #include <stdarg.h>
32 > #include <errno.h>
33 > #include <netdb.h>
34  
35 < #define RECONFIGURE_RETURN_CODE 2
36 < #define UDP_MAX_PACKET_SIZE 8192
35 > #include "ukcprog.h"
36 > #include "statgrab.h"
37  
38 + #define LOG_CRIT 0
39 + #define LOG_ERR 1
40 + #define LOG_INFO 2
41 + #define LOG_DEBUG 3
42 +
43   typedef struct{
44 <        int fm_port;
45 <        char *fm_host;
44 >        int filtermanager_port;
45 >        char *filtermanager_host;
46  
47 <        char *my_ip;    
48 <        char *my_fqdn;
47 >        char *host_ip;
48 >        char *host_fqdn;
49 >
50          char *server_fqdn;
51          int server_udp_port;
52 <        int server_tcp_port;
52 >        
53 >        /* Weird stuff iscream wants sent to it */
54          char *last_modified;
55 <        char *files_list;
56 <        char *key;
55 >        char *file_list;
56 >
57          int udp_update_time;
58 <        int tcp_update_time;
58 > //      int config_ttl;
59  
60 +        time_t config_ttl;
61 +
62   }ihost_state_t;
63  
64 < char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
65 <        char *reply;
66 <        fprintf(f_w, "%s\n", sendString);
57 <        fflush(f_w);
58 <        reply=fpgetline(f_r);
59 <        /* Returns pointer to static buffer */
60 <        return reply;
61 < }      
64 > typedef struct{
65 >        int verbose;
66 >        int daemon;
67  
68 < int ihost_configure(ihost_state_t *ihost_state){
68 >        FILE *log;
69 > }ihost_config_t;        
70 >
71 > typedef struct{
72          struct sockaddr_in addr;
73 <        struct in_addr haddr;
74 <        struct sockaddr ip;
67 <        int ip_len;
68 <        int sd;
69 <        FILE *fm_fd_r, *fm_fd_w;
70 <        char *reply;
71 <        char *reply_ptr;
73 >        int sock;
74 > }udp_sockinfo_t;
75  
76 <        /* Check to see if anything needs to be free'd */
74 <        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
75 <        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
76 <        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
77 <        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
76 > ihost_config_t ihost_config;
77  
78 <        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
80 <                errf("Can't create AF_INET socket (%m)");
81 <                return -1;
82 <        }
78 > extern int errno;
79  
80 <        if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
81 <                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
82 <                return -1;
80 > /* Taken from the openssh code. Its licence included in function.*/
81 > //#ifndef HAVE_STRLCAT
82 >
83 > /*
84 > * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
85 > * All rights reserved.
86 > *
87 > * Redistribution and use in source and binary forms, with or without
88 > * modification, are permitted provided that the following conditions
89 > * are met:
90 > * 1. Redistributions of source code must retain the above copyright
91 > *    notice, this list of conditions and the following disclaimer.
92 > * 2. Redistributions in binary form must reproduce the above copyright
93 > *    notice, this list of conditions and the following disclaimer in the
94 > *    documentation and/or other materials provided with the distribution.
95 > * 3. The name of the author may not be used to endorse or promote products
96 > *    derived from this software without specific prior written permission.
97 > *
98 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
99 > * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
100 > * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
101 > * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
102 > * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
103 > * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
104 > * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
105 > * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
106 > * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
107 > * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108 > */
109 > /*
110 > * Appends src to string dst of size siz (unlike strncat, siz is the
111 > * full size of dst, not space left).  At most siz-1 characters
112 > * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
113 > * Returns strlen(src) + MIN(siz, strlen(initial dst)).
114 > * If retval >= siz, truncation occurred.
115 > */
116 > size_t
117 > strlcat(dst, src, siz)
118 >        char *dst;
119 >        const char *src;
120 >        size_t siz;
121 > {
122 >        register char *d = dst;
123 >        register const char *s = src;
124 >        register size_t n = siz;
125 >        size_t dlen;
126 >
127 >        /* Find the end of dst and adjust bytes left but don't go past end */
128 >        while (n-- != 0 && *d != '\0')
129 >                d++;
130 >        dlen = d - dst;
131 >        n = siz - dlen;
132 >
133 >        if (n == 0)
134 >                return(dlen + strlen(s));
135 >        while (*s != '\0') {
136 >                if (n != 1) {
137 >                        *d++ = *s;
138 >                        n--;
139 >                }
140 >                s++;
141 >        }
142 >        *d = '\0';
143 >
144 >        return(dlen + (s - src));       /* count does not include NUL */
145 > }
146 >
147 > //#endif
148 >
149 >
150 > void log_msg(int level, char *format, ...){
151 >        int cur_errno;
152 >        va_list ap;
153 >
154 >        cur_errno=errno;
155 >
156 >        if(level<=ihost_config.verbose){
157 >                va_start(ap, format);
158 >                vfprintf(ihost_config.log, format, ap);
159 >                va_end(ap);
160 >                if(level==LOG_CRIT){
161 >                        fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno));
162 >                }else{
163 >                        fprintf(ihost_config.log, "\n");
164 >                }
165          }
166 + }
167  
168 <        memset(&addr, 0, sizeof addr);
169 <        addr.sin_family = AF_INET;
170 <        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
171 <        addr.sin_port =  htons(ihost_state->fm_port);
168 > /* Takes many pointers, checks if they are NULL or not, and then free's them */
169 > /* Deprciated - and i only wrote it today! :)
170 > void m_free(int num_pointers, ...){
171 >        int x=0;
172 >        va_list ap;
173 >        void *p;
174  
175 <        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
176 <                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
177 <                return -1;
175 >        va_start(ap, num_pointers);
176 >        for(;x<num_pointers;x++){
177 >                p=va_arg(ap, void*);
178 >                if(p!=NULL){
179 >                        free(p);
180 >                }
181          }
182 +        va_end(ap);
183 + }
184 + */
185  
186 <        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
187 <        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
188 <                errf("Failed to open read stream (%m)");
189 <                return -1;
186 > int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){
187 >
188 >        struct in_addr haddr;
189 >
190 >        log_msg(LOG_DEBUG, "Resolving name for udp connection");
191 >        if(get_host_addr(hostname, &haddr) != 0){
192 >                log_msg(LOG_CRIT, "Failed to lookup name");
193 >                return 1;
194          }
195  
196 <        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
197 <                errf("Failed to open write stream (%m)");
198 <                return -1;
196 >        if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
197 >                log_msg(LOG_CRIT, "Failed to create UDP socket");
198 >                return 1;
199          }
200 <        ip_len=sizeof ip;
201 <        memset(&ip, 0, ip_len);
202 <        if((getsockname(sd, &ip, &ip_len)) != 0){
203 <                errf("Failed to get IP address (%m)");
204 <                return -1;
200 >
201 >        memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in));
202 >        udp_sockinfo->addr.sin_family=AF_INET;
203 >        memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr));
204 >        udp_sockinfo->addr.sin_port = htons(port);
205 >
206 >        log_msg(LOG_DEBUG, "Socket created");
207 >        return 0;
208 > }
209 >
210 > FILE *create_tcp_connection(char *hostname, int port){
211 >        int sock;
212 >        struct sockaddr_in addr;
213 >        struct in_addr haddr;
214 >        FILE *f;
215 >
216 >        log_msg(LOG_DEBUG, "Creating tcp socket");      
217 >        if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){
218 >                log_msg(LOG_CRIT, "Failed to make TCP Socket");
219 >                return NULL;
220          }
221 <        if (ip.sa_family!=AF_INET){
222 <                errf("sa family is wrong type");
223 <                return -1;
221 >
222 >        if((get_host_addr(hostname, &haddr))!=0){
223 >                log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
224 >                return NULL;
225          }
119        
120        if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){
121                errf("Failed to get IP (%m)");
122                return -1;
123        }      
226  
227 <        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
228 <        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
229 <                errf("Server error");  
230 <                return -1;
227 >        memset(&addr, 0, sizeof(addr));
228 >        addr.sin_family = AF_INET;
229 >        memcpy(&addr.sin_addr, &haddr, sizeof(haddr));
230 >        addr.sin_port = htons(port);
231 >
232 >        log_msg(LOG_DEBUG, "Creating a tcp connection");
233 >        if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){
234 >                log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port);
235 >                return NULL;
236          }
237  
238 <        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
239 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
240 <                errf("Server error (%m)");
241 <                return -1;
238 >        if((f=fdopen(sock, "r+"))==NULL){
239 >                log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection");
240 >                close(sock);
241 >                return NULL;
242          }
243 <        if((ihost_state->last_modified=strdup(reply)) == NULL){
244 <                errf("strdup failed (%m)");
245 <                return -1;
246 <        }
243 >
244 >        return f;
245 > }
246 >
247 > int tcp_comm(FILE *f, char *send, char **response, char *expected){
248 >
249 >        log_msg(LOG_DEBUG, "Sending %s", send);
250 >        fprintf(f, "%s\n", send);
251 >        fflush(f);
252 >        *response=fpgetline(f);
253 >        fseek(f, 0, SEEK_CUR);
254 >
255 >        if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response);
256 >
257 >        if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1;
258          
259 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
260 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
261 <                errf("Server error (%m)");
259 >        if(expected==NULL) return 0;
260 >
261 >        if((strcmp(expected, *response))==0) return 0;
262 >
263 >        log_msg(LOG_DEBUG, "Did not get expected response");
264 >        return -1;
265 > }
266 >
267 > int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){
268 >        if((tcp_comm(f, send, response, expected))!=0){
269                  return -1;
270          }
146        if((ihost_state->files_list=strdup(reply)) == NULL){
147                errf("strdup failed (%m)");
148                return -1;
149        }
271  
272 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
273 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
274 <                errf("Server error (%m)");
275 <                return -1;
272 >        *response=strdup(*response);
273 >        if (response==NULL) return -1;
274 >
275 >        return 0;
276 > }
277 >        
278 > int ihost_getconfig(ihost_state_t *ihost_state){
279 >
280 >        FILE *tcp_con;
281 >        char *response;
282 >        char *response_ptr;
283 >
284 >        /* Keep these in case of a failure and so it can keep running on the old config */
285 >        char *file_list=NULL;  
286 >        char *last_modified=NULL;
287 >        char *host_fqdn=NULL;
288 >        char *host_ip=NULL;
289 >        int udp_update_time=0;
290 >        char *server_fqdn=NULL;
291 >        int server_udp_port=0;
292 >        time_t config_ttl=0;
293 >
294 >        if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){
295 >                goto error;
296          }
297 <        if((ihost_state->my_fqdn=strdup(reply)) == NULL){
298 <                errf("strdup failed (%m)");
299 <                return -1;
297 >
298 >        if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){
299 >                if(tcp_con==NULL){
300 >                        goto error;
301 >                }      
302 >        
303 >                if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){
304 >                        goto error;
305 >                }
306 >        
307 >                if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){
308 >                        goto error;
309 >                }
310 >        
311 >                if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){
312 >                        if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
313 >                                goto error;
314 >                        }
315 >                        fclose(tcp_con);
316 >                        return 0;
317 >                }else{
318 >                        if((strcmp(response, "EXPIRED"))!=0){
319 >                                goto error;
320 >                        }
321 >                }
322          }
323  
324 <        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
325 <        if(reply== NULL){
326 <                errf("Server error (%m)");
327 <                return -1;
324 >        /* If we got to here, the config must of expired */
325 >
326 >        if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){
327 >                goto error;
328          }
166        if (strncasecmp(reply, "ERROR", 5) != 0){
167                ihost_state->udp_update_time=atoi(reply);
168        }
329  
330 <        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
331 <        if(reply== NULL){
172 <                errf("Server error (%m)");
173 <                return -1;
330 >        if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){
331 >                goto error;
332          }
333 <        if (strncasecmp(reply, "ERROR", 5) != 0){
334 <                ihost_state->tcp_update_time=atoi(reply);
333 >        last_modified=response;
334 >                        
335 >        if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){
336 >                goto error;
337          }
338 +        file_list=response;
339  
340 <        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
341 <        if(reply== NULL){
181 <                errf("Server error (%m)");
182 <                return -1;
340 >        if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){
341 >                goto error;
342          }
343 +        host_fqdn=response;
344  
345 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
346 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
187 <                errf("Server error FILTER failed (%m)");
188 <                return -1;
345 >        if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){
346 >                goto error;
347          }
348 <        reply_ptr=strchr(reply,';');
349 <        if (reply_ptr==NULL){
350 <                errf("Incorrect data returned");
351 <                return -1;
348 >        host_ip=response;
349 >
350 >        if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){
351 >                goto error;
352          }
353 <        *reply_ptr='\0';
354 <        if((ihost_state->server_fqdn=strdup(reply)) == NULL){
355 <                errf("strdup failed (%m)");
356 <                return -1;
353 >        udp_update_time=atoi(response);
354 >
355 >        if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){
356 >                goto error;
357          }
358 <        reply=reply_ptr + 1;
201 <        reply_ptr=strchr(reply,';');
202 <        if (reply_ptr==NULL){
203 <                errf("Incorrect data returned 2");
204 <                return -1;
205 <        }
206 <        *reply_ptr='\0';
207 <        ihost_state->server_udp_port=atoi(reply);
208 <        reply=reply_ptr+1;
209 <        ihost_state->server_tcp_port=atoi(reply);
210 <        if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
211 <                errf("Incorrect data returned 3 ");
212 <                return -1;
213 <        }
358 >        config_ttl=atoi(response);
359  
360 <        reply=sock_comm(fm_fd_r, fm_fd_w, "END");
361 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
217 <                errf("Server error (%m)");
218 <                return -1;
360 >        if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){
361 >                goto error;
362          }
363  
364 <        if(fclose(fm_fd_r) !=0){
365 <                errf("Failed to close read FD (%m)");
366 <                return -1;
364 >        if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){
365 >                goto error;
366 >        }else{
367 >                response_ptr=strchr(response,';');
368 >                if(response_ptr==NULL){
369 >                        log_msg(LOG_ERR, "Incorrect data sent by server");
370 >                        goto error;
371 >                }
372 >                *response_ptr='\0';
373 >                server_fqdn=strdup(response);
374 >                if(server_fqdn==NULL){
375 >                        goto error;
376 >                }
377 >                response_ptr++;
378 >                if(response_ptr==NULL){
379 >                        log_msg(LOG_ERR, "Incorrect data sent by server");
380 >                        goto error;
381 >                }
382 >
383 >                printf("string : %s\n", response_ptr);
384 >                server_udp_port=atoi(response_ptr);
385 >
386 >                if (server_udp_port==0){
387 >                        log_msg(LOG_ERR, "Incorrect data sent by server");
388 >                        goto error;
389 >                }
390          }
391 <        if(fclose(fm_fd_w) !=0){
392 <                errf("Failed to close write FD (%m)");
393 <                return -1;
391 >        
392 >        if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
393 >                goto error;
394          }
395  
396 +        fclose(tcp_con);
397 +
398 +        /* We have the data we need, and its all been read correctly */
399 +
400 +        /* Free the old data before pointing them to the new data. m_free copes should
401 +         * this already be NULL */
402 +        if(ihost_state->file_list!=NULL) free(ihost_state->file_list);
403 +        if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
404 +        if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
405 +        if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
406 +        if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
407 +
408 +        ihost_state->file_list=file_list;
409 +        ihost_state->last_modified=last_modified;
410 +        ihost_state->host_fqdn=host_fqdn;
411 +        ihost_state->host_ip=host_ip;
412 +        ihost_state->server_fqdn=server_fqdn;
413 +        ihost_state->server_udp_port=server_udp_port;
414 +        ihost_state->udp_update_time=udp_update_time;
415 +        ihost_state->config_ttl=config_ttl;
416 +
417 +        log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time);
418 +        log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl);
419 +
420          return 0;
421 +
422 + error:
423 +
424 +        if(file_list!=NULL) free(file_list);
425 +        if(last_modified!=NULL) free(last_modified);
426 +        if(host_fqdn!=NULL) free(host_fqdn);
427 +        if(server_fqdn!=NULL) free(server_fqdn);
428 +        if(host_ip!=NULL) free(host_ip);
429 +        fclose(tcp_con);
430 +
431 +        return -1;
432   }
433  
434 < int heartbeat(ihost_state_t *ihost_state){
435 <        struct sockaddr_in addr;
436 <        struct in_addr haddr;
437 <        int sd;
438 <        FILE *fm_fd_r, *fm_fd_w;
439 <        char *reply;
440 <        int exitcode=0;
434 > int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){
435 >        char tmp[size];
436 >        cpu_percent_t *cpu_percent;    
437 >        mem_stat_t *mem_stats;
438 >        load_stat_t *load_stats;
439 >        user_stat_t *user_stats;
440 >        swap_stat_t *swap_stats;
441 >        general_stat_t *general_stats;
442 >        disk_stat_t *disk_stats;
443 >        diskio_stat_t *diskio_stats;
444 >        process_stat_t *process_stats;
445 >        network_stat_t *network_stats;
446 >        page_stat_t *page_stats;
447 >        int disk_entries=0;
448 >        int diskio_entries=0;
449 >        int network_entries=0;
450  
451 <        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
452 <                errf("Can't create AF_INET socket (%m)");
453 <                return -1;
244 <        }
451 >        int counter;
452 >        long long x;
453 >        long long y;
454  
455 <        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
456 <                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
457 <                return -1;
249 <        }
455 >        /* Print start of the packet we want */
456 >        snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \
457 >                 seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip);
458  
459 <        memset(&addr, 0, sizeof addr);
460 <        addr.sin_family = AF_INET;
461 <        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
462 <        addr.sin_port =  htons(ihost_state->server_tcp_port);
459 >        /* Get cpu stats, check it is correct, then fill in its entry for the xml */
460 >        if((cpu_percent=cpu_percent_usage())==NULL){
461 >                log_msg(LOG_CRIT, "Failed to get cpu statistics");
462 >        }else{
463 >                snprintf(tmp, size, \
464 >                        "<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \
465 >                        cpu_percent->user, \
466 >                        cpu_percent->kernel, \
467 >                        cpu_percent->idle, \
468 >                        cpu_percent->iowait, \
469 >                        cpu_percent->swap);
470  
471 <        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
472 <                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
258 <                return -1;
259 <        }
471 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
472 >        }
473  
474 <        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
475 <        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
476 <                errf("Failed to open stream (%m)");
477 <                return -1;
478 <        }
474 >        
475 >        /*Get mem stats, and fill in xml */    
476 >        if((mem_stats=get_memory_stats())==NULL){
477 >                log_msg(LOG_CRIT, "Failed to get memory statistics");
478 >        }else{
479 >                snprintf(tmp, size, \
480 >                        "<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \
481 >                        mem_stats->total, \
482 >                        mem_stats->free, \
483 >                        mem_stats->used, \
484 >                        mem_stats->cache);
485 >                
486 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
487 >        }
488  
267        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
268                errf("Failed to open stream (%m)");
269                return -1;
270        }
489  
490 <        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
491 <        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
492 <                errf("Server error");
493 <                return -1;
494 <        }
490 >        /* Get load stats */    
491 >        if((load_stats=get_load_stats())==NULL){
492 >                log_msg(LOG_CRIT, "Failed to get load statistics");
493 >        }else{
494 >                snprintf(tmp, size, \
495 >                        "<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \
496 >                        load_stats->min1, \
497 >                        load_stats->min5, \
498 >                        load_stats->min15);
499 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
500 >        }
501  
278        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
279        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
280                errf("Server error");
281                return -1;
282        }
502  
503 <        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
504 <        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
505 <                errf("Server error");
506 <                return -1;
507 <        }
503 >        /* get user stats */
504 >        
505 >        if((user_stats=get_user_stats())==NULL){
506 >                log_msg(LOG_CRIT, "Failed to get user statistics");
507 >        }else{
508  
509 <        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
510 <        if (reply==NULL) {
511 <                errf("Server error");
512 <                return -1;
513 <        }
514 <        if (strncasecmp(reply, "ERROR", 5) == 0){
296 <        /* Means the config has changed */
297 <                exitcode=RECONFIGURE_RETURN_CODE;
509 >                snprintf(tmp, size, \
510 >                        "<users><list>%s</list><count>%d</count></users>", \
511 >                        user_stats->name_list, \
512 >                        user_stats->num_entries);
513 >                
514 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
515          }
299        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
300        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
301                errf("Server error");
302                return -1;
303        }
304        if (ihost_state->key!=NULL) free(ihost_state->key);
305
306        if((ihost_state->key=strdup(reply)) == NULL){
307                errf("strdup failed (%m)");
308                return -1;
309        }
516  
311        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
312        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
313                errf("Server error (%m)");
314                return -1;
315        }
517  
518 <        fflush(fm_fd_r);
519 <        fflush(fm_fd_w);
518 >        /* swap stats */
519 >        if((swap_stats=get_swap_stats())==NULL){
520 >                log_msg(LOG_CRIT, "Failed to get swap statistics");    
521 >        }else{
522 >                snprintf(tmp, size, \
523 >                        "<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\
524 >                        swap_stats->total, \
525 >                        swap_stats->used, \
526 >                        swap_stats->free);
527 >        
528 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
529 >        }
530  
320        if(fclose(fm_fd_r) !=0){
321                errf("Failed to close read FD (%m)");
322                return -1;
323        }
324        if(fclose(fm_fd_w) !=0){
325                errf("Failed to close write FD (%m)");
326                return -1;
327        }
531  
532 <        return exitcode;                
533 < }
532 >        /* general stats */
533 >        
534 >        if((general_stats=get_general_stats())==NULL){
535 >                log_msg(LOG_CRIT, "Failed to get general statistics");
536 >        }else{
537 >                snprintf(tmp, size, \
538 >                        "<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \
539 >                        general_stats->os_name, \
540 >                        general_stats->os_release, \
541 >                        general_stats->os_version, \
542 >                        general_stats->hostname, \
543 >                        general_stats->platform, \
544 >                        (long)general_stats->uptime);
545 >                
546 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
547 >        
548 >        }
549  
332 char *stat_grab(ihost_state_t *ihost_state, int counter){
333 #define NUM_STATS 9
334        char *stats[NUM_STATS];
335        char *xml_data=NULL;
336        char *xml_data_p;
337        int x=0;
550          
551 <        stats[0]=get_cpu_stats();
552 <        stats[1]=get_disk_stats();
553 <        stats[2]=get_load_stats();      
554 <        stats[3]=get_memory_stats();
555 <        stats[4]=get_os_info();
556 <        stats[5]=get_page_stats();
557 <        stats[6]=get_process_stats();
558 <        stats[7]=get_swap_stats();
559 <        stats[8]=get_user_stats();
551 >        /* process stats */
552 >        if((process_stats=get_process_stats())==NULL){  
553 >                log_msg(LOG_CRIT, "Failed to get general statistics");
554 >        }else{
555 >                snprintf(tmp, size, \
556 >                        "<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\
557 >                        process_stats->sleeping, \
558 >                        process_stats->running, \
559 >                        process_stats->zombie, \
560 >                        process_stats->stopped, \
561 >                        process_stats->total);
562  
563 <        for(;x<NUM_STATS;x++){
564 <                if(stats[x]==NULL){
565 <                        return NULL;
566 <                }
567 <                if(xml_data==NULL){
568 <                        if((xml_data=strf("%s", stats[x])) == NULL){
569 <                                errf("str failed (%m)");
570 <                                return NULL;
571 <                        }
563 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
564 >
565 >        }
566 >
567 >
568 >        /* Get paging stats */
569 >        if((page_stats=get_page_stats_diff())==NULL){
570 >                log_msg(LOG_CRIT, "Failed to get paging statistics");
571 >        }else{
572 >                if(page_stats->systime!=0){
573 >                        x=page_stats->pages_pagein / page_stats->systime;
574 >                        y=page_stats->pages_pageout / page_stats->systime;
575                  }else{
576 <                        xml_data_p=xml_data;
577 <                        if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){
361 <                                errf("str failed (%m)");
362 <                                return NULL;
363 <                        }
364 <                        free(xml_data_p);
576 >                        x=page_stats->pages_pagein;
577 >                        y=page_stats->pages_pageout;
578                  }
579 <                free(stats[x]);
579 >                snprintf(tmp, size, \
580 >                        "<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \
581 >                        x, \
582 >                        y);
583 >        
584 >                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
585          }
586 <        xml_data_p=xml_data;
587 <        xml_data=strf("<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\" key=\"%s\">%s</packet>", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data);
588 <        free(xml_data_p);
586 >
587 >
588 >        /* get diskio stats */
589          
590 <        return xml_data;
591 < }
590 >        if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
591 >                log_msg(LOG_CRIT, "Failed to get diskio statistics");
592 >        }else{
593 >                strlcat(xml, "<diskio>", size);
594 >                for(counter=0;counter<diskio_entries;counter++){
595  
596 < int send_stats(ihost_state_t *ihost_state, char *data_stream){
597 <        struct sockaddr_in addr;
598 <        struct in_addr haddr;
596 >                        if(diskio_stats->systime!=0){
597 >                                x=diskio_stats->read_bytes / diskio_stats->systime;
598 >                                y=diskio_stats->write_bytes / diskio_stats->systime;
599 >                        }else{
600 >                                x=diskio_stats->read_bytes;
601 >                                y=diskio_stats->write_bytes;
602 >                        }
603 >        
604 >                        snprintf(tmp, size, \
605 >                                "<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \
606 >                                counter, \
607 >                                diskio_stats->disk_name, \
608 >                                x, \
609 >                                y, \
610 >                                counter);
611  
612 <        int sd;
613 <        size_t len;
612 >                        strlcat(xml, tmp, size);
613 >                        diskio_stats++;
614 >                }
615  
616 <        len=strlen(data_stream);
617 <        if(len>UDP_MAX_PACKET_SIZE){
384 <                errf("Too big to send to server. Please reconfigure client and server and recompile");
385 <                exit(1);
616 >                if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error;
617 >
618          }
619 +
620          
621 <        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
622 <                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
623 <                return -1;
624 <        }
621 >        /* get networks stats */
622 >        
623 >        if((network_stats=get_network_stats_diff(&network_entries))==NULL){
624 >                log_msg(LOG_CRIT, "Failed to get network statistics");
625 >        }else{
626 >                strlcat(xml, "<net>", size);
627 >                for(counter=0;counter<network_entries;counter++){
628 >                        if(network_stats->systime!=0){
629 >                                x=network_stats->rx / network_stats->systime;
630 >                                y=network_stats->tx / network_stats->systime;
631 >                        }else{
632 >                                x=network_stats->rx;
633 >                                y=network_stats->tx;
634 >                        }
635  
636 <        if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
637 <                errf("failed to create UDP socket (%m)");
638 <                return -1;
636 >                        snprintf(tmp, size, \
637 >                                "<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \
638 >                                counter, \
639 >                                network_stats->interface_name, \
640 >                                x, \
641 >                                y, \
642 >                                counter);
643 >
644 >                        strlcat(xml, tmp, size);
645 >                        network_stats++;
646 >                }
647 >
648 >                if(strlcat(xml, "</net>", size) >= size) goto too_big_error;
649 >
650          }
651  
398        memset(&addr, 0, sizeof(addr));
399        addr.sin_family=AF_INET;
400        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
401        addr.sin_port =  htons(ihost_state->server_udp_port);
652  
653 <        if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){
654 <                errf("Send the wrong number of bytes (%m)");
655 <                return -1;
653 >        /* get disk stats */
654 >        
655 >        if((disk_stats=get_disk_stats(&disk_entries))==NULL){
656 >                log_msg(LOG_CRIT, "Failed to get disk statistics");
657 >        }else{
658 >                strlcat(xml, "<disk>", size);
659 >                for(counter=0;counter<disk_entries;counter++){
660 >                        snprintf(tmp, size, \
661 >                                "<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \
662 >                                counter, \
663 >                                disk_stats->device_name, \
664 >                                disk_stats->mnt_point, \
665 >                                disk_stats->fs_type, \
666 >                                disk_stats->size, \
667 >                                disk_stats->used, \
668 >                                disk_stats->avail, \
669 >                                disk_stats->total_inodes, \
670 >                                disk_stats->used_inodes, \
671 >                                disk_stats->free_inodes, \
672 >                                counter);
673 >
674 >                        strlcat(xml, tmp, size);
675 >
676 >                        disk_stats++;
677 >                }
678 >
679 >                if(strlcat(xml, "</disk>", size) >= size) goto too_big_error;
680 >
681          }
682 +        
683  
684 <        close(sd);
684 >        if(strlcat(xml, "</packet>", size) >= size) goto too_big_error;
685  
686 <        return 0;      
686 >        /*If we got to here, it should of all been filled in nicely now */
687 >        return 0;
688 >
689 > too_big_error:
690 >        log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet");
691 >        return -1;
692   }
693  
694 +
695 +        
696 + void usage(char *progname){
697 +        fprintf(stderr, "Usage %s [options] server port\n", progname);
698 +        fprintf(stderr, "Options\n");
699 +        fprintf(stderr, "  -v           Verbose mode,-vv would make even more verbose\n");
700 +        fprintf(stderr, "  -f           Foreground mode, print errors to stderr\n");
701 +        fprintf(stderr, "  -V           Print version number\n");
702 +        fprintf(stderr, "  -h           Prints this help page\n");
703 +        exit(1);
704 + }
705 +
706   int main(int argc, char **argv){
707 +
708          ihost_state_t ihost_state;
709 <        int heartbeat_exit;
710 <        int counter=0;
711 <        long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
712 <        int sleep_delay=0;
713 <        char *xml_stats;
709 >        udp_sockinfo_t udp_sockinfo;
710 >        
711 >        int cmdopt;
712 >        extern int optind;
713 >        pid_t pid;
714 >        FILE *f;
715 >        int packet_num=0;
716 >        int len;
717  
718 <        /* NULL'ify so i can tell if i need to free it or not */
422 <        ihost_state.fm_host=NULL;
423 <        ihost_state.my_fqdn=NULL;
424 <        ihost_state.server_fqdn=NULL;
425 <        ihost_state.last_modified=NULL;
426 <        ihost_state.files_list=NULL;
427 <        ihost_state.key=NULL;
718 >        char packet[MAX_UDP_PACKET_SIZE];
719  
720 <        errf_set_progname(argv[0]);
721 <        if(argc!=3){
722 <                errf_usage("<host> <port>");    
720 >        time_t cur_time, sleep_delay, udp_time=0, config_time=0;
721 >
722 >        /* Set default settings */      
723 >        ihost_config.verbose=1;
724 >        ihost_config.daemon=1;
725 >        /* Set all errors to go down stderr until told otherwise */
726 >        ihost_config.log=stderr;
727 >
728 >        /* Blank ihost_state to default settings */
729 >        ihost_state.filtermanager_host=NULL;
730 >        ihost_state.host_fqdn=NULL;
731 >        ihost_state.host_ip=NULL;
732 >        ihost_state.server_fqdn=NULL;
733 >        ihost_state.file_list=NULL;
734 >        ihost_state.last_modified=NULL;
735 >
736 >        while((cmdopt=getopt(argc, argv, "vfhV")) != -1){
737 >                switch(cmdopt){
738 >                        case 'v':
739 >                                ihost_config.verbose++;
740 >                                break;
741 >
742 >                        case 'f':
743 >                                /* Force syslog logging since stderr will be closed in this case */
744 >                                ihost_config.daemon=0;
745 >                                break;
746 >
747 >                        case 'h':
748 >                                usage(argv[0]);
749 >                                break;
750 >
751 >                        case 'V':
752 >                                fprintf(stderr, "%s version %s\n", argv[0], VERSION);
753 >                                break;
754 >
755 >                        default:
756 >                                usage(argv[0]);
757 >                                exit(1);
758 >                }
759 >        }
760 >
761 >        if(argc!=optind+2){
762 >                usage(argv[0]);
763 >                exit(1);
764 >        }
765 >
766 >        ihost_state.filtermanager_host=strdup(argv[optind]);
767 >        ihost_state.filtermanager_port=atoi(argv[optind+1]);
768 >        
769 >        if(gethostbyname(ihost_state.filtermanager_host)==NULL){
770 >                log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings");
771                  exit(1);
772          }
773 +        if(ihost_state.filtermanager_port==0){
774 +                log_msg(LOG_ERR, "Invalid port number");
775 +                exit(1);
776 +        }
777  
778 <        ihost_state.fm_host=argv[1];
779 <        ihost_state.fm_port=atoi(argv[2]);
778 >        if(ihost_config.daemon){
779 >                pid=fork();
780 >                if(pid==-1){
781 >                        log_msg(LOG_CRIT, "Failed to background exiting");
782 >                        exit(1);        
783 >                }else if(pid!=0){
784 >                        /* Parent process */
785 >                        return 0;
786 >                }
787 >                /* We should now be in the background*/
788 >                if(setsid()==-1){
789 >                        log_msg(LOG_CRIT, "setsid failed");
790 >                        exit(1);
791 >                }
792 >        
793 >                if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){
794 >                        ihost_config.log=stderr;
795 >                        log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE);
796 >                        exit(1);
797 >                }
798  
799 <        if(ihost_configure(&ihost_state)!=0){
800 <                errf("configure failed");
801 <                /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
802 <                happens.. But we dont :) (at the moment) */
803 <                exit(1);
799 >                fclose(stdin);
800 >                fclose(stdout);
801 >                fclose(stderr);
802 >
803 >        }
804 >
805 >        log_msg(LOG_INFO, "Starting ihost");
806 >        
807 >        log_msg(LOG_DEBUG,"Writing PID FILE");
808 >
809 >        pid=getpid();
810 >
811 >        if((f=fopen(PID_FILE,"w")) == NULL){
812 >                log_msg(LOG_CRIT, "Failed to write PID file");
813 >        }else{
814 >                if((fprintf(f,"%d",(int)pid)) <= 0 ){
815 >                        log_msg(LOG_CRIT, "Failed to write PID file");
816 >                }
817 >                if((fclose(f))!=0){
818 >                        log_msg(LOG_CRIT, "failed to close PID file");
819 >                }
820 >        }
821 >
822 >        /* Get the initial config from the filter manager. Should this fail,
823 >         * wait, and then try again. */
824 >
825 >        get_diskio_stats_diff(&packet_num);
826 >        packet_num=0;
827 >
828 >        while(ihost_getconfig(&ihost_state)!=0){
829 >                log_msg(LOG_ERR, "Failed to get ihost config");
830 >                sleep(10);
831          }
832  
833 +        printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);
834 +        while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
835 +                log_msg(LOG_ERR, "Failed to create udp socket");        
836 +                sleep(10);
837 +        }
838 +
839 +        config_time=time(NULL)+ihost_state.config_ttl;
840 +
841 +        /* Now have config.. collect data and send as often as required */
842          for(;;){
843                  cur_time=time(NULL);
447                if(cur_time>=tcp_time){
448                        /*printf("sending TCP\n");*/
449                        heartbeat_exit=heartbeat(&ihost_state);
450                        if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
451                                /*errf("heartbeat needs to be reconfigured");*/
452                                ihost_configure(&ihost_state);
453                                /* So udp doesn't wait til next sending before updating */
454                                udp_time=0;
455                        }
456                        if(heartbeat_exit==-1){
457                                errf("ah crap");
458                                exit(1);
459                        }
460                        tcp_time=time(NULL)+ihost_state.tcp_update_time;
461                }
844  
845                  if(cur_time>=udp_time){
846 <                        /*printf("sending UDP\n");*/
847 <                        stat_grab_time=time(NULL);
466 <                        if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
467 <                                errf("Failed to get stats (%m)");
468 <                                exit(1);
846 >                        if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){
847 >                                log_msg(LOG_ERR, "Failed to get system stats");
848                          }
849 <                        stat_grab_time=time(NULL)-stat_grab_time;
850 <                        send_stats(&ihost_state, xml_stats);
851 <                        free(xml_stats);
852 <                        udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time;
849 >
850 >                        len=strlen(packet);
851 >                        log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet);
852 >                        
853 >                        if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){
854 >                                log_msg(LOG_CRIT, "Failed to send packet");
855 >                        }
856 >                        udp_time=cur_time+ihost_state.udp_update_time;
857 >                        log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time);
858                  }
859 +                
860 +                if(cur_time>=config_time){
861 +                        if(ihost_getconfig(&ihost_state)!=0){
862 +                                /* If we can't get the config, try again 5 minutes time */
863 +                                log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time");
864 +                                config_time=time(NULL)+300;
865 +                        }else{
866 +                                close(udp_sockinfo.sock);
867  
868 <                if(tcp_time<udp_time){
869 <                        sleep_delay=tcp_time-time(NULL);
870 <                }else{
871 <                        sleep_delay=udp_time-time(NULL);
868 >                                while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
869 >                                        log_msg(LOG_CRIT, "Failed to create udp socket");
870 >                                        sleep(10);
871 >                                }
872 >
873 >                                config_time=time(NULL)+ihost_state.config_ttl;
874 >
875 >                                log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl);
876 >                        }
877                  }
878  
879 <                /*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/
879 >                sleep_delay=udp_time-time(NULL);
880 >                log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay);
881                  if(sleep_delay>0) sleep(sleep_delay);
882 <        }
883 <        return 0;
882 >        }
883 >                        
884 >        return(0);
885   }
487

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines