blob: d9eac104abd59411de8b91a6218573294ff3cfc9 [file] [log] [blame]
Mike Frysingerc52bd4c2020-03-18 05:40:25 -04001// Copyright 2020 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#include "debugd/src/ipaddrs_tool.h"
6
7#include <base/strings/string_number_conversions.h>
8#include <base/strings/string_util.h>
9#include <base/strings/stringprintf.h>
Simon Glass2b1da092020-05-21 12:24:16 -060010#include <brillo/process/process.h>
Mike Frysingerc52bd4c2020-03-18 05:40:25 -040011
12#include "debugd/src/process_with_output.h"
13
14namespace debugd {
15
16namespace {
17
18const char kIpTool[] = "/bin/ip";
19
20bool RunOneIPCommand(std::vector<std::string>* result,
21 const std::vector<std::string>& argv) {
22 ProcessWithOutput p;
23 if (!p.Init()) {
24 result->push_back("[ ProcessWithOutput::Init() failed ]");
25 return false;
26 }
27
28 p.AddArg(kIpTool);
29 for (const auto& arg : argv) {
30 p.AddArg(arg);
31 }
32
33 if (p.Run()) {
34 result->push_back("[ ProcessWithOutput::Run() failed ]");
35 return false;
36 }
37
38 p.GetOutputLines(result);
39
40 // GetOutputLines() overwrites |result|, so the heading needs to be
41 // inserted afterward.
42 result->insert(
43 result->begin(),
44 base::StringPrintf("[ ip %s ]", base::JoinString(argv, " ").c_str()));
45 return true;
46}
47
48} // namespace
49
50std::vector<std::string> IpAddrsTool::GetIpAddresses(
51 const brillo::VariantDictionary& options) {
52 std::vector<std::string> full_result, cmd_result;
53
54 std::string ip_version = "-4";
55 if (brillo::GetVariantValueOrDefault<bool>(options, "v6"))
56 ip_version = "-6";
57
58 RunOneIPCommand(&full_result, {ip_version, "addr", "show"});
59
60 return full_result;
61}
62
63} // namespace debugd