Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
dhcpclient: Perform ioctls to set addr / add route
Browse files Browse the repository at this point in the history
  • Loading branch information
byteduck committed Mar 20, 2024
1 parent bca13ab commit 5fb74f8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion services/dhcpclient/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <unistd.h>
#include <ifaddrs.h>
#include <libduck/Log.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <net/route.h>

using namespace Duck;

Expand Down Expand Up @@ -180,5 +183,36 @@ Duck::Result Client::setup_interface(const Client::Interface& interface, const I
if (sockid < 0)
return errno;

// TODO
ifreq req;
strncpy(req.ifr_name, interface.name.c_str(), IFNAMSIZ);

// Set IP
*((sockaddr_in*) &req.ifr_addr) = addr.as_sockaddr(0);
if (ioctl(sockid, SIOCSIFADDR, &req) < 0) {
close(sockid);
return errno;
}

// Set subnet mask
*((sockaddr_in*) &req.ifr_addr) = subnet.as_sockaddr(0);
if (ioctl(sockid, SIOCSIFNETMASK, &req) < 0) {
close(sockid);
return errno;
}

// Add route for gateway
if (gateway.has_value()) {
rtentry entry;
entry.rt_dev = (char*) interface.name.c_str();
*((sockaddr_in*) &entry.rt_gateway) = gateway.value().as_sockaddr(0);
entry.rt_flags = RTF_UP | RTF_GATEWAY;

if (ioctl(sockid, SIOCADDRT, &entry) < 0) {
close(sockid);
return errno;
}
}

close(sockid);
return Result::SUCCESS;
}

0 comments on commit 5fb74f8

Please sign in to comment.