File: gateway_linux.go

package info (click to toggle)
golang-github-jackpal-gateway 1.0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 276 kB
  • sloc: sh: 38; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 787 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//go:build linux
// +build linux

package gateway

import (
	"fmt"
	"io"
	"net"
	"os"
)

const (
	// See http://man7.org/linux/man-pages/man8/route.8.html
	file = "/proc/net/route"
)

func readRoutes() ([]byte, error) {
	f, err := os.Open(file)
	if err != nil {
		return nil, fmt.Errorf("can't access %s", file)
	}
	defer f.Close()

	bytes, err := io.ReadAll(f)
	if err != nil {
		return nil, fmt.Errorf("can't read %s", file)
	}

	return bytes, nil
}

func discoverGatewayOSSpecific() (ip net.IP, err error) {
	bytes, err := readRoutes()
	if err != nil {
		return nil, err
	}
	return parseLinuxGatewayIP(bytes)
}

func discoverGatewayInterfaceOSSpecific() (ip net.IP, err error) {
	bytes, err := readRoutes()
	if err != nil {
		return nil, err
	}
	return parseLinuxInterfaceIP(bytes)
}