mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
ifconfig: Allow setting an adapter's IPv4 address
You can now do things like "ifconfig e1k0 192.168.1.2" to set the IPv4 address of e1k0. Pretty neat!
This commit is contained in:
parent
2482fc3538
commit
797e132d0c
1 changed files with 33 additions and 3 deletions
|
@ -1,9 +1,12 @@
|
||||||
#include <AK/String.h>
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
|
#include <net/if.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
String si_bytes(unsigned bytes)
|
String si_bytes(unsigned bytes)
|
||||||
{
|
{
|
||||||
|
@ -18,8 +21,35 @@ String si_bytes(unsigned bytes)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
UNUSED_PARAM(argc);
|
if (argc == 3) {
|
||||||
UNUSED_PARAM(argv);
|
String ifname = argv[1];
|
||||||
|
auto address = IPv4Address::from_string(argv[2]);
|
||||||
|
|
||||||
|
if (!address.has_value()) {
|
||||||
|
fprintf(stderr, "Invalid IPv4 address: '%s'\n", argv[2]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
|
if (fd < 0) {
|
||||||
|
perror("socket");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ifreq ifr;
|
||||||
|
memset(&ifr, 0, sizeof(ifr));
|
||||||
|
|
||||||
|
strncpy(ifr.ifr_name, ifname.characters(), IFNAMSIZ);
|
||||||
|
ifr.ifr_addr.sa_family = AF_INET;
|
||||||
|
((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t();
|
||||||
|
|
||||||
|
int rc = ioctl(fd, SIOCSIFADDR, &ifr);
|
||||||
|
if (rc < 0) {
|
||||||
|
perror("ioctl(SIOCSIFADDR)");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
auto file = CFile::construct("/proc/net/adapters");
|
auto file = CFile::construct("/proc/net/adapters");
|
||||||
if (!file->open(CIODevice::ReadOnly)) {
|
if (!file->open(CIODevice::ReadOnly)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue