github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/pkg/networkfs/resolvconf/resolvconf.go (about)

     1  package resolvconf
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  var (
    11  	nsRegexp     = regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
    12  	searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
    13  )
    14  
    15  func Get() ([]byte, error) {
    16  	resolv, err := ioutil.ReadFile("/etc/resolv.conf")
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return resolv, nil
    21  }
    22  
    23  // getLines parses input into lines and strips away comments.
    24  func getLines(input []byte, commentMarker []byte) [][]byte {
    25  	lines := bytes.Split(input, []byte("\n"))
    26  	var output [][]byte
    27  	for _, currentLine := range lines {
    28  		var commentIndex = bytes.Index(currentLine, commentMarker)
    29  		if commentIndex == -1 {
    30  			output = append(output, currentLine)
    31  		} else {
    32  			output = append(output, currentLine[:commentIndex])
    33  		}
    34  	}
    35  	return output
    36  }
    37  
    38  // GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
    39  func GetNameservers(resolvConf []byte) []string {
    40  	nameservers := []string{}
    41  	for _, line := range getLines(resolvConf, []byte("#")) {
    42  		var ns = nsRegexp.FindSubmatch(line)
    43  		if len(ns) > 0 {
    44  			nameservers = append(nameservers, string(ns[1]))
    45  		}
    46  	}
    47  	return nameservers
    48  }
    49  
    50  // GetNameserversAsCIDR returns nameservers (if any) listed in
    51  // /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
    52  // This function's output is intended for net.ParseCIDR
    53  func GetNameserversAsCIDR(resolvConf []byte) []string {
    54  	nameservers := []string{}
    55  	for _, nameserver := range GetNameservers(resolvConf) {
    56  		nameservers = append(nameservers, nameserver+"/32")
    57  	}
    58  	return nameservers
    59  }
    60  
    61  // GetSearchDomains returns search domains (if any) listed in /etc/resolv.conf
    62  // If more than one search line is encountered, only the contents of the last
    63  // one is returned.
    64  func GetSearchDomains(resolvConf []byte) []string {
    65  	domains := []string{}
    66  	for _, line := range getLines(resolvConf, []byte("#")) {
    67  		match := searchRegexp.FindSubmatch(line)
    68  		if match == nil {
    69  			continue
    70  		}
    71  		domains = strings.Fields(string(match[1]))
    72  	}
    73  	return domains
    74  }
    75  
    76  func Build(path string, dns, dnsSearch []string) error {
    77  	content := bytes.NewBuffer(nil)
    78  	for _, dns := range dns {
    79  		if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
    80  			return err
    81  		}
    82  	}
    83  	if len(dnsSearch) > 0 {
    84  		if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
    85  			if _, err := content.WriteString("search " + searchString + "\n"); err != nil {
    86  				return err
    87  			}
    88  		}
    89  	}
    90  
    91  	return ioutil.WriteFile(path, content.Bytes(), 0644)
    92  }