@@ -23,7 +23,7 @@ public sealed class BgpSession : IDisposable
2323 private readonly CancellationTokenSource _cts = new ( ) ;
2424 private readonly Action < string , uint > ? _onPeerIdentified ;
2525 private readonly PeerStore ? _peerStore ;
26- private readonly RipeStatProvider ? _ripeStatProvider ;
26+ private readonly PrefixService ? _prefixService ;
2727 private readonly AppConfig ? _appConfig ;
2828
2929 private BgpFsmState _state = BgpFsmState . Idle ;
@@ -47,7 +47,7 @@ public BgpSession(
4747 ILogger < BgpSession > logger ,
4848 Action < string , uint > ? onPeerIdentified = null ,
4949 PeerStore ? peerStore = null ,
50- RipeStatProvider ? ripeStatProvider = null ,
50+ PrefixService ? prefixService = null ,
5151 AppConfig ? appConfig = null )
5252 {
5353 _socket = socket ;
@@ -60,7 +60,7 @@ public BgpSession(
6060 _logger = logger ;
6161 _onPeerIdentified = onPeerIdentified ;
6262 _peerStore = peerStore ;
63- _ripeStatProvider = ripeStatProvider ;
63+ _prefixService = prefixService ;
6464 _appConfig = appConfig ;
6565 }
6666
@@ -270,44 +270,40 @@ private async Task SendAllRoutesAsync()
270270 var nextHop = BgpConstants . IPAddressToUint ( _bgpConfig . GetRouterIdAddress ( ) ) ;
271271 var routes = new List < Route > ( ) ;
272272
273- // Try dynamic route fetching from peer subscriptions
274- if ( _peerStore is not null && _ripeStatProvider is not null && _appConfig is not null )
273+ if ( _peerStore is not null && _prefixService is not null && _appConfig is not null )
275274 {
276275 var peer = _peerStore . GetPeerByIp ( _peerConfig . Address ) ;
277276 if ( peer is not null )
278277 {
279278 _peerStore . UpdateSessionStatus ( _peerConfig . Address , true ) ;
280279
281280 var subscriptionNames = _peerStore . GetSubscriptions ( peer . Id ) ;
282- var asnLists = _appConfig . RipeStat ? . AsnLists
281+ var asns = _appConfig . RipeStat ? . AsnLists
283282 . Where ( l => subscriptionNames . Contains ( l . Name ) )
283+ . SelectMany ( l => l . Asns )
284284 . ToList ( ) ?? [ ] ;
285285
286- // Fetch prefixes for each subscribed AS list
287- foreach ( var asnList in asnLists )
286+ if ( asns . Count > 0 )
288287 {
289- foreach ( var asn in asnList . Asns )
288+ try
290289 {
291- try
290+ var prefixes = await _prefixService . GetPrefixesForAsns ( asns ) ;
291+ foreach ( var ( prefix , length , asn ) in prefixes )
292292 {
293- var prefixes = await _ripeStatProvider . GetPrefixesAsync ( asn ) ;
294- foreach ( var ( prefix , length ) in prefixes )
293+ routes . Add ( new Route
295294 {
296- routes . Add ( new Route
297- {
298- Prefix = prefix ,
299- PrefixLength = length ,
300- NextHop = nextHop ,
301- AsPath = [ asn ]
302- } ) ;
303- }
304- _logger . LogInformation ( "Fetched {Count} prefixes for AS{Asn} ({List}) for {Peer}" ,
305- prefixes . Count , asn , asnList . Name , _peerConfig . Address ) ;
306- }
307- catch ( Exception ex )
308- {
309- _logger . LogError ( ex , "Failed to fetch prefixes for AS{Asn} for {Peer}" , asn , _peerConfig . Address ) ;
295+ Prefix = prefix ,
296+ PrefixLength = length ,
297+ NextHop = nextHop ,
298+ AsPath = [ asn ]
299+ } ) ;
310300 }
301+ _logger . LogInformation ( "Fetched {Count} prefixes for {Peer} from subscriptions" ,
302+ routes . Count , _peerConfig . Address ) ;
303+ }
304+ catch ( Exception ex )
305+ {
306+ _logger . LogError ( ex , "Failed to fetch prefixes for {Peer}" , _peerConfig . Address ) ;
311307 }
312308 }
313309
@@ -327,6 +323,46 @@ private async Task SendAllRoutesAsync()
327323 } ) ;
328324 }
329325
326+ if ( routes . Count > 0 )
327+ {
328+ await SendRoutesAsync ( nextHop , routes ) ;
329+ return ;
330+ }
331+ }
332+ else
333+ {
334+ // Unknown peer — auto-register and send default RU list
335+ _logger . LogInformation ( "Unknown peer {Ip}, auto-registering with RU defaults" , _peerConfig . Address ) ;
336+
337+ _peerStore . CreatePeer ( _peerConfig . Address , _remoteAsn , null ) ;
338+
339+ var ruAsns = _appConfig . RipeStat ? . AsnLists
340+ . Where ( l => l . Country == "RU" )
341+ . SelectMany ( l => l . Asns )
342+ . ToList ( ) ;
343+
344+ if ( ruAsns is { Count : > 0 } )
345+ {
346+ try
347+ {
348+ var prefixes = await _prefixService . GetPrefixesForAsns ( ruAsns ) ;
349+ foreach ( var ( prefix , length , asn ) in prefixes )
350+ {
351+ routes . Add ( new Route
352+ {
353+ Prefix = prefix ,
354+ PrefixLength = length ,
355+ NextHop = nextHop ,
356+ AsPath = [ asn ]
357+ } ) ;
358+ }
359+ }
360+ catch ( Exception ex )
361+ {
362+ _logger . LogError ( ex , "Failed to fetch RU prefixes for {Peer}" , _peerConfig . Address ) ;
363+ }
364+ }
365+
330366 if ( routes . Count > 0 )
331367 {
332368 await SendRoutesAsync ( nextHop , routes ) ;
@@ -335,7 +371,7 @@ private async Task SendAllRoutesAsync()
335371 }
336372 }
337373
338- // Fallback : send from shared route table
374+ // Final fallback : send from shared route table
339375 var tableRoutes = _routeTable . GetAll ( ) ;
340376 if ( tableRoutes . Count == 0 ) return ;
341377
0 commit comments