1 | package proxyproto
|
---|
2 |
|
---|
3 | // AddressFamilyAndProtocol represents address family and transport protocol.
|
---|
4 | type AddressFamilyAndProtocol byte
|
---|
5 |
|
---|
6 | const (
|
---|
7 | UNSPEC AddressFamilyAndProtocol = '\x00'
|
---|
8 | TCPv4 AddressFamilyAndProtocol = '\x11'
|
---|
9 | UDPv4 AddressFamilyAndProtocol = '\x12'
|
---|
10 | TCPv6 AddressFamilyAndProtocol = '\x21'
|
---|
11 | UDPv6 AddressFamilyAndProtocol = '\x22'
|
---|
12 | UnixStream AddressFamilyAndProtocol = '\x31'
|
---|
13 | UnixDatagram AddressFamilyAndProtocol = '\x32'
|
---|
14 | )
|
---|
15 |
|
---|
16 | // IsIPv4 returns true if the address family is IPv4 (AF_INET4), false otherwise.
|
---|
17 | func (ap AddressFamilyAndProtocol) IsIPv4() bool {
|
---|
18 | return ap&0xF0 == 0x10
|
---|
19 | }
|
---|
20 |
|
---|
21 | // IsIPv6 returns true if the address family is IPv6 (AF_INET6), false otherwise.
|
---|
22 | func (ap AddressFamilyAndProtocol) IsIPv6() bool {
|
---|
23 | return ap&0xF0 == 0x20
|
---|
24 | }
|
---|
25 |
|
---|
26 | // IsUnix returns true if the address family is UNIX (AF_UNIX), false otherwise.
|
---|
27 | func (ap AddressFamilyAndProtocol) IsUnix() bool {
|
---|
28 | return ap&0xF0 == 0x30
|
---|
29 | }
|
---|
30 |
|
---|
31 | // IsStream returns true if the transport protocol is TCP or STREAM (SOCK_STREAM), false otherwise.
|
---|
32 | func (ap AddressFamilyAndProtocol) IsStream() bool {
|
---|
33 | return ap&0x0F == 0x01
|
---|
34 | }
|
---|
35 |
|
---|
36 | // IsDatagram returns true if the transport protocol is UDP or DGRAM (SOCK_DGRAM), false otherwise.
|
---|
37 | func (ap AddressFamilyAndProtocol) IsDatagram() bool {
|
---|
38 | return ap&0x0F == 0x02
|
---|
39 | }
|
---|
40 |
|
---|
41 | // IsUnspec returns true if the transport protocol or address family is unspecified, false otherwise.
|
---|
42 | func (ap AddressFamilyAndProtocol) IsUnspec() bool {
|
---|
43 | return (ap&0xF0 == 0x00) || (ap&0x0F == 0x00)
|
---|
44 | }
|
---|
45 |
|
---|
46 | func (ap AddressFamilyAndProtocol) toByte() byte {
|
---|
47 | if ap.IsIPv4() && ap.IsStream() {
|
---|
48 | return byte(TCPv4)
|
---|
49 | } else if ap.IsIPv4() && ap.IsDatagram() {
|
---|
50 | return byte(UDPv4)
|
---|
51 | } else if ap.IsIPv6() && ap.IsStream() {
|
---|
52 | return byte(TCPv6)
|
---|
53 | } else if ap.IsIPv6() && ap.IsDatagram() {
|
---|
54 | return byte(UDPv6)
|
---|
55 | } else if ap.IsUnix() && ap.IsStream() {
|
---|
56 | return byte(UnixStream)
|
---|
57 | } else if ap.IsUnix() && ap.IsDatagram() {
|
---|
58 | return byte(UnixDatagram)
|
---|
59 | }
|
---|
60 |
|
---|
61 | return byte(UNSPEC)
|
---|
62 | }
|
---|