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