-
Notifications
You must be signed in to change notification settings - Fork 1
/
internet-socket.go
75 lines (62 loc) · 2.04 KB
/
internet-socket.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
72
73
74
75
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package iana
import (
"net/netip"
"github.com/haraldrudell/parl/perrors"
)
// InternetSocket is a unique named type for socket identifiers based on protocol, IP address and possibly port number.
// - InternetSocket is fmt.Stringer
// - InternetSocket is ordered
type InternetSocket string
// NewInternetSocket returns an InternetSocket socket identifier based on protocol, IP address and port
func NewInternetSocket(protocol Protocol, addrPort netip.AddrPort) (internetSocket InternetSocket, err error) {
if !protocol.IsValid() {
err = perrors.NewPF("protocol not valid")
return
}
if !addrPort.IsValid() {
err = perrors.NewPF("addrPort not valid")
return
}
// remove possible Zone
if addrPort.Addr().Zone() != "" {
addrPort = netip.AddrPortFrom(addrPort.Addr().WithZone(""), addrPort.Port())
}
if addrPort.Port() == 0 {
internetSocket, err = NewInternetSocketNoPort(protocol, addrPort.Addr())
return
}
internetSocket = InternetSocket(protocol.String() + addrPort.String())
return
}
// NewInternetSocket1 returns an InternetSocket socket identifier based on protocol, IP address and port, panics on error
func NewInternetSocket1(protocol Protocol, addrPort netip.AddrPort) (internetSocket InternetSocket) {
var err error
internetSocket, err = NewInternetSocket(protocol, addrPort)
if err != nil {
panic(err)
}
return
}
// NewInternetSocketNoPort returns an InternetSocket socket identifier based on protocol and IP address
func NewInternetSocketNoPort(protocol Protocol, addr netip.Addr) (internetSocket InternetSocket, err error) {
if !protocol.IsValid() {
err = perrors.NewPF("protocol not valid")
return
}
if !addr.IsValid() {
err = perrors.NewPF("Addr not valid")
return
}
internetSocket = InternetSocket(protocol.String() + addr.String())
return
}
func (is InternetSocket) IsZeroValue() (isZeroValue bool) {
return is == ""
}
func (is InternetSocket) String() (s string) {
return string(is)
}