|
| 1 | +package pan |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | +) |
| 7 | + |
| 8 | +// socket roles (selector internals) |
| 9 | +const dialer = 0 |
| 10 | +const listener = 1 |
| 11 | + |
| 12 | +/* |
| 13 | +! |
| 14 | +\brief this class provides the service of path selection for a remote address to scion-sockets |
| 15 | +Every scion-socket has one. |
| 16 | +
|
| 17 | + It should not be cluttered with Refresh()/Update()/PathDown or any other technical |
| 18 | + methods that are required by the pathStatedDB or pathPool to update the selectors state. |
| 19 | +*/ |
| 20 | +type CombiSelector interface { |
| 21 | + Close() error |
| 22 | + // PathDown(PathFingerprint, PathInterface) // pathDownNotifyee |
| 23 | + |
| 24 | + // called when the scion-socket is bound to an address |
| 25 | + LocalAddrChanged(newlocal UDPAddr) |
| 26 | + Path(remote UDPAddr) (*Path, error) |
| 27 | + //Refresh([]*Path) Selector |
| 28 | + // Refresh(paths []*Path, remote UDPAddr) |
| 29 | + Record(remote UDPAddr, path *Path) |
| 30 | + //Update( )# |
| 31 | + // setter the respective defaults |
| 32 | + SetReplySelector(ReplySelector) |
| 33 | + SetSelector(sel func() Selector) |
| 34 | + SetPolicy(pol func() Policy) |
| 35 | + |
| 36 | + // setter for AS specific path policies |
| 37 | + // SetSelectorFor(remote IA, sel Selector) |
| 38 | + // SetPolicyFor(remote IA, pol Policy) |
| 39 | + // SetPolicedSelectorFor(remote IA, sel Selector, pol Policy) |
| 40 | + |
| 41 | + // initialize(local UDPAddr, remote UDPAddr, paths []*Path) |
| 42 | + // maybe make this pubilc and let the ScionCocketCall it, |
| 43 | + // in its ctor (or once the local addr is known i.e after Bind() was called ) |
| 44 | +} |
| 45 | + |
| 46 | +type DefaultCombiSelector struct { |
| 47 | + local UDPAddr |
| 48 | + roles map[UDPAddr]int // is this host the dialer or listener for the connection to this remote host |
| 49 | + // decided from which method is called first for a remote address X |
| 50 | + // Record(X)->listener or Path(X)->dialer |
| 51 | + |
| 52 | + // maybe make map of pair (, ,) ?! |
| 53 | + // policies map[UDPAddr]Policy |
| 54 | + policy_factory func() Policy |
| 55 | + selector_factory func() Selector |
| 56 | + |
| 57 | + // TODO: this state should be confined in size somehow |
| 58 | + // i.e. drop selectors with LRU scheme |
| 59 | + // Note that this is not an attack vector, as this state can only be increased |
| 60 | + // by deliberate decisions of this host to dial a remote for which it does not yet has a selector |
| 61 | + selectors map[IA]Selector |
| 62 | + subscribers map[IA]*pathRefreshSubscriber |
| 63 | + replyselector ReplySelector |
| 64 | +} |
| 65 | + |
| 66 | +func (s *DefaultCombiSelector) needPathTo(remote UDPAddr) bool { |
| 67 | + return s.local.IA != remote.IA |
| 68 | +} |
| 69 | + |
| 70 | +func (s *DefaultCombiSelector) Close() error { |
| 71 | + |
| 72 | + for _, v := range s.subscribers { |
| 73 | + if e := v.Close(); e != nil { |
| 74 | + return e |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for _, v := range s.selectors { |
| 79 | + if e := v.Close(); e != nil { |
| 80 | + return e |
| 81 | + } |
| 82 | + } |
| 83 | + if e := s.replyselector.Close(); e != nil { |
| 84 | + return e |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func NewDefaultCombiSelector(local UDPAddr) (CombiSelector, error) { |
| 91 | + selector := &DefaultCombiSelector{ |
| 92 | + local: local, |
| 93 | + roles: make(map[UDPAddr]int), |
| 94 | + // policies: make(map[UDPAddr]Policy), |
| 95 | + policy_factory: func() Policy { |
| 96 | + var s Policy = nil |
| 97 | + return s |
| 98 | + }, |
| 99 | + selector_factory: func() Selector { return NewDefaultSelector() }, |
| 100 | + selectors: make(map[IA]Selector), |
| 101 | + subscribers: make(map[IA]*pathRefreshSubscriber), |
| 102 | + replyselector: NewDefaultReplySelector(), |
| 103 | + } |
| 104 | + |
| 105 | + selector.replyselector.Initialize(local) |
| 106 | + |
| 107 | + return selector, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (s *DefaultCombiSelector) SetReplySelector(rep ReplySelector) { |
| 111 | + s.replyselector = rep |
| 112 | +} |
| 113 | + |
| 114 | +func (s *DefaultCombiSelector) LocalAddrChanged(newlocal UDPAddr) { |
| 115 | + s.local = newlocal |
| 116 | +} |
| 117 | + |
| 118 | +func (s *DefaultCombiSelector) Path(remote UDPAddr) (*Path, error) { |
| 119 | + if r, ok := s.roles[remote]; ok { |
| 120 | + // the role is already decided |
| 121 | + if r == dialer { |
| 122 | + if s.needPathTo(remote) { |
| 123 | + sel := s.selectors[remote.IA] |
| 124 | + sel.NewRemote(remote) |
| 125 | + return sel.Path(remote) |
| 126 | + } else { |
| 127 | + return nil, errors.New("if src and dst are in same AS and no scion path is required, the connection shouldnt request one") |
| 128 | + } |
| 129 | + } else { |
| 130 | + return s.replyselector.Path(remote) |
| 131 | + } |
| 132 | + } else { |
| 133 | + // no role yet -> no path to remote has been requested yet Path() |
| 134 | + // so we are acting as a server |
| 135 | + s.roles[remote] = dialer |
| 136 | + |
| 137 | + // set up a refresherSubscriber etc .. |
| 138 | + if s.needPathTo(remote) { |
| 139 | + var selector Selector |
| 140 | + var policy Policy |
| 141 | + |
| 142 | + if s.policy_factory != nil { |
| 143 | + policy = s.policy_factory() |
| 144 | + } |
| 145 | + if s.selector_factory != nil { |
| 146 | + selector = s.selector_factory() |
| 147 | + } else { |
| 148 | + selector = NewDefaultSelector() |
| 149 | + } |
| 150 | + var ctx context.Context = context.Background() |
| 151 | + // Todo: set timeout for path request |
| 152 | + subscriber, err := openPathRefreshSubscriber(ctx, s.local, remote, policy, selector) |
| 153 | + if err != nil { |
| 154 | + |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + s.selectors[remote.IA] = selector |
| 158 | + s.subscribers[remote.IA] = subscriber |
| 159 | + |
| 160 | + return selector.Path(remote) |
| 161 | + } else { |
| 162 | + return nil, errors.New("if src and dst are in same AS and no scion path is required, the connection shouldnt request one") |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (s *DefaultCombiSelector) SetPolicy(pol func() Policy) { |
| 168 | + s.policy_factory = pol |
| 169 | +} |
| 170 | + |
| 171 | +func (s *DefaultCombiSelector) SetSelector(sel func() Selector) { |
| 172 | + s.selector_factory = sel |
| 173 | +} |
| 174 | + |
| 175 | +func (s *DefaultCombiSelector) Record(remote UDPAddr, path *Path) { |
| 176 | + |
| 177 | + if r, ok := s.roles[remote]; ok { |
| 178 | + // the role is already decided |
| 179 | + if r == listener { |
| 180 | + s.replyselector.Record(remote, path) |
| 181 | + } |
| 182 | + } else { |
| 183 | + // no role yet -> no path to remote has been requested yet Path() |
| 184 | + // so we are acting as a server |
| 185 | + s.roles[remote] = listener |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments