github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/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  
    25  const filename = "net-info.json"
    26  
    27  type NetInfo struct {
    28  	NetName  string `json:"netName"`
    29  	ConfPath string `json:"netConf"`
    30  	IfName   string `json:"ifName"`
    31  	IP       net.IP `json:"ip"`
    32  	Args     string `json:"args"`
    33  	Mask     net.IP `json:"mask"` // we used IP instead of IPMask because support for json serialization (we don't need specifc functionalities
    34  	HostIP   net.IP `json:"-"`
    35  }
    36  
    37  func LoadAt(cdirfd int) ([]NetInfo, error) {
    38  	fd, err := syscall.Openat(cdirfd, filename, syscall.O_RDONLY, 0)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	f := os.NewFile(uintptr(fd), filename)
    44  
    45  	var info []NetInfo
    46  	err = json.NewDecoder(f).Decode(&info)
    47  	return info, err
    48  }
    49  
    50  func Save(root string, info []NetInfo) error {
    51  	f, err := os.Create(filepath.Join(root, filename))
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer f.Close()
    56  
    57  	return json.NewEncoder(f).Encode(info)
    58  }