bard_window *bard_make_help_window(bard_reader *br) { bard_window *hw; int indent = 10; char *bhs; hw = bard_window_new("help", br->display->screen_width-(2*indent), br->display->screen_height-(2*indent), br->display->screen->format); hw->x_offset = indent; hw->y_offset = indent; hw->font_name = cst_strdup(get_param_string(br->config,"-font",BARD_DEFAULT_FONT)); hw->font_size = get_param_int(br->config,"-help_font_size", get_param_int(br->config,"-font_size", BARD_DEFAULT_FONT_SIZE)); /* We assume we can open this font, as if we couldn't when initializing */ /* the display window, we'd have failed by now */ hw->font = bard_window_open_font(hw->font_name,hw->font_size); hw->background_color = bard_color_get(br->colors, get_param_string(br->config,"-help_background_color","cornsilk")); hw->foreground_color = bard_color_get(br->colors, get_param_string(br->config,"-help_foreground_color","steelblue")); hw->highlight_color = bard_color_get(br->colors, get_param_string(br->config,"-help_highlight_color","blue")); hw->tm=10; hw->bm=10; hw->tlm=10; hw->trm=10; bhs = cst_alloc(char,cst_strlen(bard_help_string)+ cst_strlen(BARD_PROJECT_VERSION)+ cst_strlen(BARD_PROJECT_STATE)+ cst_strlen(BARD_PROJECT_DATE)+1); cst_sprintf(bhs,bard_help_string,BARD_PROJECT_VERSION, BARD_PROJECT_STATE, BARD_PROJECT_DATE); hw->ts = ts_open_string(bhs, cst_ts_default_whitespacesymbols, "", cst_ts_default_prepunctuationsymbols, cst_ts_default_postpunctuationsymbols); hw->ts_mode = "literal"; cst_free(bhs); hw->update = bard_window_help_update; /* Put something on the screen */ bard_window_display_from_pos(hw,0); return hw; }
static __inline__ gchar * get_media_path_stored(const gchar *path, gint type, gint *ls) { static gint seq = 0; gchar *v, *v2, *new_path; gint ch, level; if (get_path_ch_and_level(path, &ch, &level)) return g_strdup(path); v = get_param_string(path, "dev=[A-Z]{3}-[A-Z]{3}-\\d{8}"); if (!v) {/* dev=MSS-ID, returned */ return g_strdup(path); } v2 = get_param_string(path, "recordType=\\d+&startTime=\\d{14}+&endTime=\\d{14}+"); if (!v2) { g_free(v); return g_strdup(path); } new_path = g_strdup_printf("/%s/media=%d/channel=%d&level=%d/seq=%d/%s", v, type, ch, level, ++seq, v2); g_free(v); g_free(v2); *ls = 1; return new_path; }
static __inline__ gint get_path_ch_and_level(const gchar *path, gint *chp, gint *lp) { gint ch = -1, level = -1; gchar *v; v = get_param_string(path, "channel=\\d+"); if (v) { sscanf(v, "channel=%d", &ch); g_free(v); } v = get_param_string(path, "level=\\d+"); if (v) { sscanf(v, "level=%d", &level); g_free(v); } if (ch >= 0 && level >= 0) { *chp = ch; *lp = level; return 0; } return -1; }
static void add_raw_data(cst_utterance *u, const char *raw_data, cst_features *attributes) { /* Add all tokens in raw _data to u */ cst_tokenstream *ts; cst_relation *r; cst_item *t; const char *token; r = utt_relation_create(u,"Token"); ts = ts_open_string(raw_data, get_param_string(u->features,"text_whitespace",NULL), get_param_string(u->features,"text_singlecharsymbols",NULL), get_param_string(u->features,"text_prepunctuation",NULL), get_param_string(u->features,"text_pospunctuation",NULL)); while (!(ts_eof(ts))) { t = relation_append(r,NULL); feat_copy_into(item_feats(t),attributes); token = ts_get(ts); if (cst_strlen(token) > 0) { t = relation_append(r,NULL); item_set_string(t,"name",token); item_set_string(t,"whitespace",ts->whitespace); item_set_string(t,"prepunctuation",ts->prepunctuation); item_set_string(t,"punc",ts->postpunctuation); } } }
cst_utterance *default_tokenization(cst_utterance *u) { const char *text,*token; cst_tokenstream *fd; cst_item *t; cst_relation *r; text = utt_input_text(u); r = utt_relation_create(u,"Token"); fd = ts_open_string(text, get_param_string(u->features,"text_whitespace",NULL), get_param_string(u->features,"text_singlecharsymbols",NULL), get_param_string(u->features,"text_prepunctuation",NULL), get_param_string(u->features,"text_postpunctuation",NULL)); while(!ts_eof(fd)) { token = ts_get(fd); if (cst_strlen(token) > 0) { t = relation_append(r,NULL); item_set_string(t,"name",token); item_set_string(t,"whitespace",fd->whitespace); item_set_string(t,"prepunctuation",fd->prepunctuation); item_set_string(t,"punc",fd->postpunctuation); item_set_int(t,"file_pos",fd->file_pos); item_set_int(t,"line_number",fd->line_number); } } ts_close(fd); return u; }
int main(int argc, char **argv) { cst_wave *nw, *all; cst_val *files; const cst_val *w; cst_val *wavelist; cst_features *args; int i,j; float ntime; int stime; const char *nwfile; args = new_features(); files = cst_args(argv,argc, "usage: combine_waves OPTIONS\n" "Combine waves into single waveform\n" "-o <string> Output waveform\n" "-f <int> Input sample rate (for raw input)\n" "-itype <string> Input type, raw or headered\n" "-wavelist <string> File containing times and wave filenames\n", args); wavelist = get_wavelist(get_param_string(args,"-wavelist","-")); if (wavelist == 0) return -1; all = new_wave(); for (w = wavelist; w; w = val_cdr(w)) { ntime = decode_time(val_string(val_car(w))); nwfile = val_string(val_car(val_cdr(w))); nw = new_wave(); if (cst_wave_load_riff(nw,nwfile) != CST_OK_FORMAT) { fprintf(stderr, "combine_waves: can't read file or wrong format \"%s\"\n", nwfile); continue; } stime = ntime * nw->sample_rate; cst_wave_resize(all,stime+nw->num_samples,1); for (i=0,j=stime; i<nw->num_samples; i++,j++) { /* this will cause overflows */ all->samples[j] += nw->samples[i]; } delete_wave(nw); } cst_wave_save_riff(all,get_param_string(args,"-o","-")); return 0; }
static __inline__ gchar * get_media_path(const gchar *path, gint *ls, gint *ls_store) { gint media_type; gchar *v; *ls = 0; if (strchr(path, '.')) /* .mp3 .mp4 ?*/ return g_strdup(path); v = get_param_string(path, "media=\\d+"); if (v) { media_type = atoi(v + 6); g_free(v); switch (media_type) { case 0: /* media=0 */ *ls_store = 0; return get_media_path_live(path, ls); default: *ls_store = 1; return get_media_path_stored(path, media_type, ls); } } return g_strdup(path); }
cst_utterance *join_units_simple(cst_utterance *utt) { cst_wave *w = 0; cst_lpcres *lpcres; const char *resynth_type; const cst_val *streaming_info_val; resynth_type = get_param_string(utt->features, "resynth_type", "fixed"); asis_to_pm(utt); concat_units(utt); lpcres = val_lpcres(utt_feat_val(utt, "target_lpcres")); streaming_info_val = get_param_val(utt->features, "streaming_info", NULL); if (streaming_info_val) { lpcres->asi = val_audio_streaming_info(streaming_info_val); lpcres->asi->utt = utt; } if (cst_streq(resynth_type, "fixed")) w = lpc_resynth_fixedpoint(lpcres); else { cst_errmsg("unknown resynthesis type %s\n", resynth_type); cst_error(); /* Should not happen */ } utt_set_wave(utt, w); return utt; }
float flite_ssml_to_speech(const char *filename, cst_voice *voice, const char *outtype) { cst_tokenstream *ts; int fp; cst_wave *w; float d; if ((ts = ts_open(filename, get_param_string(voice->features,"text_whitespace",NULL), get_param_string(voice->features,"text_singlecharsymbols",NULL), get_param_string(voice->features,"text_prepunctuation",NULL), get_param_string(voice->features,"text_postpunctuation",NULL))) == NULL) { cst_errmsg("failed to open file \"%s\" for ssml reading\n", filename); return 1; } fp = get_param_int(voice->features,"file_start_position",0); if (fp > 0) ts_set_stream_pos(ts,fp); /* If its a file to write to, create and save an empty wave file */ /* as we are going to incrementally append to it */ if (!cst_streq(outtype,"play") && !cst_streq(outtype,"none") && !cst_streq(outtype,"stream")) { w = new_wave(); cst_wave_resize(w,0,1); cst_wave_set_sample_rate(w,16000); cst_wave_save_riff(w,outtype); /* an empty wave */ delete_wave(w); } d = flite_ssml_to_speech_ts(ts,voice,outtype); ts_close(ts); return d; }
void H_label(Rainbow_hdr *rainbow_header, char* buf) { int labelnum; char label; sscanf(buf, "%c%d", &label, &labelnum); if (labelnum == 3) rainbow_header->filetype = get_param_int(buf); else if (labelnum == 8) strcpy(rainbow_header->radarname, get_param_string(buf)); }
cst_utterance *join_units_modified_lpc(cst_utterance *utt) { cst_wave *w = 0; cst_lpcres *lpcres; const char *resynth_type; const cst_val *streaming_info_val; resynth_type = get_param_string(utt->features, "resynth_type", "float"); f0_targets_to_pm(utt); concat_units(utt); lpcres = val_lpcres(utt_feat_val(utt, "target_lpcres")); streaming_info_val = get_param_val(utt->features, "streaming_info", NULL); if (streaming_info_val) { lpcres->asi = val_audio_streaming_info(streaming_info_val); lpcres->asi->utt = utt; } if (cst_streq(resynth_type, "float")) w = lpc_resynth(lpcres); else if (cst_streq(resynth_type, "fixed")) { w = lpc_resynth_fixedpoint(lpcres); } else { cst_errmsg("unknown resynthesis type %s\n", resynth_type); cst_error(); /* Should not happen */ } if (w == NULL) { /* Synthesis Failed, probably because it was interrupted */ utt_set_feat_int(utt, "Interrupted", 1); w = new_wave(); } utt_set_wave(utt, w); return utt; }
static __inline__ gchar * get_media_path_live(const gchar *path, gint *ls) { gchar *v,*new_path; gint ch, level; if (get_path_ch_and_level(path, &ch, &level)) return g_strdup(path); v = get_param_string(path, "dev=[A-Z]{3}-[A-Z]{3}-\\d{8}"); if (v) { new_path = g_strdup_printf("/%s/media=0/channel=%d&level=%d/seq=0", v, ch, level); g_free(v); *ls = 1; return new_path; } return g_strdup(path); }
cst_utterance *join_units(cst_utterance *utt) { /* Make a waveform form the units */ const char *join_type; join_type = get_param_string(utt->features, "join_type", "modified_lpc"); if (cst_streq(join_type, "none")) return utt; #if 0 else if (cst_streq(join_type, "windowed_join")) join_units_windowed(utt); #endif else if (cst_streq(join_type, "simple_join")) join_units_simple(utt); else if (cst_streq(join_type, "modified_lpc")) join_units_modified_lpc(utt); return utt; }
cst_utterance *flowm_print_relation_callback(cst_utterance *u) { /* Say the details of a named relation for display */ char rst[FL_MAX_MSG_CHARS]; const char *name; const char *relname; cst_item *item; char *space; space = ""; relname = get_param_string(u->features,"print_info_relation", NULL); cst_sprintf(rst,"%s: ",relname); if (!relname) { mbstowcs(fl_tts_msg,"",FL_MAX_MSG_CHARS); return u; } for (item=relation_head(utt_relation(u,relname)); item; item=item_next(item)) { name = item_feat_string(item,"name"); if (cst_strlen(name)+1+4 < FL_MAX_MSG_CHARS) cst_sprintf(rst,"%s%s%s",rst,space,name); else if (cst_strlen(rst)+4 < FL_MAX_MSG_CHARS) cst_sprintf(rst,"%s ...",rst); else break; space = " "; } mbstowcs(fl_tts_msg,rst,FL_MAX_MSG_CHARS); return u; }
float flite_file_to_speech(const char *filename, cst_voice *voice, const char *outtype) { cst_utterance *utt; cst_tokenstream *ts; const char *token; cst_item *t; cst_relation *tokrel; float d, durs = 0; int num_tokens; cst_breakfunc breakfunc = default_utt_break; if ((ts = ts_open(filename, get_param_string(voice->features,"text_whitespace",NULL), get_param_string(voice->features,"text_singlecharsymbols",NULL), get_param_string(voice->features,"text_prepunctuation",NULL), get_param_string(voice->features,"text_postpunctuation",NULL))) == NULL) { cst_errmsg("failed to open file \"%s\" for reading\n", filename); return 1; } if (feat_present(voice->features,"utt_break")) breakfunc = val_breakfunc(feat_val(voice->features,"utt_break")); /* If its a file to write to delete it as we're going to */ /* incrementally append to it */ if (!cst_streq(outtype,"play") && !cst_streq(outtype,"none")) { cst_wave *w; w = new_wave(); cst_wave_resize(w,0,1); cst_wave_set_sample_rate(w,16000); cst_wave_save_riff(w,outtype); /* an empty wave */ delete_wave(w); } num_tokens = 0; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); while (!ts_eof(ts) || num_tokens > 0) { token = ts_get(ts); if ((strlen(token) == 0) || (num_tokens > 500) || /* need an upper bound */ (relation_head(tokrel) && breakfunc(ts,token,tokrel))) { /* An end of utt */ d = flite_tokens_to_speech(utt,voice,outtype); utt = NULL; if (d < 0) goto out; durs += d; if (ts_eof(ts)) goto out; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); num_tokens = 0; } num_tokens++; t = relation_append(tokrel, NULL); item_set_string(t,"name",token); item_set_string(t,"whitespace",ts->whitespace); item_set_string(t,"prepunctuation",ts->prepunctuation); item_set_string(t,"punc",ts->postpunctuation); item_set_int(t,"file_pos",ts->file_pos); item_set_int(t,"line_number",ts->line_number); } out: delete_utterance(utt); ts_close(ts); return durs; }
float flite_file_to_speech(const char *filename, cst_voice *voice, const char *outtype) { cst_utterance *utt; cst_tokenstream *ts; const char *token; cst_item *t; cst_relation *tokrel; float durs = 0; int num_tokens; cst_wave *w; cst_breakfunc breakfunc = default_utt_break; cst_uttfunc utt_user_callback = 0; int fp; if ((ts = ts_open(filename, get_param_string(voice->features,"text_whitespace",NULL), get_param_string(voice->features,"text_singlecharsymbols",NULL), get_param_string(voice->features,"text_prepunctuation",NULL), get_param_string(voice->features,"text_postpunctuation",NULL))) == NULL) { cst_errmsg("failed to open file \"%s\" for reading\n", filename); return 1; } fp = get_param_int(voice->features,"file_start_position",0); if (fp > 0) ts_set_stream_pos(ts,fp); if (feat_present(voice->features,"utt_break")) breakfunc = val_breakfunc(feat_val(voice->features,"utt_break")); if (feat_present(voice->features,"utt_user_callback")) utt_user_callback = val_uttfunc(feat_val(voice->features,"utt_user_callback")); /* If its a file to write to, create and save an empty wave file */ /* as we are going to incrementally append to it */ if (!cst_streq(outtype,"play") && !cst_streq(outtype,"none") && !cst_streq(outtype,"stream")) { w = new_wave(); cst_wave_resize(w,0,1); cst_wave_set_sample_rate(w,16000); cst_wave_save_riff(w,outtype); /* an empty wave */ delete_wave(w); } num_tokens = 0; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); while (!ts_eof(ts) || num_tokens > 0) { token = ts_get(ts); if ((cst_strlen(token) == 0) || (num_tokens > 500) || /* need an upper bound */ (relation_head(tokrel) && breakfunc(ts,token,tokrel))) { /* An end of utt, so synthesize it */ if (utt_user_callback) utt = (utt_user_callback)(utt); if (utt) { utt = flite_do_synth(utt,voice,utt_synth_tokens); durs += flite_process_output(utt,outtype,TRUE); delete_utterance(utt); utt = NULL; } else break; if (ts_eof(ts)) break; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); num_tokens = 0; } num_tokens++; t = relation_append(tokrel, NULL); item_set_string(t,"name",token); item_set_string(t,"whitespace",ts->whitespace); item_set_string(t,"prepunctuation",ts->prepunctuation); item_set_string(t,"punc",ts->postpunctuation); /* Mark it at the beginning of the token */ item_set_int(t,"file_pos", ts->file_pos-(1+ /* as we are already on the next char */ cst_strlen(token)+ cst_strlen(ts->prepunctuation)+ cst_strlen(ts->postpunctuation))); item_set_int(t,"line_number",ts->line_number); } delete_utterance(utt); ts_close(ts); return durs; }
const char *flite_get_param_string(const cst_features *f, const char *name, const char *def) { return get_param_string(f,name,def); }
static float flite_ssml_to_speech_ts(cst_tokenstream *ts, cst_voice *voice, const char *outtype) { cst_features *ssml_feats, *ssml_word_feats; cst_features *attributes; const char *token; char *tag; cst_utterance *utt; cst_relation *tokrel; int num_tokens; cst_breakfunc breakfunc = default_utt_break; cst_uttfunc utt_user_callback = 0; float durs = 0.0; cst_item *t; ssml_feats = new_features(); ssml_word_feats = new_features(); set_charclasses(ts, " \t\n\r", ssml_singlecharsymbols_general, get_param_string(voice->features,"text_prepunctuation",""), get_param_string(voice->features,"text_postpunctuation","") ); if (feat_present(voice->features,"utt_break")) breakfunc = val_breakfunc(feat_val(voice->features,"utt_break")); if (feat_present(voice->features,"utt_user_callback")) utt_user_callback = val_uttfunc(feat_val(voice->features,"utt_user_callback")); num_tokens = 0; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); while (!ts_eof(ts) || num_tokens > 0) { token = ts_get(ts); if (cst_streq("<",token)) { /* A tag */ tag = cst_upcase(ts_get(ts)); if (cst_streq("/",tag)) /* an end tag */ { tag = cst_upcase(ts_get(ts)); attributes = ssml_get_attributes(ts); feat_set_string(attributes,"_type","end"); } else attributes = ssml_get_attributes(ts); utt = ssml_apply_tag(tag,attributes,utt,ssml_word_feats); cst_free(tag); } else if (cst_streq("&",token)) { /* an escape sequence */ /* skip to ; and insert value in rawdata */ } else { if ((cst_strlen(token) == 0) || (num_tokens > 500) || /* need an upper bound */ (relation_head(tokrel) && breakfunc(ts,token,tokrel))) { /* An end of utt, so synthesize it */ if (utt_user_callback) utt = (utt_user_callback)(utt); if (utt) { utt = flite_do_synth(utt,voice,utt_synth_tokens); durs += flite_process_output(utt,outtype,TRUE); delete_utterance(utt); utt = NULL; } else break; if (ts_eof(ts)) break; utt = new_utterance(); tokrel = utt_relation_create(utt, "Token"); num_tokens = 0; } num_tokens++; t = relation_append(tokrel, NULL); item_set_string(t,"name",token); item_set_string(t,"whitespace",ts->whitespace); item_set_string(t,"prepunctuation",ts->prepunctuation); item_set_string(t,"punc",ts->postpunctuation); /* Mark it at the beginning of the token */ item_set_int(t,"file_pos", ts->file_pos-(1+ /* as we are already on the next char */ cst_strlen(token)+ cst_strlen(ts->prepunctuation)+ cst_strlen(ts->postpunctuation))); item_set_int(t,"line_number",ts->line_number); } } delete_utterance(utt); return durs; }
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; }