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.25 by pajs, Wed May 22 09:01:01 2002 UTC vs.
Revision 1.40 by pajs, Sun Apr 6 12:08:37 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines