mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
ifconfig: Port to Core::Stream and Core::System
This commit is contained in:
parent
78a94cd3b7
commit
1d67a1e73f
1 changed files with 9 additions and 30 deletions
|
@ -10,18 +10,12 @@
|
|||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/NumberFormat.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <net/if.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
|
@ -37,8 +31,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
args_parser.parse(arguments);
|
||||
|
||||
if (value_ipv4.is_empty() && value_adapter.is_empty() && value_mask.is_empty()) {
|
||||
auto file = TRY(Core::File::open("/sys/kernel/net/adapters", Core::OpenMode::ReadOnly));
|
||||
auto json = TRY(JsonValue::from_string(file->read_all()));
|
||||
auto file = TRY(Core::Stream::File::open("/sys/kernel/net/adapters"sv, Core::Stream::OpenMode::Read));
|
||||
auto file_contents = TRY(file->read_all());
|
||||
auto json = TRY(JsonValue::from_string(file_contents));
|
||||
|
||||
json.as_array().for_each([](auto& value) {
|
||||
auto& if_object = value.as_object();
|
||||
|
@ -81,11 +76,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
auto fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
|
@ -98,11 +89,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
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;
|
||||
}
|
||||
TRY(Core::System::ioctl(fd, SIOCSIFADDR, &ifr));
|
||||
}
|
||||
|
||||
if (!value_mask.is_empty()) {
|
||||
|
@ -113,11 +100,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
auto fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
|
@ -130,11 +113,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
ifr.ifr_netmask.sa_family = AF_INET;
|
||||
((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t();
|
||||
|
||||
int rc = ioctl(fd, SIOCSIFNETMASK, &ifr);
|
||||
if (rc < 0) {
|
||||
perror("ioctl(SIOCSIFNETMASK)");
|
||||
return 1;
|
||||
}
|
||||
TRY(Core::System::ioctl(fd, SIOCSIFNETMASK, &ifr));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue