26 |
|
|
27 |
|
}ihost_state_t; |
28 |
|
|
29 |
< |
char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){ |
29 |
> |
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
30 |
|
char *reply; |
31 |
< |
fprintf(f_w, "%s", sendString); |
31 |
> |
fprintf(f_w, "%s\n", sendString); |
32 |
|
fflush(f_w); |
33 |
|
reply=fpgetline(f_r); |
34 |
|
/* Returns pointer to static buffer */ |
44 |
|
char *reply_ptr; |
45 |
|
|
46 |
|
/* Check to see if anything needs to be free'd */ |
47 |
– |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
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); |
71 |
|
|
72 |
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
73 |
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
74 |
< |
errf("Failed to open stream (%m)"); |
74 |
> |
errf("Failed to open read stream (%m)"); |
75 |
|
return -1; |
76 |
|
} |
77 |
|
|
78 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
79 |
< |
errf("Failed to open stream (%m)"); |
79 |
> |
errf("Failed to open write stream (%m)"); |
80 |
|
return -1; |
81 |
|
} |
82 |
|
|
83 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n"); |
83 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
84 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
85 |
|
errf("Server error"); |
86 |
|
return -1; |
87 |
|
} |
88 |
|
|
89 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n"); |
89 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
90 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
91 |
|
errf("Server error (%m)"); |
92 |
|
return -1; |
96 |
|
return -1; |
97 |
|
} |
98 |
|
|
99 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); |
99 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
100 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
101 |
|
errf("Server error (%m)"); |
102 |
|
return -1; |
103 |
|
} |
104 |
< |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
105 |
< |
errf("strdup failed (%m)"); |
106 |
< |
return -1; |
107 |
< |
} |
104 |
> |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
105 |
> |
errf("strdup failed (%m)"); |
106 |
> |
return -1; |
107 |
> |
} |
108 |
|
|
109 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n"); |
109 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
110 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
111 |
|
errf("Server error (%m)"); |
112 |
|
return -1; |
116 |
|
return -1; |
117 |
|
} |
118 |
|
|
119 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n"); |
119 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
120 |
|
if(reply== NULL){ |
121 |
|
errf("Server error (%m)"); |
122 |
|
return -1; |
125 |
|
ihost_state->udp_update_time=atoi(reply); |
126 |
|
} |
127 |
|
|
128 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n"); |
128 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
129 |
|
if(reply== NULL){ |
130 |
|
errf("Server error (%m)"); |
131 |
|
return -1; |
134 |
|
ihost_state->tcp_update_time=atoi(reply); |
135 |
|
} |
136 |
|
|
137 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n"); |
137 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
138 |
|
if(reply== NULL){ |
139 |
|
errf("Server error (%m)"); |
140 |
|
return -1; |
141 |
|
} |
142 |
|
|
143 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n"); |
143 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
144 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
145 |
|
errf("Server error (%m)"); |
146 |
|
return -1; |
155 |
|
errf("strdup failed (%m)"); |
156 |
|
return -1; |
157 |
|
} |
158 |
< |
reply=++reply_ptr; |
158 |
> |
reply=reply_ptr + 1; |
159 |
|
reply_ptr=strchr(reply,';'); |
160 |
|
if (reply_ptr==NULL){ |
161 |
|
errf("Incorrect data returned 2"); |
163 |
|
} |
164 |
|
*reply_ptr='\0'; |
165 |
|
ihost_state->server_udp_port=atoi(reply); |
166 |
< |
reply=++reply_ptr; |
166 |
> |
reply=reply_ptr+1; |
167 |
|
ihost_state->server_tcp_port=atoi(reply); |
168 |
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
169 |
|
errf("Incorrect data returned 3 "); |
170 |
|
return -1; |
171 |
|
} |
172 |
|
|
173 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
173 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
174 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
175 |
|
errf("Server error (%m)"); |
176 |
|
return -1; |
177 |
|
} |
178 |
|
|
179 |
|
if(fclose(fm_fd_r) !=0){ |
180 |
< |
errf("Failed to close FD (%m)"); |
180 |
> |
errf("Failed to close read FD (%m)"); |
181 |
|
return -1; |
182 |
|
} |
183 |
|
if(fclose(fm_fd_w) !=0){ |
184 |
< |
errf("Failed to close FD (%m)"); |
184 |
> |
errf("Failed to close write FD (%m)"); |
185 |
|
return -1; |
186 |
|
} |
187 |
|
|
227 |
|
return -1; |
228 |
|
} |
229 |
|
|
230 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); |
231 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
230 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
231 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
232 |
|
errf("Server error"); |
233 |
|
return -1; |
234 |
|
} |
235 |
< |
|
236 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n"); |
237 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
235 |
> |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
236 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
237 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
238 |
|
errf("Server error"); |
239 |
|
return -1; |
240 |
|
} |
250 |
|
errf("Server error"); |
251 |
|
return -1; |
252 |
|
} |
253 |
< |
if (strncasecmp(reply, "ERROR", 2) == 0){ |
253 |
> |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
254 |
|
/* Means the config has changed */ |
255 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
256 |
|
} |
257 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n"); |
258 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
257 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
258 |
> |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
259 |
|
errf("Server error"); |
260 |
|
return -1; |
261 |
|
} |
266 |
|
return -1; |
267 |
|
} |
268 |
|
|
269 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
269 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
270 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
271 |
|
errf("Server error (%m)"); |
272 |
|
return -1; |
273 |
|
} |
274 |
|
|
275 |
+ |
if(fclose(fm_fd_r) !=0){ |
276 |
+ |
errf("Failed to close read FD (%m)"); |
277 |
+ |
return -1; |
278 |
+ |
} |
279 |
+ |
if(fclose(fm_fd_w) !=0){ |
280 |
+ |
errf("Failed to close write FD (%m)"); |
281 |
+ |
return -1; |
282 |
+ |
} |
283 |
+ |
|
284 |
|
return exitcode; |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
int main(int argc, char **argv){ |
289 |
|
ihost_state_t ihost_state; |
290 |
+ |
int heartbeat_exit; |
291 |
+ |
int counter=0; |
292 |
|
|
293 |
+ |
|
294 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
295 |
|
ihost_state.fm_host=NULL; |
296 |
|
ihost_state.my_fqdn=NULL; |
310 |
|
|
311 |
|
if(ihost_configure(&ihost_state)!=0){ |
312 |
|
errf("configure failed"); |
313 |
+ |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
314 |
+ |
happens.. But we dont :) (at the moment) */ |
315 |
+ |
exit(1); |
316 |
|
} |
317 |
|
|
318 |
+ |
while(TRUE){ |
319 |
|
|
320 |
+ |
heartbeat_exit=heartbeat(&ihost_state); |
321 |
+ |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
322 |
+ |
errf("heartbeat needs to be reconfigured"); |
323 |
+ |
ihost_configure(&ihost_state); |
324 |
+ |
} |
325 |
+ |
if(heartbeat_exit==-1){ |
326 |
+ |
errf("ah crap"); |
327 |
+ |
exit(1); |
328 |
+ |
} |
329 |
+ |
printf("Count : %d\n",counter++); |
330 |
+ |
printf("waiting %d\n",ihost_state.tcp_update_time); |
331 |
+ |
sleep(ihost_state.tcp_update_time); |
332 |
+ |
} |
333 |
|
return 0; |
334 |
|
} |
335 |
|
|