types.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // TransportPort represent a local Layer 4 endpoint
  12. type TransportPort struct {
  13. Proto Protocol
  14. Port uint16
  15. }
  16. // GetCopy returns a copy of this TransportPort structure instance
  17. func (t *TransportPort) GetCopy() TransportPort {
  18. return TransportPort{Proto: t.Proto, Port: t.Port}
  19. }
  20. // PortBinding represent a port binding between the container an the host
  21. type PortBinding struct {
  22. Proto Protocol
  23. IP net.IP
  24. Port uint16
  25. HostIP net.IP
  26. HostPort uint16
  27. }
  28. // HostAddr returns the host side transport address
  29. func (p PortBinding) HostAddr() (net.Addr, error) {
  30. switch p.Proto {
  31. case UDP:
  32. return &net.UDPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  33. case TCP:
  34. return &net.TCPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  35. default:
  36. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  37. }
  38. }
  39. // ContainerAddr returns the container side transport address
  40. func (p PortBinding) ContainerAddr() (net.Addr, error) {
  41. switch p.Proto {
  42. case UDP:
  43. return &net.UDPAddr{IP: p.IP, Port: int(p.Port)}, nil
  44. case TCP:
  45. return &net.TCPAddr{IP: p.IP, Port: int(p.Port)}, nil
  46. default:
  47. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  48. }
  49. }
  50. // GetCopy returns a copy of this PortBinding structure instance
  51. func (p *PortBinding) GetCopy() PortBinding {
  52. return PortBinding{
  53. Proto: p.Proto,
  54. IP: GetIPCopy(p.IP),
  55. Port: p.Port,
  56. HostIP: GetIPCopy(p.HostIP),
  57. HostPort: p.HostPort,
  58. }
  59. }
  60. // Equal checks if this instance of PortBinding is equal to the passed one
  61. func (p *PortBinding) Equal(o *PortBinding) bool {
  62. if p == o {
  63. return true
  64. }
  65. if o == nil {
  66. return false
  67. }
  68. if p.Proto != o.Proto || p.Port != o.Port || p.HostPort != o.HostPort {
  69. return false
  70. }
  71. if p.IP != nil {
  72. if !p.IP.Equal(o.IP) {
  73. return false
  74. }
  75. } else {
  76. if o.IP != nil {
  77. return false
  78. }
  79. }
  80. if p.HostIP != nil {
  81. if !p.HostIP.Equal(o.HostIP) {
  82. return false
  83. }
  84. } else {
  85. if o.HostIP != nil {
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. // ErrInvalidProtocolBinding is returned when the port binding protocol is not valid.
  92. type ErrInvalidProtocolBinding string
  93. func (ipb ErrInvalidProtocolBinding) Error() string {
  94. return fmt.Sprintf("invalid transport protocol: %s", string(ipb))
  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. }
  162. /******************************
  163. * Well-known Error Interfaces
  164. ******************************/
  165. // MaskableError is an interface for errors which can be ignored by caller
  166. type MaskableError interface {
  167. // Maskable makes implementer into MaskableError type
  168. Maskable()
  169. }
  170. // BadRequestError is an interface for errors originated by a bad request
  171. type BadRequestError interface {
  172. // BadRequest makes implementer into BadRequestError type
  173. BadRequest()
  174. }
  175. // NotFoundError is an interface for errors raised because a needed resource is not available
  176. type NotFoundError interface {
  177. // NotFound makes implementer into NotFoundError type
  178. NotFound()
  179. }
  180. // ForbiddenError is an interface for errors which denote an valid request that cannot be honored
  181. type ForbiddenError interface {
  182. // Forbidden makes implementer into ForbiddenError type
  183. Forbidden()
  184. }
  185. // NoServiceError is an interface for errors returned when the required service is not available
  186. type NoServiceError interface {
  187. // NoService makes implementer into NoServiceError type
  188. NoService()
  189. }
  190. // TimeoutError is an interface for errors raised because of timeout
  191. type TimeoutError interface {
  192. // Timeout makes implementer into TimeoutError type
  193. Timeout()
  194. }
  195. // NotImplementedError is an interface for errors raised because of requested functionality is not yet implemented
  196. type NotImplementedError interface {
  197. // NotImplemented makes implementer into NotImplementedError type
  198. NotImplemented()
  199. }
  200. // InternalError is an interface for errors raised because of an internal error
  201. type InternalError interface {
  202. // Internal makes implementer into InternalError type
  203. Internal()
  204. }
  205. /******************************
  206. * Weel-known Error Formatters
  207. ******************************/
  208. // BadRequestErrorf creates an instance of BadRequestError
  209. func BadRequestErrorf(format string, params ...interface{}) error {
  210. return badRequest(fmt.Sprintf(format, params...))
  211. }
  212. // NotFoundErrorf creates an instance of NotFoundError
  213. func NotFoundErrorf(format string, params ...interface{}) error {
  214. return notFound(fmt.Sprintf(format, params...))
  215. }
  216. // ForbiddenErrorf creates an instance of ForbiddenError
  217. func ForbiddenErrorf(format string, params ...interface{}) error {
  218. return forbidden(fmt.Sprintf(format, params...))
  219. }
  220. // NoServiceErrorf creates an instance of NoServiceError
  221. func NoServiceErrorf(format string, params ...interface{}) error {
  222. return noService(fmt.Sprintf(format, params...))
  223. }
  224. // NotImplementedErrorf creates an instance of NotImplementedError
  225. func NotImplementedErrorf(format string, params ...interface{}) error {
  226. return notImpl(fmt.Sprintf(format, params...))
  227. }
  228. // TimeoutErrorf creates an instance of TimeoutError
  229. func TimeoutErrorf(format string, params ...interface{}) error {
  230. return timeout(fmt.Sprintf(format, params...))
  231. }
  232. // InternalErrorf creates an instance of InternalError
  233. func InternalErrorf(format string, params ...interface{}) error {
  234. return internal(fmt.Sprintf(format, params...))
  235. }
  236. // InternalMaskableErrorf creates an instance of InternalError and MaskableError
  237. func InternalMaskableErrorf(format string, params ...interface{}) error {
  238. return maskInternal(fmt.Sprintf(format, params...))
  239. }
  240. /***********************
  241. * Internal Error Types
  242. ***********************/
  243. type badRequest string
  244. func (br badRequest) Error() string {
  245. return string(br)
  246. }
  247. func (br badRequest) BadRequest() {}
  248. type maskBadRequest string
  249. type notFound string
  250. func (nf notFound) Error() string {
  251. return string(nf)
  252. }
  253. func (nf notFound) NotFound() {}
  254. type forbidden string
  255. func (frb forbidden) Error() string {
  256. return string(frb)
  257. }
  258. func (frb forbidden) Forbidden() {}
  259. type noService string
  260. func (ns noService) Error() string {
  261. return string(ns)
  262. }
  263. func (ns noService) NoService() {}
  264. type maskNoService string
  265. type timeout string
  266. func (to timeout) Error() string {
  267. return string(to)
  268. }
  269. func (to timeout) Timeout() {}
  270. type notImpl string
  271. func (ni notImpl) Error() string {
  272. return string(ni)
  273. }
  274. func (ni notImpl) NotImplemented() {}
  275. type internal string
  276. func (nt internal) Error() string {
  277. return string(nt)
  278. }
  279. func (nt internal) Internal() {}
  280. type maskInternal string
  281. func (mnt maskInternal) Error() string {
  282. return string(mnt)
  283. }
  284. func (mnt maskInternal) Internal() {}
  285. func (mnt maskInternal) Maskable() {}