forked from pion/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
71 lines (61 loc) · 1.44 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
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"net"
)
func parseMulticastAnswerAddr(in net.Addr) (net.IP, bool) {
switch addr := in.(type) {
case *net.IPAddr:
return addr.IP, true
case *net.UDPAddr:
return addr.IP, true
case *net.TCPAddr:
return addr.IP, true
}
return nil, false
}
func parseAddr(in net.Addr) (net.IP, int, NetworkType, bool) {
switch addr := in.(type) {
case *net.UDPAddr:
return addr.IP, addr.Port, NetworkTypeUDP4, true
case *net.TCPAddr:
return addr.IP, addr.Port, NetworkTypeTCP4, true
}
return nil, 0, 0, false
}
func createAddr(network NetworkType, ip net.IP, port int) net.Addr {
switch {
case network.IsTCP():
return &net.TCPAddr{IP: ip, Port: port}
default:
return &net.UDPAddr{IP: ip, Port: port}
}
}
func addrEqual(a, b net.Addr) bool {
aIP, aPort, aType, aOk := parseAddr(a)
if !aOk {
return false
}
bIP, bPort, bType, bOk := parseAddr(b)
if !bOk {
return false
}
return aType == bType && aIP.Equal(bIP) && aPort == bPort
}
// AddrPort is an IP and a port number.
type AddrPort [18]byte
func toAddrPort(addr net.Addr) AddrPort {
var ap AddrPort
switch addr := addr.(type) {
case *net.UDPAddr:
copy(ap[:16], addr.IP.To16())
ap[16] = uint8(addr.Port >> 8)
ap[17] = uint8(addr.Port)
case *net.TCPAddr:
copy(ap[:16], addr.IP.To16())
ap[16] = uint8(addr.Port >> 8)
ap[17] = uint8(addr.Port)
}
return ap
}