Files
2026-05-31 14:34:00 -04:00

107 lines
2.2 KiB
C

/*
* CS 361: Template project driver
*
* Name:
*/
#include <assert.h>
#include <getopt.h>
#include <inttypes.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "client.h"
int cmdline (int, char **, char **, bool *, short int *);
void
usage (void)
{
printf ("Usage: net [options] domain\n");
printf (" Options are:\n");
printf (" -6 Use IPv6 addresses\n");
printf (" -p P Lookup address for application protocol P\n");
}
int
main (int argc, char **argv)
{
char *protocol = "http";
bool ipv6 = false;
short int port = 0;
if (cmdline (argc, argv, &protocol, &ipv6, &port) < 0)
{
usage ();
return EXIT_FAILURE;
}
char *domain = argv[optind];
// MINIMUM requirements:
// Get the server list for the specified domain and protocol
// Given this command-line:
// ./net -p http www.jmu.edu
// Print the following output:
// www.jmu.edu http: TCP IPv4 134.126.10.50
// The requested transport-layer protocol should be based on the
// requested application-layer protocol. TCP is default, but use
// UDP for protocols "53", "67", "dns", or "dhcp".
bool tcp = true;
char *udpProtocols[] = { "53", "67", "dns", "dhcp" };
for (size_t i = 0; i < 4; i++)
{
if (strcmp (protocol, udpProtocols[i]) == 0)
{
tcp = false;
break;
}
}
struct addrinfo *server = get_server_list (domain, protocol, tcp, ipv6);
char *server_string = serv_string (server);
printf ("%s %s: %s\n", domain, protocol, server_string);
free (server);
free (server_string);
return EXIT_SUCCESS;
}
/* DO NOT MODIFY THIS FUNCTION */
int
cmdline (int argc, char **argv, char **protocol, bool *ipv6, short int *port)
{
int option;
while ((option = getopt (argc, argv, "p:6sh")) != -1)
{
switch (option)
{
// Change this to merge -p and -o as same flag
case 'p':
*protocol = optarg;
break;
case '6':
*ipv6 = true;
break;
case 'h':
return -1;
break;
default:
return -1;
}
}
if (optind != argc - 1)
return -1;
return 0;
}