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