1*c2c66affSColin Finck #include <headers.h>
2*c2c66affSColin Finck #include <datatypes.h>
3*c2c66affSColin Finck #include <display.h>
4*c2c66affSColin Finck #include <options.h>
5*c2c66affSColin Finck #include <leases.h>
6*c2c66affSColin Finck #include <utils.h>
7*c2c66affSColin Finck 
init_leases_list()8*c2c66affSColin Finck int init_leases_list()
9*c2c66affSColin Finck {
10*c2c66affSColin Finck   DHCPLIST *temp;
11*c2c66affSColin Finck   int i, j;
12*c2c66affSColin Finck   u8b chaddr[16];
13*c2c66affSColin Finck   FILE *config;
14*c2c66affSColin Finck   char line[80];
15*c2c66affSColin Finck   char textsubnet[16];
16*c2c66affSColin Finck   char textlease[5];
17*c2c66affSColin Finck   char textrouter[16];
18*c2c66affSColin Finck   char textmask[16];
19*c2c66affSColin Finck   char textlowrange[4], texthighrange[4];
20*c2c66affSColin Finck   char textserver[16];
21*c2c66affSColin Finck   u8b ip0, ip1, ip2, ip3;
22*c2c66affSColin Finck   u8b lowrange, highrange;
23*c2c66affSColin Finck   u8b textmac[17], textip[16];
24*c2c66affSColin Finck   u32b lease;
25*c2c66affSColin Finck 
26*c2c66affSColin Finck   /* Be nice variables and behave yourselves */
27*c2c66affSColin Finck 
28*c2c66affSColin Finck   for( j = 0; j < 16; j++ )
29*c2c66affSColin Finck     {
30*c2c66affSColin Finck       chaddr[j] = 0;
31*c2c66affSColin Finck       textsubnet[j] = 0;
32*c2c66affSColin Finck       textrouter[j] = 0;
33*c2c66affSColin Finck       textmask[j] = 0;
34*c2c66affSColin Finck       textip[j] = 0;
35*c2c66affSColin Finck       textmac[j] = 0;
36*c2c66affSColin Finck     }
37*c2c66affSColin Finck 
38*c2c66affSColin Finck   textlease[0] = 0;
39*c2c66affSColin Finck   textlowrange[0] = 0;
40*c2c66affSColin Finck   texthighrange[0] = 0;
41*c2c66affSColin Finck 
42*c2c66affSColin Finck   /* Now we can read our configuration file */
43*c2c66affSColin Finck 
44*c2c66affSColin Finck   config = fopen( "dhcp.conf", "r" );
45*c2c66affSColin Finck   if( !config )
46*c2c66affSColin Finck     {
47*c2c66affSColin Finck       perror("Reading config files");
48*c2c66affSColin Finck       exit( 0 );
49*c2c66affSColin Finck     }
50*c2c66affSColin Finck 
51*c2c66affSColin Finck   /* We _DO_ need a better parser */
52*c2c66affSColin Finck   list = (DHCPLIST *)malloc( sizeof( DHCPLIST ));
53*c2c66affSColin Finck   temp = list;
54*c2c66affSColin Finck   temp->back = NULL;
55*c2c66affSColin Finck 
56*c2c66affSColin Finck   while( (!feof( config )) && ( line ))
57*c2c66affSColin Finck     {
58*c2c66affSColin Finck       fscanf( config, "%s", line);
59*c2c66affSColin Finck       if( !strcmp( line, "subnet" ))
60*c2c66affSColin Finck 	/* Read subnet parameters */
61*c2c66affSColin Finck 	fscanf( config, "%s", textsubnet );
62*c2c66affSColin Finck 
63*c2c66affSColin Finck       if( !strcmp( line, "lease" ))
64*c2c66affSColin Finck 	/* read lease parameters */
65*c2c66affSColin Finck 	fscanf( config, "%s", textlease );
66*c2c66affSColin Finck 
67*c2c66affSColin Finck       if( !strcmp( line, "router" ))
68*c2c66affSColin Finck 	fscanf( config, "%s", textrouter );
69*c2c66affSColin Finck 
70*c2c66affSColin Finck       if( !strcmp( line, "mask" ))
71*c2c66affSColin Finck 	fscanf( config, "%s", textmask );
72*c2c66affSColin Finck 
73*c2c66affSColin Finck       if( !strcmp( line, "range" ))
74*c2c66affSColin Finck 	fscanf( config, "%s %s", textlowrange, texthighrange );
75*c2c66affSColin Finck       if( !strcmp( line, "server" ))
76*c2c66affSColin Finck 	fscanf( config, "%s", textserver );
77*c2c66affSColin Finck       if( !strcmp( line, "host" ))
78*c2c66affSColin Finck 	{
79*c2c66affSColin Finck 	  /* Host Specific Configuration */
80*c2c66affSColin Finck 	  fscanf( config, "%s %s", textmac, textip );
81*c2c66affSColin Finck 	  str2mac( textmac, temp->chaddr );
82*c2c66affSColin Finck 	  temp->type = STATIC;
83*c2c66affSColin Finck 	  temp->data.ip = inet_addr( textip );
84*c2c66affSColin Finck 	  temp->next = (DHCPLIST *)malloc( sizeof( DHCPLIST ));
85*c2c66affSColin Finck 	  temp->next->back = temp;
86*c2c66affSColin Finck 	  temp = temp->next;
87*c2c66affSColin Finck 	  temp->next =NULL;
88*c2c66affSColin Finck 	}
89*c2c66affSColin Finck     }
90*c2c66affSColin Finck   fclose( config );
91*c2c66affSColin Finck 
92*c2c66affSColin Finck   lowrange = (u8b)atoi( textlowrange );
93*c2c66affSColin Finck   highrange = (u8b)atoi( texthighrange );
94*c2c66affSColin Finck   lease = (u32b)atoi( textlease );
95*c2c66affSColin Finck 
96*c2c66affSColin Finck   /* Creating Static IP */
97*c2c66affSColin Finck 
98*c2c66affSColin Finck   for( temp = list; temp->next; temp = temp->next )
99*c2c66affSColin Finck     {
100*c2c66affSColin Finck       temp->available = FREE;
101*c2c66affSColin Finck       temp->xid = 0;
102*c2c66affSColin Finck       temp->data.router = inet_addr( textrouter );
103*c2c66affSColin Finck       temp->data.mask = inet_addr( textmask );
104*c2c66affSColin Finck       temp->data.lease = lease;
105*c2c66affSColin Finck       temp->data.siaddr = inet_addr( textserver );
106*c2c66affSColin Finck     }
107*c2c66affSColin Finck 
108*c2c66affSColin Finck   /* Creating Dynamic IP */
109*c2c66affSColin Finck 
110*c2c66affSColin Finck   for( i = lowrange; i < (highrange + 1); i++ )
111*c2c66affSColin Finck     {
112*c2c66affSColin Finck       temp->available = FREE;
113*c2c66affSColin Finck       temp->xid = 0;
114*c2c66affSColin Finck       temp->type = DYNAMIC;
115*c2c66affSColin Finck       maccpy( temp->chaddr, chaddr );
116*c2c66affSColin Finck       split_ip( textsubnet, &ip0, 0 );
117*c2c66affSColin Finck       split_ip( textsubnet, &ip1, 1 );
118*c2c66affSColin Finck       split_ip( textsubnet, &ip2, 2 );
119*c2c66affSColin Finck       temp->data.ip = i;
120*c2c66affSColin Finck       temp->data.ip = temp->data.ip << 8;
121*c2c66affSColin Finck       temp->data.ip += ip2;
122*c2c66affSColin Finck       temp->data.ip = temp->data.ip << 8;
123*c2c66affSColin Finck       temp->data.ip += ip1;
124*c2c66affSColin Finck       temp->data.ip = temp->data.ip << 8;
125*c2c66affSColin Finck       temp->data.ip += ip0;
126*c2c66affSColin Finck       temp->data.router = inet_addr( textrouter );
127*c2c66affSColin Finck       temp->data.mask = inet_addr( textmask );
128*c2c66affSColin Finck       temp->data.lease = lease;
129*c2c66affSColin Finck       temp->data.siaddr = inet_addr( textserver );
130*c2c66affSColin Finck       temp->next = (DHCPLIST *)malloc( sizeof( DHCPLIST ));
131*c2c66affSColin Finck       temp->next->back = temp;
132*c2c66affSColin Finck       temp = temp->next;
133*c2c66affSColin Finck     }
134*c2c66affSColin Finck   return 0;
135*c2c66affSColin Finck }
136*c2c66affSColin Finck 
find_lease(DHCPLEASE * dhcpl,u32b xid,u8b chaddr[])137*c2c66affSColin Finck int find_lease( DHCPLEASE *dhcpl, u32b xid, u8b chaddr[] )
138*c2c66affSColin Finck {
139*c2c66affSColin Finck   int result = -2;
140*c2c66affSColin Finck   DHCPLIST *temp;
141*c2c66affSColin Finck 
142*c2c66affSColin Finck   if( !dhcpl )
143*c2c66affSColin Finck     return -1;
144*c2c66affSColin Finck 
145*c2c66affSColin Finck   for( temp = list; temp; temp=temp->next )
146*c2c66affSColin Finck     if( !maccmp( temp->chaddr, chaddr ) )
147*c2c66affSColin Finck       release_lease( dhcpl, xid, chaddr);
148*c2c66affSColin Finck 
149*c2c66affSColin Finck   for( temp = list; temp; temp=temp->next )
150*c2c66affSColin Finck     if( ( !maccmp( temp->chaddr, chaddr )) && ( temp->type == STATIC ))
151*c2c66affSColin Finck       {
152*c2c66affSColin Finck 	dhcpl->ip = temp->data.ip;
153*c2c66affSColin Finck 	dhcpl->router = temp->data.router;
154*c2c66affSColin Finck 	dhcpl->mask = temp->data.mask;
155*c2c66affSColin Finck 	dhcpl->lease = temp->data.lease;
156*c2c66affSColin Finck 	dhcpl->siaddr = temp->data.siaddr;
157*c2c66affSColin Finck 	fprintf( stdout, "Assigning Static IP! \n");
158*c2c66affSColin Finck 	temp->available = PROCESSING;
159*c2c66affSColin Finck 	temp->xid = xid;
160*c2c66affSColin Finck 	temp->ltime = MAX_PROCESS_TIME;
161*c2c66affSColin Finck 	maccpy( temp->chaddr, chaddr);
162*c2c66affSColin Finck 	result = 0;
163*c2c66affSColin Finck 	return result;
164*c2c66affSColin Finck       }
165*c2c66affSColin Finck     else if( ( temp->available & FREE )  && ( temp->type == DYNAMIC ))
166*c2c66affSColin Finck       {
167*c2c66affSColin Finck 	dhcpl->ip = temp->data.ip;
168*c2c66affSColin Finck 	dhcpl->router = temp->data.router;
169*c2c66affSColin Finck 	dhcpl->mask = temp->data.mask;
170*c2c66affSColin Finck 	dhcpl->lease = temp->data.lease;
171*c2c66affSColin Finck 	dhcpl->siaddr = temp->data.siaddr;
172*c2c66affSColin Finck 	fprintf( stdout, "Assigning Dynamic IP! \n");
173*c2c66affSColin Finck 	temp->available = PROCESSING;
174*c2c66affSColin Finck 	temp->xid = xid;
175*c2c66affSColin Finck 	temp->ltime = MAX_PROCESS_TIME;
176*c2c66affSColin Finck 	maccpy( temp->chaddr, chaddr);
177*c2c66affSColin Finck 	result = 0;
178*c2c66affSColin Finck 	return result;
179*c2c66affSColin Finck       }
180*c2c66affSColin Finck   return result;
181*c2c66affSColin Finck }
182*c2c66affSColin Finck 
confirm_lease(DHCPLEASE * dhcpl,u32b xid)183*c2c66affSColin Finck int confirm_lease( DHCPLEASE *dhcpl, u32b xid )
184*c2c66affSColin Finck {
185*c2c66affSColin Finck   int result = -1;
186*c2c66affSColin Finck   DHCPLIST *temp;
187*c2c66affSColin Finck 
188*c2c66affSColin Finck   for( temp = list; temp; temp=temp->next )
189*c2c66affSColin Finck     if( temp->xid == xid )
190*c2c66affSColin Finck       {
191*c2c66affSColin Finck 	dhcpl->ip = temp->data.ip;
192*c2c66affSColin Finck 	dhcpl->router = temp->data.router;
193*c2c66affSColin Finck 	dhcpl->mask = temp->data.mask;
194*c2c66affSColin Finck 	dhcpl->lease = temp->data.lease;
195*c2c66affSColin Finck 	dhcpl->siaddr = temp->data.siaddr;
196*c2c66affSColin Finck 	temp->available = BUSY;
197*c2c66affSColin Finck 	temp->ltime = temp->data.lease;
198*c2c66affSColin Finck 	result = 0;
199*c2c66affSColin Finck 	return result;
200*c2c66affSColin Finck       }
201*c2c66affSColin Finck   return result;
202*c2c66affSColin Finck }
203*c2c66affSColin Finck 
release_lease(DHCPLEASE * dhcpl,u32b xid,u8b chaddr[16])204*c2c66affSColin Finck int release_lease( DHCPLEASE *dhcpl, u32b xid, u8b chaddr[16] )
205*c2c66affSColin Finck {
206*c2c66affSColin Finck   int result = -1, i;
207*c2c66affSColin Finck   DHCPLIST *temp;
208*c2c66affSColin Finck   u8b nchaddr[16];
209*c2c66affSColin Finck 
210*c2c66affSColin Finck   for( i = 0; i < 16; i++ )
211*c2c66affSColin Finck     nchaddr[i] = 0;
212*c2c66affSColin Finck 
213*c2c66affSColin Finck   if( !dhcpl )
214*c2c66affSColin Finck     return -1;
215*c2c66affSColin Finck 
216*c2c66affSColin Finck   for( temp = list; temp; temp=temp->next )
217*c2c66affSColin Finck     if( !maccmp( temp->chaddr, chaddr ) )
218*c2c66affSColin Finck       {
219*c2c66affSColin Finck 	/* We found the address */
220*c2c66affSColin Finck 	result = 0;
221*c2c66affSColin Finck 	fprintf( stdout, "Deleting %X::%X::%X::%X::%X::%X \n", temp->chaddr[0], temp->chaddr[1], temp->chaddr[2], temp->chaddr[3], temp->chaddr[4], temp->chaddr[5] );
222*c2c66affSColin Finck 	temp->available = FREE;
223*c2c66affSColin Finck 	temp->xid = 0;
224*c2c66affSColin Finck 	/*	maccpy( temp->chaddr, nchaddr ); */
225*c2c66affSColin Finck       } else {
226*c2c66affSColin Finck 	/* No such address */
227*c2c66affSColin Finck 	result = -1;
228*c2c66affSColin Finck       }
229*c2c66affSColin Finck 
230*c2c66affSColin Finck   return result;
231*c2c66affSColin Finck }
232