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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines