--- projects/cms/source/ihost/ihost.c 2002/05/10 17:11:30 1.1 +++ projects/cms/source/ihost/ihost.c 2002/05/10 20:05:05 1.2 @@ -24,12 +24,11 @@ typedef struct{ }ihost_state_t; -char* sock_comm(FILE *f, char* sendString){ +char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){ char *reply; - - fprintf(f, sendString); - fflush(f); - reply=fpgetline(f); + fprintf(f_w, "%s", sendString); + fflush(f_w); + reply=fpgetline(f_r); /* Returns pointer to static buffer */ return reply; } @@ -38,8 +37,9 @@ int ihost_configure(ihost_state_t *ihost_state){ struct sockaddr_in addr; struct in_addr haddr; int sd; - FILE *fm_fd; + FILE *fm_fd_r, *fm_fd_w; char *reply; + char *reply_ptr; if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { errf("Can't create AF_INET socket (%m)"); @@ -61,26 +61,32 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - if ((fm_fd=fdopen(sd,"r+")) == NULL){ + /* 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; } - - reply=sock_comm(fm_fd, "STARTCONFIG\n"); + + 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, "STARTCONFIG\n"); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { errf("Server error"); return -1; } - reply=sock_comm(fm_fd, "LASTMODIFIED\n"); - if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ + reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n"); + if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ errf("Server error (%m)"); return -1; } ihost_state->last_modified=atol(reply); - reply=sock_comm(fm_fd, "FILELIST\n"); - if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ + reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); + if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ errf("Server error (%m)"); return -1; } @@ -89,8 +95,8 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - reply=sock_comm(fm_fd, "FQDN\n"); - if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){ + reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n"); + if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ errf("Server error (%m)"); return -1; } @@ -99,7 +105,7 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - reply=sock_comm(fm_fd, "UDPUpdateTime\n"); + reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n"); if(reply== NULL){ errf("Server error (%m)"); return -1; @@ -108,7 +114,7 @@ int ihost_configure(ihost_state_t *ihost_state){ ihost_state->udp_update_time=atoi(reply); } - reply=sock_comm(fm_fd, "TCPUpdateTime\n"); + reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n"); if(reply== NULL){ errf("Server error (%m)"); return -1; @@ -117,20 +123,70 @@ int ihost_configure(ihost_state_t *ihost_state){ ihost_state->tcp_update_time=atoi(reply); } - reply=sock_comm(fm_fd, "ENDCONFIG\n"); + reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n"); if(reply== NULL){ errf("Server error (%m)"); return -1; } - + reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n"); + if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ + errf("Server error (%m)"); + return -1; + } + reply_ptr=strchr(reply,';'); + if (reply_ptr==NULL){ + errf("Incorrect data returned"); + return -1; + } + *reply_ptr='\0'; + if((ihost_state->server_fqdn=strdup(reply)) == NULL){ + errf("strdup failed (%m)"); + return -1; + } + reply=++reply_ptr; + reply_ptr=strchr(reply,';'); + if (reply_ptr==NULL){ + errf("Incorrect data returned 2"); + return -1; + } + *reply_ptr='\0'; + ihost_state->server_udp_port=atoi(reply); + reply=++reply_ptr; + ihost_state->server_tcp_port=atoi(reply); + if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ + errf("Incorrect data returned 3 "); + 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; + } + if(fclose(fm_fd_r) !=0){ + errf("Failed to close FD (%m)"); + return -1; + } + if(fclose(fm_fd_w) !=0){ + errf("Failed to close FD (%m)"); + return -1; + } + return 0; } int main(){ + ihost_state_t ihost_state; + + ihost_state.fm_host=strdup("kernow.ukc.ac.uk"); + ihost_state.fm_port=4567; + + if(ihost_configure(&ihost_state)!=0){ + errf("configure failed"); + } return 0; }