messages.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package remote
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/docker/libnetwork/types"
  6. )
  7. type response struct {
  8. Err string
  9. }
  10. type maybeError interface {
  11. getError() string
  12. }
  13. func (r *response) getError() string {
  14. return r.Err
  15. }
  16. type createNetworkRequest struct {
  17. NetworkID string
  18. Options map[string]interface{}
  19. }
  20. type createNetworkResponse struct {
  21. response
  22. }
  23. type deleteNetworkRequest struct {
  24. NetworkID string
  25. }
  26. type deleteNetworkResponse struct {
  27. response
  28. }
  29. type createEndpointRequest struct {
  30. NetworkID string
  31. EndpointID string
  32. Interfaces []*endpointInterface
  33. Options map[string]interface{}
  34. }
  35. type endpointInterface struct {
  36. ID int
  37. Address string
  38. AddressIPv6 string
  39. MacAddress string
  40. }
  41. type staticRoute struct {
  42. Destination string
  43. RouteType int
  44. NextHop string
  45. InterfaceID int
  46. }
  47. type createEndpointResponse struct {
  48. response
  49. Interfaces []*endpointInterface
  50. }
  51. func toAddr(ipAddr string) (*net.IPNet, error) {
  52. ip, ipnet, err := net.ParseCIDR(ipAddr)
  53. if err != nil {
  54. return nil, err
  55. }
  56. ipnet.IP = ip
  57. return ipnet, nil
  58. }
  59. type iface struct {
  60. ID int
  61. Address *net.IPNet
  62. AddressIPv6 *net.IPNet
  63. MacAddress net.HardwareAddr
  64. }
  65. func (r *createEndpointResponse) parseInterfaces() ([]*iface, error) {
  66. var ifaces = make([]*iface, len(r.Interfaces))
  67. for i, inIf := range r.Interfaces {
  68. var err error
  69. outIf := &iface{ID: inIf.ID}
  70. if inIf.Address != "" {
  71. if outIf.Address, err = toAddr(inIf.Address); err != nil {
  72. return nil, err
  73. }
  74. }
  75. if inIf.AddressIPv6 != "" {
  76. if outIf.AddressIPv6, err = toAddr(inIf.AddressIPv6); err != nil {
  77. return nil, err
  78. }
  79. }
  80. if inIf.MacAddress != "" {
  81. if outIf.MacAddress, err = net.ParseMAC(inIf.MacAddress); err != nil {
  82. return nil, err
  83. }
  84. }
  85. ifaces[i] = outIf
  86. }
  87. return ifaces, nil
  88. }
  89. func (r *joinResponse) parseStaticRoutes() ([]*types.StaticRoute, error) {
  90. var routes = make([]*types.StaticRoute, len(r.StaticRoutes))
  91. for i, inRoute := range r.StaticRoutes {
  92. var err error
  93. outRoute := &types.StaticRoute{InterfaceID: inRoute.InterfaceID, RouteType: inRoute.RouteType}
  94. if inRoute.Destination != "" {
  95. if outRoute.Destination, err = toAddr(inRoute.Destination); err != nil {
  96. return nil, err
  97. }
  98. }
  99. if inRoute.NextHop != "" {
  100. outRoute.NextHop = net.ParseIP(inRoute.NextHop)
  101. if outRoute.NextHop == nil {
  102. return nil, fmt.Errorf("failed to parse nexthop IP %s", inRoute.NextHop)
  103. }
  104. }
  105. routes[i] = outRoute
  106. }
  107. return routes, nil
  108. }
  109. type deleteEndpointRequest struct {
  110. NetworkID string
  111. EndpointID string
  112. }
  113. type deleteEndpointResponse struct {
  114. response
  115. }
  116. type endpointInfoRequest struct {
  117. NetworkID string
  118. EndpointID string
  119. }
  120. type endpointInfoResponse struct {
  121. response
  122. Value map[string]interface{}
  123. }
  124. type joinRequest struct {
  125. NetworkID string
  126. EndpointID string
  127. SandboxKey string
  128. Options map[string]interface{}
  129. }
  130. type ifaceName struct {
  131. SrcName string
  132. DstPrefix string
  133. }
  134. type joinResponse struct {
  135. response
  136. InterfaceNames []*ifaceName
  137. Gateway string
  138. GatewayIPv6 string
  139. StaticRoutes []*staticRoute
  140. HostsPath string
  141. ResolvConfPath string
  142. }
  143. type leaveRequest struct {
  144. NetworkID string
  145. EndpointID string
  146. }
  147. type leaveResponse struct {
  148. response
  149. }