1 /*
2  * thrulayd.c -- network throughput tester (the server part).
3  *
4  * Written by Stanislav Shalunov, http://www.internet2.edu/~shalunov/
5  *            Bernhard Lutzmann, belu@users.sf.net
6  *            Federico Montesino Pouzols, fedemp@altern.org
7  *
8  * @(#) $Id: thrulayd.c,v 1.3.2.17 2006/08/20 18:06:19 fedemp Exp $
9  *
10  * Copyright 2003, 2006, Internet2.
11  * Legal conditions are in file LICENSE
12  * (MD5 = ecfa50d1b0bfbb81b658c810d0476a52).
13  */
14 
15 /**
16  * @file thrulayd.c
17  *
18  * @short thrulay server command line tool.
19  **/
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <getopt.h>
29 
30 #include "server.h"
31 #include "thrulayd.h"
32 #include "util.h"
33 
34 static void
print_usage(void)35 print_usage(void)
36 {
37 	fprintf(stderr, "Usage: thrulayd [-h] [-V] [-v] [-a address ] [-w#] "
38 		"[-p#] [-d] [-j group]\n");
39 	fprintf(stderr, "\t-h\t\tdisplay this help and exit\n");
40 	fprintf(stderr, "\t-V\t\toutput version information and exit\n");
41 	fprintf(stderr, "\t-v\t\tverbose reporting of results\n");
42 	fprintf(stderr, "\t-a address\tadd address to list of allowed hosts "
43 			"(CIDR syntax)\n");
44 	fprintf(stderr, "\t-w#\t\twindow, in bytes (default: %dB)\n",
45 		THRULAY_DEFAULT_WINDOW);
46 	fprintf(stderr, "\t-p#\t\tserver port (default: %d)\n",
47 		THRULAY_DEFAULT_SERVER_TCP_PORT);
48 	fprintf(stderr, "\t-d \t\tdebug (no daemon, log to stderr)\n");
49 	fprintf(stderr, "\t-j group\tjoin a multicast group\n");
50 }
51 
52 void __attribute__((noreturn))
usage(void)53 usage(void)
54 {
55 	print_usage();
56 	exit(1);
57 }
58 
59 static void __attribute__((noreturn))
version(void)60 version(void)
61 {
62 	printf(THRULAY_VERSION " (server part) release ");
63 	printf(PACKAGE_VERSION "\n");
64 	printf("Copyright (c) 2006, Internet2.\n");
65 	printf("See the source for copying conditions.\n");
66 	exit(0);
67 }
68 
69 int
main(int argc,char * argv[])70 main(int argc, char *argv[])
71 {
72 	int window = THRULAY_DEFAULT_WINDOW;	/* Window size, in bytes. */
73 	int port = SERVER_TCP_PORT;	/* Server TCP port number. */
74 	char *mcast_address = NULL;         /* Multicast group to join to. */
75 	int argcorig = argc;
76 	int log_type = LOGTYPE_SYSLOG;		/* default is syslog */
77 	int reporting_verbosity = 0;
78 	int ch, rc;
79 
80 	while ((ch = getopt(argc, argv, "hVva:w:p:dj:")) != -1) {
81 		switch (ch) {
82 		case 'h':
83 			print_usage();
84 			exit(0);
85 		case 'V':
86 			version();
87 		case 'v':
88 			reporting_verbosity = 1;
89 			break;
90 		case 'a':
91 			if (acl_allow_add(optarg) == -1) {
92 				fprintf(stderr, "unable to add host to ACL "
93 						"list\n");
94 				usage();
95 			}
96 			break;
97 		case 'w':
98 			window = atoi(optarg);
99 			if (window <= 0) {
100 				fprintf(stderr, "window must be "
101 					"a positive integer (in bytes)\n");
102 				usage();
103 			}
104 			break;
105 		case 'p':
106 			port = atoi(optarg);
107 			if (port <= 0) {
108 				fprintf(stderr, "port must be "
109 					"a positive integer\n");
110 				usage();
111 			}
112 			break;
113 		case 'd':
114 			/* Activate debugging mode. */
115 			log_type = LOGTYPE_STDERR;
116 			break;
117 		case 'j':
118 #ifdef ENABLE_THRULAY_MULTICAST
119 			mcast_address = strdup(optarg);
120 #else
121 			fprintf(stderr,"multicast is disabled, continuing\n");
122 #endif
123 			break;
124 		default:
125 			usage();
126 		}
127 	}
128 	argc = argcorig;
129 
130 	argc -= optind;
131 	argv += optind;
132 
133 	if (argc != 0)
134 		usage();
135 
136 	rc = thrulay_server_init(log_type, reporting_verbosity);
137 	if (rc < 0) {
138 		fprintf(stderr, "While initializing: ");
139 		error(ERR_FATAL, thrulay_server_strerror(rc));
140 	}
141 
142 	rc = thrulay_server_listen(port, window);
143 	if (rc < 0) {
144 		fprintf(stderr, "Server listen failed: ");
145 		error(ERR_FATAL, thrulay_server_strerror(rc));
146 	}
147 
148 	/* Run thrulay server forever */
149 	rc = thrulay_server_start(0, mcast_address);
150 	if (rc < 0) {
151 		fprintf(stderr, "Server failed: ");
152 		error(ERR_FATAL, thrulay_server_strerror(rc));
153 	}
154 
155 	free(mcast_address);
156 	return 0;
157 }
158