Browse code

Merge pull request #1613 from thijsterlouw/proper_resolvconf_parsing

Proper resolv.conf parsing

Michael Crosby authored on 2013/08/31 04:10:45
Showing 2 changed files
... ...
@@ -781,21 +781,37 @@ func GetResolvConf() ([]byte, error) {
781 781
 // CheckLocalDns looks into the /etc/resolv.conf,
782 782
 // it returns true if there is a local nameserver or if there is no nameserver.
783 783
 func CheckLocalDns(resolvConf []byte) bool {
784
-	if !bytes.Contains(resolvConf, []byte("nameserver")) {
784
+	var parsedResolvConf = StripComments(resolvConf, []byte("#"))
785
+	if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
785 786
 		return true
786 787
 	}
787
-
788 788
 	for _, ip := range [][]byte{
789 789
 		[]byte("127.0.0.1"),
790 790
 		[]byte("127.0.1.1"),
791 791
 	} {
792
-		if bytes.Contains(resolvConf, ip) {
792
+		if bytes.Contains(parsedResolvConf, ip) {
793 793
 			return true
794 794
 		}
795 795
 	}
796 796
 	return false
797 797
 }
798 798
 
799
+// StripComments parses input into lines and strips away comments.
800
+func StripComments(input []byte, commentMarker []byte) []byte {
801
+	lines := bytes.Split(input, []byte("\n"))
802
+	var output []byte
803
+	for _, currentLine := range lines {
804
+		var commentIndex = bytes.Index(currentLine, commentMarker)
805
+		if ( commentIndex == -1 ) {
806
+			output = append(output, currentLine...)
807
+		} else {
808
+			output = append(output, currentLine[:commentIndex]...)
809
+		}
810
+		output = append(output, []byte("\n")...)
811
+	}
812
+	return output
813
+}
814
+
799 815
 func ParseHost(host string, port int, addr string) string {
800 816
 	if strings.HasPrefix(addr, "unix://") {
801 817
 		return addr
... ...
@@ -323,6 +323,16 @@ func TestCheckLocalDns(t *testing.T) {
323 323
 nameserver 10.0.2.3
324 324
 search dotcloud.net`: false,
325 325
 		`# Dynamic
326
+#nameserver 127.0.0.1
327
+nameserver 10.0.2.3
328
+search dotcloud.net`: false,
329
+		`# Dynamic
330
+nameserver 10.0.2.3 #not used 127.0.1.1
331
+search dotcloud.net`: false,
332
+		`# Dynamic
333
+#nameserver 10.0.2.3
334
+#search dotcloud.net`: true,
335
+		`# Dynamic
326 336
 nameserver 127.0.0.1
327 337
 search dotcloud.net`: true,
328 338
 		`# Dynamic