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 #include <stdio.h>
2725cf1a30Sjl139090 #include <stdlib.h>
2825cf1a30Sjl139090 #include <stdarg.h>
2925cf1a30Sjl139090 #include <strings.h>
3025cf1a30Sjl139090 #include <sys/types.h>
3125cf1a30Sjl139090 #include <sys/socket.h>
3225cf1a30Sjl139090 #include <netinet/in.h>
3325cf1a30Sjl139090 #include <arpa/inet.h>
3425cf1a30Sjl139090 #include <libintl.h>
3525cf1a30Sjl139090 #include <locale.h>
3625cf1a30Sjl139090 #include <libdscp.h>
3725cf1a30Sjl139090
3825cf1a30Sjl139090 #if !defined(TEXT_DOMAIN)
3925cf1a30Sjl139090 #define TEXT_DOMAIN "SYS_TEST"
4025cf1a30Sjl139090 #endif
4125cf1a30Sjl139090
4225cf1a30Sjl139090 #define OPT_SP 1
4325cf1a30Sjl139090 #define OPT_DOMAIN 2
4425cf1a30Sjl139090
4525cf1a30Sjl139090 static void usage(void);
4625cf1a30Sjl139090 static void parse_options(int, char **, int *);
4725cf1a30Sjl139090 static int get_address(int, char *);
4825cf1a30Sjl139090 static void trace(char *, ...);
4925cf1a30Sjl139090 static void err(char *, ...);
5025cf1a30Sjl139090 static char *dscp_strerror(int);
5125cf1a30Sjl139090
5225cf1a30Sjl139090 static int verbose = 0;
5325cf1a30Sjl139090
5425cf1a30Sjl139090 int
main(int argc,char ** argv)5525cf1a30Sjl139090 main(int argc, char **argv)
5625cf1a30Sjl139090 {
5725cf1a30Sjl139090 int options;
5825cf1a30Sjl139090 char saddr[INET_ADDRSTRLEN];
5925cf1a30Sjl139090 char daddr[INET_ADDRSTRLEN];
6025cf1a30Sjl139090
6125cf1a30Sjl139090 (void) setlocale(LC_ALL, "");
6225cf1a30Sjl139090 (void) textdomain(TEXT_DOMAIN);
6325cf1a30Sjl139090
6425cf1a30Sjl139090 parse_options(argc, argv, &options);
6525cf1a30Sjl139090
6625cf1a30Sjl139090 /*
6725cf1a30Sjl139090 * Get the desired IP addresses.
6825cf1a30Sjl139090 */
6925cf1a30Sjl139090 if ((options & OPT_SP) != 0) {
7025cf1a30Sjl139090 trace(gettext("Looking up SP address...\n"));
7125cf1a30Sjl139090 if (get_address(DSCP_ADDR_REMOTE, saddr) < 0) {
72*8bfd22b4Sraghuram err(gettext("SP Address lookup failed. Aborting.\n"));
7325cf1a30Sjl139090 exit(-1);
7425cf1a30Sjl139090 }
7525cf1a30Sjl139090 }
7625cf1a30Sjl139090 if ((options & OPT_DOMAIN) != 0) {
7725cf1a30Sjl139090 trace(gettext("Looking up domain address...\n"));
7825cf1a30Sjl139090 if (get_address(DSCP_ADDR_LOCAL, daddr) < 0) {
79*8bfd22b4Sraghuram err(gettext("Domain Address lookup failed. "
80*8bfd22b4Sraghuram "Aborting.\n"));
8125cf1a30Sjl139090 exit(-1);
8225cf1a30Sjl139090 }
8325cf1a30Sjl139090 }
8425cf1a30Sjl139090
8525cf1a30Sjl139090 /*
8625cf1a30Sjl139090 * Print the IP addresses.
8725cf1a30Sjl139090 */
8825cf1a30Sjl139090 if (options == OPT_SP) {
8925cf1a30Sjl139090 (void) printf("%s\n", saddr);
9025cf1a30Sjl139090 } else if (options == OPT_DOMAIN) {
9125cf1a30Sjl139090 (void) printf("%s\n", daddr);
9225cf1a30Sjl139090 } else {
9325cf1a30Sjl139090 (void) printf(gettext("Domain Address: %s\n"), daddr);
9425cf1a30Sjl139090 (void) printf(gettext("SP Address: %s\n"), saddr);
9525cf1a30Sjl139090 }
9625cf1a30Sjl139090
9725cf1a30Sjl139090 return (0);
9825cf1a30Sjl139090 }
9925cf1a30Sjl139090
10025cf1a30Sjl139090 /*
10125cf1a30Sjl139090 * parse_options()
10225cf1a30Sjl139090 *
10325cf1a30Sjl139090 * Parse the commandline options.
10425cf1a30Sjl139090 */
10525cf1a30Sjl139090 static void
parse_options(int argc,char ** argv,int * options)10625cf1a30Sjl139090 parse_options(int argc, char **argv, int *options)
10725cf1a30Sjl139090 {
10825cf1a30Sjl139090 int i;
10925cf1a30Sjl139090 int c;
11025cf1a30Sjl139090 extern int opterr;
11125cf1a30Sjl139090 extern int optopt;
11225cf1a30Sjl139090
11325cf1a30Sjl139090 /*
11425cf1a30Sjl139090 * Unless told otherwise, print everything.
11525cf1a30Sjl139090 */
11625cf1a30Sjl139090 *options = (OPT_SP | OPT_DOMAIN);
11725cf1a30Sjl139090
11825cf1a30Sjl139090 /*
11925cf1a30Sjl139090 * Skip this routine if no options exist.
12025cf1a30Sjl139090 */
12125cf1a30Sjl139090 if (argc == 1) {
12225cf1a30Sjl139090 return;
12325cf1a30Sjl139090 }
12425cf1a30Sjl139090
12525cf1a30Sjl139090 /*
12625cf1a30Sjl139090 * Scan for the -h option separately, so that
12725cf1a30Sjl139090 * other commandline options are ignored.
12825cf1a30Sjl139090 */
12925cf1a30Sjl139090 for (i = 1; i < argc; i++) {
13025cf1a30Sjl139090 if (strcmp(argv[i], "-h") == 0) {
13125cf1a30Sjl139090 usage();
13225cf1a30Sjl139090 exit(0);
13325cf1a30Sjl139090 }
13425cf1a30Sjl139090 }
13525cf1a30Sjl139090
13625cf1a30Sjl139090 /*
13725cf1a30Sjl139090 * Disable the built-in error reporting, so that
13825cf1a30Sjl139090 * error messages can be properly internationalized.
13925cf1a30Sjl139090 */
14025cf1a30Sjl139090 opterr = 0;
14125cf1a30Sjl139090
14225cf1a30Sjl139090 /*
14325cf1a30Sjl139090 * The main loop for parsing options.
14425cf1a30Sjl139090 */
14525cf1a30Sjl139090 while ((c = getopt(argc, argv, "vsd")) != -1) {
14625cf1a30Sjl139090 switch (c) {
14725cf1a30Sjl139090 case 'v':
14825cf1a30Sjl139090 verbose = 1;
14925cf1a30Sjl139090 break;
15025cf1a30Sjl139090 case 's':
15125cf1a30Sjl139090 if (*options == OPT_DOMAIN) {
15225cf1a30Sjl139090 err(gettext("cannot use -s and -d together"));
15325cf1a30Sjl139090 usage();
15425cf1a30Sjl139090 exit(-1);
15525cf1a30Sjl139090 }
15625cf1a30Sjl139090 *options = OPT_SP;
15725cf1a30Sjl139090 break;
15825cf1a30Sjl139090 case 'd':
15925cf1a30Sjl139090 if (*options == OPT_SP) {
16025cf1a30Sjl139090 err(gettext("cannot use -s and -d together"));
16125cf1a30Sjl139090 usage();
16225cf1a30Sjl139090 exit(-1);
16325cf1a30Sjl139090 }
16425cf1a30Sjl139090 *options = OPT_DOMAIN;
16525cf1a30Sjl139090 break;
16625cf1a30Sjl139090 default:
16725cf1a30Sjl139090 err(gettext("invalid option -%c"), optopt);
16825cf1a30Sjl139090 usage();
16925cf1a30Sjl139090 exit(-1);
17025cf1a30Sjl139090 }
17125cf1a30Sjl139090 }
17225cf1a30Sjl139090 }
17325cf1a30Sjl139090
17425cf1a30Sjl139090 /*
17525cf1a30Sjl139090 * usage()
17625cf1a30Sjl139090 *
17725cf1a30Sjl139090 * Print a brief synopsis of the program's usage.
17825cf1a30Sjl139090 */
17925cf1a30Sjl139090 static void
usage(void)18025cf1a30Sjl139090 usage(void)
18125cf1a30Sjl139090 {
18225cf1a30Sjl139090 (void) printf(gettext("Usage: prtdscp -h \n"));
18325cf1a30Sjl139090 (void) printf(gettext(" prtdscp [-v] [-s|-d]\n"));
18425cf1a30Sjl139090 }
18525cf1a30Sjl139090
18625cf1a30Sjl139090 /*
18725cf1a30Sjl139090 * get_address()
18825cf1a30Sjl139090 *
18925cf1a30Sjl139090 * Retrieve a DSCP IP address using libdscp.
19025cf1a30Sjl139090 */
19125cf1a30Sjl139090 static int
get_address(int which,char * addr)19225cf1a30Sjl139090 get_address(int which, char *addr)
19325cf1a30Sjl139090 {
19425cf1a30Sjl139090 int len;
19525cf1a30Sjl139090 int error;
19625cf1a30Sjl139090 struct sockaddr_in *sin;
19725cf1a30Sjl139090 struct sockaddr saddr;
19825cf1a30Sjl139090
19925cf1a30Sjl139090 error = dscpAddr(0, which, &saddr, &len);
20025cf1a30Sjl139090 if (error != DSCP_OK) {
201*8bfd22b4Sraghuram trace(gettext("dscpAddr() failed: %s"), dscp_strerror(error));
20225cf1a30Sjl139090 return (-1);
20325cf1a30Sjl139090 }
20425cf1a30Sjl139090
20525cf1a30Sjl139090 /* LINTED pointer cast may result in improper alignment */
20625cf1a30Sjl139090 sin = (struct sockaddr_in *)&saddr;
20725cf1a30Sjl139090 if (inet_ntop(AF_INET, &(sin->sin_addr), addr, sizeof (*sin)) == NULL) {
208*8bfd22b4Sraghuram trace(gettext("address string conversion failed."));
20925cf1a30Sjl139090 return (-1);
21025cf1a30Sjl139090 }
21125cf1a30Sjl139090
21225cf1a30Sjl139090 return (0);
21325cf1a30Sjl139090 }
21425cf1a30Sjl139090
21525cf1a30Sjl139090 /*
21625cf1a30Sjl139090 * trace()
21725cf1a30Sjl139090 *
21825cf1a30Sjl139090 * Print tracing statements to stderr when in verbose mode.
21925cf1a30Sjl139090 */
22025cf1a30Sjl139090 /*PRINTFLIKE1*/
22125cf1a30Sjl139090 static void
trace(char * fmt,...)22225cf1a30Sjl139090 trace(char *fmt, ...)
22325cf1a30Sjl139090 {
22425cf1a30Sjl139090 va_list args;
22525cf1a30Sjl139090
22625cf1a30Sjl139090 if (verbose != 0) {
22725cf1a30Sjl139090 va_start(args, fmt);
22825cf1a30Sjl139090 (void) vfprintf(stderr, fmt, args);
22925cf1a30Sjl139090 va_end(args);
23025cf1a30Sjl139090 }
23125cf1a30Sjl139090 }
23225cf1a30Sjl139090
23325cf1a30Sjl139090 /*
23425cf1a30Sjl139090 * err()
23525cf1a30Sjl139090 *
23625cf1a30Sjl139090 * Print error messages to stderr.
23725cf1a30Sjl139090 */
23825cf1a30Sjl139090 /*PRINTFLIKE1*/
23925cf1a30Sjl139090 static void
err(char * fmt,...)24025cf1a30Sjl139090 err(char *fmt, ...)
24125cf1a30Sjl139090 {
24225cf1a30Sjl139090 va_list args;
24325cf1a30Sjl139090
24425cf1a30Sjl139090 va_start(args, fmt);
24525cf1a30Sjl139090
24625cf1a30Sjl139090 (void) fprintf(stderr, gettext("ERROR: "));
24725cf1a30Sjl139090 (void) vfprintf(stderr, fmt, args);
24825cf1a30Sjl139090 (void) fprintf(stderr, "\n");
24925cf1a30Sjl139090
25025cf1a30Sjl139090 va_end(args);
25125cf1a30Sjl139090 }
25225cf1a30Sjl139090
25325cf1a30Sjl139090 /*
25425cf1a30Sjl139090 * dscp_strerror()
25525cf1a30Sjl139090 *
25625cf1a30Sjl139090 * Convert a DSCP error value into a localized string.
25725cf1a30Sjl139090 */
25825cf1a30Sjl139090 static char *
dscp_strerror(int error)25925cf1a30Sjl139090 dscp_strerror(int error)
26025cf1a30Sjl139090 {
26125cf1a30Sjl139090 switch (error) {
26225cf1a30Sjl139090 case DSCP_OK:
26325cf1a30Sjl139090 return (gettext("Success."));
26425cf1a30Sjl139090 case DSCP_ERROR:
26525cf1a30Sjl139090 return (gettext("General error."));
26625cf1a30Sjl139090 case DSCP_ERROR_ALREADY:
26725cf1a30Sjl139090 return (gettext("Socket already bound."));
26825cf1a30Sjl139090 case DSCP_ERROR_INVALID:
26925cf1a30Sjl139090 return (gettext("Invalid arguments."));
27025cf1a30Sjl139090 case DSCP_ERROR_NOENT:
27125cf1a30Sjl139090 return (gettext("No entry found."));
27225cf1a30Sjl139090 case DSCP_ERROR_DB:
27325cf1a30Sjl139090 return (gettext("Error reading database."));
27425cf1a30Sjl139090 case DSCP_ERROR_REJECT:
27525cf1a30Sjl139090 return (gettext("Connection rejected."));
27625cf1a30Sjl139090 default:
27725cf1a30Sjl139090 return (gettext("Unknown failure."));
27825cf1a30Sjl139090 }
27925cf1a30Sjl139090 }
280