How to pipe through the output of hping3 from child to parent?

517 views Asked by At

I lately asked a question about how to call hping3 within a c program. The solution provided works, but i now have another problem. The output from hping3 must be passed to the parent process, so i did the following. Note that this is only the codesnippet, where i think the bug is within. The following snippets runs inside a loop, where i iterate through a range of ip addresses, this is necessary to provide two different distribution strategies within my mpi program.

//Stick together the params
sprintf(params, "--scan %u %u.%u.%u.%u -V", *(portarray + i), (iterator & 0xFF000000)>>24, (iterator & 0x00FF0000)>>16, (iterator & 0x0000FF00)>>8, (iterator & 0x000000FF));
            //Pipe and check status
            if(pipe(pipes)==-1){
               perror("Error piping");
            }
            //Fork and check status
            pid=fork();
            if(pid == -1){
               perror("Error forking");
            } else if(pid > 0){
               //Parent does not write
               close(pipes[1]);
               //Save stdout from pipe
               nbytes = read(pipes[0], buffer, sizeof(buffer));
               //Parent, wait for child
               waitpid(pid, &status, 0);
               //Print out pipe
               printf("hping3: (%.*s)\n", nbytes, buffer);
               wait(NULL);
               close(pipes[0]);
            } else {
               //Child does not read
               close(pipes[0]);
               //Map stdout and stderr to write pipe-end
               dup2(pipes[1], 1);
               dup2(pipes[1], 2);
               //Child, exec hping with params
               execl("sudo /usr/sbin/hping3","sudo /usr/sbin/hping3",params,NULL);
               close(pipes[1]);
               //Exit child to prevent fork-bomb
               return 0;
            }
            //Sleep for specified delaytime
            sleep((unsigned int)delay);

I can't figure out where the problem is. Output is the following (in a loop of course):

hping3()

The hping3 program prints out to stdout and stderr, i tested it on the shell via output redirection to a file.

1

There are 1 answers

4
ezorita On

How is buffer defined? Did you ever check the value of nbytes? Are you sure that hping is being executed? I'd try with the following:

int buffsize  = 100;
char * buffer = malloc(buffsize);

if (pid == -1) {
   perror("Error forking");
} else if (pid > 0) {
   //Parent does not write
   close(pipes[1]);
   // I'd first wait for the child to finish.
   waitpid(pid, &status, 0);
   // Save stdout from pipe 
   // recall that sizeof(buffer) != buffsize when malloc'ed.
   int count = 0;
   while((nbytes = read(pipes[0], buffer+count, buffsize-count) != -1) count += nbytes;
   //Print out pipe
   printf("hping3: (%.*s)\n", nbytes, buffer);
   wait(NULL);
   close(pipes[0]);
} else {
   //Child does not read
   close(pipes[0]);
   //Map stdout and stderr to write pipe-end
   dup2(pipes[1],1);
   dup2(pipes[1],2);
   // Close pipes[1]
   close(pipes[1]);
   //Child, exec hping with params
   if (execl("sudo /usr/sbin/hping3","sudo /usr/sbin/hping3",params,NULL) == -1)
      perror("execl error");
   //Exit child to prevent fork-bomb
   return 0;
}