types.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. // GetMinimalIP returns the address in its shortest form
  175. func GetMinimalIP(ip net.IP) net.IP {
  176. if ip != nil && ip.To4() != nil {
  177. return ip.To4()
  178. }
  179. return ip
  180. }
  181. // GetMinimalIPNet returns a copy of the passed IP Network with congruent ip and mask notation
  182. func GetMinimalIPNet(nw *net.IPNet) *net.IPNet {
  183. if nw == nil {
  184. return nil
  185. }
  186. if len(nw.IP) == 16 && nw.IP.To4() != nil {
  187. m := nw.Mask
  188. if len(m) == 16 {
  189. m = m[12:16]
  190. }
  191. return &net.IPNet{IP: nw.IP.To4(), Mask: m}
  192. }
  193. return nw
  194. }
  195. var v4inV6MaskPrefix = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  196. // GetHostPartIP returns the host portion of the ip address identified by the mask.
  197. // IP address representation is not modified. If address and mask are not compatible
  198. // an error is returned.
  199. func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
  200. // Find the effective starting of address and mask
  201. is := 0
  202. ms := 0
  203. if len(ip) == net.IPv6len && ip.To4() != nil {
  204. is = 12
  205. }
  206. if len(ip[is:]) == net.IPv4len && len(mask) == net.IPv6len && bytes.Equal(mask[:12], v4inV6MaskPrefix) {
  207. ms = 12
  208. }
  209. // Check if address and mask are semantically compatible
  210. if len(ip[is:]) != len(mask[ms:]) {
  211. return nil, fmt.Errorf("cannot compute host portion ip address as ip and mask are not compatible: (%#v, %#v)", ip, mask)
  212. }
  213. // Compute host portion
  214. out := GetIPCopy(ip)
  215. for i := 0; i < len(mask[ms:]); i++ {
  216. out[is+i] &= ^mask[ms+i]
  217. }
  218. return out, nil
  219. }
  220. const (
  221. // NEXTHOP indicates a StaticRoute with an IP next hop.
  222. NEXTHOP = iota
  223. // CONNECTED indicates a StaticRoute with a interface for directly connected peers.
  224. CONNECTED
  225. )
  226. // StaticRoute is a statically-provisioned IP route.
  227. type StaticRoute struct {
  228. Destination *net.IPNet
  229. RouteType int // NEXT_HOP or CONNECTED
  230. // NextHop will be resolved by the kernel (i.e. as a loose hop).
  231. NextHop net.IP
  232. // InterfaceID must refer to a defined interface on the
  233. // Endpoint to which the routes are specified. Routes specified this way
  234. // are interpreted as directly connected to the specified interface (no
  235. // next hop will be used).
  236. InterfaceID int
  237. }
  238. // GetCopy returns a copy of this StaticRoute structure
  239. func (r *StaticRoute) GetCopy() *StaticRoute {
  240. d := GetIPNetCopy(r.Destination)
  241. nh := GetIPCopy(r.NextHop)
  242. return &StaticRoute{Destination: d,
  243. RouteType: r.RouteType,
  244. NextHop: nh,
  245. InterfaceID: r.InterfaceID}
  246. }
  247. /******************************
  248. * Well-known Error Interfaces
  249. ******************************/
  250. // MaskableError is an interface for errors which can be ignored by caller
  251. type MaskableError interface {
  252. // Maskable makes implementer into MaskableError type
  253. Maskable()
  254. }
  255. // RetryError is an interface for errors which might get resolved through retry
  256. type RetryError interface {
  257. // Retry makes implementer into RetryError type
  258. Retry()
  259. }
  260. // BadRequestError is an interface for errors originated by a bad request
  261. type BadRequestError interface {
  262. // BadRequest makes implementer into BadRequestError type
  263. BadRequest()
  264. }
  265. // NotFoundError is an interface for errors raised because a needed resource is not available
  266. type NotFoundError interface {
  267. // NotFound makes implementer into NotFoundError type
  268. NotFound()
  269. }
  270. // ForbiddenError is an interface for errors which denote an valid request that cannot be honored
  271. type ForbiddenError interface {
  272. // Forbidden makes implementer into ForbiddenError type
  273. Forbidden()
  274. }
  275. // NoServiceError is an interface for errors returned when the required service is not available
  276. type NoServiceError interface {
  277. // NoService makes implementer into NoServiceError type
  278. NoService()
  279. }
  280. // TimeoutError is an interface for errors raised because of timeout
  281. type TimeoutError interface {
  282. // Timeout makes implementer into TimeoutError type
  283. Timeout()
  284. }
  285. // NotImplementedError is an interface for errors raised because of requested functionality is not yet implemented
  286. type NotImplementedError interface {
  287. // NotImplemented makes implementer into NotImplementedError type
  288. NotImplemented()
  289. }
  290. // InternalError is an interface for errors raised because of an internal error
  291. type InternalError interface {
  292. // Internal makes implementer into InternalError type
  293. Internal()
  294. }
  295. /******************************
  296. * Well-known Error Formatters
  297. ******************************/
  298. // BadRequestErrorf creates an instance of BadRequestError
  299. func BadRequestErrorf(format string, params ...interface{}) error {
  300. return badRequest(fmt.Sprintf(format, params...))
  301. }
  302. // NotFoundErrorf creates an instance of NotFoundError
  303. func NotFoundErrorf(format string, params ...interface{}) error {
  304. return notFound(fmt.Sprintf(format, params...))
  305. }
  306. // ForbiddenErrorf creates an instance of ForbiddenError
  307. func ForbiddenErrorf(format string, params ...interface{}) error {
  308. return forbidden(fmt.Sprintf(format, params...))
  309. }
  310. // NoServiceErrorf creates an instance of NoServiceError
  311. func NoServiceErrorf(format string, params ...interface{}) error {
  312. return noService(fmt.Sprintf(format, params...))
  313. }
  314. // NotImplementedErrorf creates an instance of NotImplementedError
  315. func NotImplementedErrorf(format string, params ...interface{}) error {
  316. return notImpl(fmt.Sprintf(format, params...))
  317. }
  318. // TimeoutErrorf creates an instance of TimeoutError
  319. func TimeoutErrorf(format string, params ...interface{}) error {
  320. return timeout(fmt.Sprintf(format, params...))
  321. }
  322. // InternalErrorf creates an instance of InternalError
  323. func InternalErrorf(format string, params ...interface{}) error {
  324. return internal(fmt.Sprintf(format, params...))
  325. }
  326. // InternalMaskableErrorf creates an instance of InternalError and MaskableError
  327. func InternalMaskableErrorf(format string, params ...interface{}) error {
  328. return maskInternal(fmt.Sprintf(format, params...))
  329. }
  330. // RetryErrorf creates an instance of RetryError
  331. func RetryErrorf(format string, params ...interface{}) error {
  332. return retry(fmt.Sprintf(format, params...))
  333. }
  334. /***********************
  335. * Internal Error Types
  336. ***********************/
  337. type badRequest string
  338. func (br badRequest) Error() string {
  339. return string(br)
  340. }
  341. func (br badRequest) BadRequest() {}
  342. type maskBadRequest string
  343. type notFound string
  344. func (nf notFound) Error() string {
  345. return string(nf)
  346. }
  347. func (nf notFound) NotFound() {}
  348. type forbidden string
  349. func (frb forbidden) Error() string {
  350. return string(frb)
  351. }
  352. func (frb forbidden) Forbidden() {}
  353. type noService string
  354. func (ns noService) Error() string {
  355. return string(ns)
  356. }
  357. func (ns noService) NoService() {}
  358. type maskNoService string
  359. type timeout string
  360. func (to timeout) Error() string {
  361. return string(to)
  362. }
  363. func (to timeout) Timeout() {}
  364. type notImpl string
  365. func (ni notImpl) Error() string {
  366. return string(ni)
  367. }
  368. func (ni notImpl) NotImplemented() {}
  369. type internal string
  370. func (nt internal) Error() string {
  371. return string(nt)
  372. }
  373. func (nt internal) Internal() {}
  374. type maskInternal string
  375. func (mnt maskInternal) Error() string {
  376. return string(mnt)
  377. }
  378. func (mnt maskInternal) Internal() {}
  379. func (mnt maskInternal) Maskable() {}
  380. type retry string
  381. func (r retry) Error() string {
  382. return string(r)
  383. }
  384. func (r retry) Retry() {}