types.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Package types contains types that are common across libnetwork project
  2. package types
  3. import (
  4. "bytes"
  5. "fmt"
  6. "net"
  7. "strings"
  8. )
  9. // UUID represents a globally unique ID of various resources like network and endpoint
  10. type UUID string
  11. // ErrInvalidProtocolBinding is returned when the port binding protocol is not valid.
  12. type ErrInvalidProtocolBinding string
  13. func (ipb ErrInvalidProtocolBinding) Error() string {
  14. return fmt.Sprintf("invalid transport protocol: %s", string(ipb))
  15. }
  16. // TransportPort represent a local Layer 4 endpoint
  17. type TransportPort struct {
  18. Proto Protocol
  19. Port uint16
  20. }
  21. // GetCopy returns a copy of this TransportPort structure instance
  22. func (t *TransportPort) GetCopy() TransportPort {
  23. return TransportPort{Proto: t.Proto, Port: t.Port}
  24. }
  25. // PortBinding represent a port binding between the container an the host
  26. type PortBinding struct {
  27. Proto Protocol
  28. IP net.IP
  29. Port uint16
  30. HostIP net.IP
  31. HostPort uint16
  32. }
  33. // HostAddr returns the host side transport address
  34. func (p PortBinding) HostAddr() (net.Addr, error) {
  35. switch p.Proto {
  36. case UDP:
  37. return &net.UDPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  38. case TCP:
  39. return &net.TCPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  40. default:
  41. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  42. }
  43. }
  44. // ContainerAddr returns the container side transport address
  45. func (p PortBinding) ContainerAddr() (net.Addr, error) {
  46. switch p.Proto {
  47. case UDP:
  48. return &net.UDPAddr{IP: p.IP, Port: int(p.Port)}, nil
  49. case TCP:
  50. return &net.TCPAddr{IP: p.IP, Port: int(p.Port)}, nil
  51. default:
  52. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  53. }
  54. }
  55. // GetCopy returns a copy of this PortBinding structure instance
  56. func (p *PortBinding) GetCopy() PortBinding {
  57. return PortBinding{
  58. Proto: p.Proto,
  59. IP: GetIPCopy(p.IP),
  60. Port: p.Port,
  61. HostIP: GetIPCopy(p.HostIP),
  62. HostPort: p.HostPort,
  63. }
  64. }
  65. // Equal checks if this instance of PortBinding is equal to the passed one
  66. func (p *PortBinding) Equal(o *PortBinding) bool {
  67. if p == o {
  68. return true
  69. }
  70. if o == nil {
  71. return false
  72. }
  73. if p.Proto != o.Proto || p.Port != o.Port || p.HostPort != o.HostPort {
  74. return false
  75. }
  76. if p.IP != nil {
  77. if !p.IP.Equal(o.IP) {
  78. return false
  79. }
  80. } else {
  81. if o.IP != nil {
  82. return false
  83. }
  84. }
  85. if p.HostIP != nil {
  86. if !p.HostIP.Equal(o.HostIP) {
  87. return false
  88. }
  89. } else {
  90. if o.HostIP != nil {
  91. return false
  92. }
  93. }
  94. return true
  95. }
  96. const (
  97. // ICMP is for the ICMP ip protocol
  98. ICMP = 1
  99. // TCP is for the TCP ip protocol
  100. TCP = 6
  101. // UDP is for the UDP ip protocol
  102. UDP = 17
  103. )
  104. // Protocol represents a IP protocol number
  105. type Protocol uint8
  106. func (p Protocol) String() string {
  107. switch p {
  108. case ICMP:
  109. return "icmp"
  110. case TCP:
  111. return "tcp"
  112. case UDP:
  113. return "udp"
  114. default:
  115. return fmt.Sprintf("%d", p)
  116. }
  117. }
  118. // ParseProtocol returns the respective Protocol type for the passed string
  119. func ParseProtocol(s string) Protocol {
  120. switch strings.ToLower(s) {
  121. case "icmp":
  122. return ICMP
  123. case "udp":
  124. return UDP
  125. case "tcp":
  126. return TCP
  127. default:
  128. return 0
  129. }
  130. }
  131. // GetMacCopy returns a copy of the passed MAC address
  132. func GetMacCopy(from net.HardwareAddr) net.HardwareAddr {
  133. to := make(net.HardwareAddr, len(from))
  134. copy(to, from)
  135. return to
  136. }
  137. // GetIPCopy returns a copy of the passed IP address
  138. func GetIPCopy(from net.IP) net.IP {
  139. to := make(net.IP, len(from))
  140. copy(to, from)
  141. return to
  142. }
  143. // GetIPNetCopy returns a copy of the passed IP Network
  144. func GetIPNetCopy(from *net.IPNet) *net.IPNet {
  145. if from == nil {
  146. return nil
  147. }
  148. bm := make(net.IPMask, len(from.Mask))
  149. copy(bm, from.Mask)
  150. return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
  151. }
  152. // CompareIPNet returns equal if the two IP Networks are equal
  153. func CompareIPNet(a, b *net.IPNet) bool {
  154. if a == b {
  155. return true
  156. }
  157. if a == nil || b == nil {
  158. return false
  159. }
  160. return a.IP.Equal(b.IP) && bytes.Equal(a.Mask, b.Mask)
  161. }