blob: 98cb0894c093ddf8afa9415089379575694db170 [file] [log] [blame]
Hugo Benichif818c782021-04-10 00:09:50 +09001// Copyright 2021 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PATCHPANEL_SYSTEM_H_
6#define PATCHPANEL_SYSTEM_H_
7
8#include <net/if.h>
9#include <net/route.h>
10#include <sys/ioctl.h>
Hugo Benichi96ee6f62021-06-28 14:07:20 +090011#include <sys/types.h>
12
13#include <string>
Hugo Benichif818c782021-04-10 00:09:50 +090014
15namespace patchpanel {
16
17// cros lint will yell to force using int16/int64 instead of long here, however
18// note that unsigned long IS the correct signature for ioctl in Linux kernel -
19// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
20using ioctl_req_t = unsigned long; // NOLINT(runtime/int)
21
22// Stateless class used for holding all utility functions with side
23// effects on the environment. Facilitates mocking these functions in unit
24// tests.
25class System {
26 public:
Hugo Benichi153c7112021-02-22 17:46:33 +090027 // Enum used for restricting the possible paths that SysNetSet can write to.
28 enum SysNet {
29 // Used for modifying "net.ipv4.ip_forward"
30 IPv4Forward = 1,
31 // Used for modifying "net.ipv4.ip_local_port_range"
32 IPLocalPortRange,
33 // Used for modifying "net.ipv4.conf.%s.route_localnet", requires an
34 // interface
35 // argument
36 IPv4RouteLocalnet,
37 // Used for modifying "net.ipv6.conf.%s.accept_ra", requires an interface
38 // argument
39 IPv6AcceptRA,
40 // Used for modifying "net.ipv6.conf.all.forwarding"
41 IPv6Forward,
42 // Used for enabling netfilter connection tracking helper modules.
43 ConntrackHelper,
44 };
45
Hugo Benichif818c782021-04-10 00:09:50 +090046 System() = default;
47 System(const System&) = delete;
48 System& operator=(const System&) = delete;
49 virtual ~System() = default;
50
Hugo Benichi153c7112021-02-22 17:46:33 +090051 // Write |content| to a "/proc/sys/net/" path as specified by |target|
52 virtual bool SysNetSet(SysNet target,
53 const std::string& content,
54 const std::string& iface = "");
55
Hugo Benichif818c782021-04-10 00:09:50 +090056 virtual int Ioctl(int fd, ioctl_req_t request, const char* argp);
57 int Ioctl(int fd, ioctl_req_t request, uint64_t arg);
58 int Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr);
59 int Ioctl(int fd, ioctl_req_t request, struct rtentry* route);
60
Hugo Benichi96ee6f62021-06-28 14:07:20 +090061 virtual pid_t WaitPid(pid_t pid, int* wstatus, int options = 0);
62
Hugo Benichi153c7112021-02-22 17:46:33 +090063 static bool Write(const std::string& path, const std::string& content);
64
Hugo Benichif818c782021-04-10 00:09:50 +090065 private:
66};
67
68} // namespace patchpanel
69
70#endif // PATCHPANEL_SYSTEM_H_