networkmanager

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2023 License: BSD-2-Clause Imports: 8 Imported by: 0

README

networkmanager

简介

这是一个基于 netlink 开发的 GoLang 以太网管理包。请使用root用户运行软件,建议不要使用其他任何的网络管理工具。可以在纯Linux内核系统下运行。

例子


var josnStr, config = `
	{
		"iface": "eth0",
		"address": "192.168.1.58",
		"gateway": "192.168.1.1"
		"dns": "192.168.1.1"
	}
`, &struct {
	Iface 	string `json:"iface"`
	Address string `json:"address"`
	Gateway string `json:"gateway"`
	DNS 	string `json:"dns"`
}{}

func main() {
	var object *networkmanager.Interface
	if err := json.Unmarshal([]byte(josnStr), config); err == nil {
		if object, err = networkmanager.InterfaceByName(config.Iface); err != nil {
			fmt.Println("InterfaceByName error", err)
			return
		}
	}
	fmt.Println("Up Interface", object.Up())
	// 静态IP
	// custom(object)
	// DHCP获取IP
	dhcp(object)
	exec.Command("bash", "-c", `echo "nameserver `+config.DNS+`" >> /etc/resolv.conf`).CombinedOutput()
	object.Close()
	fmt.Println("end")
}

func custom(manager *networkmanager.Interface) {
	fmt.Println("AddIP", manager.AddIP(networkmanager.Addrs{Local: net.ParseIP(config.Address)}))
	if routeList, err := manager.RouteList(); err == nil {
		fmt.Println("Get RouteList")
		for _, val := range routeList {
			fmt.Println("Print Route", *val)
		}
		fmt.Println("RouteList End")
	}
	fmt.Println("Gateway AddRoute", manager.AddRoute(networkmanager.Routes{Gw: net.ParseIP(config.Gateway)}))
	go func() {
		time.Sleep(time.Second*10)
		fmt.Println("os.Exit(1)")
		os.Exit(1)
	}()
}

func dhcp(manager *networkmanager.Interface) {
	rt := networkmanager.Routes{Gw: net.IP{0,0,0,0}}
	fmt.Println("AddRoute: ", manager.AddRoute(rt))
	conn, _ := dhcpv4.NewDhcpV4Conn(config.Iface, packet.IPv4{255,255,255,255})
	dhc1, err := conn.Discover()
	if err != nil {
		fmt.Println("Discover err: ", err)
		return
	}
	dhc1, err = conn.Request(*dhc1)
	if err != nil {
		fmt.Println("Request err: ", err)
		return
	}
	for _, v := range dhc1.Options {
		if v.Code == uint8(packet.DHCP_Requested_IP_Address) {
			config.Address = (*packet.IPv4)(v.Value).String()
			fmt.Println("Address: ", config.Address)
		}
		if v.Code == uint8(packet.DHCP_Router) {
			config.Gateway = (*packet.IPv4)(v.Value).String()
			config.DNS = config.Gateway
			fmt.Println("Gateway: ", config.Gateway)
		}
	}
	fmt.Println("RemoveRoute: ", manager.RemoveRoute(rt))
	custom(manager)
}

Documentation

Overview

@@ @ Author : Eacher @ Date : 2023-06-27 09:39:36 @ LastEditTime : 2023-07-08 16:08:37 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/address.go @@

@@ @ Author : Eacher @ Date : 2023-06-21 08:16:59 @ LastEditTime : 2023-07-08 16:08:49 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/interfaces.go @@

@@ @ Author : Eacher @ Date : 2023-06-26 08:01:05 @ LastEditTime : 2023-07-08 15:39:34 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/netlinkmessage.go @@

@@ @ Author : Eacher @ Date : 2023-06-27 09:38:13 @ LastEditTime : 2023-07-08 16:09:02 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/route.go @@

Index

Constants

View Source
const (
	SOCK_CLOEXEC  = syscall.SOCK_CLOEXEC
	SOL_SOCKET    = syscall.SOL_SOCKET
	SOCK_RAW      = syscall.SOCK_RAW
	SO_RCVTIMEO   = syscall.SO_RCVTIMEO
	NLMSG_HDRLEN  = syscall.NLMSG_HDRLEN
	NETLINK_ROUTE = syscall.NETLINK_ROUTE

	AF_NETLINK = syscall.AF_NETLINK
	AF_UNSPEC  = syscall.AF_UNSPEC
	AF_INET    = syscall.AF_INET
	AF_INET6   = syscall.AF_INET6

	IFA_LOCAL     = syscall.IFA_LOCAL
	IFA_BROADCAST = syscall.IFA_BROADCAST
	IFA_ANYCAST   = syscall.IFA_ANYCAST
	IFA_LABEL     = syscall.IFA_LABEL
	IFA_CACHEINFO = syscall.IFA_CACHEINFO
	IFA_ADDRESS   = syscall.IFA_ADDRESS
	IFF_UP        = syscall.IFF_UP

	NLM_F_CREATE  = syscall.NLM_F_CREATE
	NLM_F_REQUEST = syscall.NLM_F_REQUEST
	NLM_F_EXCL    = syscall.NLM_F_EXCL
	NLM_F_ACK     = syscall.NLM_F_ACK
	NLM_F_DUMP    = syscall.NLM_F_DUMP
	NLM_F_REPLACE = syscall.NLM_F_REPLACE

	RTM_NEWADDR  = syscall.RTM_NEWADDR
	RTM_GETADDR  = syscall.RTM_GETADDR
	RTM_DELADDR  = syscall.RTM_DELADDR
	RTM_NEWLINK  = syscall.RTM_NEWLINK
	RTM_GETROUTE = syscall.RTM_GETROUTE
	RTM_NEWROUTE = syscall.RTM_NEWROUTE
	RTM_DELROUTE = syscall.RTM_DELROUTE

	RTA_DST       = syscall.RTA_DST
	RTA_SRC       = syscall.RTA_SRC
	RTA_PREFSRC   = syscall.RTA_PREFSRC
	RTA_IIF       = syscall.RTA_IIF
	RTA_OIF       = syscall.RTA_OIF
	RTA_GATEWAY   = syscall.RTA_GATEWAY
	RTA_PRIORITY  = syscall.RTA_PRIORITY
	RTA_METRICS   = syscall.RTA_METRICS
	RTA_FLOW      = syscall.RTA_FLOW
	RTA_TABLE     = syscall.RTA_TABLE
	RTA_CACHEINFO = syscall.RTA_CACHEINFO

	RT_TABLE_MAIN     = syscall.RT_TABLE_MAIN
	RT_SCOPE_UNIVERSE = syscall.RT_SCOPE_UNIVERSE
	RTPROT_BOOT       = syscall.RTPROT_BOOT
	RTN_UNICAST       = syscall.RTN_UNICAST

	RTNLGRP_ND_USEROPT = syscall.RTNLGRP_ND_USEROPT

	SizeofRtAttr = syscall.SizeofRtAttr
)

Variables

This section is empty.

Functions

func DeserializeNlMsgerr

func DeserializeNlMsgerr(nlm *syscall.NetlinkMessage) error

func RtAttrToSliceByte

func RtAttrToSliceByte(types uint16, ip net.IP, ips ...net.IP) []byte

func SerializeAddrs

func SerializeAddrs(a *Addrs, idx uint32) []byte

func SerializeRoutes

func SerializeRoutes(r *Routes, idx uint32) []byte

Types

type Addrs

type Addrs struct {
	*packet.IfAddrmsg

	Local     net.IP
	Broadcast net.IP
	Anycast   net.IP

	Cache *cacheInfo
	// contains filtered or unexported fields
}

type Interface

type Interface struct {
	// contains filtered or unexported fields
}

func InterfaceByName

func InterfaceByName(ifname string) (*Interface, error)

func (*Interface) AddIP

func (ifi *Interface) AddIP(a Addrs) error

func (*Interface) AddRoute

func (ifi *Interface) AddRoute(r Routes) error

func (*Interface) Close

func (ifi *Interface) Close()

func (*Interface) Down

func (ifi *Interface) Down() error

func (*Interface) IPList

func (ifi *Interface) IPList() ([]*Addrs, error)

func (*Interface) RemoveIP

func (ifi *Interface) RemoveIP(a Addrs) error

func (*Interface) RemoveRoute

func (ifi *Interface) RemoveRoute(r Routes) error

func (*Interface) ReplaceIP

func (ifi *Interface) ReplaceIP(a *Addrs) error

func (*Interface) ReplaceRoute

func (ifi *Interface) ReplaceRoute(r *Routes) error

func (*Interface) RouteList

func (ifi *Interface) RouteList() ([]*Routes, error)

func (*Interface) RtnetlinkConn

func (ifi *Interface) RtnetlinkConn() *rtnetlink.RtnetlinkConn

func (*Interface) Up

func (ifi *Interface) Up() error

type NetlinkMessage

type NetlinkMessage struct {
	Message []*syscall.NetlinkMessage
	// contains filtered or unexported fields
}

type Routes

type Routes struct {
	*packet.RtMsg

	Priority uint32
	Dst      net.IP
	Src      net.IP
	Gw       net.IP
	// contains filtered or unexported fields
}

Directories

Path Synopsis
@@ @ Author : Eacher @ Date : 2023-07-01 09:08:50 @ LastEditTime : 2023-07-07 09:25:50 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/socket.go @@
@@ @ Author : Eacher @ Date : 2023-07-01 09:08:50 @ LastEditTime : 2023-07-07 09:25:50 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/socket.go @@
arp
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-07 14:41:31 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/arp/arp.go @@
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-07 14:41:31 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/arp/arp.go @@
dhcpv4
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-07 10:57:23 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/dhcpv4/dhcpv4.go @@
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-07 10:57:23 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/dhcpv4/dhcpv4.go @@
rtnetlink
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-08 16:12:33 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/rtnetlink/rtnetlink.go @@
@@ @ Author : Eacher @ Date : 2023-06-29 15:13:47 @ LastEditTime : 2023-07-08 16:12:33 @ LastEditors : Eacher @ --------------------------------------------------------------------------------< @ Description : @ --------------------------------------------------------------------------------< @ FilePath : /networkmanager/socket/rtnetlink/rtnetlink.go @@

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL