Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package xproxyd |
| 6 | |
| 7 | import ( |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 8 | "v.io/v23" |
| 9 | "v.io/v23/context" |
| 10 | "v.io/v23/flow" |
| 11 | "v.io/v23/flow/message" |
| 12 | "v.io/v23/naming" |
| 13 | "v.io/v23/security" |
| 14 | ) |
| 15 | |
| 16 | // setEndpointRoutingID returns a copy of ep with RoutingId changed to rid. |
| 17 | func setEndpointRoutingID(ep naming.Endpoint, rid naming.RoutingID) (naming.Endpoint, error) { |
| 18 | network, address, routes, _, bnames, mountable := getEndpointParts(ep) |
| 19 | opts := routes |
| 20 | opts = append(opts, bnames...) |
| 21 | opts = append(opts, rid) |
| 22 | opts = append(opts, mountable) |
| 23 | epString := naming.FormatEndpoint(network, address, opts...) |
| 24 | return v23.NewEndpoint(epString) |
| 25 | } |
| 26 | |
| 27 | // setEndpointRoutes returns a copy of ep with Routes changed to routes. |
| 28 | func setEndpointRoutes(ep naming.Endpoint, routes []string) (naming.Endpoint, error) { |
| 29 | network, address, _, rid, bnames, mountable := getEndpointParts(ep) |
| 30 | opts := routeOpts(routes) |
| 31 | opts = append(opts, bnames...) |
| 32 | opts = append(opts, rid) |
| 33 | opts = append(opts, mountable) |
| 34 | epString := naming.FormatEndpoint(network, address, opts...) |
| 35 | return v23.NewEndpoint(epString) |
| 36 | } |
| 37 | |
| 38 | // getEndpointParts returns all the fields of ep. |
| 39 | func getEndpointParts(ep naming.Endpoint) (network string, address string, |
| 40 | routes []naming.EndpointOpt, rid naming.RoutingID, |
| 41 | blessingNames []naming.EndpointOpt, mountable naming.EndpointOpt) { |
| 42 | network, address = ep.Addr().Network(), ep.Addr().String() |
| 43 | routes = routeOpts(ep.Routes()) |
| 44 | rid = ep.RoutingID() |
| 45 | blessingNames = blessingOpts(ep.BlessingNames()) |
| 46 | mountable = naming.ServesMountTable(ep.ServesMountTable()) |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | func routeOpts(routes []string) []naming.EndpointOpt { |
| 51 | var routeOpts []naming.EndpointOpt |
| 52 | for _, route := range routes { |
| 53 | routeOpts = append(routeOpts, naming.RouteOpt(route)) |
| 54 | } |
| 55 | return routeOpts |
| 56 | } |
| 57 | |
| 58 | func blessingOpts(blessingNames []string) []naming.EndpointOpt { |
| 59 | var blessingOpts []naming.EndpointOpt |
| 60 | for _, b := range blessingNames { |
| 61 | blessingOpts = append(blessingOpts, naming.BlessingOpt(b)) |
| 62 | } |
| 63 | return blessingOpts |
| 64 | } |
| 65 | |
| 66 | type proxyBlessingsForPeer struct{} |
| 67 | |
| 68 | // TODO(suharshs): Figure out what blessings to present here. And present discharges. |
| 69 | func (proxyBlessingsForPeer) run(ctx *context.T, lep, rep naming.Endpoint, rb security.Blessings, |
| 70 | rd map[string]security.Discharge) (security.Blessings, map[string]security.Discharge, error) { |
| 71 | return v23.GetPrincipal(ctx).BlessingStore().Default(), nil, nil |
| 72 | } |
| 73 | |
Suharsh Sivakumar | 9814377 | 2015-09-10 11:40:53 -0700 | [diff] [blame] | 74 | func writeMessage(ctx *context.T, m message.Message, f flow.Flow) error { |
| 75 | w, err := message.Append(ctx, m, nil) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | _, err = f.WriteMsg(w) |
| 80 | return err |
| 81 | } |
Suharsh Sivakumar | 9814377 | 2015-09-10 11:40:53 -0700 | [diff] [blame] | 82 | |
| 83 | func readMessage(ctx *context.T, f flow.Flow) (message.Message, error) { |
| 84 | b, err := f.ReadMsg() |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return message.Read(ctx, b) |
| 89 | } |