1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright (c) 2000 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  * Print SUNWbinfiles DHCP network containers.
  31  */
  32 
  33 #include <stdlib.h>
  34 #include <unistd.h>
  35 #include <sys/types.h>
  36 #include <netinet/in.h>
  37 #include <arpa/inet.h>
  38 #include <stdio.h>
  39 #include <fcntl.h>
  40 #include <errno.h>
  41 #include <string.h>
  42 #include <stddef.h>
  43 #include <sys/socket.h>
  44 #include "../dhcp_network.h"
  45 
  46 static void print_hashes(int, dn_header_t);
  47 
  48 int
  49 main(int argc, char **argv)
  50 {
  51         int confd;
  52         dn_header_t header;
  53         char netmask[INET_ADDRSTRLEN], network[INET_ADDRSTRLEN];
  54         struct in_addr in_addr;
  55         unsigned int i;
  56 
  57         if (argc < 2) {
  58                 (void) fprintf(stderr, "usage: %s container [container ...]\n",
  59                     argv[0]);
  60                 return (EXIT_FAILURE);
  61         }
  62 
  63         for (i = 1; argv[i] != NULL; i++) {
  64                 confd = open(argv[i], O_RDONLY);
  65                 if (confd == -1) {
  66                         (void) fprintf(stderr, "%s: cannot open container "
  67                             "`%s': %s\n", argv[0], argv[i], strerror(errno));
  68                         continue;
  69                 }
  70 
  71                 if (read(confd, &header, sizeof (header)) != sizeof (header) ||
  72                     header.dnh_magic != DN_MAGIC) {
  73                         (void) fprintf(stderr, "%s: container `%s' is not a "
  74                             "binfiles network container\n", argv[0], argv[i]);
  75                         continue;
  76                 }
  77 
  78                 (void) printf("binfiles network container `%s':\n", argv[i]);
  79 
  80                 in_addr.s_addr = header.dnh_network;
  81                 (void) inet_ntop(AF_INET, &in_addr, network, INET_ADDRSTRLEN);
  82                 in_addr.s_addr = header.dnh_netmask;
  83                 (void) inet_ntop(AF_INET, &in_addr, netmask, INET_ADDRSTRLEN);
  84 
  85                 (void) printf("%12s: %s\n", "network", network);
  86                 (void) printf("%12s: %s\n", "netmask", netmask);
  87                 (void) printf("%12s: %d\n", "dirtybit", header.dnh_dirty);
  88                 (void) printf("%12s: %d\n", "version", header.dnh_version);
  89                 (void) printf("%12s: %d\n", "active image", header.dnh_image);
  90                 (void) printf("%12s: %d\n", "temp image", header.dnh_tempimage);
  91                 (void) printf("%12s: %d\n", "checks", header.dnh_checks);
  92                 (void) printf("%12s: %d\n", "errors", header.dnh_errors);
  93                 print_hashes(confd, header);
  94                 (void) close(confd);
  95         }
  96 
  97         return (EXIT_SUCCESS);
  98 }
  99 
 100 static void
 101 print_hashes(int confd, dn_header_t header)
 102 {
 103         dn_filerec_t rec;
 104         dn_recid_t recid;
 105         unsigned int image, hash;
 106 
 107         for (hash = 0; hash < DN_CIDHASHSZ; hash++) {
 108                 for (image = 0; image < 2; image++) {
 109                         if (header.dnh_cidhash[hash][image] == DN_NOREC)
 110                                 continue;
 111 
 112                         (void) printf(" hash %4d/%d: ", hash, image);
 113                         recid = header.dnh_cidhash[hash][image];
 114                         for (; recid != DN_NOREC; recid = rec.rec_next[image]) {
 115                                 if (pread(confd, &rec, sizeof (dn_rec_t),
 116                                     RECID2OFFSET(recid)) != sizeof (dn_rec_t)) {
 117                                         (void) fprintf(stderr, "cannot read "
 118                                             "recid %d: %s", recid,
 119                                             strerror(errno));
 120                                         break;
 121                                 }
 122                                 (void) printf("%d<-[%d]->%d ",
 123                                     rec.rec_prev[image], recid,
 124                                     rec.rec_next[image]);
 125                         }
 126                         (void) printf("\n");
 127                 }
 128         }
 129 }