#include #include #include #include #include #include #include #include #include #include /*for file attributes */ #include #include #include "iserver.h" void Initialize_MainPtr() { int i; for(i=0; i< Max_number_servers; i++) MainPtr[i] = NULL; } /********************************************************************/ void Initialize_Servers( struct mirror_servers *sl) { char emp_str[] = ""; Initialize_MainPtr(); strcpy(sl->server_name , emp_str); strcpy(sl->IPaddress , emp_str); sl->next = NULL; } /************************************************************/ void pr_inet(char **listptr, int length){ struct in_addr *ptr; while ( (ptr = (struct in_addr *) *listptr++) != NULL) printf(" Internet address is: %s\n", inet_ntoa(*ptr) ); } /*********************************************************************/ void fix_names(struct mirror_servers *m_s){ char *new_line, *s; s = (char*)malloc(sizeof(char)); *s = '\n' ; if ( (new_line = strstr(m_s->server_name, s) ) != NULL ) *new_line = '\0' ; printf("%sCH \n", m_s->server_name) ; } /***********************************************************************/ /***********************************************************************/ char * ConvertAddress(char **listptr, int length){ struct in_addr *ptr; ptr = (struct in_addr *) *listptr++ ; return inet_ntoa(*ptr); } /***********************************************************************/ void Get_IP_address(void) { struct hostent *ptr; register struct hostent *hostptr; hostptr = gethostbyname(ms->server_name); /*exit(0);*/ if (hostptr == NULL ) { /*exit(0);*/ printf("unable to gethostbyname\n"); exit(1); } /*exit(0);*/ switch (hostptr->h_addrtype) { case AF_INET: /*printf("In case AF..\n"); */ pr_inet(hostptr->h_addr_list, hostptr->h_length); /*printf("to convert addr...\n");*/ ms->IPaddress=(char *)malloc(20); strcpy(ms->IPaddress,ConvertAddress(hostptr->h_addr_list, hostptr->h_length)); printf("GetIP: addr is %s\n", ms->IPaddress); break; default: printf("unknown address type\n"); break; } } /*********************************************************************/ void Configure_Servers(char *my_input_file) { FILE *sptr; char *serv; char MyBuffer[MaxLine] ; int j; int next_set_servers = 0 ; /* counter for unrelated sites*/ int n = 0 ; /* server name length */ struct mirror_servers *prev, ss; prev = NULL ; if ( (sptr = fopen(my_input_file, "r") ) == NULL ) printf(" Cannot open file %s \n, input_file_name"); /* reads one server per line; a blank line indcates no more mirrors for this server */ ms = (struct mirror_servers *)malloc(sizeof(struct mirror_servers)); if (ms == NULL) exit(0); MainPtr[next_set_servers] = ms ; while (fgets(MyBuffer, MaxLine, sptr) != NULL) { if ( (n = strlen(MyBuffer) ) <=1) { /*balnk line was read */ next_set_servers++; MainPtr[next_set_servers] = ms ; prev->next = NULL; printf("------\n"); } else { ms->server_name= (char*) malloc( strlen(MyBuffer) ); strncpy(ms->server_name, MyBuffer, strlen(MyBuffer)-1); ms->server_name[strlen(MyBuffer)-1] = '\0'; Get_IP_address(); /*run DNS to get IP addr and save it in address field*/ prev = ms; ms = (struct mirror_servers *)malloc(sizeof(struct mirror_servers)); if (ms == NULL) exit(0); prev->next = ms; /*current one will point to new */ n=0; for(j=0; j < MaxLine; j++) MyBuffer[j]= '\0'; } } /* end while */ prev->next = NULL; /*last structure points to NULL */ } /* end function Configure */ /***************************************************************/ void print_all_servers(void){ int i= 0; char *new_line; struct mirror_servers *ms; while(MainPtr[i] != NULL){ printf("Server set %d: \n", i+1); ms = MainPtr[i]; while( ms != NULL){ printf("%s ", ms->server_name) ; printf("%s ", ms->IPaddress); printf("%f ", ms->delay); printf("%f ", ms->b_Result); printf("%f \n", ms->c_Result) ; ms = ms->next; } printf("------------\n"); i++; } return; } /************************************************************/ void Calculate_weights(void){ FILE *allPtr; char my_all_probes_file[] = "/tmp/statFile"; float weight; int i= 0; struct mirror_servers *ms; if ( (allPtr = fopen(my_all_probes_file, "w") ) == NULL ) printf(" Cannot open file %s \n", my_all_probes_file); while(MainPtr[i] != NULL){ ms = MainPtr[i]; while( ms != NULL){ weight = (float)(( 1 + ms->delay) * 10000)/(1+ ms->b_Result + ms->c_Result); weight = (float)(1+ ms->b_Result + ms->c_Result)/(( 1 + ms->delay) * 10000); weight = weight * 100 ; fprintf( allPtr,"%s %f\n", ms->server_name, weight); ms = ms->next; } i++; } fclose(allPtr); return; } /************************************************************/ int search_name(char *h_name){ struct mirror_servers *ptr; int i = 0; int FOUND = FALSE; ptr = MainPtr[i]; /*PTR_FOUND = malloc(sizeof(struct mirror_servers));*/ /* PTR_FOUND = NULL;*/ while( (MainPtr[i] != NULL && strcmp(ptr->server_name, h_name) != 0 ) ) { printf("in search %s\n", ptr->server_name); ptr = ptr->next; if (ptr == NULL ) { i = i + 1; ptr = MainPtr[i]; } } if ( MainPtr[i] == NULL ) { FOUND = FALSE; printf("returning host not found\n"); PTR_FOUND = NULL; i = -1; /*return FAILURE;*/ return i; } else { FOUND = TRUE; printf("In search..%s row is %d\n", MainPtr[i]->server_name, i); PTR_FOUND = ptr; return i; } }/*end search_name */ /*********************************************************/ void prompt_user_for_hostname(void){ /*myhost = malloc(sizeof(MaxLine) );*/ printf("Enter hostname:"); scanf(" %s", myhost); return ; } /**********************************************************/ char *erase_string(char cleanup[] ) { int i; i = 0; while (cleanup[i] != '\0' ) { cleanup[i] = '\0' ; i++; } return cleanup; } /***********************************************************/ void Get_Probe_Result( struct mirror_servers *current_ms ) { char MyBuffer[MaxLine] ; int i, j ; char probeResult[] = "Final" ; FILE *rptr; char my_result_file[] = "res.txt"; if ( (rptr = fopen(my_result_file, "r") ) == NULL ) printf(" Cannot open file %s \n", my_result_file); /*current_ms->b_Result = malloc(sizeof(Result_length) );*/ printf("In Get_probe...\n"); while (fgets(MyBuffer, MaxLine, rptr) != NULL) { printf("MyBuffer %s\n", MyBuffer); if ( strstr(MyBuffer, probeResult) != NULL ) { printf("match: %s \n", MyBuffer); j= 0; for (i= 17; i < 28; i++){ /*current_ms->b_Result[j] = MyBuffer[i];*/ j++; } /*current_ms->b_Result[j] = '\0';*/ /*printf("server:%s probe:%s\n", current_ms->server_name, current_ms->b_Result);*/ } /*end if */ } fclose(rptr); return ; } /************************************************************/ int Read_Probe_FD_Into_Socket(u_short probe_port){ /*printf("In util: port_probe = %d, addr = %d\n", from.sin_port, from.sin_addr );*/ int sock; int rd; /* file descriptor w/results obtained from bprobe/cprobe */ int n ; int count = 0; int length; struct msghdr msg; struct sockaddr_in to, from; printf("In Read_probe FD_into_socket...\n"); /*****************/ from.sin_family = AF_INET; /* create socket from which to read */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* create name structure with wildcard using INADDR_ANY */ from.sin_addr.s_addr = INADDR_ANY; from.sin_port = 0; /* port no. < 1024 IPPORT_RESERVED is for privilleged processes */ /* here we just initialize it, it will be assigned value after call */ /* getsockname() */ if (bind(sock, &from, sizeof(struct sockaddr_in))) { perror("util:binding name to datagram socket"); exit(1); } length = sizeof(from); if (getsockname(sock, &from, &length)) { perror("getting socket name"); exit(1); } /* Find assigned port value and print out. */ printf("util:socket has port #%d\n", ntohs(from.sin_port)); /***********************/ to.sin_family = AF_INET; to.sin_port = probe_port ; to.sin_addr.s_addr = from.sin_addr.s_addr; /* INITIALIZE mshhdr fields */ /*sendto(sock, &m, sizeof(m), 0, &to, sizeof(to));*/ connect(sock, &to, sizeof(sock)); rd = 5; return (rd); } /***********************************************************/ /************************************************************/ /* This function will get the probe command ./bprobe or ./cprobe as input. It will get a server name from the database and attach it to the command and make a system call to get probe values for all servers in database. The result of each probe are saved in res.txt file */ /*** ****/ void Run_Probe( char *probe_command) { char MyBuffer[MaxLine]; FILE *rptr, *cptr; char my_result_file[] = "res.txt"; char c_result_file[] = "c_res.txt"; int j; int i= 0; /*char my_result_file[] = "res.txt";*/ struct mirror_servers *ms, *delay_saved_ms; char *comm; char ispace[] = " "; char iresult_file[] = " "; /* save result of probe here*/ /*char iresult_file[] = "> res.txt &"; /* save result of probe here*/ /*struct stat *buf;*/ int fd ; u_short port; int n= 0; /* */ char buf[MaxLine]; struct sockaddr_in Addr; comm = (char*)malloc(sizeof(Full_command) ); erase_string(comm); strncpy(comm, probe_command, strlen(probe_command)-1 ); /*strncpy(comm, probe_command, 7);*/ printf("before loop command is:%s\n", comm); delay_saved_ms = NULL; /*if ( (result_fd = open( my_result_file, O_RDONLY ) ) < 0) printf("error opening file %s \n", my_result_file);*/ while(MainPtr[i] != NULL){ ms = MainPtr[i]; while( ms != NULL){ /*printf("server is : %s \n", ms->server_name);*/ /* following strcat lines form the command ./bprobe servername OR ./cprobe servername */ strcat(comm, ispace); strcat(comm, ms->server_name) ; printf("comm is:%s\n", comm); /*strcat(comm, ispace);*/ /*strcat(comm, iresult_file);*/ system(comm); erase_string(comm); /* erases current system command string */ strncpy(comm, probe_command, strlen(probe_command)-1); printf("COMM: %s\n", comm); delay_saved_ms = ms; ms = ms->next; } i++; } printf("ALL PROBES DONE\n"); free(comm); /*scanf("Bprobe port # = %d\n" , &port);*/ /*scanf("Bprobe Addr = %ld\n", &Addr.sin_addr );*/ /*printf("In util: bport= %d %ld\n", ntohs(port), inet_ntoa(Addr.sin_addr) );*/ /*print_all_servers();*/ return; } /******************************************************/ char *printsockaddr(struct sockaddr_in *arg) { struct hostent *hp ; printf("sockaddr_in: "); /*printf("Domain=%d, ", arg->sin_family);*/ hp=gethostbyaddr((char*)&(arg->sin_addr), sizeof(arg->sin_addr), AF_INET); printf("Hostname=%s, ", hp->h_name); printf("Port=%d, ", ntohs(arg->sin_port)); printf("Address=%s,\n", inet_ntoa(arg->sin_addr)); return (hp->h_name); }