19 char textlowrange
[4], texthighrange
[4];
21 u8b ip0
, ip1
, ip2
, ip3
;
22 u8b lowrange
, highrange
;
23 u8b textmac
[17], textip
[16];
26 /* Be nice variables and behave yourselves */
28 for( j
= 0; j
< 16; j
++ )
42 /* Now we can read our configuration file */
44 config
= fopen( "dhcp.conf", "r" );
47 perror("Reading config files");
51 /* We _DO_ need a better parser */
52 list
= (DHCPLIST
*)malloc( sizeof( DHCPLIST
));
56 while( (!feof( config
)) && ( line
))
58 fscanf( config
, "%s", line
);
59 if( !strcmp( line
, "subnet" ))
60 /* Read subnet parameters */
61 fscanf( config
, "%s", textsubnet
);
63 if( !strcmp( line
, "lease" ))
64 /* read lease parameters */
65 fscanf( config
, "%s", textlease
);
67 if( !strcmp( line
, "router" ))
68 fscanf( config
, "%s", textrouter
);
70 if( !strcmp( line
, "mask" ))
71 fscanf( config
, "%s", textmask
);
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" ))
79 /* Host Specific Configuration */
80 fscanf( config
, "%s %s", textmac
, textip
);
81 str2mac( textmac
, temp
->chaddr
);
83 temp
->data
.ip
= inet_addr( textip
);
84 temp
->next
= (DHCPLIST
*)malloc( sizeof( DHCPLIST
));
85 temp
->next
->back
= temp
;
92 lowrange
= (u8b
)atoi( textlowrange
);
93 highrange
= (u8b
)atoi( texthighrange
);
94 lease
= (u32b
)atoi( textlease
);
96 /* Creating Static IP */
98 for( temp
= list
; temp
->next
; temp
= temp
->next
)
100 temp
->available
= FREE
;
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
);
108 /* Creating Dynamic IP */
110 for( i
= lowrange
; i
< (highrange
+ 1); i
++ )
112 temp
->available
= FREE
;
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 );
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
;
137 int find_lease( DHCPLEASE
*dhcpl
, u32b xid
, u8b chaddr
[] )
145 for( temp
= list
; temp
; temp
=temp
->next
)
146 if( !maccmp( temp
->chaddr
, chaddr
) )
147 release_lease( dhcpl
, xid
, chaddr
);
149 for( temp
= list
; temp
; temp
=temp
->next
)
150 if( ( !maccmp( temp
->chaddr
, chaddr
)) && ( temp
->type
== STATIC
))
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
;
160 temp
->ltime
= MAX_PROCESS_TIME
;
161 maccpy( temp
->chaddr
, chaddr
);
165 else if( ( temp
->available
& FREE
) && ( temp
->type
== DYNAMIC
))
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
;
175 temp
->ltime
= MAX_PROCESS_TIME
;
176 maccpy( temp
->chaddr
, chaddr
);
183 int confirm_lease( DHCPLEASE
*dhcpl
, u32b xid
)
188 for( temp
= list
; temp
; temp
=temp
->next
)
189 if( temp
->xid
== xid
)
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
;
204 int release_lease( DHCPLEASE
*dhcpl
, u32b xid
, u8b chaddr
[16] )
210 for( i
= 0; i
< 16; i
++ )
216 for( temp
= list
; temp
; temp
=temp
->next
)
217 if( !maccmp( temp
->chaddr
, chaddr
) )
219 /* We found the address */
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
;
224 /* maccpy( temp->chaddr, nchaddr ); */
226 /* No such address */