Blame view

go/src/themaru/network.go 1.74 KB
c35246804   김태훈   기능 추가 및 변경
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  package main
  
  import (
  	"errors"
  	"regexp"
  	"strconv"
  )
  
  func setNetwork(env map[string]string) error {
  	if ok := validateEthernetAddr(env["ethaddr"]); !ok {
  		return errors.New("invalid ethernet address")
  	}
  
  	if ok := validateIpAddr(env["ipaddr"]); !ok {
  		return errors.New("invalid ip address")
  	}
  
  	if ok := validateIpAddr(env["netmask"]); !ok {
  		return errors.New("invalid netmask")
  	}
  
  	if ok := validateIpAddr(env["gateway"]); !ok {
  		return errors.New("invalid gateway")
  	}
  
  	if hasDualNetwork {
  		if ok := validateEthernetAddr(env["ethaddr2"]); !ok {
  			return errors.New("invalid ethernet address 2")
  		}
  
  		if ok := validateIpAddr(env["ipaddr2"]); !ok {
  			return errors.New("invalid ip address 2")
  		}
  
  		if ok := validateIpAddr(env["netmask2"]); !ok {
  			return errors.New("invalid netmask 2")
  		}
  
  		if ok := validateIpAddr(env["gateway2"]); !ok {
  			return errors.New("invalid gateway 2")
  		}
  	}
  
  	return setEnv(0, env)
  }
  
  func validateEthernetAddr(addr string) (ok bool) {
  	r := regexp.MustCompile(`([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2})`)
  	s := r.FindStringSubmatch(addr)
  	if len(s) != 7 || s[0] != addr {
  		return false
  	}
  
  	for i, v := range s {
  		if i == 0 {
  			continue
  		}
  
  		u, err := strconv.ParseUint(v, 16, 8)
  		if err != nil {
  			return false
  		}
  
  		if u > 255 {
  			return false
  		}
  	}
  
  	return true
  }
  
  func validateIpAddr(addr string) (ok bool) {
  	r := regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)`)
  	s := r.FindStringSubmatch(addr)
  	if len(s) != 5 || s[0] != addr {
  		return false
  	}
  
  	for i, v := range s {
  		if i == 0 {
  			continue
  		}
  
  		u, err := strconv.ParseUint(v, 10, 8)
  		if err != nil {
  			return false
  		}
  
  		if u > 255 {
  			return false
  		}
  	}
  
  	return true
  }