1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:17:35 +00:00

Userland: ifconfig can change the IP address of the default gateway

ioctl can now perform a request for a specific route and change
the address of it's default gateway.
This commit is contained in:
marprok 2020-03-14 21:00:49 +02:00 committed by Andreas Kling
parent 45d7ea1b63
commit 0fd5f0e4bd
5 changed files with 129 additions and 42 deletions

View file

@ -31,6 +31,7 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
@ -164,9 +165,27 @@ int main(int argc, char** argv)
}
if (value_gateway) {
// ioctl does not support rtentry yet
fprintf(stderr, "Changing the gateway is not supported yet\n");
return 1;
auto address = IPv4Address::from_string(value_gateway);
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0) {
perror("socket");
return 1;
}
struct rtentry rt;
memset(&rt, 0, sizeof(rt));
rt.rt_dev = const_cast<char*>(ifname.characters());
rt.rt_gateway.sa_family = AF_INET;
((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = address.value().to_in_addr_t();
rt.rt_flags = RTF_UP | RTF_GATEWAY;
int rc = ioctl(fd, SIOCADDRT, &rt);
if (rc < 0) {
perror("ioctl(SIOCADDRT)");
return 1;
}
}
}
return 0;