125cf1a30Sjl139090 /*
225cf1a30Sjl139090 * CDDL HEADER START
325cf1a30Sjl139090 *
425cf1a30Sjl139090 * The contents of this file are subject to the terms of the
525cf1a30Sjl139090 * Common Development and Distribution License (the "License").
625cf1a30Sjl139090 * You may not use this file except in compliance with the License.
725cf1a30Sjl139090 *
825cf1a30Sjl139090 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925cf1a30Sjl139090 * or http://www.opensolaris.org/os/licensing.
1025cf1a30Sjl139090 * See the License for the specific language governing permissions
1125cf1a30Sjl139090 * and limitations under the License.
1225cf1a30Sjl139090 *
1325cf1a30Sjl139090 * When distributing Covered Code, include this CDDL HEADER in each
1425cf1a30Sjl139090 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525cf1a30Sjl139090 * If applicable, add the following below this CDDL HEADER, with the
1625cf1a30Sjl139090 * fields enclosed by brackets "[]" replaced with your own identifying
1725cf1a30Sjl139090 * information: Portions Copyright [yyyy] [name of copyright owner]
1825cf1a30Sjl139090 *
1925cf1a30Sjl139090 * CDDL HEADER END
2025cf1a30Sjl139090 */
2125cf1a30Sjl139090 /*
2225cf1a30Sjl139090 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2325cf1a30Sjl139090 * Use is subject to license terms.
2425cf1a30Sjl139090 */
2525cf1a30Sjl139090
2625cf1a30Sjl139090 #pragma ident "%Z%%M% %I% %E% SMI"
2725cf1a30Sjl139090
2825cf1a30Sjl139090 #include <stdio.h>
2925cf1a30Sjl139090 #include <stdlib.h>
3025cf1a30Sjl139090 #include <stdarg.h>
3125cf1a30Sjl139090 #include <strings.h>
3225cf1a30Sjl139090 #include <sys/types.h>
3325cf1a30Sjl139090 #include <sys/socket.h>
3425cf1a30Sjl139090 #include <netinet/in.h>
3525cf1a30Sjl139090 #include <arpa/inet.h>
3625cf1a30Sjl139090 #include <libintl.h>
3725cf1a30Sjl139090 #include <locale.h>
3825cf1a30Sjl139090 #include <libdscp.h>
3925cf1a30Sjl139090
4025cf1a30Sjl139090 #if !defined(TEXT_DOMAIN)
4125cf1a30Sjl139090 #define TEXT_DOMAIN "SYS_TEST"
4225cf1a30Sjl139090 #endif
4325cf1a30Sjl139090
4425cf1a30Sjl139090 #define OPT_SP 1
4525cf1a30Sjl139090 #define OPT_DOMAIN 2
4625cf1a30Sjl139090
4725cf1a30Sjl139090 static void usage(void);
4825cf1a30Sjl139090 static void parse_options(int, char **, int *);
4925cf1a30Sjl139090 static int get_address(int, char *);
5025cf1a30Sjl139090 static void trace(char *, ...);
5125cf1a30Sjl139090 static void err(char *, ...);
5225cf1a30Sjl139090 static char *dscp_strerror(int);
5325cf1a30Sjl139090
5425cf1a30Sjl139090 static int verbose = 0;
5525cf1a30Sjl139090
5625cf1a30Sjl139090 int
main(int argc,char ** argv)5725cf1a30Sjl139090 main(int argc, char **argv)
5825cf1a30Sjl139090 {
5925cf1a30Sjl139090 int options;
6025cf1a30Sjl139090 char saddr[INET_ADDRSTRLEN];
6125cf1a30Sjl139090 char daddr[INET_ADDRSTRLEN];
6225cf1a30Sjl139090
6325cf1a30Sjl139090 (void) setlocale(LC_ALL, "");
6425cf1a30Sjl139090 (void) textdomain(TEXT_DOMAIN);
6525cf1a30Sjl139090
6625cf1a30Sjl139090 parse_options(argc, argv, &options);
6725cf1a30Sjl139090
6825cf1a30Sjl139090 /*
6925cf1a30Sjl139090 * Get the desired IP addresses.
7025cf1a30Sjl139090 */
7125cf1a30Sjl139090 if ((options & OPT_SP) != 0) {
7225cf1a30Sjl139090 trace(gettext("Looking up SP address...\n"));
7325cf1a30Sjl139090 if (get_address(DSCP_ADDR_REMOTE, saddr) < 0) {
74*8bfd22b4Sraghuram err(gettext("SP Address lookup failed. Aborting.\n"));
7525cf1a30Sjl139090 exit(-1);
7625cf1a30Sjl139090 }
7725cf1a30Sjl139090 }
7825cf1a30Sjl139090 if ((options & OPT_DOMAIN) != 0) {
7925cf1a30Sjl139090 trace(gettext("Looking up domain address...\n"));
8025cf1a30Sjl139090 if (get_address(DSCP_ADDR_LOCAL, daddr) < 0) {
81*8bfd22b4Sraghuram err(gettext("Domain Address lookup failed. "
82*8bfd22b4Sraghuram "Aborting.\n"));
8325cf1a30Sjl139090 exit(-1);
8425cf1a30Sjl139090 }
8525cf1a30Sjl139090 }
8625cf1a30Sjl139090
8725cf1a30Sjl139090 /*
8825cf1a30Sjl139090 * Print the IP addresses.
8925cf1a30Sjl139090 */
9025cf1a30Sjl139090 if (options == OPT_SP) {
9125cf1a30Sjl139090 (void) printf("%s\n", saddr);
9225cf1a30Sjl139090 } else if (options == OPT_DOMAIN) {
9325cf1a30Sjl139090 (void) printf("%s\n", daddr);
9425cf1a30Sjl139090 } else {
9525cf1a30Sjl139090 (void) printf(gettext("Domain Address: %s\n"), daddr);
9625cf1a30Sjl139090 (void) printf(gettext("SP Address: %s\n"), saddr);
9725cf1a30Sjl139090 }
9825cf1a30Sjl139090
9925cf1a30Sjl139090 return (0);
10025cf1a30Sjl139090 }
10125cf1a30Sjl139090
10225cf1a30Sjl139090 /*
10325cf1a30Sjl139090 * parse_options()
10425cf1a30Sjl139090 *
10525cf1a30Sjl139090 * Parse the commandline options.
10625cf1a30Sjl139090 */
10725cf1a30Sjl139090 static void
parse_options(int argc,char ** argv,int * options)10825cf1a30Sjl139090 parse_options(int argc, char **argv, int *options)
10925cf1a30Sjl139090 {
11025cf1a30Sjl139090 int i;
11125cf1a30Sjl139090 int c;
11225cf1a30Sjl139090 extern int opterr;
11325cf1a30Sjl139090 extern int optopt;
11425cf1a30Sjl139090
11525cf1a30Sjl139090 /*
11625cf1a30Sjl139090 * Unless told otherwise, print everything.
11725cf1a30Sjl139090 */
11825cf1a30Sjl139090 *options = (OPT_SP | OPT_DOMAIN);
11925cf1a30Sjl139090
12025cf1a30Sjl139090 /*
12125cf1a30Sjl139090 * Skip this routine if no options exist.
12225cf1a30Sjl139090 */
12325cf1a30Sjl139090 if (argc == 1) {
12425cf1a30Sjl139090 return;
12525cf1a30Sjl139090 }
12625cf1a30Sjl139090
12725cf1a30Sjl139090 /*
12825cf1a30Sjl139090 * Scan for the -h option separately, so that
12925cf1a30Sjl139090 * other commandline options are ignored.
13025cf1a30Sjl139090 */
13125cf1a30Sjl139090 for (i = 1; i < argc; i++) {
13225cf1a30Sjl139090 if (strcmp(argv[i], "-h") == 0) {
13325cf1a30Sjl139090 usage();
13425cf1a30Sjl139090 exit(0);
13525cf1a30Sjl139090 }
13625cf1a30Sjl139090 }
13725cf1a30Sjl139090
13825cf1a30Sjl139090 /*
13925cf1a30Sjl139090 * Disable the built-in error reporting, so that
14025cf1a30Sjl139090 * error messages can be properly internationalized.
14125cf1a30Sjl139090 */
14225cf1a30Sjl139090 opterr = 0;
14325cf1a30Sjl139090
14425cf1a30Sjl139090 /*
14525cf1a30Sjl139090 * The main loop for parsing options.
14625cf1a30Sjl139090 */
14725cf1a30Sjl139090 while ((c = getopt(argc, argv, "vsd")) != -1) {
14825cf1a30Sjl139090 switch (c) {
14925cf1a30Sjl139090 case 'v':
15025cf1a30Sjl139090 verbose = 1;
15125cf1a30Sjl139090 break;
15225cf1a30Sjl139090 case 's':
15325cf1a30Sjl139090 if (*options == OPT_DOMAIN) {
15425cf1a30Sjl139090 err(gettext("cannot use -s and -d together"));
15525cf1a30Sjl139090 usage();
15625cf1a30Sjl139090 exit(-1);
15725cf1a30Sjl139090 }
15825cf1a30Sjl139090 *options = OPT_SP;
15925cf1a30Sjl139090 break;
16025cf1a30Sjl139090 case 'd':
16125cf1a30Sjl139090 if (*options == OPT_SP) {
16225cf1a30Sjl139090 err(gettext("cannot use -s and -d together"));
16325cf1a30Sjl139090 usage();
16425cf1a30Sjl139090 exit(-1);
16525cf1a30Sjl139090 }
16625cf1a30Sjl139090 *options = OPT_DOMAIN;
16725cf1a30Sjl139090 break;
16825cf1a30Sjl139090 default:
16925cf1a30Sjl139090 err(gettext("invalid option -%c"), optopt);
17025cf1a30Sjl139090 usage();
17125cf1a30Sjl139090 exit(-1);
17225cf1a30Sjl139090 }
17325cf1a30Sjl139090 }
17425cf1a30Sjl139090 }
17525cf1a30Sjl139090
17625cf1a30Sjl139090 /*
17725cf1a30Sjl139090 * usage()
17825cf1a30Sjl139090 *
17925cf1a30Sjl139090 * Print a brief synopsis of the program's usage.
18025cf1a30Sjl139090 */
18125cf1a30Sjl139090 static void
usage(void)18225cf1a30Sjl139090 usage(void)
18325cf1a30Sjl139090 {
18425cf1a30Sjl139090 (void) printf(gettext("Usage: prtdscp -h \n"));
18525cf1a30Sjl139090 (void) printf(gettext(" prtdscp [-v] [-s|-d]\n"));
18625cf1a30Sjl139090 }
18725cf1a30Sjl139090
18825cf1a30Sjl139090 /*
18925cf1a30Sjl139090 * get_address()
19025cf1a30Sjl139090 *
19125cf1a30Sjl139090 * Retrieve a DSCP IP address using libdscp.
19225cf1a30Sjl139090 */
19325cf1a30Sjl139090 static int
get_address(int which,char * addr)19425cf1a30Sjl139090 get_address(int which, char *addr)
19525cf1a30Sjl139090 {
19625cf1a30Sjl139090 int len;
19725cf1a30Sjl139090 int error;
19825cf1a30Sjl139090 struct sockaddr_in *sin;
19925cf1a30Sjl139090 struct sockaddr saddr;
20025cf1a30Sjl139090
20125cf1a30Sjl139090 error = dscpAddr(0, which, &saddr, &len);
20225cf1a30Sjl139090 if (error != DSCP_OK) {
203*8bfd22b4Sraghuram trace(gettext("dscpAddr() failed: %s"), dscp_strerror(error));
20425cf1a30Sjl139090 return (-1);
20525cf1a30Sjl139090 }
20625cf1a30Sjl139090
20725cf1a30Sjl139090 /* LINTED pointer cast may result in improper alignment */
20825cf1a30Sjl139090 sin = (struct sockaddr_in *)&saddr;
20925cf1a30Sjl139090 if (inet_ntop(AF_INET, &(sin->sin_addr), addr, sizeof (*sin)) == NULL) {
210*8bfd22b4Sraghuram trace(gettext("address string conversion failed."));
21125cf1a30Sjl139090 return (-1);
21225cf1a30Sjl139090 }
21325cf1a30Sjl139090
21425cf1a30Sjl139090 return (0);
21525cf1a30Sjl139090 }
21625cf1a30Sjl139090
21725cf1a30Sjl139090 /*
21825cf1a30Sjl139090 * trace()
21925cf1a30Sjl139090 *
22025cf1a30Sjl139090 * Print tracing statements to stderr when in verbose mode.
22125cf1a30Sjl139090 */
22225cf1a30Sjl139090 /*PRINTFLIKE1*/
22325cf1a30Sjl139090 static void
trace(char * fmt,...)22425cf1a30Sjl139090 trace(char *fmt, ...)
22525cf1a30Sjl139090 {
22625cf1a30Sjl139090 va_list args;
22725cf1a30Sjl139090
22825cf1a30Sjl139090 if (verbose != 0) {
22925cf1a30Sjl139090 va_start(args, fmt);
23025cf1a30Sjl139090 (void) vfprintf(stderr, fmt, args);
23125cf1a30Sjl139090 va_end(args);
23225cf1a30Sjl139090 }
23325cf1a30Sjl139090 }
23425cf1a30Sjl139090
23525cf1a30Sjl139090 /*
23625cf1a30Sjl139090 * err()
23725cf1a30Sjl139090 *
23825cf1a30Sjl139090 * Print error messages to stderr.
23925cf1a30Sjl139090 */
24025cf1a30Sjl139090 /*PRINTFLIKE1*/
24125cf1a30Sjl139090 static void
err(char * fmt,...)24225cf1a30Sjl139090 err(char *fmt, ...)
24325cf1a30Sjl139090 {
24425cf1a30Sjl139090 va_list args;
24525cf1a30Sjl139090
24625cf1a30Sjl139090 va_start(args, fmt);
24725cf1a30Sjl139090
24825cf1a30Sjl139090 (void) fprintf(stderr, gettext("ERROR: "));
24925cf1a30Sjl139090 (void) vfprintf(stderr, fmt, args);
25025cf1a30Sjl139090 (void) fprintf(stderr, "\n");
25125cf1a30Sjl139090
25225cf1a30Sjl139090 va_end(args);
25325cf1a30Sjl139090 }
25425cf1a30Sjl139090
25525cf1a30Sjl139090 /*
25625cf1a30Sjl139090 * dscp_strerror()
25725cf1a30Sjl139090 *
25825cf1a30Sjl139090 * Convert a DSCP error value into a localized string.
25925cf1a30Sjl139090 */
26025cf1a30Sjl139090 static char *
dscp_strerror(int error)26125cf1a30Sjl139090 dscp_strerror(int error)
26225cf1a30Sjl139090 {
26325cf1a30Sjl139090 switch (error) {
26425cf1a30Sjl139090 case DSCP_OK:
26525cf1a30Sjl139090 return (gettext("Success."));
26625cf1a30Sjl139090 case DSCP_ERROR:
26725cf1a30Sjl139090 return (gettext("General error."));
26825cf1a30Sjl139090 case DSCP_ERROR_ALREADY:
26925cf1a30Sjl139090 return (gettext("Socket already bound."));
27025cf1a30Sjl139090 case DSCP_ERROR_INVALID:
27125cf1a30Sjl139090 return (gettext("Invalid arguments."));
27225cf1a30Sjl139090 case DSCP_ERROR_NOENT:
27325cf1a30Sjl139090 return (gettext("No entry found."));
27425cf1a30Sjl139090 case DSCP_ERROR_DB:
27525cf1a30Sjl139090 return (gettext("Error reading database."));
27625cf1a30Sjl139090 case DSCP_ERROR_REJECT:
27725cf1a30Sjl139090 return (gettext("Connection rejected."));
27825cf1a30Sjl139090 default:
27925cf1a30Sjl139090 return (gettext("Unknown failure."));
28025cf1a30Sjl139090 }
28125cf1a30Sjl139090 }
282