--- projects/cms/source/ihost/ihost.c 2002/05/10 21:32:38 1.3 +++ projects/cms/source/ihost/ihost.c 2002/05/12 12:00:33 1.4 @@ -8,6 +8,8 @@ #include #include +#define RECONFIGURE_RETURN_CODE 2 + typedef struct{ int fm_port; char *fm_host; @@ -16,7 +18,7 @@ typedef struct{ char *server_fqdn; int server_udp_port; int server_tcp_port; - long last_modified; + char *last_modified; char *files_list; char *key; int udp_update_time; @@ -83,7 +85,10 @@ int ihost_configure(ihost_state_t *ihost_state){ errf("Server error (%m)"); return -1; } - ihost_state->last_modified=atol(reply); + if((ihost_state->last_modified=strdup(reply)) == NULL){ + errf("strdup failed (%m)"); + return -1; + } reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ @@ -176,6 +181,93 @@ int ihost_configure(ihost_state_t *ihost_state){ return 0; } + +int heartbeat(ihost_state_t *ihost_state){ + struct sockaddr_in addr; + struct in_addr haddr; + int sd; + FILE *fm_fd_r, *fm_fd_w; + char *reply; + int exitcode=0; + + if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { + errf("Can't create AF_INET socket (%m)"); + return -1; + } + + if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ + errf("Failed to resolve address %s (%m)", ihost_state->fm_host); + return -1; + } + + memset((char *)&addr, 0, sizeof addr); + addr.sin_family = AF_INET; + memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); + addr.sin_port = htons(ihost_state->server_tcp_port); + + if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { + errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); + return -1; + } + + /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ + if ((fm_fd_r=fdopen(sd,"r")) == NULL){ + errf("Failed to open stream (%m)"); + return -1; + } + + if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ + errf("Failed to open stream (%m)"); + return -1; + } + + reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + errf("Server error"); + return -1; + } + + reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n"); + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + errf("Server error"); + return -1; + } + + reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); + if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { + errf("Server error"); + return -1; + } + + reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); + if (reply==NULL) { + errf("Server error"); + return -1; + } + if (strncasecmp(reply, "ERROR", 2) == 0){ + /* Means the config has changed */ + exitcode=RECONFIGURE_RETURN_CODE; + } + reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n"); + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + errf("Server error"); + return -1; + } + + if((ihost_state->key=strdup(reply)) == NULL){ + errf("strdup failed (%m)"); + return -1; + } + + reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); + if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ + errf("Server error (%m)"); + return -1; + } + + return exitcode; +} + int main(int argc, char **argv){ ihost_state_t ihost_state;