-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
63 lines (55 loc) · 1.06 KB
/
addr.go
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
package mdp
import (
"net"
"reflect"
"github.com/poohvpn/icmdp"
"github.com/poohvpn/pooh"
"github.com/rs/zerolog/log"
)
type Addr struct {
IP net.IP
Port int
Zone string
sess *session
}
func (a *Addr) Network() string { return "mdp" }
func (a *Addr) String() string {
return (&net.UDPAddr{
IP: a.IP,
Port: a.Port,
Zone: a.Zone,
}).String()
}
func fromNetAddr(netAddr net.Addr) *Addr {
addr := new(Addr)
switch a := netAddr.(type) {
case *net.TCPAddr:
addr.IP = a.IP
addr.Port = a.Port
addr.Zone = a.Zone
case *net.UDPAddr:
addr.IP = a.IP
addr.Port = a.Port
addr.Zone = a.Zone
case *icmdp.Addr:
addr.IP = a.IP
addr.Port = int(a.ID)
addr.Zone = a.Zone
case *Addr:
addr.IP = a.IP
addr.Port = a.Port
addr.Zone = a.Zone
default:
log.Panic().Str("addr.(type)", reflect.TypeOf(netAddr).String()).Msg("unknown addr type")
}
return addr
}
type DualStackAddr struct {
IP4 net.IP
IP6 net.IP
Port int
Zone string
}
func (a *DualStackAddr) invalid() bool {
return !(pooh.IsIPv4(a.IP4) || pooh.IsIPv6(a.IP6)) || a.Port == 0
}