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