github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/networking/netinfo/netinfo.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package netinfo
    16  
    17  import (
    18  	"encoding/json"
    19  	"net"
    20  	"os"
    21  	"path/filepath"
    22  	"syscall"
    23  
    24  	"github.com/appc/cni/pkg/types"
    25  )
    26  
    27  const filename = "net-info.json"
    28  
    29  type NetInfo struct {
    30  	NetName  string          `json:"netName"`
    31  	ConfPath string          `json:"netConf"`
    32  	IfName   string          `json:"ifName"`
    33  	IP       net.IP          `json:"ip"`
    34  	Args     string          `json:"args"`
    35  	Mask     net.IP          `json:"mask"` // we used IP instead of IPMask because support for json serialization (we don't need specific functionalities)
    36  	HostIP   net.IP          `json:"-"`
    37  	IP4      *types.IPConfig `json:"-"`
    38  }
    39  
    40  func LoadAt(cdirfd int) ([]NetInfo, error) {
    41  	fd, err := syscall.Openat(cdirfd, filename, syscall.O_RDONLY, 0)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	f := os.NewFile(uintptr(fd), filename)
    47  
    48  	var info []NetInfo
    49  	err = json.NewDecoder(f).Decode(&info)
    50  	return info, err
    51  }
    52  
    53  func Save(root string, info []NetInfo) error {
    54  	f, err := os.Create(filepath.Join(root, filename))
    55  	if err != nil {
    56  		return err
    57  	}
    58  	defer f.Close()
    59  
    60  	return json.NewEncoder(f).Encode(info)
    61  }