@@ -21,6 +21,10 @@ pub struct Ipv4Addr(pub [u8; ADDR_SIZE]);
21
21
22
22
impl Ipv4Addr {
23
23
const EMPTY : Self = Self ( [ 0 ; ADDR_SIZE ] ) ;
24
+
25
+ fn as_u32 ( & self ) -> u32 {
26
+ byteorder:: BigEndian :: read_u32 ( & self . 0 )
27
+ }
24
28
}
25
29
26
30
impl Display for Ipv4Addr {
@@ -373,6 +377,43 @@ fn get_macaddress<'a>() -> &'a [u8; 6] {
373
377
. expect ( "[ DHCPD ] (EE) failed to retrieve the mac address" )
374
378
}
375
379
380
+ fn configure ( interface : & str , ip : Ipv4Addr ) -> io:: Result < ( ) > {
381
+ let fd = cvt ( unsafe { libc:: socket ( libc:: AF_INET , libc:: SOCK_DGRAM , 0 ) } ) ?;
382
+ let socket = libc:: sockaddr_in {
383
+ sin_family : libc:: AF_INET as _ ,
384
+ sin_addr : libc:: in_addr {
385
+ s_addr : ip. as_u32 ( ) ,
386
+ } ,
387
+
388
+ sin_port : 0 ,
389
+ sin_zero : [ 0 ; 8 ] ,
390
+ } ;
391
+
392
+ let mut ifr: libc:: ifreq = unsafe { core:: mem:: zeroed ( ) } ;
393
+ assert ! ( interface. len( ) <= libc:: IFNAMSIZ ) ;
394
+
395
+ // SAFETY: We have verified above that the name will fit
396
+ // in the buffer.
397
+ unsafe {
398
+ core:: ptr:: copy_nonoverlapping (
399
+ interface. as_ptr ( ) ,
400
+ ifr. ifr_name . as_mut_ptr ( ) as * mut u8 ,
401
+ interface. len ( ) ,
402
+ ) ;
403
+ }
404
+
405
+ ifr. ifr_ifru . ifru_addr = unsafe { * ( & socket as * const _ as * const libc:: sockaddr ) } ;
406
+
407
+ // Set the interface address.
408
+ unsafe {
409
+ cvt ( libc:: ioctl ( fd, libc:: SIOCSIFADDR , & ifr) ) ?;
410
+ }
411
+
412
+ Ok ( ( ) )
413
+ }
414
+
415
+ const DEFAULT_NIC : & str = "eth0" ; // FIXME: retrieve the default NIC from the kernel
416
+
376
417
pub fn main ( ) -> Result < ( ) , Box < dyn Error > > {
377
418
let socket = UdpSocket :: bind ( ( "0.0.0.0" , 68 ) ) ?;
378
419
socket. connect ( ( "255.255.255.255" , 67 ) ) ?;
@@ -425,6 +466,8 @@ pub fn main() -> Result<(), Box<dyn Error>> {
425
466
let subnet_mask = subnet_mask. unwrap ( ) ;
426
467
let dns = dns. unwrap ( ) ;
427
468
469
+ configure ( DEFAULT_NIC , ack. your_ip ) ?;
470
+
428
471
println ! ( "[ DHCPD ] (!!) Configured:" ) ;
429
472
println ! ( "[ DHCPD ] (!!) IP: {}" , ack. your_ip) ;
430
473
println ! ( "[ DHCPD ] (!!) Default Gateway: {}" , default_gateway) ;
0 commit comments