xref: /onnv-gate/usr/src/cmd/prtdscp/sparc/sun4u/prtdscp.c (revision 1772:78cca3d2cc4b)
1*1772Sjl139090 /*
2*1772Sjl139090  * CDDL HEADER START
3*1772Sjl139090  *
4*1772Sjl139090  * The contents of this file are subject to the terms of the
5*1772Sjl139090  * Common Development and Distribution License (the "License").
6*1772Sjl139090  * You may not use this file except in compliance with the License.
7*1772Sjl139090  *
8*1772Sjl139090  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1772Sjl139090  * or http://www.opensolaris.org/os/licensing.
10*1772Sjl139090  * See the License for the specific language governing permissions
11*1772Sjl139090  * and limitations under the License.
12*1772Sjl139090  *
13*1772Sjl139090  * When distributing Covered Code, include this CDDL HEADER in each
14*1772Sjl139090  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1772Sjl139090  * If applicable, add the following below this CDDL HEADER, with the
16*1772Sjl139090  * fields enclosed by brackets "[]" replaced with your own identifying
17*1772Sjl139090  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1772Sjl139090  *
19*1772Sjl139090  * CDDL HEADER END
20*1772Sjl139090  */
21*1772Sjl139090 /*
22*1772Sjl139090  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1772Sjl139090  * Use is subject to license terms.
24*1772Sjl139090  */
25*1772Sjl139090 
26*1772Sjl139090 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*1772Sjl139090 
28*1772Sjl139090 #include <stdio.h>
29*1772Sjl139090 #include <stdlib.h>
30*1772Sjl139090 #include <stdarg.h>
31*1772Sjl139090 #include <strings.h>
32*1772Sjl139090 #include <sys/types.h>
33*1772Sjl139090 #include <sys/socket.h>
34*1772Sjl139090 #include <netinet/in.h>
35*1772Sjl139090 #include <arpa/inet.h>
36*1772Sjl139090 #include <libintl.h>
37*1772Sjl139090 #include <locale.h>
38*1772Sjl139090 #include <libdscp.h>
39*1772Sjl139090 
40*1772Sjl139090 #if	!defined(TEXT_DOMAIN)
41*1772Sjl139090 #define	TEXT_DOMAIN "SYS_TEST"
42*1772Sjl139090 #endif
43*1772Sjl139090 
44*1772Sjl139090 #define	OPT_SP		1
45*1772Sjl139090 #define	OPT_DOMAIN	2
46*1772Sjl139090 
47*1772Sjl139090 static void	usage(void);
48*1772Sjl139090 static void	parse_options(int, char **, int *);
49*1772Sjl139090 static int	get_address(int, char *);
50*1772Sjl139090 static void	trace(char *, ...);
51*1772Sjl139090 static void	err(char *, ...);
52*1772Sjl139090 static char	*dscp_strerror(int);
53*1772Sjl139090 
54*1772Sjl139090 static int	verbose = 0;
55*1772Sjl139090 
56*1772Sjl139090 int
57*1772Sjl139090 main(int argc, char **argv)
58*1772Sjl139090 {
59*1772Sjl139090 	int	options;
60*1772Sjl139090 	char	saddr[INET_ADDRSTRLEN];
61*1772Sjl139090 	char	daddr[INET_ADDRSTRLEN];
62*1772Sjl139090 
63*1772Sjl139090 	(void) setlocale(LC_ALL, "");
64*1772Sjl139090 	(void) textdomain(TEXT_DOMAIN);
65*1772Sjl139090 
66*1772Sjl139090 	parse_options(argc, argv, &options);
67*1772Sjl139090 
68*1772Sjl139090 	/*
69*1772Sjl139090 	 * Get the desired IP addresses.
70*1772Sjl139090 	 */
71*1772Sjl139090 	if ((options & OPT_SP) != 0) {
72*1772Sjl139090 		trace(gettext("Looking up SP address...\n"));
73*1772Sjl139090 		if (get_address(DSCP_ADDR_REMOTE, saddr) < 0) {
74*1772Sjl139090 			trace(gettext("Lookup failed.  Aborting.\n"));
75*1772Sjl139090 			exit(-1);
76*1772Sjl139090 		}
77*1772Sjl139090 	}
78*1772Sjl139090 	if ((options & OPT_DOMAIN) != 0) {
79*1772Sjl139090 		trace(gettext("Looking up domain address...\n"));
80*1772Sjl139090 		if (get_address(DSCP_ADDR_LOCAL, daddr) < 0) {
81*1772Sjl139090 			trace(gettext("Lookup failed.  Aborting.\n"));
82*1772Sjl139090 			exit(-1);
83*1772Sjl139090 		}
84*1772Sjl139090 	}
85*1772Sjl139090 
86*1772Sjl139090 	/*
87*1772Sjl139090 	 * Print the IP addresses.
88*1772Sjl139090 	 */
89*1772Sjl139090 	if (options == OPT_SP) {
90*1772Sjl139090 		(void) printf("%s\n", saddr);
91*1772Sjl139090 	} else if (options == OPT_DOMAIN) {
92*1772Sjl139090 		(void) printf("%s\n", daddr);
93*1772Sjl139090 	} else {
94*1772Sjl139090 		(void) printf(gettext("Domain Address: %s\n"), daddr);
95*1772Sjl139090 		(void) printf(gettext("SP Address: %s\n"), saddr);
96*1772Sjl139090 	}
97*1772Sjl139090 
98*1772Sjl139090 	return (0);
99*1772Sjl139090 }
100*1772Sjl139090 
101*1772Sjl139090 /*
102*1772Sjl139090  * parse_options()
103*1772Sjl139090  *
104*1772Sjl139090  *	Parse the commandline options.
105*1772Sjl139090  */
106*1772Sjl139090 static void
107*1772Sjl139090 parse_options(int argc, char **argv, int *options)
108*1772Sjl139090 {
109*1772Sjl139090 	int		i;
110*1772Sjl139090 	int		c;
111*1772Sjl139090 	extern int	opterr;
112*1772Sjl139090 	extern int	optopt;
113*1772Sjl139090 
114*1772Sjl139090 	/*
115*1772Sjl139090 	 * Unless told otherwise, print everything.
116*1772Sjl139090 	 */
117*1772Sjl139090 	*options = (OPT_SP | OPT_DOMAIN);
118*1772Sjl139090 
119*1772Sjl139090 	/*
120*1772Sjl139090 	 * Skip this routine if no options exist.
121*1772Sjl139090 	 */
122*1772Sjl139090 	if (argc == 1) {
123*1772Sjl139090 		return;
124*1772Sjl139090 	}
125*1772Sjl139090 
126*1772Sjl139090 	/*
127*1772Sjl139090 	 * Scan for the -h option separately, so that
128*1772Sjl139090 	 * other commandline options are ignored.
129*1772Sjl139090 	 */
130*1772Sjl139090 	for (i = 1; i < argc; i++) {
131*1772Sjl139090 		if (strcmp(argv[i], "-h") == 0) {
132*1772Sjl139090 			usage();
133*1772Sjl139090 			exit(0);
134*1772Sjl139090 		}
135*1772Sjl139090 	}
136*1772Sjl139090 
137*1772Sjl139090 	/*
138*1772Sjl139090 	 * Disable the built-in error reporting, so that
139*1772Sjl139090 	 * error messages can be properly internationalized.
140*1772Sjl139090 	 */
141*1772Sjl139090 	opterr = 0;
142*1772Sjl139090 
143*1772Sjl139090 	/*
144*1772Sjl139090 	 * The main loop for parsing options.
145*1772Sjl139090 	 */
146*1772Sjl139090 	while ((c = getopt(argc, argv, "vsd")) != -1) {
147*1772Sjl139090 		switch (c) {
148*1772Sjl139090 		case 'v':
149*1772Sjl139090 			verbose = 1;
150*1772Sjl139090 			break;
151*1772Sjl139090 		case 's':
152*1772Sjl139090 			if (*options == OPT_DOMAIN) {
153*1772Sjl139090 				err(gettext("cannot use -s and -d together"));
154*1772Sjl139090 				usage();
155*1772Sjl139090 				exit(-1);
156*1772Sjl139090 			}
157*1772Sjl139090 			*options = OPT_SP;
158*1772Sjl139090 			break;
159*1772Sjl139090 		case 'd':
160*1772Sjl139090 			if (*options == OPT_SP) {
161*1772Sjl139090 				err(gettext("cannot use -s and -d together"));
162*1772Sjl139090 				usage();
163*1772Sjl139090 				exit(-1);
164*1772Sjl139090 			}
165*1772Sjl139090 			*options = OPT_DOMAIN;
166*1772Sjl139090 			break;
167*1772Sjl139090 		default:
168*1772Sjl139090 			err(gettext("invalid option -%c"), optopt);
169*1772Sjl139090 			usage();
170*1772Sjl139090 			exit(-1);
171*1772Sjl139090 		}
172*1772Sjl139090 	}
173*1772Sjl139090 }
174*1772Sjl139090 
175*1772Sjl139090 /*
176*1772Sjl139090  * usage()
177*1772Sjl139090  *
178*1772Sjl139090  *	Print a brief synopsis of the program's usage.
179*1772Sjl139090  */
180*1772Sjl139090 static void
181*1772Sjl139090 usage(void)
182*1772Sjl139090 {
183*1772Sjl139090 	(void) printf(gettext("Usage:  prtdscp -h \n"));
184*1772Sjl139090 	(void) printf(gettext("        prtdscp [-v] [-s|-d]\n"));
185*1772Sjl139090 }
186*1772Sjl139090 
187*1772Sjl139090 /*
188*1772Sjl139090  * get_address()
189*1772Sjl139090  *
190*1772Sjl139090  *	Retrieve a DSCP IP address using libdscp.
191*1772Sjl139090  */
192*1772Sjl139090 static int
193*1772Sjl139090 get_address(int which, char *addr)
194*1772Sjl139090 {
195*1772Sjl139090 	int			len;
196*1772Sjl139090 	int			error;
197*1772Sjl139090 	struct sockaddr_in	*sin;
198*1772Sjl139090 	struct sockaddr		saddr;
199*1772Sjl139090 
200*1772Sjl139090 	error = dscpAddr(0, which, &saddr, &len);
201*1772Sjl139090 	if (error != DSCP_OK) {
202*1772Sjl139090 		err(gettext("dscpAddr() failed: %s"), dscp_strerror(error));
203*1772Sjl139090 		return (-1);
204*1772Sjl139090 	}
205*1772Sjl139090 
206*1772Sjl139090 	/* LINTED pointer cast may result in improper alignment */
207*1772Sjl139090 	sin = (struct sockaddr_in *)&saddr;
208*1772Sjl139090 	if (inet_ntop(AF_INET, &(sin->sin_addr), addr, sizeof (*sin)) == NULL) {
209*1772Sjl139090 		err(gettext("address string conversion failed."));
210*1772Sjl139090 		return (-1);
211*1772Sjl139090 	}
212*1772Sjl139090 
213*1772Sjl139090 	return (0);
214*1772Sjl139090 }
215*1772Sjl139090 
216*1772Sjl139090 /*
217*1772Sjl139090  * trace()
218*1772Sjl139090  *
219*1772Sjl139090  *	Print tracing statements to stderr when in verbose mode.
220*1772Sjl139090  */
221*1772Sjl139090 /*PRINTFLIKE1*/
222*1772Sjl139090 static void
223*1772Sjl139090 trace(char *fmt, ...)
224*1772Sjl139090 {
225*1772Sjl139090 	va_list	args;
226*1772Sjl139090 
227*1772Sjl139090 	if (verbose != 0) {
228*1772Sjl139090 		va_start(args, fmt);
229*1772Sjl139090 		(void) vfprintf(stderr, fmt, args);
230*1772Sjl139090 		va_end(args);
231*1772Sjl139090 	}
232*1772Sjl139090 }
233*1772Sjl139090 
234*1772Sjl139090 /*
235*1772Sjl139090  * err()
236*1772Sjl139090  *
237*1772Sjl139090  *	Print error messages to stderr.
238*1772Sjl139090  */
239*1772Sjl139090 /*PRINTFLIKE1*/
240*1772Sjl139090 static void
241*1772Sjl139090 err(char *fmt, ...)
242*1772Sjl139090 {
243*1772Sjl139090 	va_list	args;
244*1772Sjl139090 
245*1772Sjl139090 	va_start(args, fmt);
246*1772Sjl139090 
247*1772Sjl139090 	(void) fprintf(stderr, gettext("ERROR: "));
248*1772Sjl139090 	(void) vfprintf(stderr, fmt, args);
249*1772Sjl139090 	(void) fprintf(stderr, "\n");
250*1772Sjl139090 
251*1772Sjl139090 	va_end(args);
252*1772Sjl139090 }
253*1772Sjl139090 
254*1772Sjl139090 /*
255*1772Sjl139090  * dscp_strerror()
256*1772Sjl139090  *
257*1772Sjl139090  *	Convert a DSCP error value into a localized string.
258*1772Sjl139090  */
259*1772Sjl139090 static char *
260*1772Sjl139090 dscp_strerror(int error)
261*1772Sjl139090 {
262*1772Sjl139090 	switch (error) {
263*1772Sjl139090 	case DSCP_OK:
264*1772Sjl139090 		return (gettext("Success."));
265*1772Sjl139090 	case DSCP_ERROR:
266*1772Sjl139090 		return (gettext("General error."));
267*1772Sjl139090 	case DSCP_ERROR_ALREADY:
268*1772Sjl139090 		return (gettext("Socket already bound."));
269*1772Sjl139090 	case DSCP_ERROR_INVALID:
270*1772Sjl139090 		return (gettext("Invalid arguments."));
271*1772Sjl139090 	case DSCP_ERROR_NOENT:
272*1772Sjl139090 		return (gettext("No entry found."));
273*1772Sjl139090 	case DSCP_ERROR_DB:
274*1772Sjl139090 		return (gettext("Error reading database."));
275*1772Sjl139090 	case DSCP_ERROR_REJECT:
276*1772Sjl139090 		return (gettext("Connection rejected."));
277*1772Sjl139090 	default:
278*1772Sjl139090 		return (gettext("Unknown failure."));
279*1772Sjl139090 	}
280*1772Sjl139090 }
281