43 |
|
char *reply; |
44 |
|
char *reply_ptr; |
45 |
|
|
46 |
+ |
/* Check to see if anything needs to be free'd */ |
47 |
+ |
if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); |
48 |
+ |
if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
49 |
+ |
if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
50 |
+ |
if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
51 |
+ |
|
52 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
53 |
|
errf("Can't create AF_INET socket (%m)"); |
54 |
|
return -1; |
91 |
|
errf("Server error (%m)"); |
92 |
|
return -1; |
93 |
|
} |
94 |
< |
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
95 |
< |
errf("strdup failed (%m)"); |
94 |
> |
if((ihost_state->last_modified=malloc((strlen(reply))+1)) == NULL){ |
95 |
> |
errf("malloc failed (%m)"); |
96 |
|
return -1; |
97 |
|
} |
98 |
+ |
strcpy(ihost_state->last_modified, reply); |
99 |
+ |
ihost_state->last_modified[(strlen(ihost_state->last_modified))]='\n'; |
100 |
|
|
101 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); |
102 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
103 |
|
errf("Server error (%m)"); |
104 |
|
return -1; |
105 |
|
} |
106 |
< |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
107 |
< |
errf("strdup failed (%m)"); |
108 |
< |
return -1; |
109 |
< |
} |
106 |
> |
if((ihost_state->files_list=malloc((strlen(reply))+1)) == NULL){ |
107 |
> |
errf("malloc failed (%m)"); |
108 |
> |
return -1; |
109 |
> |
} |
110 |
> |
strcpy(ihost_state->files_list, reply); |
111 |
> |
ihost_state->files_list[(strlen(ihost_state->files_list))]='\n'; |
112 |
|
|
113 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n"); |
114 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
232 |
|
} |
233 |
|
|
234 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); |
235 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
235 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
236 |
|
errf("Server error"); |
237 |
|
return -1; |
238 |
|
} |
239 |
< |
|
239 |
> |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
240 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n"); |
241 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
241 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
242 |
|
errf("Server error"); |
243 |
|
return -1; |
244 |
|
} |
245 |
|
|
246 |
+ |
printf("filelist %s\n",ihost_state->files_list); |
247 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
248 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
249 |
|
errf("Server error"); |
255 |
|
errf("Server error"); |
256 |
|
return -1; |
257 |
|
} |
258 |
< |
if (strncasecmp(reply, "ERROR", 2) == 0){ |
258 |
> |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
259 |
|
/* Means the config has changed */ |
260 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
261 |
|
} |
262 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n"); |
263 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
263 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
264 |
|
errf("Server error"); |
265 |
|
return -1; |
266 |
|
} |
267 |
< |
|
268 |
< |
if((ihost_state->key=strdup(reply)) == NULL){ |
269 |
< |
errf("strdup failed (%m)"); |
267 |
> |
if (ihost_state->key!=NULL) free(ihost_state->key); |
268 |
> |
|
269 |
> |
if((ihost_state->key=malloc(strlen(reply)+1)) == NULL){ |
270 |
> |
errf("malloc failed (%m)"); |
271 |
|
return -1; |
272 |
|
} |
273 |
+ |
strcpy(ihost_state->key, reply); |
274 |
+ |
ihost_state->key[(strlen(ihost_state->key))]='\n'; |
275 |
|
|
276 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
276 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT\n"); |
277 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
278 |
|
errf("Server error (%m)"); |
279 |
|
return -1; |
280 |
|
} |
281 |
|
|
282 |
+ |
if(fclose(fm_fd_r) !=0){ |
283 |
+ |
errf("Failed to close FD (%m)"); |
284 |
+ |
return -1; |
285 |
+ |
} |
286 |
+ |
if(fclose(fm_fd_w) !=0){ |
287 |
+ |
errf("Failed to close FD (%m)"); |
288 |
+ |
return -1; |
289 |
+ |
} |
290 |
+ |
|
291 |
|
return exitcode; |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
int main(int argc, char **argv){ |
296 |
|
ihost_state_t ihost_state; |
297 |
+ |
int heartbeat_exit; |
298 |
+ |
int counter=0; |
299 |
+ |
|
300 |
+ |
|
301 |
+ |
/* NULL'ify so i can tell if i need to free it or not */ |
302 |
+ |
ihost_state.fm_host=NULL; |
303 |
+ |
ihost_state.my_fqdn=NULL; |
304 |
+ |
ihost_state.server_fqdn=NULL; |
305 |
+ |
ihost_state.last_modified=NULL; |
306 |
+ |
ihost_state.files_list=NULL; |
307 |
+ |
ihost_state.key=NULL; |
308 |
+ |
|
309 |
|
errf_set_progname(argv[0]); |
310 |
|
if(argc!=3){ |
311 |
|
errf_usage("<host> <port>"); |
317 |
|
|
318 |
|
if(ihost_configure(&ihost_state)!=0){ |
319 |
|
errf("configure failed"); |
320 |
+ |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
321 |
+ |
happens.. But we dont :) (at the moment) */ |
322 |
+ |
exit(1); |
323 |
|
} |
324 |
|
|
325 |
+ |
while(TRUE){ |
326 |
+ |
|
327 |
+ |
heartbeat_exit=heartbeat(&ihost_state); |
328 |
+ |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
329 |
+ |
errf("heartbeat needs to be reconfigured"); |
330 |
+ |
ihost_configure(&ihost_state); |
331 |
+ |
} |
332 |
+ |
if(heartbeat_exit==-1){ |
333 |
+ |
errf("ah crap"); |
334 |
+ |
exit(1); |
335 |
+ |
} |
336 |
+ |
printf("Count : %d\n",counter++); |
337 |
+ |
printf("waiting %d\n",ihost_state.tcp_update_time); |
338 |
+ |
sleep(ihost_state.tcp_update_time); |
339 |
+ |
} |
340 |
|
return 0; |
341 |
|
} |
342 |
|
|