1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
  14  */
  15 
  16 
  17 #include <sys/note.h>
  18 #include <stdarg.h>
  19 #include <stdio.h>
  20 #include <addisc.h>
  21 #include <netinet/in.h>
  22 #include <arpa/inet.h>
  23 
  24 int debug;
  25 char *domainname = NULL;
  26 
  27 void print_ds(ad_disc_ds_t *);
  28 void mylogger(int pri, const char *format, ...);
  29 
  30 int
  31 main(int argc, char *argv[])
  32 {
  33         ad_disc_t ad_ctx = NULL;
  34         boolean_t autodisc;
  35         ad_disc_ds_t *dc, *gc;
  36         char *s;
  37         int c;
  38 
  39         while ((c = getopt(argc, argv, "d")) != -1) {
  40                 switch (c) {
  41                 case '?':
  42                         (void) fprintf(stderr, "bad option: -%c\n", optopt);
  43                         return (1);
  44                 case 'd':
  45                         debug++;
  46                         break;
  47                 }
  48         }
  49 
  50         if (optind < argc)
  51                 domainname = argv[optind];
  52 
  53         adutils_set_logger(mylogger);
  54         adutils_set_debug(AD_DEBUG_ALL, debug);
  55 
  56         ad_ctx = ad_disc_init();
  57         ad_disc_set_StatusFP(ad_ctx, stdout);
  58 
  59         if (domainname)
  60                 (void) ad_disc_set_DomainName(ad_ctx, domainname);
  61 
  62         ad_disc_refresh(ad_ctx);
  63 
  64         dc = ad_disc_get_DomainController(ad_ctx,
  65             AD_DISC_PREFER_SITE, &autodisc);
  66         if (dc == NULL) {
  67                 (void) printf("getdc failed\n");
  68                 return (1);
  69         }
  70         (void) printf("Found a DC:\n");
  71         print_ds(dc);
  72         free(dc);
  73 
  74         s = ad_disc_get_ForestName(ad_ctx, NULL);
  75         (void) printf("Forest: %s\n", s);
  76         free(s);
  77 
  78         s = ad_disc_get_SiteName(ad_ctx, NULL);
  79         (void) printf("Site: %s\n", s);
  80         free(s);
  81 
  82         gc = ad_disc_get_GlobalCatalog(ad_ctx,
  83             AD_DISC_PREFER_SITE, &autodisc);
  84         if (gc != NULL) {
  85                 (void) printf("Found a GC:\n");
  86                 print_ds(gc);
  87                 free(gc);
  88         }
  89 
  90         ad_disc_done(ad_ctx);
  91         ad_disc_fini(ad_ctx);
  92         ad_ctx = NULL;
  93 
  94         return (0);
  95 }
  96 
  97 void
  98 print_ds(ad_disc_ds_t *ds)
  99 {
 100         char buf[64];
 101 
 102         for (; ds->host[0] != '\0'; ds++) {
 103                 const char *p;
 104 
 105                 (void) printf("Name: %s\n", ds->host);
 106                 (void) printf(" flags: 0x%X\n", ds->flags);
 107                 if (ds->addr.ss_family == AF_INET) {
 108                         struct sockaddr_in *sin;
 109                         sin = (struct sockaddr_in *)&ds->addr;
 110                         p = inet_ntop(AF_INET, &sin->sin_addr,
 111                             buf, sizeof (buf));
 112                         if (p == NULL)
 113                                 p = "?";
 114                         (void) printf(" A %s %d\n", p, ds->port);
 115                 }
 116                 if (ds->addr.ss_family == AF_INET6) {
 117                         struct sockaddr_in6 *sin6;
 118                         sin6 = (struct sockaddr_in6 *)&ds->addr;
 119                         p = inet_ntop(AF_INET6, &sin6->sin6_addr,
 120                             buf, sizeof (buf));
 121                         if (p == NULL)
 122                                 p = "?";
 123                         (void) printf(" AAAA %s %d\n", p, ds->port);
 124                 }
 125         }
 126 }
 127 
 128 /* printflike */
 129 void
 130 mylogger(int pri, const char *format, ...)
 131 {
 132         _NOTE(ARGUNUSED(pri))
 133         va_list args;
 134 
 135         va_start(args, format);
 136         (void) vfprintf(stderr, format, args);
 137         (void) fprintf(stderr, "\n");
 138         va_end(args);
 139 }
 140 
 141 /*
 142  * This is a unit-test program.  Always enable libumem debugging.
 143  */
 144 const char *
 145 _umem_debug_init(void)
 146 {
 147         return ("default,verbose"); /* $UMEM_DEBUG setting */
 148 }
 149 
 150 const char *
 151 _umem_logging_init(void)
 152 {
 153         return ("fail,contents"); /* $UMEM_LOGGING setting */
 154 }