Esempio n. 1
0
// GetNetlinkSocketAt opens a netlink socket in the network namespace newNs
// and positions the thread back into the network namespace specified by curNs,
// when done. If curNs is close, the function derives the current namespace and
// moves back into it when done. If newNs is close, the socket will be opened
// in the current network namespace.
func GetNetlinkSocketAt(newNs, curNs netns.NsHandle, protocol int) (*NetlinkSocket, error) {
	var err error

	if newNs.IsOpen() {
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()
		if !curNs.IsOpen() {
			if curNs, err = netns.Get(); err != nil {
				return nil, fmt.Errorf("could not get current namespace while creating netlink socket: %v", err)
			}
			defer curNs.Close()
		}
		if err := netns.Set(newNs); err != nil {
			return nil, fmt.Errorf("failed to set into network namespace %d while creating netlink socket: %v", newNs, err)
		}
		defer netns.Set(curNs)
	}

	return getNetlinkSocket(protocol)
}
Esempio n. 2
0
func conditionalClose(ns *netns.NsHandle) {
	if ns != nil && ns.IsOpen() {
		ns.Close()
	}
}