Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package ipc |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
Cosmos Nicolaou | bae615a | 2014-08-27 23:32:31 -0700 | [diff] [blame] | 6 | "net" |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 7 | "reflect" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 8 | "strings" |
| 9 | "sync" |
| 10 | "time" |
| 11 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron2" |
| 13 | "veyron.io/veyron/veyron2/config" |
| 14 | "veyron.io/veyron/veyron2/context" |
| 15 | "veyron.io/veyron/veyron2/ipc" |
| 16 | "veyron.io/veyron/veyron2/ipc/stream" |
| 17 | "veyron.io/veyron/veyron2/naming" |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 18 | "veyron.io/veyron/veyron2/options" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 19 | "veyron.io/veyron/veyron2/security" |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 20 | mttypes "veyron.io/veyron/veyron2/services/mounttable/types" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 21 | "veyron.io/veyron/veyron2/verror" |
| 22 | "veyron.io/veyron/veyron2/vlog" |
| 23 | "veyron.io/veyron/veyron2/vom" |
| 24 | "veyron.io/veyron/veyron2/vtrace" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 25 | |
| 26 | "veyron.io/veyron/veyron/lib/glob" |
| 27 | "veyron.io/veyron/veyron/lib/netstate" |
| 28 | "veyron.io/veyron/veyron/runtimes/google/lib/publisher" |
| 29 | inaming "veyron.io/veyron/veyron/runtimes/google/naming" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 30 | ivtrace "veyron.io/veyron/veyron/runtimes/google/vtrace" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var ( |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 34 | errServerStopped = verror.Abortedf("ipc: server is stopped") |
| 35 | ) |
| 36 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 37 | type server struct { |
| 38 | sync.Mutex |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 39 | ctx context.T // context used by the server to make internal RPCs. |
| 40 | streamMgr stream.Manager // stream manager to listen for new flows. |
| 41 | publisher publisher.Publisher // publisher to publish mounttable mounts. |
| 42 | listenerOpts []stream.ListenerOpt // listener opts passed to Listen. |
| 43 | listeners map[stream.Listener]*dhcpListener // listeners created by Listen. |
| 44 | disp ipc.Dispatcher // dispatcher to serve RPCs |
| 45 | active sync.WaitGroup // active goroutines we've spawned. |
| 46 | stopped bool // whether the server has been stopped. |
| 47 | stoppedChan chan struct{} // closed when the server has been stopped. |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 48 | ns naming.Namespace |
Cosmos Nicolaou | e6e87f1 | 2014-06-03 14:29:10 -0700 | [diff] [blame] | 49 | servesMountTable bool |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 50 | // TODO(cnicolaou): remove this when the publisher tracks published names |
| 51 | // and can return an appropriate error for RemoveName on a name that |
| 52 | // wasn't 'Added' for this server. |
| 53 | names map[string]struct{} |
| 54 | reservedOpt options.ReservedNameDispatcher |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 55 | // TODO(cnicolaou): add roaming stats to ipcStats |
| 56 | stats *ipcStats // stats for this server. |
| 57 | } |
| 58 | |
Benjamin Prosnitz | fdfbf7b | 2014-10-08 09:47:21 -0700 | [diff] [blame] | 59 | var _ ipc.Server = (*server)(nil) |
| 60 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 61 | type dhcpListener struct { |
| 62 | sync.Mutex |
| 63 | publisher *config.Publisher // publisher used to fork the stream |
| 64 | name string // name of the publisher stream |
| 65 | ep *inaming.Endpoint // endpoint returned after listening and choosing an address to be published |
| 66 | port string |
| 67 | ch chan config.Setting // channel to receive settings over |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 70 | func InternalNewServer(ctx context.T, streamMgr stream.Manager, ns naming.Namespace, opts ...ipc.ServerOpt) (ipc.Server, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 71 | s := &server{ |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 72 | ctx: ctx, |
| 73 | streamMgr: streamMgr, |
| 74 | publisher: publisher.New(ctx, ns, publishPeriod), |
| 75 | listeners: make(map[stream.Listener]*dhcpListener), |
| 76 | stoppedChan: make(chan struct{}), |
| 77 | ns: ns, |
| 78 | stats: newIPCStats(naming.Join("ipc", "server", streamMgr.RoutingID().String())), |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 79 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 80 | for _, opt := range opts { |
Bogdan Caprita | 187269b | 2014-05-13 19:59:46 -0700 | [diff] [blame] | 81 | switch opt := opt.(type) { |
| 82 | case stream.ListenerOpt: |
| 83 | // Collect all ServerOpts that are also ListenerOpts. |
| 84 | s.listenerOpts = append(s.listenerOpts, opt) |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 85 | case options.ServesMountTable: |
Cosmos Nicolaou | e6e87f1 | 2014-06-03 14:29:10 -0700 | [diff] [blame] | 86 | s.servesMountTable = bool(opt) |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 87 | case options.ReservedNameDispatcher: |
| 88 | s.reservedOpt = opt |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | return s, nil |
| 92 | } |
| 93 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 94 | func (s *server) Published() ([]string, error) { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 95 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 96 | s.Lock() |
| 97 | defer s.Unlock() |
| 98 | if s.stopped { |
| 99 | return nil, errServerStopped |
| 100 | } |
| 101 | return s.publisher.Published(), nil |
| 102 | } |
| 103 | |
| 104 | // resolveToAddress will try to resolve the input to an address using the |
| 105 | // mount table, if the input is not already an address. |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 106 | func (s *server) resolveToAddress(address string) (string, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 107 | if _, err := inaming.NewEndpoint(address); err == nil { |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 108 | return address, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 109 | } |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 110 | var names []string |
| 111 | if s.ns != nil { |
| 112 | var err error |
| 113 | if names, err = s.ns.Resolve(s.ctx, address); err != nil { |
| 114 | return "", err |
| 115 | } |
| 116 | } else { |
| 117 | names = append(names, address) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 118 | } |
| 119 | for _, n := range names { |
| 120 | address, suffix := naming.SplitAddressName(n) |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 121 | if suffix != "" && suffix != "//" { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 122 | continue |
| 123 | } |
| 124 | if _, err := inaming.NewEndpoint(address); err == nil { |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 125 | return address, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 128 | return "", fmt.Errorf("unable to resolve %q to an endpoint", address) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 131 | /* |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 132 | // ipAddressChooser returns the preferred IP address, which is, |
| 133 | // a public IPv4 address, then any non-loopback IPv4, then a public |
| 134 | // IPv6 address and finally any non-loopback/link-local IPv6 |
| 135 | // It is replicated here to avoid a circular dependency and will, in any case, |
| 136 | // go away when we transition away from Listen to the ListenX API. |
| 137 | func ipAddressChooser(network string, addrs []ipc.Address) ([]ipc.Address, error) { |
| 138 | if !netstate.IsIPProtocol(network) { |
| 139 | return nil, fmt.Errorf("can't support network protocol %q", network) |
| 140 | } |
| 141 | accessible := netstate.AddrList(addrs) |
| 142 | |
| 143 | // Try and find an address on a interface with a default route. |
| 144 | predicates := []netstate.AddressPredicate{netstate.IsPublicUnicastIPv4, |
| 145 | netstate.IsUnicastIPv4, netstate.IsPublicUnicastIPv6} |
| 146 | for _, predicate := range predicates { |
| 147 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 148 | onDefaultRoutes := addrs.Filter(netstate.IsOnDefaultRoute) |
| 149 | if len(onDefaultRoutes) > 0 { |
| 150 | return onDefaultRoutes, nil |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // We failed to find any addresses with default routes, try again |
| 156 | // but without the default route requirement. |
| 157 | for _, predicate := range predicates { |
| 158 | if addrs := accessible.Filter(predicate); len(addrs) > 0 { |
| 159 | return addrs, nil |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return nil, fmt.Errorf("failed to find any usable address for %q", network) |
| 164 | } |
| 165 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 166 | func (s *server) Listen(protocol, address string) (naming.Endpoint, error) { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 167 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 168 | s.Lock() |
| 169 | // Shortcut if the server is stopped, to avoid needlessly creating a |
| 170 | // listener. |
| 171 | if s.stopped { |
| 172 | s.Unlock() |
| 173 | return nil, errServerStopped |
| 174 | } |
| 175 | s.Unlock() |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 176 | var proxyName string |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 177 | if protocol == inaming.Network { |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 178 | proxyName = address |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 179 | var err error |
| 180 | if address, err = s.resolveToAddress(address); err != nil { |
| 181 | return nil, err |
| 182 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 183 | } |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 184 | // TODO(cnicolaou): pass options.ServesMountTable to streamMgr.Listen so that |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 185 | // it can more cleanly set the IsMountTable bit in the endpoint. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 186 | ln, ep, err := s.streamMgr.Listen(protocol, address, s.listenerOpts...) |
| 187 | if err != nil { |
| 188 | vlog.Errorf("ipc: Listen on %v %v failed: %v", protocol, address, err) |
| 189 | return nil, err |
| 190 | } |
Cosmos Nicolaou | bae615a | 2014-08-27 23:32:31 -0700 | [diff] [blame] | 191 | iep, ok := ep.(*inaming.Endpoint) |
| 192 | if !ok { |
| 193 | return nil, fmt.Errorf("ipc: Listen on %v %v failed translating internal endpoint data types", protocol, address) |
| 194 | } |
| 195 | |
Robin Thellend | 7f42427 | 2014-09-04 10:42:14 -0700 | [diff] [blame] | 196 | if protocol != inaming.Network { |
| 197 | // We know the endpoint format, so we crack it open... |
| 198 | switch iep.Protocol { |
| 199 | case "tcp", "tcp4", "tcp6": |
| 200 | host, port, err := net.SplitHostPort(iep.Address) |
| 201 | if err != nil { |
| 202 | return nil, err |
| 203 | } |
| 204 | ip := net.ParseIP(host) |
| 205 | if ip == nil { |
| 206 | return nil, fmt.Errorf("ipc: Listen(%q, %q) failed to parse IP address from address", protocol, address) |
| 207 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 208 | if ip.IsUnspecified() { |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 209 | addrs, err := netstate.GetAccessibleIPs() |
| 210 | if err == nil { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 211 | if a, err := ipAddressChooser(iep.Protocol, addrs); err == nil && len(a) > 0 { |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 212 | iep.Address = net.JoinHostPort(a[0].Address().String(), port) |
Cosmos Nicolaou | 9a24655 | 2014-08-29 13:07:29 -0700 | [diff] [blame] | 213 | } |
Cosmos Nicolaou | f7a11d9 | 2014-08-29 09:56:07 -0700 | [diff] [blame] | 214 | } |
Cosmos Nicolaou | bae615a | 2014-08-27 23:32:31 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 219 | s.Lock() |
| 220 | if s.stopped { |
| 221 | s.Unlock() |
| 222 | // Ignore error return since we can't really do much about it. |
| 223 | ln.Close() |
| 224 | return nil, errServerStopped |
| 225 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 226 | s.listeners[ln] = nil |
Cosmos Nicolaou | bae615a | 2014-08-27 23:32:31 -0700 | [diff] [blame] | 227 | // We have a single goroutine per listener to accept new flows. |
| 228 | // Each flow is served from its own goroutine. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 229 | s.active.Add(1) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 230 | if protocol == inaming.Network { |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 231 | go func(ln stream.Listener, ep *inaming.Endpoint, proxy string) { |
Cosmos Nicolaou | bae615a | 2014-08-27 23:32:31 -0700 | [diff] [blame] | 232 | s.proxyListenLoop(ln, ep, proxy) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 233 | s.active.Done() |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 234 | }(ln, iep, proxyName) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 235 | } else { |
| 236 | go func(ln stream.Listener, ep naming.Endpoint) { |
| 237 | s.listenLoop(ln, ep) |
| 238 | s.active.Done() |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 239 | }(ln, iep) |
Bogdan Caprita | 187269b | 2014-05-13 19:59:46 -0700 | [diff] [blame] | 240 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 241 | s.Unlock() |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 242 | s.publisher.AddServer(s.publishEP(iep, s.servesMountTable), s.servesMountTable) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 243 | return ep, nil |
| 244 | } |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 245 | */ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 246 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 247 | // externalEndpoint examines the endpoint returned by the stream listen call |
| 248 | // and fills in the address to publish to the mount table. It also returns the |
| 249 | // IP host address that it selected for publishing to the mount table. |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 250 | func (s *server) externalEndpoint(chooser ipc.AddressChooser, lep naming.Endpoint) (*inaming.Endpoint, *net.IPAddr, error) { |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 251 | // We know the endpoint format, so we crack it open... |
| 252 | iep, ok := lep.(*inaming.Endpoint) |
| 253 | if !ok { |
| 254 | return nil, nil, fmt.Errorf("failed translating internal endpoint data types") |
| 255 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 256 | switch iep.Protocol { |
| 257 | case "tcp", "tcp4", "tcp6": |
| 258 | host, port, err := net.SplitHostPort(iep.Address) |
| 259 | if err != nil { |
| 260 | return nil, nil, err |
| 261 | } |
| 262 | ip := net.ParseIP(host) |
| 263 | if ip == nil { |
| 264 | return nil, nil, fmt.Errorf("failed to parse %q as an IP host", host) |
| 265 | } |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 266 | if ip.IsUnspecified() && chooser != nil { |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 267 | // Need to find a usable IP address since the call to listen |
| 268 | // didn't specify one. |
| 269 | addrs, err := netstate.GetAccessibleIPs() |
| 270 | if err == nil { |
Cosmos Nicolaou | 66bc120 | 2014-09-30 20:42:43 -0700 | [diff] [blame] | 271 | // TODO(cnicolaou): we could return multiple addresses here, |
| 272 | // all of which can be exported to the mount table. Look at |
| 273 | // this after we transition fully to ListenX. |
| 274 | if a, err := chooser(iep.Protocol, addrs); err == nil && len(a) > 0 { |
| 275 | iep.Address = net.JoinHostPort(a[0].Address().String(), port) |
| 276 | return iep, a[0].Address().(*net.IPAddr), nil |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } else { |
| 280 | // Listen used a fixed IP address, which essentially disables |
| 281 | // roaming. |
| 282 | return iep, nil, nil |
| 283 | } |
| 284 | } |
| 285 | return iep, nil, nil |
| 286 | } |
| 287 | |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 288 | func (s *server) Listen(listenSpec ipc.ListenSpec) (naming.Endpoint, error) { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 289 | defer vlog.LogCall()() |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 290 | s.Lock() |
| 291 | // Shortcut if the server is stopped, to avoid needlessly creating a |
| 292 | // listener. |
| 293 | if s.stopped { |
| 294 | s.Unlock() |
| 295 | return nil, errServerStopped |
| 296 | } |
| 297 | s.Unlock() |
| 298 | |
Cosmos Nicolaou | 778cb7e | 2014-09-10 15:07:43 -0700 | [diff] [blame] | 299 | protocol := listenSpec.Protocol |
| 300 | address := listenSpec.Address |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 301 | proxyAddress := "" |
Cosmos Nicolaou | 778cb7e | 2014-09-10 15:07:43 -0700 | [diff] [blame] | 302 | if len(listenSpec.Proxy) > 0 { |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 303 | if address, err := s.resolveToAddress(listenSpec.Proxy); err != nil { |
| 304 | return nil, err |
| 305 | } else { |
| 306 | proxyAddress = address |
| 307 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 308 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 309 | |
| 310 | ln, lep, err := s.streamMgr.Listen(protocol, address, s.listenerOpts...) |
| 311 | if err != nil { |
| 312 | vlog.Errorf("ipc: Listen on %v %v failed: %v", protocol, address, err) |
| 313 | return nil, err |
| 314 | } |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 315 | ep, ipaddr, err := s.externalEndpoint(listenSpec.AddressChooser, lep) |
Cosmos Nicolaou | c0e4b79 | 2014-09-25 10:57:52 -0700 | [diff] [blame] | 316 | if err != nil { |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 317 | ln.Close() |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 318 | return nil, err |
| 319 | } |
| 320 | |
| 321 | s.Lock() |
| 322 | if s.stopped { |
| 323 | s.Unlock() |
| 324 | // Ignore error return since we can't really do much about it. |
| 325 | ln.Close() |
| 326 | return nil, errServerStopped |
| 327 | } |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 328 | |
Srdjan Petrovic | 69381a9 | 2014-10-30 13:20:55 -0700 | [diff] [blame] | 329 | var ip net.IP |
| 330 | if ipaddr != nil { |
| 331 | ip = net.ParseIP(ipaddr.String()) |
| 332 | } else { |
| 333 | vlog.VI(2).Infof("the address %q requested for listening contained a fixed IP address which disables roaming, use :0 instead", address) |
| 334 | } |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 335 | publisher := listenSpec.StreamPublisher |
Srdjan Petrovic | 69381a9 | 2014-10-30 13:20:55 -0700 | [diff] [blame] | 336 | if ip != nil && !ip.IsLoopback() && publisher != nil { |
Cosmos Nicolaou | 767b62d | 2014-09-19 13:58:40 -0700 | [diff] [blame] | 337 | streamName := listenSpec.StreamName |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 338 | ch := make(chan config.Setting) |
| 339 | _, err := publisher.ForkStream(streamName, ch) |
| 340 | if err != nil { |
| 341 | return nil, fmt.Errorf("failed to fork stream %q: %s", streamName, err) |
| 342 | } |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 343 | _, port, _ := net.SplitHostPort(ep.Address) |
| 344 | dhcpl := &dhcpListener{ep: ep, port: port, ch: ch, name: streamName, publisher: publisher} |
| 345 | |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 346 | // We have a goroutine to listen for dhcp changes. |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 347 | s.active.Add(1) |
| 348 | // goroutine to listen for address changes. |
| 349 | go func(dl *dhcpListener) { |
| 350 | s.dhcpLoop(dl) |
| 351 | s.active.Done() |
| 352 | }(dhcpl) |
| 353 | s.listeners[ln] = dhcpl |
| 354 | } else { |
| 355 | s.listeners[ln] = nil |
| 356 | } |
| 357 | |
| 358 | // We have a goroutine per listener to accept new flows. |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 359 | // Each flow is served from its own goroutine. |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 360 | s.active.Add(1) |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 361 | |
| 362 | // goroutine to listen for connections |
| 363 | go func(ln stream.Listener, ep naming.Endpoint) { |
| 364 | s.listenLoop(ln, ep) |
| 365 | s.active.Done() |
Cosmos Nicolaou | 778cb7e | 2014-09-10 15:07:43 -0700 | [diff] [blame] | 366 | }(ln, lep) |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 367 | |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 368 | if len(proxyAddress) > 0 { |
| 369 | pln, pep, err := s.streamMgr.Listen(inaming.Network, proxyAddress, s.listenerOpts...) |
| 370 | if err != nil { |
| 371 | vlog.Errorf("ipc: Listen on %v %v failed: %v", protocol, address, err) |
| 372 | return nil, err |
| 373 | } |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 374 | ipep, ok := pep.(*inaming.Endpoint) |
| 375 | if !ok { |
| 376 | return nil, fmt.Errorf("failed translating internal endpoint data types") |
| 377 | } |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 378 | // We have a goroutine for listening on proxy connections. |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 379 | s.active.Add(1) |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 380 | go func(ln stream.Listener, ep *inaming.Endpoint, proxy string) { |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 381 | s.proxyListenLoop(ln, ep, proxy) |
| 382 | s.active.Done() |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 383 | }(pln, ipep, listenSpec.Proxy) |
Cosmos Nicolaou | bc74314 | 2014-10-06 21:27:18 -0700 | [diff] [blame] | 384 | s.listeners[pln] = nil |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 385 | // TODO(cnicolaou,p): AddServer no longer needs to take the |
| 386 | // servesMountTable bool since it can be extracted from the endpoint. |
| 387 | s.publisher.AddServer(s.publishEP(ipep, s.servesMountTable), s.servesMountTable) |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 388 | } else { |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 389 | s.publisher.AddServer(s.publishEP(ep, s.servesMountTable), s.servesMountTable) |
Cosmos Nicolaou | bf350a6 | 2014-09-12 08:16:24 -0700 | [diff] [blame] | 390 | } |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 391 | s.Unlock() |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 392 | return ep, nil |
| 393 | } |
| 394 | |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 395 | func (s *server) publishEP(ep *inaming.Endpoint, servesMountTable bool) string { |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 396 | var name string |
| 397 | if !s.servesMountTable { |
| 398 | // Make sure that client MountTable code doesn't try and |
| 399 | // ResolveStep past this final address. |
| 400 | name = "//" |
| 401 | } |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 402 | ep.IsMountTable = servesMountTable |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 403 | return naming.JoinAddressName(ep.String(), name) |
| 404 | } |
| 405 | |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 406 | func (s *server) proxyListenLoop(ln stream.Listener, iep *inaming.Endpoint, proxy string) { |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 407 | const ( |
| 408 | min = 5 * time.Millisecond |
| 409 | max = 5 * time.Minute |
| 410 | ) |
| 411 | for { |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 412 | s.listenLoop(ln, iep) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 413 | // The listener is done, so: |
| 414 | // (1) Unpublish its name |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 415 | s.publisher.RemoveServer(s.publishEP(iep, s.servesMountTable)) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 416 | // (2) Reconnect to the proxy unless the server has been stopped |
| 417 | backoff := min |
| 418 | ln = nil |
Cosmos Nicolaou | 29ee985 | 2014-10-15 11:38:55 -0700 | [diff] [blame] | 419 | // TODO(ashankar,cnicolaou): this code is way too confusing and should |
| 420 | // be cleaned up. |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 421 | for ln == nil { |
| 422 | select { |
| 423 | case <-time.After(backoff): |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 424 | resolved, err := s.resolveToAddress(proxy) |
| 425 | if err != nil { |
| 426 | vlog.VI(1).Infof("Failed to resolve proxy %q (%v), will retry in %v", proxy, err, backoff) |
Cosmos Nicolaou | 29ee985 | 2014-10-15 11:38:55 -0700 | [diff] [blame] | 427 | if backoff = backoff * 2; backoff > max { |
| 428 | backoff = max |
| 429 | } |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 430 | break |
| 431 | } |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 432 | var ep naming.Endpoint |
Asim Shankar | dee311d | 2014-08-01 17:41:31 -0700 | [diff] [blame] | 433 | ln, ep, err = s.streamMgr.Listen(inaming.Network, resolved, s.listenerOpts...) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 434 | if err == nil { |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 435 | var ok bool |
| 436 | iep, ok = ep.(*inaming.Endpoint) |
| 437 | if !ok { |
| 438 | vlog.Errorf("failed translating internal endpoint data types") |
| 439 | ln = nil |
| 440 | continue |
| 441 | } |
| 442 | vlog.VI(1).Infof("Reconnected to proxy at %q listener: (%v, %v)", proxy, ln, iep) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 443 | break |
| 444 | } |
| 445 | if backoff = backoff * 2; backoff > max { |
| 446 | backoff = max |
| 447 | } |
| 448 | vlog.VI(1).Infof("Proxy reconnection failed, will retry in %v", backoff) |
| 449 | case <-s.stoppedChan: |
| 450 | return |
| 451 | } |
| 452 | } |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 453 | // TODO(cnicolaou,ashankar): this won't work when we are both |
| 454 | // proxying and publishing locally, which is the common case. |
| 455 | // listenLoop, dhcpLoop and the original publish are all publishing |
| 456 | // addresses to the same name, but the client is not smart enough |
| 457 | // to choose sensibly between them. |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 458 | // (3) reconnected, publish new address |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 459 | s.publisher.AddServer(s.publishEP(iep, s.servesMountTable), s.servesMountTable) |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 460 | s.Lock() |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 461 | s.listeners[ln] = nil |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 462 | s.Unlock() |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | func (s *server) listenLoop(ln stream.Listener, ep naming.Endpoint) { |
| 467 | defer vlog.VI(1).Infof("ipc: Stopped listening on %v", ep) |
| 468 | defer func() { |
| 469 | s.Lock() |
| 470 | delete(s.listeners, ln) |
| 471 | s.Unlock() |
| 472 | }() |
| 473 | for { |
| 474 | flow, err := ln.Accept() |
| 475 | if err != nil { |
| 476 | vlog.VI(10).Infof("ipc: Accept on %v failed: %v", ln, err) |
| 477 | return |
| 478 | } |
| 479 | s.active.Add(1) |
| 480 | go func(flow stream.Flow) { |
| 481 | if err := newFlowServer(flow, s).serve(); err != nil { |
| 482 | // TODO(caprita): Logging errors here is |
| 483 | // too spammy. For example, "not |
| 484 | // authorized" errors shouldn't be |
| 485 | // logged as server errors. |
| 486 | vlog.Errorf("Flow serve on %v failed: %v", ln, err) |
| 487 | } |
| 488 | s.active.Done() |
| 489 | }(flow) |
| 490 | } |
| 491 | } |
| 492 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 493 | func (s *server) applyChange(dhcpl *dhcpListener, addrs []net.Addr, fn func(string)) { |
| 494 | dhcpl.Lock() |
| 495 | defer dhcpl.Unlock() |
| 496 | for _, a := range addrs { |
| 497 | if ip := netstate.AsIP(a); ip != nil { |
| 498 | dhcpl.ep.Address = net.JoinHostPort(ip.String(), dhcpl.port) |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 499 | fn(s.publishEP(dhcpl.ep, s.servesMountTable)) |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | func (s *server) dhcpLoop(dhcpl *dhcpListener) { |
| 505 | defer vlog.VI(1).Infof("ipc: Stopped listen for dhcp changes on %v", dhcpl.ep) |
| 506 | vlog.VI(2).Infof("ipc: dhcp loop") |
| 507 | for setting := range dhcpl.ch { |
| 508 | if setting == nil { |
| 509 | return |
| 510 | } |
| 511 | switch v := setting.Value().(type) { |
| 512 | case bool: |
| 513 | return |
| 514 | case []net.Addr: |
| 515 | s.Lock() |
| 516 | if s.stopped { |
| 517 | s.Unlock() |
| 518 | return |
| 519 | } |
Cosmos Nicolaou | f410759 | 2014-10-09 17:17:11 -0700 | [diff] [blame] | 520 | // TODO(cnicolaou,ashankar): this won't work when we are both |
| 521 | // proxying and publishing locally, which is the common case. |
| 522 | // listenLoop, dhcpLoop and the original publish are all publishing |
| 523 | // addresses to the same name, but the client is not smart enough |
| 524 | // to choose sensibly between them. |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 525 | publisher := s.publisher |
| 526 | s.Unlock() |
| 527 | switch setting.Name() { |
| 528 | case ipc.NewAddrsSetting: |
| 529 | vlog.Infof("Added some addresses: %q", v) |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 530 | s.applyChange(dhcpl, v, func(name string) { publisher.AddServer(name, s.servesMountTable) }) |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 531 | case ipc.RmAddrsSetting: |
| 532 | vlog.Infof("Removed some addresses: %q", v) |
| 533 | s.applyChange(dhcpl, v, publisher.RemoveServer) |
| 534 | } |
| 535 | |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 540 | func (s *server) Serve(name string, obj interface{}, authorizer security.Authorizer) error { |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 541 | if obj == nil { |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 542 | // The ReflectInvoker inside the LeafDispatcher will panic |
| 543 | // if called for a nil value. |
| 544 | return fmt.Errorf("A nil object is not allowed") |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 545 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 546 | return s.ServeDispatcher(name, ipc.LeafDispatcher(obj, authorizer)) |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | func (s *server) ServeDispatcher(name string, disp ipc.Dispatcher) error { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 550 | s.Lock() |
| 551 | defer s.Unlock() |
| 552 | if s.stopped { |
| 553 | return errServerStopped |
| 554 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 555 | if disp == nil { |
| 556 | return fmt.Errorf("A nil dispacther is not allowed") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 557 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 558 | if s.disp != nil { |
| 559 | return fmt.Errorf("Serve or ServeDispatcher has already been called") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 560 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 561 | s.disp = disp |
| 562 | s.names = make(map[string]struct{}) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 563 | if len(name) > 0 { |
| 564 | s.publisher.AddName(name) |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 565 | s.names[name] = struct{}{} |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 566 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 567 | return nil |
| 568 | } |
| 569 | |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 570 | func (s *server) AddName(name string) error { |
| 571 | s.Lock() |
| 572 | defer s.Unlock() |
| 573 | if s.stopped { |
| 574 | return errServerStopped |
| 575 | } |
| 576 | if len(name) == 0 { |
| 577 | return fmt.Errorf("empty name") |
| 578 | } |
| 579 | s.publisher.AddName(name) |
| 580 | // TODO(cnicolaou): remove this map when the publisher's RemoveName |
| 581 | // method returns an error. |
| 582 | s.names[name] = struct{}{} |
| 583 | return nil |
| 584 | } |
| 585 | |
| 586 | func (s *server) RemoveName(name string) error { |
| 587 | s.Lock() |
| 588 | defer s.Unlock() |
| 589 | if s.stopped { |
| 590 | return errServerStopped |
| 591 | } |
| 592 | if _, present := s.names[name]; !present { |
| 593 | return fmt.Errorf("%q has not been previously used for this server", name) |
| 594 | } |
| 595 | s.publisher.RemoveName(name) |
| 596 | delete(s.names, name) |
| 597 | return nil |
| 598 | } |
| 599 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 600 | func (s *server) Stop() error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 601 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 602 | s.Lock() |
| 603 | if s.stopped { |
| 604 | s.Unlock() |
| 605 | return nil |
| 606 | } |
| 607 | s.stopped = true |
Asim Shankar | 0ea02ab | 2014-06-09 11:39:24 -0700 | [diff] [blame] | 608 | close(s.stoppedChan) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 609 | s.Unlock() |
| 610 | |
Robin Thellend | df42823 | 2014-10-06 12:50:44 -0700 | [diff] [blame] | 611 | // Delete the stats object. |
| 612 | s.stats.stop() |
| 613 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 614 | // Note, It's safe to Stop/WaitForStop on the publisher outside of the |
| 615 | // server lock, since publisher is safe for concurrent access. |
| 616 | |
| 617 | // Stop the publisher, which triggers unmounting of published names. |
| 618 | s.publisher.Stop() |
| 619 | // Wait for the publisher to be done unmounting before we can proceed to |
| 620 | // close the listeners (to minimize the number of mounted names pointing |
| 621 | // to endpoint that are no longer serving). |
| 622 | // |
| 623 | // TODO(caprita): See if make sense to fail fast on rejecting |
| 624 | // connections once listeners are closed, and parallelize the publisher |
| 625 | // and listener shutdown. |
| 626 | s.publisher.WaitForStop() |
| 627 | |
| 628 | s.Lock() |
| 629 | // Close all listeners. No new flows will be accepted, while in-flight |
| 630 | // flows will continue until they terminate naturally. |
| 631 | nListeners := len(s.listeners) |
| 632 | errCh := make(chan error, nListeners) |
Cosmos Nicolaou | bc74314 | 2014-10-06 21:27:18 -0700 | [diff] [blame] | 633 | |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 634 | for ln, dhcpl := range s.listeners { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 635 | go func(ln stream.Listener) { |
| 636 | errCh <- ln.Close() |
| 637 | }(ln) |
Cosmos Nicolaou | ef323db | 2014-09-07 22:13:28 -0700 | [diff] [blame] | 638 | if dhcpl != nil { |
| 639 | dhcpl.Lock() |
| 640 | dhcpl.publisher.CloseFork(dhcpl.name, dhcpl.ch) |
| 641 | dhcpl.ch <- config.NewBool("EOF", "stop", true) |
| 642 | dhcpl.Unlock() |
| 643 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 644 | } |
| 645 | s.Unlock() |
| 646 | var firstErr error |
| 647 | for i := 0; i < nListeners; i++ { |
| 648 | if err := <-errCh; err != nil && firstErr == nil { |
| 649 | firstErr = err |
| 650 | } |
| 651 | } |
| 652 | // At this point, we are guaranteed that no new requests are going to be |
| 653 | // accepted. |
| 654 | |
| 655 | // Wait for the publisher and active listener + flows to finish. |
| 656 | s.active.Wait() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 657 | s.Lock() |
| 658 | s.disp = nil |
| 659 | s.Unlock() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 660 | return firstErr |
| 661 | } |
| 662 | |
| 663 | // flowServer implements the RPC server-side protocol for a single RPC, over a |
| 664 | // flow that's already connected to the client. |
| 665 | type flowServer struct { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 666 | context.T |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 667 | server *server // ipc.Server that this flow server belongs to |
| 668 | disp ipc.Dispatcher // ipc.Dispatcher that will serve RPCs on this flow |
| 669 | dec *vom.Decoder // to decode requests and args from the client |
| 670 | enc *vom.Encoder // to encode responses and results to the client |
| 671 | flow stream.Flow // underlying flow |
| 672 | reservedOpt options.ReservedNameDispatcher |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 673 | |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 674 | // Fields filled in during the server invocation. |
| 675 | blessings security.Blessings |
| 676 | method, suffix string |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 677 | tags []interface{} |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 678 | discharges map[string]security.Discharge |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 679 | starttime time.Time |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 680 | endStreamArgs bool // are the stream args at EOF? |
| 681 | allowDebug bool // true if the caller is permitted to view debug information. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Benjamin Prosnitz | fdfbf7b | 2014-10-08 09:47:21 -0700 | [diff] [blame] | 684 | var _ ipc.Stream = (*flowServer)(nil) |
| 685 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 686 | func newFlowServer(flow stream.Flow, server *server) *flowServer { |
Cosmos Nicolaou | dcba93d | 2014-07-30 11:09:26 -0700 | [diff] [blame] | 687 | server.Lock() |
| 688 | disp := server.disp |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 689 | runtime := veyron2.RuntimeFromContext(server.ctx) |
Cosmos Nicolaou | dcba93d | 2014-07-30 11:09:26 -0700 | [diff] [blame] | 690 | server.Unlock() |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 691 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 692 | return &flowServer{ |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 693 | T: InternalNewContext(runtime), |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 694 | server: server, |
Cosmos Nicolaou | dcba93d | 2014-07-30 11:09:26 -0700 | [diff] [blame] | 695 | disp: disp, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 696 | // TODO(toddw): Support different codecs |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 697 | dec: vom.NewDecoder(flow), |
| 698 | enc: vom.NewEncoder(flow), |
| 699 | flow: flow, |
| 700 | reservedOpt: server.reservedOpt, |
| 701 | discharges: make(map[string]security.Discharge), |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | // Vom does not encode untyped nils. |
| 706 | // Consequently, the ipc system does not allow nil results with an interface |
| 707 | // type from server methods. The one exception being errors. |
| 708 | // |
| 709 | // For now, the following hacky assumptions are made, which will be revisited when |
| 710 | // a decision is made on how untyped nils should be encoded/decoded in |
| 711 | // vom/vom2: |
| 712 | // |
| 713 | // - Server methods return 0 or more results |
| 714 | // - Any values returned by the server that have an interface type are either |
| 715 | // non-nil or of type error. |
| 716 | func result2vom(res interface{}) vom.Value { |
| 717 | v := vom.ValueOf(res) |
| 718 | if !v.IsValid() { |
| 719 | // Untyped nils are assumed to be nil-errors. |
| 720 | var boxed verror.E |
| 721 | return vom.ValueOf(&boxed).Elem() |
| 722 | } |
| 723 | if err, iserr := res.(error); iserr { |
| 724 | // Convert errors to verror since errors are often not |
| 725 | // serializable via vom/gob (errors.New and fmt.Errorf return a |
| 726 | // type with no exported fields). |
| 727 | return vom.ValueOf(verror.Convert(err)) |
| 728 | } |
| 729 | return v |
| 730 | } |
| 731 | |
| 732 | func (fs *flowServer) serve() error { |
| 733 | defer fs.flow.Close() |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 734 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 735 | results, err := fs.processRequest() |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 736 | |
Matt Rosencrantz | 1fa3277 | 2014-10-28 11:31:46 -0700 | [diff] [blame] | 737 | ivtrace.FromContext(fs).Finish() |
| 738 | |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 739 | var traceResponse vtrace.Response |
| 740 | if fs.allowDebug { |
| 741 | traceResponse = ivtrace.Response(fs) |
| 742 | } |
| 743 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 744 | // Respond to the client with the response header and positional results. |
| 745 | response := ipc.Response{ |
| 746 | Error: err, |
| 747 | EndStreamResults: true, |
| 748 | NumPosResults: uint64(len(results)), |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 749 | TraceResponse: traceResponse, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 750 | } |
| 751 | if err := fs.enc.Encode(response); err != nil { |
| 752 | return verror.BadProtocolf("ipc: response encoding failed: %v", err) |
| 753 | } |
| 754 | if response.Error != nil { |
| 755 | return response.Error |
| 756 | } |
| 757 | for ix, res := range results { |
| 758 | if err := fs.enc.EncodeValue(result2vom(res)); err != nil { |
| 759 | return verror.BadProtocolf("ipc: result #%d [%T=%v] encoding failed: %v", ix, res, res, err) |
| 760 | } |
| 761 | } |
| 762 | // TODO(ashankar): Should unread data from the flow be drained? |
| 763 | // |
| 764 | // Reason to do so: |
| 765 | // The common stream.Flow implementation (veyron/runtimes/google/ipc/stream/vc/reader.go) |
| 766 | // uses iobuf.Slices backed by an iobuf.Pool. If the stream is not drained, these |
| 767 | // slices will not be returned to the pool leading to possibly increased memory usage. |
| 768 | // |
| 769 | // Reason to not do so: |
| 770 | // Draining here will conflict with any Reads on the flow in a separate goroutine |
| 771 | // (for example, see TestStreamReadTerminatedByServer in full_test.go). |
| 772 | // |
| 773 | // For now, go with the reason to not do so as having unread data in the stream |
| 774 | // should be a rare case. |
| 775 | return nil |
| 776 | } |
| 777 | |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 778 | func (fs *flowServer) readIPCRequest() (*ipc.Request, verror.E) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 779 | // Set a default timeout before reading from the flow. Without this timeout, |
| 780 | // a client that sends no request or a partial request will retain the flow |
| 781 | // indefinitely (and lock up server resources). |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 782 | initTimer := newTimer(defaultCallTimeout) |
| 783 | defer initTimer.Stop() |
| 784 | fs.flow.SetDeadline(initTimer.C) |
| 785 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 786 | // Decode the initial request. |
| 787 | var req ipc.Request |
| 788 | if err := fs.dec.Decode(&req); err != nil { |
| 789 | return nil, verror.BadProtocolf("ipc: request decoding failed: %v", err) |
| 790 | } |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 791 | return &req, nil |
| 792 | } |
| 793 | |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 794 | func lookupInvoker(d ipc.Dispatcher, name, method string) (ipc.Invoker, security.Authorizer, error) { |
| 795 | obj, auth, err := d.Lookup(name, method) |
| 796 | switch { |
| 797 | case err != nil: |
| 798 | return nil, nil, err |
| 799 | case obj == nil: |
| 800 | return nil, auth, nil |
| 801 | } |
| 802 | if invoker, ok := obj.(ipc.Invoker); ok { |
| 803 | return invoker, auth, nil |
| 804 | } |
| 805 | return ipc.ReflectInvoker(obj), auth, nil |
| 806 | } |
| 807 | |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 808 | func (fs *flowServer) processRequest() ([]interface{}, verror.E) { |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 809 | fs.starttime = time.Now() |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 810 | req, verr := fs.readIPCRequest() |
| 811 | if verr != nil { |
Matt Rosencrantz | 1fa3277 | 2014-10-28 11:31:46 -0700 | [diff] [blame] | 812 | // We don't know what the ipc call was supposed to be, but we'll create |
| 813 | // a placeholder span so we can capture annotations. |
| 814 | fs.T, _ = ivtrace.WithNewSpan(fs, "Failed IPC Call") |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 815 | return nil, verr |
| 816 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 817 | fs.method = req.Method |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 818 | |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 819 | // TODO(mattr): Currently this allows users to trigger trace collection |
| 820 | // on the server even if they will not be allowed to collect the |
| 821 | // results later. This might be consider a DOS vector. |
| 822 | spanName := fmt.Sprintf("Server Call: %s.%s", fs.Name(), fs.Method()) |
| 823 | fs.T, _ = ivtrace.WithContinuedSpan(fs, spanName, req.TraceRequest) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 824 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 825 | var cancel context.CancelFunc |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 826 | if req.Timeout != ipc.NoTimeout { |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 827 | fs.T, cancel = fs.WithDeadline(fs.starttime.Add(time.Duration(req.Timeout))) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 828 | } else { |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 829 | fs.T, cancel = fs.WithCancel() |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 830 | } |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 831 | fs.flow.SetDeadline(fs.Done()) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 832 | |
Matt Rosencrantz | 8689793 | 2014-10-02 09:34:34 -0700 | [diff] [blame] | 833 | // Ensure that the context gets cancelled if the flow is closed |
| 834 | // due to a network error, or client cancellation. |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 835 | go func() { |
Matt Rosencrantz | bae0821 | 2014-10-03 08:04:17 -0700 | [diff] [blame] | 836 | select { |
| 837 | case <-fs.flow.Closed(): |
| 838 | // Here we remove the contexts channel as a deadline to the flow. |
| 839 | // We do this to ensure clients get a consistent error when they read/write |
| 840 | // after the flow is closed. Since the flow is already closed, it doesn't |
| 841 | // matter that the context is also cancelled. |
| 842 | fs.flow.SetDeadline(nil) |
| 843 | cancel() |
| 844 | case <-fs.Done(): |
| 845 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 846 | }() |
| 847 | |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 848 | // If additional credentials are provided, make them available in the context |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 849 | var err error |
| 850 | if fs.blessings, err = security.NewBlessings(req.GrantedBlessings); err != nil { |
| 851 | return nil, verror.BadProtocolf("ipc: failed to decode granted blessings: %v", err) |
| 852 | } |
| 853 | // Detect unusable blessings now, rather then discovering they are unusable on first use. |
| 854 | // TODO(ashankar,ataly): Potential confused deputy attack: The client provides the |
| 855 | // server's identity as the blessing. Figure out what we want to do about this - |
| 856 | // should servers be able to assume that a blessing is something that does not |
| 857 | // have the authorizations that the server's own identity has? |
| 858 | if fs.blessings != nil && !reflect.DeepEqual(fs.blessings.PublicKey(), fs.flow.LocalPrincipal().PublicKey()) { |
| 859 | return nil, verror.BadProtocolf("ipc: blessing granted not bound to this server(%v vs %v)", fs.blessings.PublicKey(), fs.flow.LocalPrincipal().PublicKey()) |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 860 | } |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 861 | // Receive third party caveat discharges the client sent |
| 862 | for i := uint64(0); i < req.NumDischarges; i++ { |
Ankur | f044a8d | 2014-09-05 17:05:24 -0700 | [diff] [blame] | 863 | var d security.Discharge |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 864 | if err := fs.dec.Decode(&d); err != nil { |
| 865 | return nil, verror.BadProtocolf("ipc: decoding discharge %d of %d failed: %v", i, req.NumDischarges, err) |
| 866 | } |
Ankur | f044a8d | 2014-09-05 17:05:24 -0700 | [diff] [blame] | 867 | fs.discharges[d.ID()] = d |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 868 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 869 | // Lookup the invoker. |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 870 | invoker, auth, suffix, verr := fs.lookup(req.Suffix, req.Method) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 871 | fs.suffix = suffix // with leading /'s stripped |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 872 | if verr != nil { |
| 873 | return nil, verr |
| 874 | } |
| 875 | // Prepare invoker and decode args. |
| 876 | numArgs := int(req.NumPosArgs) |
Asim Shankar | 214f89c | 2014-11-03 16:35:47 -0800 | [diff] [blame] | 877 | argptrs, tags, err := invoker.Prepare(req.Method, numArgs) |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 878 | fs.tags = tags |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 879 | if err != nil { |
Cosmos Nicolaou | 9370ffa | 2014-06-02 11:01:42 -0700 | [diff] [blame] | 880 | return nil, verror.Makef(verror.ErrorID(err), "%s: name: %q", err, req.Suffix) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 881 | } |
| 882 | if len(argptrs) != numArgs { |
Cosmos Nicolaou | 9370ffa | 2014-06-02 11:01:42 -0700 | [diff] [blame] | 883 | return nil, verror.BadProtocolf(fmt.Sprintf("ipc: wrong number of input arguments for method %q, name %q (called with %d args, expected %d)", req.Method, req.Suffix, numArgs, len(argptrs))) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 884 | } |
| 885 | for ix, argptr := range argptrs { |
| 886 | if err := fs.dec.Decode(argptr); err != nil { |
| 887 | return nil, verror.BadProtocolf("ipc: arg %d decoding failed: %v", ix, err) |
| 888 | } |
| 889 | } |
Suharsh Sivakumar | cd743f7 | 2014-10-27 10:03:42 -0700 | [diff] [blame] | 890 | fs.allowDebug = fs.LocalPrincipal() == nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 891 | // Check application's authorization policy and invoke the method. |
Suharsh Sivakumar | cd743f7 | 2014-10-27 10:03:42 -0700 | [diff] [blame] | 892 | // LocalPrincipal is nil means that the server wanted to avoid authentication, |
| 893 | // and thus wanted to skip authorization as well. |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 894 | if fs.LocalPrincipal() != nil { |
Suharsh Sivakumar | cd743f7 | 2014-10-27 10:03:42 -0700 | [diff] [blame] | 895 | // Check if the caller is permitted to view debug information. |
| 896 | if err := fs.authorize(auth); err != nil { |
| 897 | return nil, err |
| 898 | } |
| 899 | fs.allowDebug = fs.authorizeForDebug(auth) == nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 900 | } |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 901 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 902 | results, err := invoker.Invoke(req.Method, fs, argptrs) |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 903 | fs.server.stats.record(req.Method, time.Since(fs.starttime)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 904 | return results, verror.Convert(err) |
| 905 | } |
| 906 | |
| 907 | // lookup returns the invoker and authorizer responsible for serving the given |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 908 | // name and method. The name is stripped of any leading slashes. If it begins |
| 909 | // with ipc.DebugKeyword, we use the internal debug dispatcher to look up the |
| 910 | // invoker. Otherwise, and we use the server's dispatcher. The (stripped) name |
Cosmos Nicolaou | 8bfacf2 | 2014-08-19 11:19:36 -0700 | [diff] [blame] | 911 | // and dispatch suffix are also returned. |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 912 | func (fs *flowServer) lookup(name, method string) (ipc.Invoker, security.Authorizer, string, verror.E) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 913 | name = strings.TrimLeft(name, "/") |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 914 | if method == "Glob" && len(name) == 0 { |
Cosmos Nicolaou | add8e4a | 2014-11-05 22:25:21 -0800 | [diff] [blame^] | 915 | return ipc.ReflectInvoker(&globInvoker{naming.ReservedNamePrefix, fs}), &acceptAllAuthorizer{}, name, nil |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 916 | } |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 917 | disp := fs.disp |
Cosmos Nicolaou | add8e4a | 2014-11-05 22:25:21 -0800 | [diff] [blame^] | 918 | if strings.HasPrefix(name, naming.ReservedNamePrefix) { |
| 919 | parts := strings.SplitN(name, "/", 2) |
| 920 | if len(parts) > 1 { |
| 921 | name = parts[1] |
| 922 | } else { |
| 923 | name = "" |
| 924 | } |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 925 | disp = fs.reservedOpt.Dispatcher |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 926 | } |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 927 | |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 928 | if disp != nil { |
Cosmos Nicolaou | 61c96c7 | 2014-11-03 11:57:56 -0800 | [diff] [blame] | 929 | invoker, auth, err := lookupInvoker(disp, name, method) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 930 | switch { |
| 931 | case err != nil: |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 932 | return nil, nil, "", verror.Convert(err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 933 | case invoker != nil: |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 934 | return invoker, auth, name, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 935 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 936 | } |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 937 | return nil, nil, "", verror.NoExistf("ipc: invoker not found for %q", name) |
| 938 | } |
| 939 | |
| 940 | type acceptAllAuthorizer struct{} |
| 941 | |
| 942 | func (acceptAllAuthorizer) Authorize(security.Context) error { |
| 943 | return nil |
| 944 | } |
| 945 | |
| 946 | type globInvoker struct { |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 947 | prefix string |
| 948 | fs *flowServer |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // Glob matches the pattern against internal object names if the double- |
| 952 | // underscore prefix is explicitly part of the pattern. Otherwise, it invokes |
| 953 | // the service's Glob method. |
| 954 | func (i *globInvoker) Glob(call ipc.ServerCall, pattern string) error { |
| 955 | g, err := glob.Parse(pattern) |
| 956 | if err != nil { |
| 957 | return err |
| 958 | } |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 959 | if strings.HasPrefix(pattern, naming.ReservedNamePrefix) { |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 960 | var err error |
| 961 | // Match against internal object names. |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 962 | if ok, _, left := g.MatchInitialSegment(i.prefix); ok { |
| 963 | if ierr := i.invokeGlob(call, i.fs.reservedOpt.Dispatcher, i.prefix, left.String()); ierr != nil { |
| 964 | err = ierr |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | return err |
| 968 | } |
| 969 | // Invoke the service's method. |
| 970 | return i.invokeGlob(call, i.fs.disp, "", pattern) |
| 971 | } |
| 972 | |
| 973 | func (i *globInvoker) invokeGlob(call ipc.ServerCall, d ipc.Dispatcher, prefix, pattern string) error { |
| 974 | if d == nil { |
| 975 | return nil |
| 976 | } |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 977 | obj, auth, err := d.Lookup("", "Glob") |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 978 | if err != nil { |
| 979 | return err |
| 980 | } |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 981 | // TODO(cnicolaou): ipc.Serve TRANSITION |
| 982 | invoker, ok := obj.(ipc.Invoker) |
| 983 | if !ok { |
Asim Shankar | 214f89c | 2014-11-03 16:35:47 -0800 | [diff] [blame] | 984 | panic(fmt.Errorf("Lookup should have returned an ipc.Invoker, returned %T", obj)) |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 985 | } |
| 986 | if obj == nil || !ok { |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 987 | return verror.NoExistf("ipc: invoker not found for Glob") |
| 988 | } |
| 989 | |
Asim Shankar | 214f89c | 2014-11-03 16:35:47 -0800 | [diff] [blame] | 990 | argptrs, tags, err := invoker.Prepare("Glob", 1) |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 991 | i.fs.tags = tags |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 992 | if err != nil { |
| 993 | return verror.Makef(verror.ErrorID(err), "%s", err) |
| 994 | } |
| 995 | if err := i.fs.authorize(auth); err != nil { |
Asim Shankar | a5457f0 | 2014-10-24 23:23:07 -0700 | [diff] [blame] | 996 | return err |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 997 | } |
| 998 | leafCall := &localServerCall{call, prefix} |
| 999 | argptrs[0] = &pattern |
| 1000 | results, err := invoker.Invoke("Glob", leafCall, argptrs) |
| 1001 | if err != nil { |
| 1002 | return err |
| 1003 | } |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 1004 | |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 1005 | if len(results) != 1 { |
| 1006 | return verror.BadArgf("unexpected number of results. Got %d, want 1", len(results)) |
| 1007 | } |
| 1008 | res := results[0] |
| 1009 | if res == nil { |
| 1010 | return nil |
| 1011 | } |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 1012 | err, ok = res.(error) |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 1013 | if !ok { |
| 1014 | return verror.BadArgf("unexpected result type. Got %T, want error", res) |
| 1015 | } |
| 1016 | return err |
| 1017 | } |
| 1018 | |
| 1019 | // An ipc.ServerCall that prepends a prefix to all the names in the streamed |
| 1020 | // MountEntry objects. |
| 1021 | type localServerCall struct { |
| 1022 | ipc.ServerCall |
| 1023 | prefix string |
| 1024 | } |
| 1025 | |
Benjamin Prosnitz | fdfbf7b | 2014-10-08 09:47:21 -0700 | [diff] [blame] | 1026 | var _ ipc.ServerCall = (*localServerCall)(nil) |
| 1027 | var _ ipc.Stream = (*localServerCall)(nil) |
| 1028 | var _ ipc.ServerContext = (*localServerCall)(nil) |
| 1029 | |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 1030 | func (c *localServerCall) Send(v interface{}) error { |
| 1031 | me, ok := v.(mttypes.MountEntry) |
| 1032 | if !ok { |
| 1033 | return verror.BadArgf("unexpected stream type. Got %T, want MountEntry", v) |
| 1034 | } |
| 1035 | me.Name = naming.Join(c.prefix, me.Name) |
| 1036 | return c.ServerCall.Send(me) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Asim Shankar | a5457f0 | 2014-10-24 23:23:07 -0700 | [diff] [blame] | 1039 | func (fs *flowServer) authorize(auth security.Authorizer) verror.E { |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1040 | if auth == nil { |
Asim Shankar | 0c73fbf | 2014-10-31 15:34:02 -0700 | [diff] [blame] | 1041 | auth = defaultAuthorizer{} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1042 | } |
Asim Shankar | a5457f0 | 2014-10-24 23:23:07 -0700 | [diff] [blame] | 1043 | if err := auth.Authorize(fs); err != nil { |
| 1044 | // TODO(ataly, ashankar): For privacy reasons, should we hide the authorizer error? |
Asim Shankar | 0c73fbf | 2014-10-31 15:34:02 -0700 | [diff] [blame] | 1045 | return verror.NoAccessf("ipc: not authorized to call %q.%q (%v)", fs.Name(), fs.Method(), err) |
Asim Shankar | a5457f0 | 2014-10-24 23:23:07 -0700 | [diff] [blame] | 1046 | } |
| 1047 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 1050 | // debugContext is a context which wraps another context but always returns |
| 1051 | // the debug label. |
| 1052 | type debugContext struct { |
| 1053 | security.Context |
| 1054 | } |
| 1055 | |
| 1056 | func (debugContext) Label() security.Label { return security.DebugLabel } |
| 1057 | |
| 1058 | // TODO(mattr): Is DebugLabel the right thing to check? |
| 1059 | func (fs *flowServer) authorizeForDebug(auth security.Authorizer) error { |
| 1060 | dc := debugContext{fs} |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1061 | if auth == nil { |
Asim Shankar | 0c73fbf | 2014-10-31 15:34:02 -0700 | [diff] [blame] | 1062 | auth = defaultAuthorizer{} |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 1063 | } |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1064 | return auth.Authorize(dc) |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1067 | // Send implements the ipc.Stream method. |
| 1068 | func (fs *flowServer) Send(item interface{}) error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1069 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1070 | // The empty response header indicates what follows is a streaming result. |
| 1071 | if err := fs.enc.Encode(ipc.Response{}); err != nil { |
| 1072 | return err |
| 1073 | } |
| 1074 | return fs.enc.Encode(item) |
| 1075 | } |
| 1076 | |
| 1077 | // Recv implements the ipc.Stream method. |
| 1078 | func (fs *flowServer) Recv(itemptr interface{}) error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1079 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1080 | var req ipc.Request |
| 1081 | if err := fs.dec.Decode(&req); err != nil { |
| 1082 | return err |
| 1083 | } |
| 1084 | if req.EndStreamArgs { |
| 1085 | fs.endStreamArgs = true |
| 1086 | return io.EOF |
| 1087 | } |
| 1088 | return fs.dec.Decode(itemptr) |
| 1089 | } |
| 1090 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 1091 | // Implementations of ipc.ServerContext methods. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1092 | |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1093 | func (fs *flowServer) Discharges() map[string]security.Discharge { |
| 1094 | //nologcall |
| 1095 | return fs.discharges |
| 1096 | } |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 1097 | |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1098 | func (fs *flowServer) Server() ipc.Server { |
| 1099 | //nologcall |
| 1100 | return fs.server |
| 1101 | } |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 1102 | func (fs *flowServer) Timestamp() time.Time { |
| 1103 | //nologcall |
| 1104 | return fs.starttime |
| 1105 | } |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1106 | func (fs *flowServer) Method() string { |
| 1107 | //nologcall |
| 1108 | return fs.method |
| 1109 | } |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 1110 | func (fs *flowServer) MethodTags() []interface{} { |
| 1111 | //nologcall |
| 1112 | return fs.tags |
| 1113 | } |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 1114 | |
| 1115 | // TODO(cnicolaou): remove Name from ipc.ServerContext and all of |
| 1116 | // its implementations |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1117 | func (fs *flowServer) Name() string { |
| 1118 | //nologcall |
| 1119 | return fs.suffix |
| 1120 | } |
| 1121 | func (fs *flowServer) Suffix() string { |
| 1122 | //nologcall |
| 1123 | return fs.suffix |
| 1124 | } |
| 1125 | func (fs *flowServer) Label() security.Label { |
| 1126 | //nologcall |
Asim Shankar | 0cad083 | 2014-11-04 01:27:38 -0800 | [diff] [blame] | 1127 | for _, t := range fs.tags { |
| 1128 | if l, ok := t.(security.Label); ok { |
| 1129 | return l |
| 1130 | } |
| 1131 | } |
| 1132 | return security.AdminLabel |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1133 | } |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1134 | func (fs *flowServer) LocalPrincipal() security.Principal { |
| 1135 | //nologcall |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1136 | return fs.flow.LocalPrincipal() |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1137 | } |
| 1138 | func (fs *flowServer) LocalBlessings() security.Blessings { |
| 1139 | //nologcall |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1140 | return fs.flow.LocalBlessings() |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1141 | } |
| 1142 | func (fs *flowServer) RemoteBlessings() security.Blessings { |
| 1143 | //nologcall |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1144 | return fs.flow.RemoteBlessings() |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1145 | } |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1146 | func (fs *flowServer) Blessings() security.Blessings { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1147 | //nologcall |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1148 | return fs.blessings |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 1149 | } |
| 1150 | func (fs *flowServer) LocalEndpoint() naming.Endpoint { |
| 1151 | //nologcall |
| 1152 | return fs.flow.LocalEndpoint() |
| 1153 | } |
| 1154 | func (fs *flowServer) RemoteEndpoint() naming.Endpoint { |
| 1155 | //nologcall |
| 1156 | return fs.flow.RemoteEndpoint() |
| 1157 | } |