Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are 3 pictures...The numbering starts over but each picture is a continuat

ID: 3791292 • Letter: T

Question

There are 3 pictures...The numbering starts over but each picture is a continuation of the previous one.

3 You will write a C/C++ program for the following task. 5 Every System Administrator sooner or later has to write a program that needs to extract information from the output of the traceroute command. Your assignment is to determine the correct commands to extract the following 7 information from the output of the traceroute command on the various platforms we use. 9 In particular, you should submit a C/C++ program (trace program) that runs a traceroute command for a DNS name (provided as an input to run this program), and extract the fo owing from the result of the traceroute 11 command 12 13 (1) all IP addresses, all DNS names, time of 3 packets in the traceroute (2) compute min, max, average time based on the routing times of packets (each with round-trip time) to the host 14 15 16 The following is the sample output oftraceroute command with www.utdallas.edu. 17 18 /cs3376/tracce calinux 1 traceroute utdallas edu 19 traceroute to ut dallas edu 10. la 71.70) 30 hops 60 byte packets 1 ut digl 92. ut dallas edu 129.110.92.1 1.794 ms 1.827 ms 1.773 ms 21 core phy-f2-pa 9-utd allas edu (129.110.83.42 0.611 ms 1.314 ms 526 ms core phy-m2-pal -utd allas edu (129.110.83.65 0.796 ms 0.992 1.305 ms ven9kcorel-loo-po4.utdallas edu 129.110 83.54) 0.416 ms 0.500 ms 0.646 ms 24 utdallas edu (10.182 71.70) 0.357 ms 0.370 ms 0.358 ms calinux1. cs3376 /trace 27 Your program should run the traceroute command in it (given the argument which is DNS name) For example calinux1. /tra cel www ut dallas edu read traceroute to utdallas edu 10.182 71.70) 30 hops 60 byte packets 31 ut digl 92. utdallas.edu 129.110.92.1 1.877 ms 1. Ba6 ms 1.831 ms 32 core phy-f2-pa 9-utd allas edu (129.110.83.42 0.650 ms 0.795 ms 0.902 ms 33 core phy-m2-pal -utd allas edu (129.110.83.65 913 ms 1.926 909 ms ven9kcorel-loo-po4.utdallas edu 129.110 83.54) 0.456 ms 0.491 ms vrtwn 9kcore2 35 loo-po4.ut dallas edu 29.110.03. 68 0.463 ms 36 utdallas edu (10.182 71.70) 0.187 ms 0.190 ms 1.247 ms 37 38 And program processes the output (each line) from traceroute command for the output as follows. traceroute to ut dallas edu 10. la 71.70) 30 hops 60 byte packets 41 DNS is ut dallas edu 42 ip is (10.182.71.70 43 1.877 ms 1.88 1.831 ms ut digl 92. utdallas.edu 129.110.92.1 45 DNS is ut digl -92.utd allas edu 46 ip is (129.110.92 47 time is 1.877 time is 1.886 time is 1.831 is 1.831000 is 1 BB 6000 is 1 864667 average 51 0.650 ms 0.79s 0.902 ms 52 core phy-f2-pa 9-utd allas edu (129.110.83.42 53 DNS is corephy-f2-po 9.utdallas edu 54 ip is (129.110.83 42 55 time is 0.650 time is 0.795 57 time is 0.902 is 0.650000 is 0.902000 is 0.782333 average 1.913 ms 1.926 ms 1.909 ms core phy-m2-pol -utd allas edu (129.110.83.65 61 DNS is corephy-m2-pol utdallas edu

Explanation / Answer

Please find the program below.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

#define MAX_LEN 4000
int main(int argc, char*argv[])
{
if(argc < 2){
printf("Usage : run the program with a DNS name to be tracerouted");
exit(0);
}

char buffer[MAX_LEN+1] = {0};
char inp[MAX_LEN + 1] = {0};
char command[40] = "traceroute ";
int out_pipe[2];
int saved_stdout;
float val = 0.0, average = 0.0 , timing[3] = {0.0f};
char time[10] = {''};
int i =0;
float min = 0.0f , max = 0.0f;

saved_stdout = dup(STDOUT_FILENO);

if(pipe(out_pipe) != 0){
exit(1);
}

char* line = NULL,*temp1 =NULL,*temp2 = NULL;
strcat(command,argv[1]);
dup2(out_pipe[1],STDOUT_FILENO);
close(out_pipe[1]);

system(command);
fflush(stdout);
read(out_pipe[0],buffer,MAX_LEN);

dup2(saved_stdout,STDOUT_FILENO);
printf("read: %s ",buffer);
char find_text[4000] = {0};
char* curline = buffer;

printf(" ");
line = strtok(curline," ");
printf("%s ",line);
temp1= strstr(line,"to");
temp1 +=2;
temp2 = strstr(line,"(");
printf("DNS is ");
while(temp1 != temp2){
putchar(*temp1);
temp1++;
}
printf(" ");

temp1 = strstr(line,")");

printf("ip is ");
while(temp2 != temp1){
putchar(*temp2);
temp2++;
}

printf(") ");

line = strtok(NULL," ");
while(line){
  

printf("%s ",line);
/*Eliminating line with '*' mark */
temp1 = strstr(line,"*");
if(temp1 == NULL){

temp2 = strstr(line,"(");
temp1 = line;
while( (isdigit(*temp1)) || (*temp1 == ' ')){
temp1++;
}
printf("DNS is ");
while(temp1 != temp2){
putchar(*temp1);
temp1++;
}

printf(" ");
temp1 = strstr(line,")");
printf("ip is ");

while(temp2 != temp1){
putchar(*temp2);
temp2++;
}
printf(") ");


/************ Calculation of first time ***************/
temp1++;
while(*temp1 == ' ')
temp1++;

temp2 = strstr(temp1,"ms");

while( (*temp1 == '.') || (isdigit(*temp1))){
time[i++] = *temp1;
temp1++;
}
timing[0] = atof(time);
printf("time is %s ",time);
/*****************************************************/

/*************** Calculatio of second time ***********/
temp2 +=2;
i=0;
memset(time,0,sizeof(time));
while(*temp2 == ' ')
temp2++;

temp1 = strstr(temp2,"ms");

while( (*temp2 == '.') || (isdigit(*temp2))){
time[i++] = *temp2;
temp2++;
}
timing[1] = atof(time);
printf("time is %s ",time);
/*****************************************************/

/************ Calculation of third time ***************/
temp1 +=2;
i=0;
memset(time,0,sizeof(time));
while(*temp1 == ' ')
temp1++;

temp2 = strstr(temp1,"ms");

while( (*temp1 == '.') || (isdigit(*temp1))){
time[i++] = *temp1;
temp1++;
}
timing[2] = atof(time);
printf("time is %s ",time);
/*****************************************************/


max = (timing[0] > timing[1] ? (timing[0] > timing[2] ? timing[0] : timing[2]) : (timing[1] > timing[2] ? timing[1] : timing[2]));
min = (timing[0] > timing[1] ? (timing[1] > timing[2] ? timing[2] : timing[1]) : (timing[0] > timing[2] ? timing[2] : timing[0]));

printf("min is <%lf>, max is <%lf>, average is <%lf> ",min,max, (timing[0] + timing[1] + timing[2])/3.0);
  
}

line = strtok(NULL," ");
}


return 0;
}

/*The code may need further changes to accomodate lines like :

1 utdig1-92.abc.edu (129.110.92.1) 1.87 ms 1.886 ms utdallas.edu (10.182.17.10) 1.831 ms

In such case the program doesn't print the 3rd timing .

To print the timings in such cases you need to check for the occurence of "(" character after 2nd timings has been extracted using strstr().

*/