8 |
|
#include <netdb.h> |
9 |
|
#include <strings.h> |
10 |
|
|
11 |
+ |
#define RECONFIGURE_RETURN_CODE 2 |
12 |
+ |
|
13 |
|
typedef struct{ |
14 |
|
int fm_port; |
15 |
|
char *fm_host; |
18 |
|
char *server_fqdn; |
19 |
|
int server_udp_port; |
20 |
|
int server_tcp_port; |
21 |
< |
long last_modified; |
21 |
> |
char *last_modified; |
22 |
|
char *files_list; |
23 |
|
char *key; |
24 |
|
int udp_update_time; |
26 |
|
|
27 |
|
}ihost_state_t; |
28 |
|
|
29 |
< |
char* sock_comm(FILE *f, char* sendString){ |
29 |
> |
char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){ |
30 |
|
char *reply; |
31 |
< |
|
32 |
< |
fprintf(f, sendString); |
33 |
< |
fflush(f); |
32 |
< |
reply=fpgetline(f); |
31 |
> |
fprintf(f_w, "%s", sendString); |
32 |
> |
fflush(f_w); |
33 |
> |
reply=fpgetline(f_r); |
34 |
|
/* Returns pointer to static buffer */ |
35 |
|
return reply; |
36 |
|
} |
39 |
|
struct sockaddr_in addr; |
40 |
|
struct in_addr haddr; |
41 |
|
int sd; |
42 |
< |
FILE *fm_fd; |
42 |
> |
FILE *fm_fd_r, *fm_fd_w; |
43 |
|
char *reply; |
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); |
48 |
+ |
if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); |
49 |
+ |
if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
50 |
+ |
if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
51 |
+ |
if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
52 |
+ |
|
53 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
54 |
|
errf("Can't create AF_INET socket (%m)"); |
55 |
|
return -1; |
70 |
|
return -1; |
71 |
|
} |
72 |
|
|
73 |
< |
if ((fm_fd=fdopen(sd,"r+")) == NULL){ |
73 |
> |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
74 |
> |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
75 |
|
errf("Failed to open stream (%m)"); |
76 |
|
return -1; |
77 |
|
} |
78 |
< |
|
79 |
< |
reply=sock_comm(fm_fd, "STARTCONFIG\n"); |
78 |
> |
|
79 |
> |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
80 |
> |
errf("Failed to open stream (%m)"); |
81 |
> |
return -1; |
82 |
> |
} |
83 |
> |
|
84 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n"); |
85 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
86 |
|
errf("Server error"); |
87 |
|
return -1; |
88 |
|
} |
89 |
|
|
90 |
< |
reply=sock_comm(fm_fd, "LASTMODIFIED\n"); |
91 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
90 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n"); |
91 |
> |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
92 |
|
errf("Server error (%m)"); |
93 |
|
return -1; |
94 |
|
} |
95 |
< |
ihost_state->last_modified=atol(reply); |
95 |
> |
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
96 |
> |
errf("strdup failed (%m)"); |
97 |
> |
return -1; |
98 |
> |
} |
99 |
|
|
100 |
< |
reply=sock_comm(fm_fd, "FILELIST\n"); |
101 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
100 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); |
101 |
> |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
102 |
|
errf("Server error (%m)"); |
103 |
|
return -1; |
104 |
|
} |
107 |
|
return -1; |
108 |
|
} |
109 |
|
|
110 |
< |
reply=sock_comm(fm_fd, "FQDN\n"); |
111 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ |
110 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n"); |
111 |
> |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
112 |
|
errf("Server error (%m)"); |
113 |
|
return -1; |
114 |
|
} |
117 |
|
return -1; |
118 |
|
} |
119 |
|
|
120 |
< |
reply=sock_comm(fm_fd, "UDPUpdateTime\n"); |
120 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n"); |
121 |
|
if(reply== NULL){ |
122 |
|
errf("Server error (%m)"); |
123 |
|
return -1; |
126 |
|
ihost_state->udp_update_time=atoi(reply); |
127 |
|
} |
128 |
|
|
129 |
< |
reply=sock_comm(fm_fd, "TCPUpdateTime\n"); |
129 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n"); |
130 |
|
if(reply== NULL){ |
131 |
|
errf("Server error (%m)"); |
132 |
|
return -1; |
135 |
|
ihost_state->tcp_update_time=atoi(reply); |
136 |
|
} |
137 |
|
|
138 |
< |
reply=sock_comm(fm_fd, "ENDCONFIG\n"); |
138 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n"); |
139 |
|
if(reply== NULL){ |
140 |
|
errf("Server error (%m)"); |
141 |
|
return -1; |
142 |
|
} |
143 |
|
|
144 |
< |
|
144 |
> |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n"); |
145 |
> |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
146 |
> |
errf("Server error (%m)"); |
147 |
> |
return -1; |
148 |
> |
} |
149 |
> |
reply_ptr=strchr(reply,';'); |
150 |
> |
if (reply_ptr==NULL){ |
151 |
> |
errf("Incorrect data returned"); |
152 |
> |
return -1; |
153 |
> |
} |
154 |
> |
*reply_ptr='\0'; |
155 |
> |
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
156 |
> |
errf("strdup failed (%m)"); |
157 |
> |
return -1; |
158 |
> |
} |
159 |
> |
reply=++reply_ptr; |
160 |
> |
reply_ptr=strchr(reply,';'); |
161 |
> |
if (reply_ptr==NULL){ |
162 |
> |
errf("Incorrect data returned 2"); |
163 |
> |
return -1; |
164 |
> |
} |
165 |
> |
*reply_ptr='\0'; |
166 |
> |
ihost_state->server_udp_port=atoi(reply); |
167 |
> |
reply=++reply_ptr; |
168 |
> |
ihost_state->server_tcp_port=atoi(reply); |
169 |
> |
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
170 |
> |
errf("Incorrect data returned 3 "); |
171 |
> |
return -1; |
172 |
> |
} |
173 |
|
|
174 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
175 |
+ |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
176 |
+ |
errf("Server error (%m)"); |
177 |
+ |
return -1; |
178 |
+ |
} |
179 |
|
|
180 |
+ |
if(fclose(fm_fd_r) !=0){ |
181 |
+ |
errf("Failed to close FD (%m)"); |
182 |
+ |
return -1; |
183 |
+ |
} |
184 |
+ |
if(fclose(fm_fd_w) !=0){ |
185 |
+ |
errf("Failed to close FD (%m)"); |
186 |
+ |
return -1; |
187 |
+ |
} |
188 |
+ |
|
189 |
|
return 0; |
190 |
+ |
} |
191 |
|
|
192 |
+ |
int heartbeat(ihost_state_t *ihost_state){ |
193 |
+ |
struct sockaddr_in addr; |
194 |
+ |
struct in_addr haddr; |
195 |
+ |
int sd; |
196 |
+ |
FILE *fm_fd_r, *fm_fd_w; |
197 |
+ |
char *reply; |
198 |
+ |
int exitcode=0; |
199 |
+ |
|
200 |
+ |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
201 |
+ |
errf("Can't create AF_INET socket (%m)"); |
202 |
+ |
return -1; |
203 |
+ |
} |
204 |
+ |
|
205 |
+ |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
206 |
+ |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
207 |
+ |
return -1; |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
memset((char *)&addr, 0, sizeof addr); |
211 |
+ |
addr.sin_family = AF_INET; |
212 |
+ |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
213 |
+ |
addr.sin_port = htons(ihost_state->server_tcp_port); |
214 |
+ |
|
215 |
+ |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
216 |
+ |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
217 |
+ |
return -1; |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
221 |
+ |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
222 |
+ |
errf("Failed to open stream (%m)"); |
223 |
+ |
return -1; |
224 |
+ |
} |
225 |
+ |
|
226 |
+ |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
227 |
+ |
errf("Failed to open stream (%m)"); |
228 |
+ |
return -1; |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); |
232 |
+ |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
233 |
+ |
errf("Server error"); |
234 |
+ |
return -1; |
235 |
+ |
} |
236 |
+ |
|
237 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n"); |
238 |
+ |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
239 |
+ |
errf("Server error"); |
240 |
+ |
return -1; |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
244 |
+ |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
245 |
+ |
errf("Server error"); |
246 |
+ |
return -1; |
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
250 |
+ |
if (reply==NULL) { |
251 |
+ |
errf("Server error"); |
252 |
+ |
return -1; |
253 |
+ |
} |
254 |
+ |
if (strncasecmp(reply, "ERROR", 2) == 0){ |
255 |
+ |
/* Means the config has changed */ |
256 |
+ |
exitcode=RECONFIGURE_RETURN_CODE; |
257 |
+ |
} |
258 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n"); |
259 |
+ |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { |
260 |
+ |
errf("Server error"); |
261 |
+ |
return -1; |
262 |
+ |
} |
263 |
+ |
if (ihost_state->key!=NULL) free(ihost_state->key); |
264 |
+ |
|
265 |
+ |
if((ihost_state->key=strdup(reply)) == NULL){ |
266 |
+ |
errf("strdup failed (%m)"); |
267 |
+ |
return -1; |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
271 |
+ |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
272 |
+ |
errf("Server error (%m)"); |
273 |
+ |
return -1; |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
return exitcode; |
277 |
|
} |
278 |
|
|
279 |
< |
int main(){ |
279 |
> |
|
280 |
> |
int main(int argc, char **argv){ |
281 |
> |
ihost_state_t ihost_state; |
282 |
> |
|
283 |
> |
/* NULL'ify so i can tell if i need to free it or not */ |
284 |
> |
ihost_state.fm_host=NULL; |
285 |
> |
ihost_state.my_fqdn=NULL; |
286 |
> |
ihost_state.server_fqdn=NULL; |
287 |
> |
ihost_state.last_modified=NULL; |
288 |
> |
ihost_state.files_list=NULL; |
289 |
> |
ihost_state.key=NULL; |
290 |
> |
|
291 |
> |
errf_set_progname(argv[0]); |
292 |
> |
if(argc!=3){ |
293 |
> |
errf_usage("<host> <port>"); |
294 |
> |
exit(1); |
295 |
> |
} |
296 |
> |
|
297 |
> |
ihost_state.fm_host=argv[1]; |
298 |
> |
ihost_state.fm_port=atoi(argv[2]); |
299 |
> |
|
300 |
> |
if(ihost_configure(&ihost_state)!=0){ |
301 |
> |
errf("configure failed"); |
302 |
> |
} |
303 |
> |
|
304 |
|
|
305 |
|
return 0; |
306 |
|
} |