source: code/trunk/vendor/github.com/pires/go-proxyproto/addr_proto.go@ 824

Last change on this file since 824 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 1.9 KB
Line 
1package proxyproto
2
3// AddressFamilyAndProtocol represents address family and transport protocol.
4type AddressFamilyAndProtocol byte
5
6const (
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.
17func (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.
22func (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.
27func (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.
32func (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.
37func (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.
42func (ap AddressFamilyAndProtocol) IsUnspec() bool {
43 return (ap&0xF0 == 0x00) || (ap&0x0F == 0x00)
44}
45
46func (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}
Note: See TracBrowser for help on using the repository browser.