/** * returns: * 0 if no High Availability * 1 if High Availability * -1 if config error */ int prepare_ha(void) { use_ha=0; if(!(jain_ping_config || servlet_ping_config)){ jain_pings_lost=servlet_pings_lost=0; return 0; } if(parse_ping(jain_ping_config,&jain_ping_period,&jain_pings_lost,&jain_ping_timeout)<0) goto error; if(parse_ping(servlet_ping_config,&servlet_ping_period,&servlet_pings_lost,&servlet_ping_timeout)<0) goto error; LM_DBG("jain: pinging period :%d max pings lost:%d ping timeout:%d\n", jain_ping_period,jain_pings_lost,jain_ping_timeout); LM_DBG("servlet: pinging period:%d max pings lost:%d ping timeout:%d\n", servlet_ping_period,servlet_pings_lost,servlet_ping_timeout); use_ha=1; return 1; error: return -1; }
int main(int argc, char**argv){ //log files logs = fopen("/var/log/weperf/sensor/sensor.log", "a+"); int clientSocket; char buffer[1024]; char recv_buff[1024]; char send_buff[1024]; struct sockaddr_in serverAddr; socklen_t addr_size; //parse configurations from file if (ini_parse("/etc/weperf/sensor-config.ini", handler, &config) < 0) { client_log("Error", "Can't load 'config.ini'\n"); return 1; } //casue zombies to be reaped automatically if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { perror(0); exit(1); } /*---- Create the socket. The three arguments are: ----*/ /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */ clientSocket = socket(PF_INET, SOCK_STREAM, 0); /*---- Configure settings of the server address struct ----*/ /* Address family = Internet */ serverAddr.sin_family = AF_INET; /* Set port number, using htons function to use proper byte order */ serverAddr.sin_port = htons(config.server_port); /* Set IP address to localhost */ serverAddr.sin_addr.s_addr = inet_addr(config.server_addr); /* Set all bits of the padding field to 0 */ memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero); /*---- Connect the socket to the server using the address struct ----*/ addr_size = sizeof serverAddr; if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)){ client_log("Error", "Connecting to server - %s", strerror(errno)); return 0; } client_log("Info", "Connected to server"); //get IP of interface - add interface to config file struct ifreq ifr; strncpy(ifr.ifr_name, config.interface, IFNAMSIZ-1); ioctl(clientSocket, SIOCGIFADDR, &ifr); struct in_addr local_ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; printf("Local IP %s\n", inet_ntoa(local_ip)); //timeout struct timeval tv; int bytes = 1; //receive loop while(bytes){ FILE *fp; fd_set rfds; FD_ZERO(&rfds); FD_SET (clientSocket, &rfds); tv.tv_sec = 60; tv.tv_usec = 0; int ready = select(clientSocket + 1, &rfds, NULL, NULL, &tv); if(ready == -1 ){ }else if(ready){ bytes = recv(clientSocket, recv_buff, sizeof(recv_buff),0); struct srrp_request * request = (struct srrp_request *) recv_buff; if(request->type == SRRP_HB){ //heatbeat request printf("Received hb request - %d bytes\n", bytes); //build response response_init((struct srrp_response *) send_buff, request->type, SRRP_SCES, request->dst_id, request->m_id); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); }else if(request->type == SRRP_ETHER){ //ethernet request printf("Received ether request - %d bytes\n", bytes); //build response response_init((struct srrp_response *) send_buff, request->type, SRRP_SCES, request->dst_id, request->m_id); //bit of a hack to break from srrp to send MAC address memcpy(&((struct srrp_response *)send_buff)->results[0], config.ether, strlen(config.ether) + 1); //also for the ip memcpy(&((struct srrp_response *)send_buff)->results[3], &local_ip, sizeof(local_ip)); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff) /*header*/ + 100 /*mac*/ + sizeof(local_ip) /*ip*/, 0); }else if(request->type == SRRP_BW){ client_log("Info", "Recevived iperf request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "iperf -c %s -p %d -t %d -y C"; char cmd[100]; //default params int dur = 10; //get parameters int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_DUR){ dur = request->params[i].value; }else{ client_log("Error", "Invalid iperf parameter"); } } //build command sprintf(cmd, cmd_fmt, inet_ntoa(request->dst_ip), config.tcp_iperf_port, dur); printf("%s\n", cmd); fp = popen(cmd, "r"); if(fp == NULL){ client_log("Error", "Failed to run command '%s'", cmd); _exit(1); } //get otuput - single line because of -y C flage char result[100]; while(fgets(result, sizeof(result)-1, fp) != NULL){} int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "iperf failed exit status %d", exit_status); //should send resonpse with failed success code _exit(1); }else{ //build response if(parse_iperf(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result)){ client_log("Error", "Failed to parse iperf response"); _exit(0); } client_log("Info", "Sending iperf results"); send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); } _exit(0); } }else if(request->type == SRRP_RTT){ client_log("Info", "Received ping request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * command_fmt = "ping -c %d %s"; char command[100]; //default params int itterations = 5; //load in params int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_ITTR){ itterations = request->params[i].value; }else{ client_log("Error", "Invalid ping parameter"); } } //build command sprintf(command, command_fmt, itterations, inet_ntoa(request->dst_ip)); printf("%s\n", command); fp = popen(command , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", command); _exit(1); } //get otuput - single line because of -y C flage char result[100]; while(fgets(result, sizeof(result)-1, fp) != NULL){} int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "command failed exit status %d", exit_status); //should respond }else{ if(parse_ping(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result)){ client_log("Error", "Failed to parse ping response"); _exit(0); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); _exit(0); } } }else if(request->type == SRRP_UDP){ client_log("Info", "Received UDP iperf request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "iperf -c %s -u -p %d -b %dK -l %d -t %d -S %d -y C"; char cmd[100]; //default params int speed = 1; int size = 1470; int duration = 10; int dscp = 0; //load in params int i; for(i = 0; i < request->length; i++){ if(request->params[i].param == SRRP_SPEED){ speed = request->params[i].value; }else if(request->params[i].param == SRRP_SIZE){ size = request->params[i].value; }else if(request->params[i].param == SRRP_DUR){ duration = request->params[i].value; }else if(request->params[i].param == SRRP_DSCP){ dscp = request->params[i].value; }else{ client_log("Error", "Invalid udp parameter"); } } sprintf(cmd, cmd_fmt, inet_ntoa(request->dst_ip), config.udp_iperf_port, speed, size, duration, dscp); printf("%s\n", cmd); fp = popen(cmd , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", cmd); _exit(1); } //get otuput - single line because of -y C flage char result[200]; while(fgets(result, sizeof(result)-1, fp) != NULL) printf("%s\n", result); int exit_status = pclose(fp); if(exit_status != 0){ client_log("Error", "udp iperf failed exit status %d", exit_status); //should send resonpse with failed success code //parse_failure() _exit(1); }else{ if(parse_udp(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, result, speed, dscp)){ client_log("Error", "Failed to parse udp response"); _exit(0); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); client_log("Info", "Sending udp iperf results"); _exit(0); } } }else if(request->type == SRRP_DNS){ client_log("Info", "Received dns request - %d bytes", bytes); if(fork() == 0){ //listen for SIGCHLD so pclose resturns the status signal(SIGCHLD, SIG_DFL); char * cmd_fmt = "nslookup %s %s"; char cmd[100]; char * domain_name = NULL; char * server = NULL; int i = 0; while(i < request->length){ if(request->params[i].param == SRRP_DN){ i = get_param_string(&domain_name, request, i); }else if(request->params[i].param == SRRP_SERVER){ i = get_param_string(&server, request, i); }else{ client_log("Error", "Invalid param"); } } if(!server || strcmp(server, "default") == 0) server = ""; if(!domain_name || strcmp(server, "default") == 0) domain_name = (char *)config.nslookup_addr; sprintf(cmd, cmd_fmt, domain_name, server); printf("%s\n", cmd); struct timeval start, end; float mtime, secs, usecs; gettimeofday(&start, NULL); fp = popen(cmd , "r"); if(fp == NULL){ client_log("Error", "Failed to run command %s", cmd); _exit(1); } //get otuput char result[200]; while(fgets(result, sizeof(result)-1, fp) != NULL){} //work out resolution time gettimeofday(&end, NULL); secs = end.tv_sec - start.tv_sec; usecs = end.tv_usec - start.tv_usec; mtime = ((secs) * 1000 + usecs/1000.0) + 0.5; int exit_status = pclose(fp); if(exit_status != 0){ client_log("Info", "DNS status failure - exit status %d", exit_status); parse_failure(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff); }else{ client_log("Info", "DNS status sucess - exit status %d", exit_status); parse_dns(request->type, request->dst_id, request->m_id, (struct srrp_response *) send_buff, mtime); } send(clientSocket, send_buff, response_size((struct srrp_response *) send_buff), 0); client_log("Info", "Sending dns response"); _exit(0); } }else{ //unrecognised data client_log("Error", "Recevied unrecognised data - %d - %d bytes", request->type, bytes); } }else{ //timeout //dont care about timeout --- keep running client_log("Error", "Timeout"); break; } } client_log("Info", "Recevied empty data, closing connection"); fclose(logs); close(clientSocket); return 0; }