types.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // Package types contains types that are common across libnetwork project
  2. package types
  3. import (
  4. "bytes"
  5. "fmt"
  6. "net"
  7. "strconv"
  8. "strings"
  9. )
  10. // constants for the IP address type
  11. const (
  12. IP = iota // IPv4 and IPv6
  13. IPv4
  14. IPv6
  15. )
  16. // UUID represents a globally unique ID of various resources like network and endpoint
  17. type UUID string
  18. // TransportPort represent a local Layer 4 endpoint
  19. type TransportPort struct {
  20. Proto Protocol
  21. Port uint16
  22. }
  23. // Equal checks if this instance of Transportport is equal to the passed one
  24. func (t *TransportPort) Equal(o *TransportPort) bool {
  25. if t == o {
  26. return true
  27. }
  28. if o == nil {
  29. return false
  30. }
  31. if t.Proto != o.Proto || t.Port != o.Port {
  32. return false
  33. }
  34. return true
  35. }
  36. // GetCopy returns a copy of this TransportPort structure instance
  37. func (t *TransportPort) GetCopy() TransportPort {
  38. return TransportPort{Proto: t.Proto, Port: t.Port}
  39. }
  40. // String returns the TransportPort structure in string form
  41. func (t *TransportPort) String() string {
  42. return fmt.Sprintf("%s/%d", t.Proto.String(), t.Port)
  43. }
  44. // FromString reads the TransportPort structure from string
  45. func (t *TransportPort) FromString(s string) error {
  46. ps := strings.Split(s, "/")
  47. if len(ps) == 2 {
  48. t.Proto = ParseProtocol(ps[0])
  49. if p, err := strconv.ParseUint(ps[1], 10, 16); err == nil {
  50. t.Port = uint16(p)
  51. return nil
  52. }
  53. }
  54. return BadRequestErrorf("invalid format for transport port: %s", s)
  55. }
  56. // PortBinding represent a port binding between the container and the host
  57. type PortBinding struct {
  58. Proto Protocol
  59. IP net.IP
  60. Port uint16
  61. HostIP net.IP
  62. HostPort uint16
  63. HostPortEnd uint16
  64. }
  65. // HostAddr returns the host side transport address
  66. func (p PortBinding) HostAddr() (net.Addr, error) {
  67. switch p.Proto {
  68. case UDP:
  69. return &net.UDPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  70. case TCP:
  71. return &net.TCPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
  72. default:
  73. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  74. }
  75. }
  76. // ContainerAddr returns the container side transport address
  77. func (p PortBinding) ContainerAddr() (net.Addr, error) {
  78. switch p.Proto {
  79. case UDP:
  80. return &net.UDPAddr{IP: p.IP, Port: int(p.Port)}, nil
  81. case TCP:
  82. return &net.TCPAddr{IP: p.IP, Port: int(p.Port)}, nil
  83. default:
  84. return nil, ErrInvalidProtocolBinding(p.Proto.String())
  85. }
  86. }
  87. // GetCopy returns a copy of this PortBinding structure instance
  88. func (p *PortBinding) GetCopy() PortBinding {
  89. return PortBinding{
  90. Proto: p.Proto,
  91. IP: GetIPCopy(p.IP),
  92. Port: p.Port,
  93. HostIP: GetIPCopy(p.HostIP),
  94. HostPort: p.HostPort,
  95. HostPortEnd: p.HostPortEnd,
  96. }
  97. }
  98. // String return the PortBinding structure in string form
  99. func (p *PortBinding) String() string {
  100. ret := fmt.Sprintf("%s/", p.Proto)
  101. if p.IP != nil {
  102. ret = fmt.Sprintf("%s%s", ret, p.IP.String())
  103. }
  104. ret = fmt.Sprintf("%s:%d/", ret, p.Port)
  105. if p.HostIP != nil {
  106. ret = fmt.Sprintf("%s%s", ret, p.HostIP.String())
  107. }
  108. ret = fmt.Sprintf("%s:%d", ret, p.HostPort)
  109. return ret
  110. }
  111. // FromString reads the TransportPort structure from string
  112. func (p *PortBinding) FromString(s string) error {
  113. ps := strings.Split(s, "/")
  114. if len(ps) != 3 {
  115. return BadRequestErrorf("invalid format for port binding: %s", s)
  116. }
  117. p.Proto = ParseProtocol(ps[0])
  118. var err error
  119. if p.IP, p.Port, err = parseIPPort(ps[1]); err != nil {
  120. return BadRequestErrorf("failed to parse Container IP/Port in port binding: %s", err.Error())
  121. }
  122. if p.HostIP, p.HostPort, err = parseIPPort(ps[2]); err != nil {
  123. return BadRequestErrorf("failed to parse Host IP/Port in port binding: %s", err.Error())
  124. }
  125. return nil
  126. }
  127. func parseIPPort(s string) (net.IP, uint16, error) {
  128. pp := strings.Split(s, ":")
  129. if len(pp) != 2 {
  130. return nil, 0, BadRequestErrorf("invalid format: %s", s)
  131. }
  132. var ip net.IP
  133. if pp[0] != "" {
  134. if ip = net.ParseIP(pp[0]); ip == nil {
  135. return nil, 0, BadRequestErrorf("invalid ip: %s", pp[0])
  136. }
  137. }
  138. port, err := strconv.ParseUint(pp[1], 10, 16)
  139. if err != nil {
  140. return nil, 0, BadRequestErrorf("invalid port: %s", pp[1])
  141. }
  142. return ip, uint16(port), nil
  143. }
  144. // Equal checks if this instance of PortBinding is equal to the passed one
  145. func (p *PortBinding) Equal(o *PortBinding) bool {
  146. if p == o {
  147. return true
  148. }
  149. if o == nil {
  150. return false
  151. }
  152. if p.Proto != o.Proto || p.Port != o.Port ||
  153. p.HostPort != o.HostPort || p.HostPortEnd != o.HostPortEnd {
  154. return false
  155. }
  156. if p.IP != nil {
  157. if !p.IP.Equal(o.IP) {
  158. return false
  159. }
  160. } else {
  161. if o.IP != nil {
  162. return false
  163. }
  164. }
  165. if p.HostIP != nil {
  166. if !p.HostIP.Equal(o.HostIP) {
  167. return false
  168. }
  169. } else {
  170. if o.HostIP != nil {
  171. return false
  172. }
  173. }
  174. return true
  175. }
  176. // ErrInvalidProtocolBinding is returned when the port binding protocol is not valid.
  177. type ErrInvalidProtocolBinding string
  178. func (ipb ErrInvalidProtocolBinding) Error() string {
  179. return fmt.Sprintf("invalid transport protocol: %s", string(ipb))
  180. }
  181. const (
  182. // ICMP is for the ICMP ip protocol
  183. ICMP = 1
  184. // TCP is for the TCP ip protocol
  185. TCP = 6
  186. // UDP is for the UDP ip protocol
  187. UDP = 17
  188. )
  189. // Protocol represents a IP protocol number
  190. type Protocol uint8
  191. func (p Protocol) String() string {
  192. switch p {
  193. case ICMP:
  194. return "icmp"
  195. case TCP:
  196. return "tcp"
  197. case UDP:
  198. return "udp"
  199. default:
  200. return fmt.Sprintf("%d", p)
  201. }
  202. }
  203. // ParseProtocol returns the respective Protocol type for the passed string
  204. func ParseProtocol(s string) Protocol {
  205. switch strings.ToLower(s) {
  206. case "icmp":
  207. return ICMP
  208. case "udp":
  209. return UDP
  210. case "tcp":
  211. return TCP
  212. default:
  213. return 0
  214. }
  215. }
  216. // GetMacCopy returns a copy of the passed MAC address
  217. func GetMacCopy(from net.HardwareAddr) net.HardwareAddr {
  218. if from == nil {
  219. return nil
  220. }
  221. to := make(net.HardwareAddr, len(from))
  222. copy(to, from)
  223. return to
  224. }
  225. // GetIPCopy returns a copy of the passed IP address
  226. func GetIPCopy(from net.IP) net.IP {
  227. if from == nil {
  228. return nil
  229. }
  230. to := make(net.IP, len(from))
  231. copy(to, from)
  232. return to
  233. }
  234. // GetIPNetCopy returns a copy of the passed IP Network
  235. func GetIPNetCopy(from *net.IPNet) *net.IPNet {
  236. if from == nil {
  237. return nil
  238. }
  239. bm := make(net.IPMask, len(from.Mask))
  240. copy(bm, from.Mask)
  241. return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
  242. }
  243. // GetIPNetCanonical returns the canonical form for the passed network
  244. func GetIPNetCanonical(nw *net.IPNet) *net.IPNet {
  245. if nw == nil {
  246. return nil
  247. }
  248. c := GetIPNetCopy(nw)
  249. c.IP = c.IP.Mask(nw.Mask)
  250. return c
  251. }
  252. // CompareIPNet returns equal if the two IP Networks are equal
  253. func CompareIPNet(a, b *net.IPNet) bool {
  254. if a == b {
  255. return true
  256. }
  257. if a == nil || b == nil {
  258. return false
  259. }
  260. return a.IP.Equal(b.IP) && bytes.Equal(a.Mask, b.Mask)
  261. }
  262. // GetMinimalIP returns the address in its shortest form
  263. func GetMinimalIP(ip net.IP) net.IP {
  264. if ip != nil && ip.To4() != nil {
  265. return ip.To4()
  266. }
  267. return ip
  268. }
  269. // GetMinimalIPNet returns a copy of the passed IP Network with congruent ip and mask notation
  270. func GetMinimalIPNet(nw *net.IPNet) *net.IPNet {
  271. if nw == nil {
  272. return nil
  273. }
  274. if len(nw.IP) == 16 && nw.IP.To4() != nil {
  275. m := nw.Mask
  276. if len(m) == 16 {
  277. m = m[12:16]
  278. }
  279. return &net.IPNet{IP: nw.IP.To4(), Mask: m}
  280. }
  281. return nw
  282. }
  283. // IsIPNetValid returns true if the ipnet is a valid network/mask
  284. // combination. Otherwise returns false.
  285. func IsIPNetValid(nw *net.IPNet) bool {
  286. return nw.String() != "0.0.0.0/0"
  287. }
  288. var v4inV6MaskPrefix = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  289. // compareIPMask checks if the passed ip and mask are semantically compatible.
  290. // It returns the byte indexes for the address and mask so that caller can
  291. // do bitwise operations without modifying address representation.
  292. func compareIPMask(ip net.IP, mask net.IPMask) (is int, ms int, err error) {
  293. // Find the effective starting of address and mask
  294. if len(ip) == net.IPv6len && ip.To4() != nil {
  295. is = 12
  296. }
  297. if len(ip[is:]) == net.IPv4len && len(mask) == net.IPv6len && bytes.Equal(mask[:12], v4inV6MaskPrefix) {
  298. ms = 12
  299. }
  300. // Check if address and mask are semantically compatible
  301. if len(ip[is:]) != len(mask[ms:]) {
  302. err = fmt.Errorf("ip and mask are not compatible: (%#v, %#v)", ip, mask)
  303. }
  304. return
  305. }
  306. // GetHostPartIP returns the host portion of the ip address identified by the mask.
  307. // IP address representation is not modified. If address and mask are not compatible
  308. // an error is returned.
  309. func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
  310. // Find the effective starting of address and mask
  311. is, ms, err := compareIPMask(ip, mask)
  312. if err != nil {
  313. return nil, fmt.Errorf("cannot compute host portion ip address because %s", err)
  314. }
  315. // Compute host portion
  316. out := GetIPCopy(ip)
  317. for i := 0; i < len(mask[ms:]); i++ {
  318. out[is+i] &= ^mask[ms+i]
  319. }
  320. return out, nil
  321. }
  322. // GetBroadcastIP returns the broadcast ip address for the passed network (ip and mask).
  323. // IP address representation is not modified. If address and mask are not compatible
  324. // an error is returned.
  325. func GetBroadcastIP(ip net.IP, mask net.IPMask) (net.IP, error) {
  326. // Find the effective starting of address and mask
  327. is, ms, err := compareIPMask(ip, mask)
  328. if err != nil {
  329. return nil, fmt.Errorf("cannot compute broadcast ip address because %s", err)
  330. }
  331. // Compute broadcast address
  332. out := GetIPCopy(ip)
  333. for i := 0; i < len(mask[ms:]); i++ {
  334. out[is+i] |= ^mask[ms+i]
  335. }
  336. return out, nil
  337. }
  338. // ParseCIDR returns the *net.IPNet represented by the passed CIDR notation
  339. func ParseCIDR(cidr string) (n *net.IPNet, e error) {
  340. var i net.IP
  341. if i, n, e = net.ParseCIDR(cidr); e == nil {
  342. n.IP = i
  343. }
  344. return
  345. }
  346. const (
  347. // NEXTHOP indicates a StaticRoute with an IP next hop.
  348. NEXTHOP = iota
  349. // CONNECTED indicates a StaticRoute with an interface for directly connected peers.
  350. CONNECTED
  351. )
  352. // StaticRoute is a statically-provisioned IP route.
  353. type StaticRoute struct {
  354. Destination *net.IPNet
  355. RouteType int // NEXT_HOP or CONNECTED
  356. // NextHop will be resolved by the kernel (i.e. as a loose hop).
  357. NextHop net.IP
  358. }
  359. // GetCopy returns a copy of this StaticRoute structure
  360. func (r *StaticRoute) GetCopy() *StaticRoute {
  361. d := GetIPNetCopy(r.Destination)
  362. nh := GetIPCopy(r.NextHop)
  363. return &StaticRoute{Destination: d,
  364. RouteType: r.RouteType,
  365. NextHop: nh,
  366. }
  367. }
  368. // InterfaceStatistics represents the interface's statistics
  369. type InterfaceStatistics struct {
  370. RxBytes uint64
  371. RxPackets uint64
  372. RxErrors uint64
  373. RxDropped uint64
  374. TxBytes uint64
  375. TxPackets uint64
  376. TxErrors uint64
  377. TxDropped uint64
  378. }
  379. func (is *InterfaceStatistics) String() string {
  380. return fmt.Sprintf("\nRxBytes: %d, RxPackets: %d, RxErrors: %d, RxDropped: %d, TxBytes: %d, TxPackets: %d, TxErrors: %d, TxDropped: %d",
  381. is.RxBytes, is.RxPackets, is.RxErrors, is.RxDropped, is.TxBytes, is.TxPackets, is.TxErrors, is.TxDropped)
  382. }
  383. /******************************
  384. * Well-known Error Interfaces
  385. ******************************/
  386. // MaskableError is an interface for errors which can be ignored by caller
  387. type MaskableError interface {
  388. // Maskable makes implementer into MaskableError type
  389. Maskable()
  390. }
  391. // RetryError is an interface for errors which might get resolved through retry
  392. type RetryError interface {
  393. // Retry makes implementer into RetryError type
  394. Retry()
  395. }
  396. // BadRequestError is an interface for errors originated by a bad request
  397. type BadRequestError interface {
  398. // BadRequest makes implementer into BadRequestError type
  399. BadRequest()
  400. }
  401. // NotFoundError is an interface for errors raised because a needed resource is not available
  402. type NotFoundError interface {
  403. // NotFound makes implementer into NotFoundError type
  404. NotFound()
  405. }
  406. // ForbiddenError is an interface for errors which denote a valid request that cannot be honored
  407. type ForbiddenError interface {
  408. // Forbidden makes implementer into ForbiddenError type
  409. Forbidden()
  410. }
  411. // NoServiceError is an interface for errors returned when the required service is not available
  412. type NoServiceError interface {
  413. // NoService makes implementer into NoServiceError type
  414. NoService()
  415. }
  416. // TimeoutError is an interface for errors raised because of timeout
  417. type TimeoutError interface {
  418. // Timeout makes implementer into TimeoutError type
  419. Timeout()
  420. }
  421. // NotImplementedError is an interface for errors raised because of requested functionality is not yet implemented
  422. type NotImplementedError interface {
  423. // NotImplemented makes implementer into NotImplementedError type
  424. NotImplemented()
  425. }
  426. // InternalError is an interface for errors raised because of an internal error
  427. type InternalError interface {
  428. // Internal makes implementer into InternalError type
  429. Internal()
  430. }
  431. /******************************
  432. * Well-known Error Formatters
  433. ******************************/
  434. // BadRequestErrorf creates an instance of BadRequestError
  435. func BadRequestErrorf(format string, params ...interface{}) error {
  436. return badRequest(fmt.Sprintf(format, params...))
  437. }
  438. // NotFoundErrorf creates an instance of NotFoundError
  439. func NotFoundErrorf(format string, params ...interface{}) error {
  440. return notFound(fmt.Sprintf(format, params...))
  441. }
  442. // ForbiddenErrorf creates an instance of ForbiddenError
  443. func ForbiddenErrorf(format string, params ...interface{}) error {
  444. return forbidden(fmt.Sprintf(format, params...))
  445. }
  446. // NoServiceErrorf creates an instance of NoServiceError
  447. func NoServiceErrorf(format string, params ...interface{}) error {
  448. return noService(fmt.Sprintf(format, params...))
  449. }
  450. // NotImplementedErrorf creates an instance of NotImplementedError
  451. func NotImplementedErrorf(format string, params ...interface{}) error {
  452. return notImpl(fmt.Sprintf(format, params...))
  453. }
  454. // TimeoutErrorf creates an instance of TimeoutError
  455. func TimeoutErrorf(format string, params ...interface{}) error {
  456. return timeout(fmt.Sprintf(format, params...))
  457. }
  458. // InternalErrorf creates an instance of InternalError
  459. func InternalErrorf(format string, params ...interface{}) error {
  460. return internal(fmt.Sprintf(format, params...))
  461. }
  462. // InternalMaskableErrorf creates an instance of InternalError and MaskableError
  463. func InternalMaskableErrorf(format string, params ...interface{}) error {
  464. return maskInternal(fmt.Sprintf(format, params...))
  465. }
  466. // RetryErrorf creates an instance of RetryError
  467. func RetryErrorf(format string, params ...interface{}) error {
  468. return retry(fmt.Sprintf(format, params...))
  469. }
  470. /***********************
  471. * Internal Error Types
  472. ***********************/
  473. type badRequest string
  474. func (br badRequest) Error() string {
  475. return string(br)
  476. }
  477. func (br badRequest) BadRequest() {}
  478. type maskBadRequest string
  479. type notFound string
  480. func (nf notFound) Error() string {
  481. return string(nf)
  482. }
  483. func (nf notFound) NotFound() {}
  484. type forbidden string
  485. func (frb forbidden) Error() string {
  486. return string(frb)
  487. }
  488. func (frb forbidden) Forbidden() {}
  489. type noService string
  490. func (ns noService) Error() string {
  491. return string(ns)
  492. }
  493. func (ns noService) NoService() {}
  494. type maskNoService string
  495. type timeout string
  496. func (to timeout) Error() string {
  497. return string(to)
  498. }
  499. func (to timeout) Timeout() {}
  500. type notImpl string
  501. func (ni notImpl) Error() string {
  502. return string(ni)
  503. }
  504. func (ni notImpl) NotImplemented() {}
  505. type internal string
  506. func (nt internal) Error() string {
  507. return string(nt)
  508. }
  509. func (nt internal) Internal() {}
  510. type maskInternal string
  511. func (mnt maskInternal) Error() string {
  512. return string(mnt)
  513. }
  514. func (mnt maskInternal) Internal() {}
  515. func (mnt maskInternal) Maskable() {}
  516. type retry string
  517. func (r retry) Error() string {
  518. return string(r)
  519. }
  520. func (r retry) Retry() {}