@@ -22,22 +22,32 @@ import (
22
22
"regexp"
23
23
"strings"
24
24
25
- "github.com/scionproto/scion/go/lib/snet "
25
+ libaddr "github.com/scionproto/scion/go/lib/addr "
26
26
)
27
27
28
+ type scionAddress struct {
29
+ ia libaddr.IA
30
+ l3 libaddr.HostAddr
31
+ }
32
+
28
33
var (
29
34
hostFilePath = "/etc/hosts"
30
- addrRegexp = regexp .MustCompile (`\d{1,4}-([0-9a-f]{1,4}:){2}[0-9a-f]{1,4},\[[^]]+\]` )
31
- hosts map [string ]string // hostname -> SCION address
32
- revHosts map [string ][]string // SCION address w/o port -> hostnames
35
+ addrRegexp = regexp .MustCompile (`^(?P<ia>\d+-[\d:A-Fa-f]+),\[(?P<host>[^\]]+)\]` )
36
+ hosts map [string ]scionAddress // hostname -> scionAddress
37
+ revHosts map [string ][]string // SCION address w/o port -> hostnames
38
+ )
39
+
40
+ const (
41
+ iaIndex = iota + 1
42
+ l3Index
33
43
)
34
44
35
45
func init () {
36
46
hostsFile , err := readHostsFile ()
37
47
if err != nil {
38
48
hostsFile = []byte {}
39
49
}
40
- err = parseHostsFile (hostsFile )
50
+ parseHostsFile (hostsFile )
41
51
if err != nil {
42
52
log .Fatal (err )
43
53
}
@@ -50,28 +60,23 @@ func AddHost(hostname, address string) error {
50
60
if addrs , ok := hosts [hostname ]; ok {
51
61
return fmt .Errorf ("Host %q already exists, address(es): %v" , hostname , addrs )
52
62
}
53
- _ , err := snet . AddrFromString ( fmt . Sprintf ( "%s:%s" , address , "0" ) )
63
+ addr , err := addrFromString ( address )
54
64
if err != nil {
55
65
return fmt .Errorf ("Cannot add host %q: %v" , hostname , err )
56
66
}
57
- hosts [hostname ] = address
67
+ hosts [hostname ] = addr
58
68
revHosts [address ] = append (revHosts [address ], hostname )
59
69
60
70
return nil
61
71
}
62
72
63
- // GetHostByName returns the SCION address corresponding to hostname.
64
- // The port is set to the default zero value.
65
- func GetHostByName (hostname string ) (* snet.Addr , error ) {
73
+ // GetHostByName returns the IA and HostAddr corresponding to hostname
74
+ func GetHostByName (hostname string ) (libaddr.IA , libaddr.HostAddr , error ) {
66
75
addr , ok := hosts [hostname ]
67
76
if ! ok {
68
- return nil , fmt .Errorf ("Address for host %q not found" , hostname )
77
+ return libaddr. IA {}, nil , fmt .Errorf ("Address for host %q not found" , hostname )
69
78
}
70
- scionAddr , err := snet .AddrFromString (addr )
71
- if err != nil {
72
- return nil , err
73
- }
74
- return scionAddr , nil
79
+ return addr .ia , addr .l3 , nil
75
80
}
76
81
77
82
// GetHostnamesByAddress returns the hostnames corresponding to address
@@ -92,24 +97,47 @@ func readHostsFile() ([]byte, error) {
92
97
return bs , nil
93
98
}
94
99
95
- func parseHostsFile (hostsFile []byte ) error {
96
- hosts = make (map [string ]string )
100
+ func parseHostsFile (hostsFile []byte ) {
101
+ hosts = make (map [string ]scionAddress )
97
102
revHosts = make (map [string ][]string )
98
103
lines := bytes .Split (hostsFile , []byte ("\n " ))
99
104
for _ , line := range lines {
100
105
fields := strings .Fields (string (line ))
101
106
if len (fields ) == 0 {
102
107
continue
103
108
}
104
- if match := addrRegexp .FindString (fields [0 ]); len (match ) == len (fields [0 ]) {
109
+ if matched := addrRegexp .MatchString (fields [0 ]); matched {
110
+ addr , err := addrFromString (fields [0 ])
111
+ if err != nil {
112
+ continue
113
+ }
114
+
115
+ // map hostnames to scionAddress
105
116
for _ , field := range fields [1 :] {
106
117
if _ , ok := hosts [field ]; ! ok {
107
- hosts [field ] = fields [ 0 ]
118
+ hosts [field ] = addr
108
119
revHosts [fields [0 ]] = append (revHosts [fields [0 ]], field )
109
120
}
110
121
}
111
122
}
112
123
113
124
}
114
- return nil
125
+ }
126
+
127
+ func addrFromString (addr string ) (scionAddress , error ) {
128
+ parts := addrRegexp .FindStringSubmatch (addr )
129
+ ia , err := libaddr .IAFromString (parts [iaIndex ])
130
+ if err != nil {
131
+ return scionAddress {}, fmt .Errorf ("Invalid IA string: %v" , parts [iaIndex ])
132
+ }
133
+ var l3 libaddr.HostAddr
134
+ if hostSVC := libaddr .HostSVCFromString (parts [l3Index ]); hostSVC != libaddr .SvcNone {
135
+ l3 = hostSVC
136
+ } else {
137
+ l3 = libaddr .HostFromIPStr (parts [l3Index ])
138
+ if l3 == nil {
139
+ return scionAddress {}, fmt .Errorf ("Invalid IP address string: %v" , parts [l3Index ])
140
+ }
141
+ }
142
+ return scionAddress {ia , l3 }, nil
115
143
}
0 commit comments