/
address.v
317 lines (270 loc) · 6.22 KB
/
address.v
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
module net
import io.util
import net.conv
import os
union AddrData {
Unix
Ip
Ip6
}
const (
addr_ip6_any = [16]u8{init: u8(0)}
addr_ip_any = [4]u8{init: u8(0)}
)
// new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr
pub fn new_ip6(port u16, addr [16]u8) Addr {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
a := Addr{
f: u8(AddrFamily.ip6)
addr: AddrData{
Ip6: Ip6{
port: n_port
}
}
}
unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) }
return a
}
// new_ip creates a new Addr from the IPv4 address family, based on the given port and addr
pub fn new_ip(port u16, addr [4]u8) Addr {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
a := Addr{
f: u8(AddrFamily.ip)
addr: AddrData{
Ip: Ip{
port: n_port
}
}
}
unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) }
return a
}
fn temp_unix() !Addr {
// create a temp file to get a filename
// close it
// remove it
// then reuse the filename
mut file, filename := util.temp_file()!
file.close()
os.rm(filename)!
addrs := resolve_addrs(filename, .unix, .udp)!
return addrs[0]
}
// family returns the family/kind of the given address `a`
pub fn (a Addr) family() AddrFamily {
return unsafe { AddrFamily(a.f) }
}
const (
max_ip_len = 24
max_ip6_len = 46
)
// str returns a string representation of `a`
pub fn (a Ip) str() string {
buf := [net.max_ip_len]char{}
res := &char(C.inet_ntop(.ip, &a.addr, &buf[0], buf.len))
if res == 0 {
return '<Unknown>'
}
saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
return '${saddr}:${port}'
}
// str returns a string representation of `a`
pub fn (a Ip6) str() string {
buf := [net.max_ip6_len]char{}
res := &char(C.inet_ntop(.ip6, &a.addr, &buf[0], buf.len))
if res == 0 {
return '<Unknown>'
}
saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
return '[${saddr}]:${port}'
}
const aoffset = __offsetof(Addr, addr)
// len returns the length in bytes of the address `a`, depending on its family
pub fn (a Addr) len() u32 {
match a.family() {
.ip {
return sizeof(Ip) + net.aoffset
}
.ip6 {
return sizeof(Ip6) + net.aoffset
}
.unix {
return sizeof(Unix) + net.aoffset
}
else {
panic('Unknown address family')
}
}
}
// resolve_addrs converts the given `addr`, `family` and `@type` to a list of addresses
pub fn resolve_addrs(addr string, family AddrFamily, @type SocketType) ![]Addr {
match family {
.ip, .ip6, .unspec {
return resolve_ipaddrs(addr, family, @type)
}
.unix {
resolved := Unix{}
if addr.len > max_unix_path {
return error('net: resolve_addrs Unix socket address is too long')
}
// Copy the unix path into the address struct
unsafe {
C.memcpy(&resolved.path, addr.str, addr.len)
}
return [
Addr{
f: u8(AddrFamily.unix)
addr: AddrData{
Unix: resolved
}
},
]
}
}
}
// resolve_addrs converts the given `addr` and `@type` to a list of addresses
pub fn resolve_addrs_fuzzy(addr string, @type SocketType) ![]Addr {
if addr.len == 0 {
return error('none')
}
// Use a small heuristic to figure out what address family this is
// (out of the ones that we support)
if addr.contains(':') {
// Colon is a reserved character in unix paths
// so this must be an ip address
return resolve_addrs(addr, .unspec, @type)
}
return resolve_addrs(addr, .unix, @type)
}
// resolve_ipaddrs converts the given `addr`, `family` and `typ` to a list of addresses
pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ![]Addr {
address, port := split_address(addr)!
if addr[0] == `:` {
match family {
.ip6 {
return [new_ip6(port, net.addr_ip6_any)]
}
.ip, .unspec {
return [new_ip(port, net.addr_ip_any)]
}
else {}
}
}
mut hints := C.addrinfo{
// ai_family: int(family)
// ai_socktype: int(typ)
// ai_flags: C.AI_PASSIVE
}
unsafe { vmemset(&hints, 0, int(sizeof(hints))) }
hints.ai_family = int(family)
hints.ai_socktype = int(typ)
hints.ai_flags = C.AI_PASSIVE
results := &C.addrinfo(unsafe { nil })
sport := '${port}'
// This might look silly but is recommended by MSDN
$if windows {
socket_error(0 - C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results))!
} $else {
x := C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results)
wrap_error(x)!
}
defer {
C.freeaddrinfo(results)
}
// Now that we have our linked list of addresses
// convert them into an array
mut addresses := []Addr{}
for result := unsafe { results }; !isnil(result); result = result.ai_next {
match unsafe { AddrFamily(result.ai_family) } {
.ip {
new_addr := Addr{
addr: AddrData{
Ip: Ip{}
}
}
unsafe {
C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen)
}
addresses << new_addr
}
.ip6 {
new_addr := Addr{
addr: AddrData{
Ip6: Ip6{}
}
}
unsafe {
C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen)
}
addresses << new_addr
}
else {
panic('Unexpected address family ${result.ai_family}')
}
}
}
return addresses
}
// str returns a string representation of the address `a`
pub fn (a Addr) str() string {
match unsafe { AddrFamily(a.f) } {
.ip {
unsafe {
return a.addr.Ip.str()
}
}
.ip6 {
unsafe {
return a.addr.Ip6.str()
}
}
.unix {
unsafe {
return tos_clone(a.addr.Unix.path[0..max_unix_path].data)
}
}
.unspec {
return '<.unspec>'
}
}
}
// addr_from_socket_handle returns an address, based on the given integer socket `handle`
pub fn addr_from_socket_handle(handle int) Addr {
mut addr := Addr{
addr: AddrData{
Ip6: Ip6{}
}
}
mut size := sizeof(addr)
C.getsockname(handle, voidptr(&addr), &size)
return addr
}
// peer_addr_from_socket_handle retrieves the ip address and port number, given a socket handle
pub fn peer_addr_from_socket_handle(handle int) !Addr {
mut addr := Addr{
addr: AddrData{
Ip6: Ip6{}
}
}
mut size := sizeof(Addr)
socket_error_message(C.getpeername(handle, voidptr(&addr), &size), 'peer_addr_from_socket_handle failed')!
return addr
}