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