From 9cc1b03a301397639e02c5a7435984943d0955fd Mon Sep 17 00:00:00 2001 From: Maalvika Bhat <42943695+maalvikabhat@users.noreply.github.com> Date: Thu, 27 Feb 2020 21:30:29 -0500 Subject: [PATCH] Checking in trout files --- exercises/ex04/trout/main.c | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 exercises/ex04/trout/main.c diff --git a/exercises/ex04/trout/main.c b/exercises/ex04/trout/main.c new file mode 100644 index 00000000..b9551102 --- /dev/null +++ b/exercises/ex04/trout/main.c @@ -0,0 +1,45 @@ +#include "trout.h" + +int main (int argc, char **argv) +{ + int c; + struct addrinfo *ai; + char *host; + + opterr = 0; + while ( (c = getopt (argc, argv, "m:")) != -1) { + switch (c) { + case 'm': + if ( (max_ttl = atoi(optarg)) <= 1) { + err_quit ("invalid -m value"); + } + break; + default: + err_quit ("unrecognizd option: %c", c); + } + } + + if (optind != argc - 1) { + err_quit ("usage: trout [ -m ] "); + } + host = argv[optind]; + ai = Host_serv (host, NULL, 0, 0); + + printf ("trout to %s (%s): %d hops max, %d data bytes\n", + ai->ai_canonname, + Sock_ntop_host (ai->ai_addr, ai->ai_addrlen), + max_ttl, datalen); + + if (ai->ai_family != AF_INET) { + err_quit ("unknown address family %d", ai->ai_family); + } + + sasend = ai->ai_addr; + salen = ai->ai_addrlen; + sarecv = Calloc (1, salen); + salast = Calloc (1, salen); + sabind = Calloc (1, salen); + + loop_ttl (); + exit (0); +}