catan

catan.git
git clone git://git.lenczewski.org/catan.git
Log | Files | Refs

agent.c (2389B)


      1 #include "agent.h"
      2 
      3 struct opts opts = {
      4 	.verbose = false,
      5 };
      6 
      7 static char const *optstr = "hv";
      8 #define POSITIONAL_ARGS 2
      9 
     10 static void
     11 usage(char **argv)
     12 {
     13 	fprintf(stderr, "Usage: %s [-hv] <addr> <port>\n", argv[0]);
     14 	fprintf(stderr, "\t-h :\n\t\tdisplays this help message\n");
     15 	fprintf(stderr, "\t-v :\n\t\tenabled verbose logging\n");
     16 	fprintf(stderr, "\n");
     17 	fprintf(stderr, "\thost :\n\t\tthe server address to connect to\n");
     18 	fprintf(stderr, "\tport :\n\t\tthe server port to connect to\n");
     19 }
     20 
     21 #include <getopt.h>
     22 
     23 static bool
     24 parse_args(int argc, char **argv)
     25 {
     26 	int opt;
     27 	while ((opt = getopt(argc, argv, optstr)) != -1) {
     28 		switch (opt) {
     29 		case 'v':
     30 			opts.verbose = true;
     31 			break;
     32 
     33 		default:
     34 			return false;
     35 		}
     36 	}
     37 
     38 	if (argc < optind + POSITIONAL_ARGS)
     39 		return false;
     40 
     41 	opts.addr = argv[optind++];
     42 	opts.port = argv[optind++];
     43 
     44 	return true;
     45 }
     46 
     47 static void
     48 kill_handler(int signum)
     49 {
     50 	fprintf(stderr, "Signal %d received\n", signum);
     51 	exit(EXIT_SUCCESS);
     52 }
     53 
     54 int
     55 main(int argc, char **argv)
     56 {
     57 	struct sigaction sigkill = {
     58 		.sa_handler = kill_handler,
     59 	};
     60 
     61 	sigaction(SIGKILL, &sigkill, NULL);
     62 
     63 	if (!parse_args(argc, argv)) {
     64 		usage(argv);
     65 		exit(EXIT_FAILURE);
     66 	}
     67 
     68 	fprintf(stderr, "====== Args ======\n");
     69 	fprintf(stderr, " verbose: %d\n", opts.verbose);
     70 	fprintf(stderr, " server: %s:%s\n", opts.addr, opts.port);
     71 
     72 	struct addrinfo hints = {
     73 		.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
     74 		.ai_family = AF_UNSPEC,
     75 		.ai_socktype = SOCK_STREAM,
     76 	}, *addrinfo, *ptr;
     77 
     78 	int res;
     79 	if ((res = getaddrinfo(opts.addr, opts.port, &hints, &addrinfo))) {
     80 		fprintf(stderr, "Failed to get server address info: %s\n", gai_strerror(res));
     81 		exit(EXIT_FAILURE);
     82 	}
     83 
     84 	int fd;
     85 	for (ptr = addrinfo; ptr; ptr = ptr->ai_next) {
     86 		fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
     87 		if (fd == -1)
     88 			continue;
     89 
     90 		if (connect(fd, ptr->ai_addr, ptr->ai_addrlen) == -1) {
     91 			perror("connect");
     92 			close(fd);
     93 			continue;
     94 		}
     95 
     96 		break;
     97 	}
     98 
     99 	if (ptr) {
    100 		char host[NI_MAXHOST], serv[NI_MAXSERV];
    101 		getnameinfo(ptr->ai_addr, ptr->ai_addrlen,
    102 			    host, ARRLEN(host), serv, ARRLEN(serv),
    103 			    AI_NUMERICHOST | AI_NUMERICSERV);
    104 
    105 		fprintf(stderr, "Connected to server: %s:%s\n", host, serv);
    106 	}
    107 
    108 	freeaddrinfo(addrinfo);
    109 
    110 	if (!ptr) {
    111 		fprintf(stderr, "Failed to connect to server\n");
    112 		exit(EXIT_FAILURE);
    113 	}
    114 
    115 	while (true) {
    116 		sleep(1);
    117 	}
    118 
    119 	exit(EXIT_SUCCESS);
    120 }