MCPcopy
hub / github.com/netbirdio/netbird / parseIfconfigOutput

Function parseIfconfigOutput

client/iface/freebsd/iface.go:28–93  ·  view source on GitHub ↗
(output []byte)

Source from the content-addressed store, hash-verified

26}
27
28func parseIfconfigOutput(output []byte) (*iface, error) {
29 // TODO: implement without allocations
30 lines := string(output)
31
32 scanner := bufio.NewScanner(strings.NewReader(lines))
33
34 var name, mtu, group string
35 var ips []string
36
37 for scanner.Scan() {
38 line := scanner.Text()
39
40 // If line contains ": flags", it's a line with interface information
41 if strings.Contains(line, ": flags") {
42 parts := strings.Fields(line)
43 if len(parts) < 4 {
44 return nil, fmt.Errorf("failed to parse line: %s", line)
45 }
46 name = strings.TrimSuffix(parts[0], ":")
47 if strings.Contains(line, "mtu") {
48 mtuIndex := 0
49 for i, part := range parts {
50 if part == "mtu" {
51 mtuIndex = i
52 break
53 }
54 }
55 mtu = parts[mtuIndex+1]
56 }
57 }
58
59 // If line contains "groups:", it's a line with interface group
60 if strings.Contains(line, "groups:") {
61 parts := strings.Fields(line)
62 if len(parts) < 2 {
63 return nil, fmt.Errorf("failed to parse line: %s", line)
64 }
65 group = parts[1]
66 }
67
68 // If line contains "inet ", it's a line with IP address
69 if strings.Contains(line, "inet ") {
70 parts := strings.Fields(line)
71 if len(parts) < 2 {
72 return nil, fmt.Errorf("failed to parse line: %s", line)
73 }
74 ips = append(ips, parts[1])
75 }
76 }
77
78 if name == "" {
79 return nil, fmt.Errorf("interface name not found in ifconfig output")
80 }
81
82 mtuInt, err := strconv.Atoi(mtu)
83 if err != nil {
84 return nil, fmt.Errorf("failed to parse MTU: %w", err)
85 }

Callers 2

LinkByNameFunction · 0.85
TestParseIfconfigOutputFunction · 0.85

Calls 1

ErrorfMethod · 0.80

Tested by 1

TestParseIfconfigOutputFunction · 0.68