| 1 |
#include <stdio.h> |
| 2 |
#include <sys/socket.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <syslog.h> |
| 6 |
#include <netinet/in.h> |
| 7 |
#include <ukcprog.h> |
| 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; |
| 16 |
|
| 17 |
char *my_fqdn; |
| 18 |
char *server_fqdn; |
| 19 |
int server_udp_port; |
| 20 |
int server_tcp_port; |
| 21 |
char *last_modified; |
| 22 |
char *files_list; |
| 23 |
char *key; |
| 24 |
int udp_update_time; |
| 25 |
int tcp_update_time; |
| 26 |
|
| 27 |
}ihost_state_t; |
| 28 |
|
| 29 |
char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){ |
| 30 |
char *reply; |
| 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 |
} |
| 37 |
|
| 38 |
int ihost_configure(ihost_state_t *ihost_state){ |
| 39 |
struct sockaddr_in addr; |
| 40 |
struct in_addr haddr; |
| 41 |
int sd; |
| 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->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; |
| 55 |
} |
| 56 |
|
| 57 |
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
| 58 |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
memset((char *)&addr, 0, sizeof addr); |
| 63 |
addr.sin_family = AF_INET; |
| 64 |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
| 65 |
addr.sin_port = htons(ihost_state->fm_port); |
| 66 |
|
| 67 |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
| 68 |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
| 69 |
return -1; |
| 70 |
} |
| 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)"); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
| 79 |
errf("Failed to open stream (%m)"); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
|
| 83 |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n"); |
| 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"); |
| 90 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
| 91 |
errf("Server error (%m)"); |
| 92 |
return -1; |
| 93 |
} |
| 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=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)){ |
| 115 |
errf("Server error (%m)"); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
| 119 |
errf("strdup failed (%m)"); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n"); |
| 124 |
if(reply== NULL){ |
| 125 |
errf("Server error (%m)"); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
| 129 |
ihost_state->udp_update_time=atoi(reply); |
| 130 |
} |
| 131 |
|
| 132 |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n"); |
| 133 |
if(reply== NULL){ |
| 134 |
errf("Server error (%m)"); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
| 138 |
ihost_state->tcp_update_time=atoi(reply); |
| 139 |
} |
| 140 |
|
| 141 |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n"); |
| 142 |
if(reply== NULL){ |
| 143 |
errf("Server error (%m)"); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n"); |
| 148 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
| 149 |
errf("Server error (%m)"); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
reply_ptr=strchr(reply,';'); |
| 153 |
if (reply_ptr==NULL){ |
| 154 |
errf("Incorrect data returned"); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
*reply_ptr='\0'; |
| 158 |
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
| 159 |
errf("strdup failed (%m)"); |
| 160 |
return -1; |
| 161 |
} |
| 162 |
reply=++reply_ptr; |
| 163 |
reply_ptr=strchr(reply,';'); |
| 164 |
if (reply_ptr==NULL){ |
| 165 |
errf("Incorrect data returned 2"); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
*reply_ptr='\0'; |
| 169 |
ihost_state->server_udp_port=atoi(reply); |
| 170 |
reply=++reply_ptr; |
| 171 |
ihost_state->server_tcp_port=atoi(reply); |
| 172 |
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
| 173 |
errf("Incorrect data returned 3 "); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); |
| 178 |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
| 179 |
errf("Server error (%m)"); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
|
| 183 |
if(fclose(fm_fd_r) !=0){ |
| 184 |
errf("Failed to close FD (%m)"); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
if(fclose(fm_fd_w) !=0){ |
| 188 |
errf("Failed to close FD (%m)"); |
| 189 |
return -1; |
| 190 |
} |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int heartbeat(ihost_state_t *ihost_state){ |
| 196 |
struct sockaddr_in addr; |
| 197 |
struct in_addr haddr; |
| 198 |
int sd; |
| 199 |
FILE *fm_fd_r, *fm_fd_w; |
| 200 |
char *reply; |
| 201 |
int exitcode=0; |
| 202 |
|
| 203 |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
| 204 |
errf("Can't create AF_INET socket (%m)"); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
| 209 |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
memset((char *)&addr, 0, sizeof addr); |
| 214 |
addr.sin_family = AF_INET; |
| 215 |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
| 216 |
addr.sin_port = htons(ihost_state->server_tcp_port); |
| 217 |
|
| 218 |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
| 219 |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
| 224 |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
| 225 |
errf("Failed to open stream (%m)"); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
| 230 |
errf("Failed to open stream (%m)"); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); |
| 235 |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
| 236 |
errf("Server error"); |
| 237 |
return -1; |
| 238 |
} |
| 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", 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"); |
| 250 |
return -1; |
| 251 |
} |
| 252 |
|
| 253 |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
| 254 |
if (reply==NULL) { |
| 255 |
errf("Server error"); |
| 256 |
return -1; |
| 257 |
} |
| 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", 5) == 0) ) { |
| 264 |
errf("Server error"); |
| 265 |
return -1; |
| 266 |
} |
| 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, "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>"); |
| 312 |
exit(1); |
| 313 |
} |
| 314 |
|
| 315 |
ihost_state.fm_host=argv[1]; |
| 316 |
ihost_state.fm_port=atoi(argv[2]); |
| 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 |
|