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