File: addr.go

package info (click to toggle)
golang-github-pion-ice.v2 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: makefile: 5
file content (48 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (2)
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
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
}