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 #pragma ident   "%Z%%M% %I%     %E% SMI"
  23 
  24 /*
  25  * Copyright (c) 1996-2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 
  29 #include <stdio.h>
  30 #include <sys/types.h>
  31 #include <unistd.h>
  32 #include <syslog.h>
  33 #include <netinet/in.h>
  34 #include <netinet/dhcp.h>
  35 #include <dhcp_gen.h>
  36 #include <dhcpd.h>
  37 #include <per_network.h>
  38 #include <dhcp_msgs.h>
  39 
  40 int debug = 1;
  41 int verbose = 1;
  42 
  43 /*
  44  * smalloc()  --  safe malloc()
  45  *
  46  * Always returns a valid pointer (if it returns at all).  The allocated
  47  * memory is initialized to all zeros.  If malloc() returns an error, a
  48  * message is printed using the syslog() function and the program aborts
  49  * with a status of 1.
  50  */
  51 char *
  52 smalloc(uint_t nbytes)
  53 {
  54         char *retvalue;
  55 
  56         if ((retvalue = (char *)malloc(nbytes)) == (char *)NULL) {
  57                 dhcp_error(LOG_ERR, DHCP_MSGS(DCMSG_NO_MEMORY));
  58                 (void) exit(1);
  59         }
  60         memset(retvalue, 0, nbytes);
  61         return (retvalue);
  62 }
  63 
  64 int
  65 main(void)
  66 {
  67         struct in_addr five_net = {109, 108, 5, 0};
  68         struct in_addr five_mask = {109, 108, 5, 255};
  69         struct in_addr serverid = {109, 108, 5, 138};
  70         struct in_addr scratch;
  71         PER_NET_DB      pndb;
  72         PN_REC          pn;
  73         uchar_t buf[MAX_CID_LEN] = {0x1, 0x0, 0x0, 0xc0, 0xee, 0xe, 0x4c };
  74         char tbuf[MAX_CID_LEN];
  75         int recs;
  76         unsigned int len;
  77         register int i, err = 0;
  78 
  79 
  80         /*
  81          * Test 0. Open the per network database, and locate a *single*
  82          * record by cid.
  83          */
  84         printf("Test 0: START ******************************************\n");
  85         memset(&pndb, 0, sizeof (pndb));
  86 
  87         if (open_per_net(&pndb, &five_net, &five_mask) != 0) {
  88                 printf("didn't work.\n");
  89                 return (1);
  90         }
  91 
  92         /*
  93          * Should only be one.
  94          */
  95         memset(&pn, 0, sizeof (pn));
  96         recs = lookup_per_net(&pndb, PN_CID, (void *)buf, 7, &serverid, &pn);
  97         if (recs < 0)
  98                 printf("lookup didn't work.\n");
  99         else {
 100                 if (recs > 0) {
 101                         len = MAX_CID_LEN;
 102                         octet_to_hexascii(buf, 7, tbuf, &len);
 103                         printf("Client id: %s\n", tbuf);
 104                         printf("flags: 0x%x\n", pn.flags);
 105                         printf("IP address is: %s\n", inet_ntoa(pn.clientip));
 106                         printf("server IP address is: %s\n",
 107                             inet_ntoa(pn.serverip));
 108 
 109                         len = MAX_CID_LEN;
 110                         octet_to_hexascii(&pn.lease, 4, tbuf, &len);
 111                         printf("lease is %s, 0x%x\n", tbuf, pn.lease);
 112                         printf("macro is %s\n", pn.macro);
 113                         printf("Number of records: %d\n", recs);
 114                 }
 115         }
 116         close_per_net(&pndb);
 117 
 118         printf("Test 0: END ******************************************\n");
 119         /* END TEST 0 ********************************************* */
 120 
 121         /*
 122          * Test 1. Open the per net database, locate all records with
 123          * cid of 0.
 124          */
 125         printf("Test 1: START ******************************************\n");
 126         if (open_per_net(&pndb, &five_net, &five_mask) != 0) {
 127                 printf("didn't work.\n");
 128                 return (1);
 129         } else {
 130                 printf("name: %s\n", pndb.name);
 131         }
 132 
 133         memset(buf, 0, MAX_CID_LEN);
 134         recs = lookup_per_net(&pndb, PN_CID, (void *)buf, 1, &serverid, &pn);
 135         if (recs < 0)
 136                 printf("lookup didn't work.\n");
 137         else {
 138                 printf("datatype: %d\n", pndb.datatype);
 139                 printf("row: %d\n", pndb.row);
 140                 if (recs > 0) {
 141                         len = MAX_CID_LEN;
 142                         octet_to_hexascii(buf, 7, tbuf, &len);
 143                         printf("Client id: %s\n", tbuf);
 144                         printf("flags: 0x%x\n", pn.flags);
 145                         printf("IP address is: %s\n", inet_ntoa(pn.clientip));
 146                         printf("server IP address is: %s\n",
 147                             inet_ntoa(pn.serverip));
 148 
 149                         len = MAX_CID_LEN;
 150                         octet_to_hexascii(&pn.lease, 4, tbuf, &len);
 151                         printf("lease is %s, 0x%x\n", tbuf, pn.lease);
 152                         printf("macro is %s\n", pn.macro);
 153                         printf("Number of records: %d\n", recs);
 154                         for (i = 0; i < recs; i++) {
 155                                 if (get_per_net(&pndb, PN_CID, &pn) != 0) {
 156                                         printf("didn't work 2: \n");
 157                                         break;
 158                                 }
 159                                 len = MAX_CID_LEN;
 160                                 octet_to_hexascii(buf, 7, tbuf, &len);
 161                                 printf("Client id: %s\n", tbuf);
 162                                 printf("flags: 0x%x\n", pn.flags);
 163                                 printf("IP address is: %s\n",
 164                                     inet_ntoa(pn.clientip));
 165                                 printf("server IP address is: %s\n",
 166                                     inet_ntoa(pn.serverip));
 167 
 168                                 len = MAX_CID_LEN;
 169                                 octet_to_hexascii(&pn.lease, 4, tbuf, &len);
 170                                 printf("lease is %s, 0x%x\n", tbuf, pn.lease);
 171                                 printf("macro is %s\n", pn.macro);
 172                         }
 173                 }
 174         }
 175 
 176         close_per_net(&pndb);
 177         printf("Test 1: END ******************************************\n");
 178         printf("Test 2: START ******************************************\n");
 179         /*
 180          * Locate client ip 109.108.5.221.
 181          */
 182         scratch.s_addr = 0x6d6c05dd;
 183         if (open_per_net(&pndb, &five_net, &five_mask) != 0) {
 184                 printf("didn't work.\n");
 185                 return (1);
 186         } else {
 187                 printf("name: %s\n", pndb.name);
 188         }
 189 
 190         recs = lookup_per_net(&pndb, PN_CLIENT_IP, (void *)&scratch, 4,
 191             &serverid, &pn);
 192         if (recs < 0)
 193                 printf("lookup didn't work.\n");
 194         else {
 195                 printf("datatype: %d\n", pndb.datatype);
 196                 printf("row: %d\n", pndb.row);
 197                 if (recs > 0) {
 198                         len = MAX_CID_LEN;
 199                         octet_to_hexascii(buf, 7, tbuf, &len);
 200                         printf("Client id: %s\n", tbuf);
 201                         printf("flags: 0x%x\n", pn.flags);
 202                         printf("IP address is: %s\n", inet_ntoa(pn.clientip));
 203                         printf("server IP address is: %s\n",
 204                             inet_ntoa(pn.serverip));
 205 
 206                         len = MAX_CID_LEN;
 207                         octet_to_hexascii(&pn.lease, 4, tbuf, &len);
 208                         printf("lease is %s, 0x%x\n", tbuf, pn.lease);
 209                         printf("macro is %s\n", pn.macro);
 210                         printf("Number of records: %d\n", recs);
 211                 }
 212         }
 213 
 214         printf("Test 2: END ******************************************\n");
 215         printf("Test 3: START ******************************************\n");
 216         if (recs > 0) {
 217                 /*
 218                  * Using the record from test 2, change the cid, flags, and
 219                  * lease, then write the record.
 220                  */
 221                 pn.cid_len = 7;
 222                 for (i = 0; (uchar_t)i < pn.cid_len; i++)
 223                         pn.cid[i] = i;
 224                 pn.flags |= F_AUTOMATIC;
 225                 pn.lease = htonl(time(NULL));
 226 
 227                 if ((err = put_per_net(&pndb, &pn, PN_CLIENT_IP)) != 0) {
 228                         printf("didn't work. error: %d\n", err);
 229                 } else
 230                         printf("it worked.\n");
 231         }
 232         close_per_net(&pndb);
 233         printf("Test 3: END ******************************************\n");
 234 
 235         return (0);
 236 }