types.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 and the host
  21. type PortBinding struct {
  22. Proto Protocol
  23. IP net.IP
  24. Port uint16
  25. HostIP net.IP
  26. HostPort uint16
  27. HostPortEnd uint16
  28. }
  29. // HostAddr returns the host side transport address
  30. func (p PortBinding) HostAddr() (net.Addr, error) {
  31. switch p.Proto {
  32. case UDP:
  33. return &net.UDPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  34. case TCP:
  35. return &net.TCPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  36. default:
  37. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  38. }
  39. }
  40. // ContainerAddr returns the container side transport address
  41. func (p PortBinding) ContainerAddr() (net.Addr, error) {
  42. switch p.Proto {
  43. case UDP:
  44. return &net.UDPAddr{IP: p.IP, Port: int(p.Port)}, nil
  45. case TCP:
  46. return &net.TCPAddr{IP: p.IP, Port: int(p.Port)}, nil
  47. default:
  48. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  49. }
  50. }
  51. // GetCopy returns a copy of this PortBinding structure instance
  52. func (p *PortBinding) GetCopy() PortBinding {
  53. return PortBinding{
  54. Proto: p.Proto,
  55. IP: GetIPCopy(p.IP),
  56. Port: p.Port,
  57. HostIP: GetIPCopy(p.HostIP),
  58. HostPort: p.HostPort,
  59. HostPortEnd: p.HostPortEnd,
  60. }
  61. }
  62. // Equal checks if this instance of PortBinding is equal to the passed one
  63. func (p *PortBinding) Equal(o *PortBinding) bool {
  64. if p == o {
  65. return true
  66. }
  67. if o == nil {
  68. return false
  69. }
  70. if p.Proto != o.Proto || p.Port != o.Port ||
  71. p.HostPort != o.HostPort || p.HostPortEnd != o.HostPortEnd {
  72. return false
  73. }
  74. if p.IP != nil {
  75. if !p.IP.Equal(o.IP) {
  76. return false
  77. }
  78. } else {
  79. if o.IP != nil {
  80. return false
  81. }
  82. }
  83. if p.HostIP != nil {
  84. if !p.HostIP.Equal(o.HostIP) {
  85. return false
  86. }
  87. } else {
  88. if o.HostIP != nil {
  89. return false
  90. }
  91. }
  92. return true
  93. }
  94. // ErrInvalidProtocolBinding is returned when the port binding protocol is not valid.
  95. type ErrInvalidProtocolBinding string
  96. func (ipb ErrInvalidProtocolBinding) Error() string {
  97. return fmt.Sprintf("invalid transport protocol: %s", string(ipb))
  98. }
  99. const (
  100. // ICMP is for the ICMP ip protocol
  101. ICMP = 1
  102. // TCP is for the TCP ip protocol
  103. TCP = 6
  104. // UDP is for the UDP ip protocol
  105. UDP = 17
  106. )
  107. // Protocol represents a IP protocol number
  108. type Protocol uint8
  109. func (p Protocol) String() string {
  110. switch p {
  111. case ICMP:
  112. return "icmp"
  113. case TCP:
  114. return "tcp"
  115. case UDP:
  116. return "udp"
  117. default:
  118. return fmt.Sprintf("%d", p)
  119. }
  120. }
  121. // ParseProtocol returns the respective Protocol type for the passed string
  122. func ParseProtocol(s string) Protocol {
  123. switch strings.ToLower(s) {
  124. case "icmp":
  125. return ICMP
  126. case "udp":
  127. return UDP
  128. case "tcp":
  129. return TCP
  130. default:
  131. return 0
  132. }
  133. }
  134. // GetMacCopy returns a copy of the passed MAC address
  135. func GetMacCopy(from net.HardwareAddr) net.HardwareAddr {
  136. to := make(net.HardwareAddr, len(from))
  137. copy(to, from)
  138. return to
  139. }
  140. // GetIPCopy returns a copy of the passed IP address
  141. func GetIPCopy(from net.IP) net.IP {
  142. to := make(net.IP, len(from))
  143. copy(to, from)
  144. return to
  145. }
  146. // GetIPNetCopy returns a copy of the passed IP Network
  147. func GetIPNetCopy(from *net.IPNet) *net.IPNet {
  148. if from == nil {
  149. return nil
  150. }
  151. bm := make(net.IPMask, len(from.Mask))
  152. copy(bm, from.Mask)
  153. return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
  154. }
  155. // GetIPNetCanonical returns the canonical form for the passed network
  156. func GetIPNetCanonical(nw *net.IPNet) *net.IPNet {
  157. if nw == nil {
  158. return nil
  159. }
  160. c := GetIPNetCopy(nw)
  161. c.IP = c.IP.Mask(nw.Mask)
  162. return c
  163. }
  164. // CompareIPNet returns equal if the two IP Networks are equal
  165. func CompareIPNet(a, b *net.IPNet) bool {
  166. if a == b {
  167. return true
  168. }
  169. if a == nil || b == nil {
  170. return false
  171. }
  172. return a.IP.Equal(b.IP) && bytes.Equal(a.Mask, b.Mask)
  173. }
  174. const (
  175. // NEXTHOP indicates a StaticRoute with an IP next hop.
  176. NEXTHOP = iota
  177. // CONNECTED indicates a StaticRoute with a interface for directly connected peers.
  178. CONNECTED
  179. )
  180. // StaticRoute is a statically-provisioned IP route.
  181. type StaticRoute struct {
  182. Destination *net.IPNet
  183. RouteType int // NEXT_HOP or CONNECTED
  184. // NextHop will be resolved by the kernel (i.e. as a loose hop).
  185. NextHop net.IP
  186. // InterfaceID must refer to a defined interface on the
  187. // Endpoint to which the routes are specified. Routes specified this way
  188. // are interpreted as directly connected to the specified interface (no
  189. // next hop will be used).
  190. InterfaceID int
  191. }
  192. // GetCopy returns a copy of this StaticRoute structure
  193. func (r *StaticRoute) GetCopy() *StaticRoute {
  194. d := GetIPNetCopy(r.Destination)
  195. nh := GetIPCopy(r.NextHop)
  196. return &StaticRoute{Destination: d,
  197. RouteType: r.RouteType,
  198. NextHop: nh,
  199. InterfaceID: r.InterfaceID}
  200. }
  201. /******************************
  202. * Well-known Error Interfaces
  203. ******************************/
  204. // MaskableError is an interface for errors which can be ignored by caller
  205. type MaskableError interface {
  206. // Maskable makes implementer into MaskableError type
  207. Maskable()
  208. }
  209. // RetryError is an interface for errors which might get resolved through retry
  210. type RetryError interface {
  211. // Retry makes implementer into RetryError type
  212. Retry()
  213. }
  214. // BadRequestError is an interface for errors originated by a bad request
  215. type BadRequestError interface {
  216. // BadRequest makes implementer into BadRequestError type
  217. BadRequest()
  218. }
  219. // NotFoundError is an interface for errors raised because a needed resource is not available
  220. type NotFoundError interface {
  221. // NotFound makes implementer into NotFoundError type
  222. NotFound()
  223. }
  224. // ForbiddenError is an interface for errors which denote an valid request that cannot be honored
  225. type ForbiddenError interface {
  226. // Forbidden makes implementer into ForbiddenError type
  227. Forbidden()
  228. }
  229. // NoServiceError is an interface for errors returned when the required service is not available
  230. type NoServiceError interface {
  231. // NoService makes implementer into NoServiceError type
  232. NoService()
  233. }
  234. // TimeoutError is an interface for errors raised because of timeout
  235. type TimeoutError interface {
  236. // Timeout makes implementer into TimeoutError type
  237. Timeout()
  238. }
  239. // NotImplementedError is an interface for errors raised because of requested functionality is not yet implemented
  240. type NotImplementedError interface {
  241. // NotImplemented makes implementer into NotImplementedError type
  242. NotImplemented()
  243. }
  244. // InternalError is an interface for errors raised because of an internal error
  245. type InternalError interface {
  246. // Internal makes implementer into InternalError type
  247. Internal()
  248. }
  249. /******************************
  250. * Well-known Error Formatters
  251. ******************************/
  252. // BadRequestErrorf creates an instance of BadRequestError
  253. func BadRequestErrorf(format string, params ...interface{}) error {
  254. return badRequest(fmt.Sprintf(format, params...))
  255. }
  256. // NotFoundErrorf creates an instance of NotFoundError
  257. func NotFoundErrorf(format string, params ...interface{}) error {
  258. return notFound(fmt.Sprintf(format, params...))
  259. }
  260. // ForbiddenErrorf creates an instance of ForbiddenError
  261. func ForbiddenErrorf(format string, params ...interface{}) error {
  262. return forbidden(fmt.Sprintf(format, params...))
  263. }
  264. // NoServiceErrorf creates an instance of NoServiceError
  265. func NoServiceErrorf(format string, params ...interface{}) error {
  266. return noService(fmt.Sprintf(format, params...))
  267. }
  268. // NotImplementedErrorf creates an instance of NotImplementedError
  269. func NotImplementedErrorf(format string, params ...interface{}) error {
  270. return notImpl(fmt.Sprintf(format, params...))
  271. }
  272. // TimeoutErrorf creates an instance of TimeoutError
  273. func TimeoutErrorf(format string, params ...interface{}) error {
  274. return timeout(fmt.Sprintf(format, params...))
  275. }
  276. // InternalErrorf creates an instance of InternalError
  277. func InternalErrorf(format string, params ...interface{}) error {
  278. return internal(fmt.Sprintf(format, params...))
  279. }
  280. // InternalMaskableErrorf creates an instance of InternalError and MaskableError
  281. func InternalMaskableErrorf(format string, params ...interface{}) error {
  282. return maskInternal(fmt.Sprintf(format, params...))
  283. }
  284. // RetryErrorf creates an instance of RetryError
  285. func RetryErrorf(format string, params ...interface{}) error {
  286. return retry(fmt.Sprintf(format, params...))
  287. }
  288. /***********************
  289. * Internal Error Types
  290. ***********************/
  291. type badRequest string
  292. func (br badRequest) Error() string {
  293. return string(br)
  294. }
  295. func (br badRequest) BadRequest() {}
  296. type maskBadRequest string
  297. type notFound string
  298. func (nf notFound) Error() string {
  299. return string(nf)
  300. }
  301. func (nf notFound) NotFound() {}
  302. type forbidden string
  303. func (frb forbidden) Error() string {
  304. return string(frb)
  305. }
  306. func (frb forbidden) Forbidden() {}
  307. type noService string
  308. func (ns noService) Error() string {
  309. return string(ns)
  310. }
  311. func (ns noService) NoService() {}
  312. type maskNoService string
  313. type timeout string
  314. func (to timeout) Error() string {
  315. return string(to)
  316. }
  317. func (to timeout) Timeout() {}
  318. type notImpl string
  319. func (ni notImpl) Error() string {
  320. return string(ni)
  321. }
  322. func (ni notImpl) NotImplemented() {}
  323. type internal string
  324. func (nt internal) Error() string {
  325. return string(nt)
  326. }
  327. func (nt internal) Internal() {}
  328. type maskInternal string
  329. func (mnt maskInternal) Error() string {
  330. return string(mnt)
  331. }
  332. func (mnt maskInternal) Internal() {}
  333. func (mnt maskInternal) Maskable() {}
  334. type retry string
  335. func (r retry) Error() string {
  336. return string(r)
  337. }
  338. func (r retry) Retry() {}