Question
NAME passaround --- listens and forwards UDP packets according to message contents. SYNOPSIS passaround [-v] [-n number] [-m message] port DESCRIPTION Passaround listens on the
NAME passaround --- listens and forwards UDP packets according to message contents. SYNOPSIS
passaround [-v] [-n number] [-m message] port
DESCRIPTION
Passaround listens on the port given in the command line for UDP packets and forwards the packet as instructed by the packet contents. Packet content is a character sequence (not a string!) of format: host(:host)* or the empty string. If the empty string, there is nothing to pass on. Else the first host name is removed from the string, and the remainder of the string (properly formatted) is sent to the named host. OPTIONS
-m Take as the "message" the first received message -n forward number packets to forward, then exit. Default is 1; 0 for loop forever. -v verbose ARGUMENTS
Port, the port number to listen on, and to send to. The from port can be ephemeral.
OUTPUT Without the verbose option the output should be strictly one line for each send and receive, in the order of occurrence: R: host:port |message-as-received| S: host:port |message-as-sent| Host and port refer to the send-to, on S, and the received from, on R. For host, please print the IP address. Going from IP to hostname is called reverse IP lookup, and reverse records are not always available.
Please help to fill the if(msg) and while part. Thank you!
#include
#define LOCALHOST "localhost" #define MAXMSGLEN 2048 #define N_REPEAT_DEFAULT 1
#define USAGE_MESSAGE "usage: passaround [-v] [-n num] [-m message] port"
int g_verbose = 0 ;
int main(int argc, char * argv[]) { int ch ; int the_port = 0 ; int n_repeat = N_REPEAT_DEFAULT ; char * msg = NULL ; int is_forever = 0 ;
assert(sizeof(short)==2) ;
while ((ch = getopt(argc, argv, "vm:n:")) != -1) { switch(ch) { case 'n': n_repeat = atoi(optarg) ; break ; case 'v': g_verbose = 1 ; break ; case 'm': msg = strdup(optarg) ; break ; case '?': default: printf("%s ",USAGE_MESSAGE) ; return 0 ; } } argc -= optind; argv += optind;
if ( argc!= 1 ) { printf("%s ",USAGE_MESSAGE) ; exit(0) ; }
the_port = atoi(*argv) ;
assert(the_port) ;
is_forever = (n_repeat == 0) ;
if ( msg ) {
// parse and send // and print S: host:port |message|
free(msg) ; n_repeat-- ; // a packet sent }
while( is_forever || n_repeat ) {
// listen for a packet // print R: host:port |message|
// if something to send, { // parse and send // and print S: host:port |message| // }
n_repeat-- ; } return 0 ; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started