11772Sjl139090 /*
21772Sjl139090 * CDDL HEADER START
31772Sjl139090 *
41772Sjl139090 * The contents of this file are subject to the terms of the
51772Sjl139090 * Common Development and Distribution License (the "License").
61772Sjl139090 * You may not use this file except in compliance with the License.
71772Sjl139090 *
81772Sjl139090 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91772Sjl139090 * or http://www.opensolaris.org/os/licensing.
101772Sjl139090 * See the License for the specific language governing permissions
111772Sjl139090 * and limitations under the License.
121772Sjl139090 *
131772Sjl139090 * When distributing Covered Code, include this CDDL HEADER in each
141772Sjl139090 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151772Sjl139090 * If applicable, add the following below this CDDL HEADER, with the
161772Sjl139090 * fields enclosed by brackets "[]" replaced with your own identifying
171772Sjl139090 * information: Portions Copyright [yyyy] [name of copyright owner]
181772Sjl139090 *
191772Sjl139090 * CDDL HEADER END
201772Sjl139090 */
211772Sjl139090 /*
221772Sjl139090 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
231772Sjl139090 * Use is subject to license terms.
241772Sjl139090 */
251772Sjl139090
261772Sjl139090 #pragma ident "%Z%%M% %I% %E% SMI"
271772Sjl139090
281772Sjl139090 #include <stdio.h>
291772Sjl139090 #include <stdlib.h>
301772Sjl139090 #include <stdarg.h>
311772Sjl139090 #include <strings.h>
321772Sjl139090 #include <sys/types.h>
331772Sjl139090 #include <sys/socket.h>
341772Sjl139090 #include <netinet/in.h>
351772Sjl139090 #include <arpa/inet.h>
361772Sjl139090 #include <libintl.h>
371772Sjl139090 #include <locale.h>
381772Sjl139090 #include <libdscp.h>
391772Sjl139090
401772Sjl139090 #if !defined(TEXT_DOMAIN)
411772Sjl139090 #define TEXT_DOMAIN "SYS_TEST"
421772Sjl139090 #endif
431772Sjl139090
441772Sjl139090 #define OPT_SP 1
451772Sjl139090 #define OPT_DOMAIN 2
461772Sjl139090
471772Sjl139090 static void usage(void);
481772Sjl139090 static void parse_options(int, char **, int *);
491772Sjl139090 static int get_address(int, char *);
501772Sjl139090 static void trace(char *, ...);
511772Sjl139090 static void err(char *, ...);
521772Sjl139090 static char *dscp_strerror(int);
531772Sjl139090
541772Sjl139090 static int verbose = 0;
551772Sjl139090
561772Sjl139090 int
main(int argc,char ** argv)571772Sjl139090 main(int argc, char **argv)
581772Sjl139090 {
591772Sjl139090 int options;
601772Sjl139090 char saddr[INET_ADDRSTRLEN];
611772Sjl139090 char daddr[INET_ADDRSTRLEN];
621772Sjl139090
631772Sjl139090 (void) setlocale(LC_ALL, "");
641772Sjl139090 (void) textdomain(TEXT_DOMAIN);
651772Sjl139090
661772Sjl139090 parse_options(argc, argv, &options);
671772Sjl139090
681772Sjl139090 /*
691772Sjl139090 * Get the desired IP addresses.
701772Sjl139090 */
711772Sjl139090 if ((options & OPT_SP) != 0) {
721772Sjl139090 trace(gettext("Looking up SP address...\n"));
731772Sjl139090 if (get_address(DSCP_ADDR_REMOTE, saddr) < 0) {
74*1870Sraghuram err(gettext("SP Address lookup failed. Aborting.\n"));
751772Sjl139090 exit(-1);
761772Sjl139090 }
771772Sjl139090 }
781772Sjl139090 if ((options & OPT_DOMAIN) != 0) {
791772Sjl139090 trace(gettext("Looking up domain address...\n"));
801772Sjl139090 if (get_address(DSCP_ADDR_LOCAL, daddr) < 0) {
81*1870Sraghuram err(gettext("Domain Address lookup failed. "
82*1870Sraghuram "Aborting.\n"));
831772Sjl139090 exit(-1);
841772Sjl139090 }
851772Sjl139090 }
861772Sjl139090
871772Sjl139090 /*
881772Sjl139090 * Print the IP addresses.
891772Sjl139090 */
901772Sjl139090 if (options == OPT_SP) {
911772Sjl139090 (void) printf("%s\n", saddr);
921772Sjl139090 } else if (options == OPT_DOMAIN) {
931772Sjl139090 (void) printf("%s\n", daddr);
941772Sjl139090 } else {
951772Sjl139090 (void) printf(gettext("Domain Address: %s\n"), daddr);
961772Sjl139090 (void) printf(gettext("SP Address: %s\n"), saddr);
971772Sjl139090 }
981772Sjl139090
991772Sjl139090 return (0);
1001772Sjl139090 }
1011772Sjl139090
1021772Sjl139090 /*
1031772Sjl139090 * parse_options()
1041772Sjl139090 *
1051772Sjl139090 * Parse the commandline options.
1061772Sjl139090 */
1071772Sjl139090 static void
parse_options(int argc,char ** argv,int * options)1081772Sjl139090 parse_options(int argc, char **argv, int *options)
1091772Sjl139090 {
1101772Sjl139090 int i;
1111772Sjl139090 int c;
1121772Sjl139090 extern int opterr;
1131772Sjl139090 extern int optopt;
1141772Sjl139090
1151772Sjl139090 /*
1161772Sjl139090 * Unless told otherwise, print everything.
1171772Sjl139090 */
1181772Sjl139090 *options = (OPT_SP | OPT_DOMAIN);
1191772Sjl139090
1201772Sjl139090 /*
1211772Sjl139090 * Skip this routine if no options exist.
1221772Sjl139090 */
1231772Sjl139090 if (argc == 1) {
1241772Sjl139090 return;
1251772Sjl139090 }
1261772Sjl139090
1271772Sjl139090 /*
1281772Sjl139090 * Scan for the -h option separately, so that
1291772Sjl139090 * other commandline options are ignored.
1301772Sjl139090 */
1311772Sjl139090 for (i = 1; i < argc; i++) {
1321772Sjl139090 if (strcmp(argv[i], "-h") == 0) {
1331772Sjl139090 usage();
1341772Sjl139090 exit(0);
1351772Sjl139090 }
1361772Sjl139090 }
1371772Sjl139090
1381772Sjl139090 /*
1391772Sjl139090 * Disable the built-in error reporting, so that
1401772Sjl139090 * error messages can be properly internationalized.
1411772Sjl139090 */
1421772Sjl139090 opterr = 0;
1431772Sjl139090
1441772Sjl139090 /*
1451772Sjl139090 * The main loop for parsing options.
1461772Sjl139090 */
1471772Sjl139090 while ((c = getopt(argc, argv, "vsd")) != -1) {
1481772Sjl139090 switch (c) {
1491772Sjl139090 case 'v':
1501772Sjl139090 verbose = 1;
1511772Sjl139090 break;
1521772Sjl139090 case 's':
1531772Sjl139090 if (*options == OPT_DOMAIN) {
1541772Sjl139090 err(gettext("cannot use -s and -d together"));
1551772Sjl139090 usage();
1561772Sjl139090 exit(-1);
1571772Sjl139090 }
1581772Sjl139090 *options = OPT_SP;
1591772Sjl139090 break;
1601772Sjl139090 case 'd':
1611772Sjl139090 if (*options == OPT_SP) {
1621772Sjl139090 err(gettext("cannot use -s and -d together"));
1631772Sjl139090 usage();
1641772Sjl139090 exit(-1);
1651772Sjl139090 }
1661772Sjl139090 *options = OPT_DOMAIN;
1671772Sjl139090 break;
1681772Sjl139090 default:
1691772Sjl139090 err(gettext("invalid option -%c"), optopt);
1701772Sjl139090 usage();
1711772Sjl139090 exit(-1);
1721772Sjl139090 }
1731772Sjl139090 }
1741772Sjl139090 }
1751772Sjl139090
1761772Sjl139090 /*
1771772Sjl139090 * usage()
1781772Sjl139090 *
1791772Sjl139090 * Print a brief synopsis of the program's usage.
1801772Sjl139090 */
1811772Sjl139090 static void
usage(void)1821772Sjl139090 usage(void)
1831772Sjl139090 {
1841772Sjl139090 (void) printf(gettext("Usage: prtdscp -h \n"));
1851772Sjl139090 (void) printf(gettext(" prtdscp [-v] [-s|-d]\n"));
1861772Sjl139090 }
1871772Sjl139090
1881772Sjl139090 /*
1891772Sjl139090 * get_address()
1901772Sjl139090 *
1911772Sjl139090 * Retrieve a DSCP IP address using libdscp.
1921772Sjl139090 */
1931772Sjl139090 static int
get_address(int which,char * addr)1941772Sjl139090 get_address(int which, char *addr)
1951772Sjl139090 {
1961772Sjl139090 int len;
1971772Sjl139090 int error;
1981772Sjl139090 struct sockaddr_in *sin;
1991772Sjl139090 struct sockaddr saddr;
2001772Sjl139090
2011772Sjl139090 error = dscpAddr(0, which, &saddr, &len);
2021772Sjl139090 if (error != DSCP_OK) {
203*1870Sraghuram trace(gettext("dscpAddr() failed: %s"), dscp_strerror(error));
2041772Sjl139090 return (-1);
2051772Sjl139090 }
2061772Sjl139090
2071772Sjl139090 /* LINTED pointer cast may result in improper alignment */
2081772Sjl139090 sin = (struct sockaddr_in *)&saddr;
2091772Sjl139090 if (inet_ntop(AF_INET, &(sin->sin_addr), addr, sizeof (*sin)) == NULL) {
210*1870Sraghuram trace(gettext("address string conversion failed."));
2111772Sjl139090 return (-1);
2121772Sjl139090 }
2131772Sjl139090
2141772Sjl139090 return (0);
2151772Sjl139090 }
2161772Sjl139090
2171772Sjl139090 /*
2181772Sjl139090 * trace()
2191772Sjl139090 *
2201772Sjl139090 * Print tracing statements to stderr when in verbose mode.
2211772Sjl139090 */
2221772Sjl139090 /*PRINTFLIKE1*/
2231772Sjl139090 static void
trace(char * fmt,...)2241772Sjl139090 trace(char *fmt, ...)
2251772Sjl139090 {
2261772Sjl139090 va_list args;
2271772Sjl139090
2281772Sjl139090 if (verbose != 0) {
2291772Sjl139090 va_start(args, fmt);
2301772Sjl139090 (void) vfprintf(stderr, fmt, args);
2311772Sjl139090 va_end(args);
2321772Sjl139090 }
2331772Sjl139090 }
2341772Sjl139090
2351772Sjl139090 /*
2361772Sjl139090 * err()
2371772Sjl139090 *
2381772Sjl139090 * Print error messages to stderr.
2391772Sjl139090 */
2401772Sjl139090 /*PRINTFLIKE1*/
2411772Sjl139090 static void
err(char * fmt,...)2421772Sjl139090 err(char *fmt, ...)
2431772Sjl139090 {
2441772Sjl139090 va_list args;
2451772Sjl139090
2461772Sjl139090 va_start(args, fmt);
2471772Sjl139090
2481772Sjl139090 (void) fprintf(stderr, gettext("ERROR: "));
2491772Sjl139090 (void) vfprintf(stderr, fmt, args);
2501772Sjl139090 (void) fprintf(stderr, "\n");
2511772Sjl139090
2521772Sjl139090 va_end(args);
2531772Sjl139090 }
2541772Sjl139090
2551772Sjl139090 /*
2561772Sjl139090 * dscp_strerror()
2571772Sjl139090 *
2581772Sjl139090 * Convert a DSCP error value into a localized string.
2591772Sjl139090 */
2601772Sjl139090 static char *
dscp_strerror(int error)2611772Sjl139090 dscp_strerror(int error)
2621772Sjl139090 {
2631772Sjl139090 switch (error) {
2641772Sjl139090 case DSCP_OK:
2651772Sjl139090 return (gettext("Success."));
2661772Sjl139090 case DSCP_ERROR:
2671772Sjl139090 return (gettext("General error."));
2681772Sjl139090 case DSCP_ERROR_ALREADY:
2691772Sjl139090 return (gettext("Socket already bound."));
2701772Sjl139090 case DSCP_ERROR_INVALID:
2711772Sjl139090 return (gettext("Invalid arguments."));
2721772Sjl139090 case DSCP_ERROR_NOENT:
2731772Sjl139090 return (gettext("No entry found."));
2741772Sjl139090 case DSCP_ERROR_DB:
2751772Sjl139090 return (gettext("Error reading database."));
2761772Sjl139090 case DSCP_ERROR_REJECT:
2771772Sjl139090 return (gettext("Connection rejected."));
2781772Sjl139090 default:
2791772Sjl139090 return (gettext("Unknown failure."));
2801772Sjl139090 }
2811772Sjl139090 }
282