static async Task ProcessMessage(BmpMessage msg) { if (msg.BmpHeader.MessageType == BmpMessageType.RouteMonitoring) { if (((RouteMonitoring)msg).BgpUpdate.EndOfRib) { var fileName = Path.GetRandomFileName(); var logFile = File.Create(fileName); using (var sw = new StreamWriter(logFile)) { foreach (var route in routes) { foreach (var nlri in route.BgpUpdate.Nlri) { var prefix = nlri.ToString(); var nextHop = route.BgpUpdate.Attributes.FirstOrDefault(x => x.AttributeType == PathAttributeType.NextHop); var asPath = route.BgpUpdate.Attributes.FirstOrDefault(x => x.AttributeType == PathAttributeType.AsPath); var asnList = string.Join(" ", ((PathAttributeASPath)asPath).ASPaths[0].ASNs.Distinct().ToList()); sw.WriteLine($"{{\"Prefix\" : \"{prefix}\", \"NextHop\": \"{((PathAttributeNextHop)nextHop).NextHop.ToString()}\", \"AS Path\": \"{asnList}\"}},"); } } } Environment.Exit(0); } if ((((RouteMonitoring)msg).BgpUpdate.Nlri.Count > 0)) { routes.Add((RouteMonitoring)msg); } } }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { try { var length = input.ReadableBytes; if (length < 6) { return; } // check bytes var data = new byte[length]; input.ReadBytes(data); var bmpMessage = BmpMessage.Create(data); if (bmpMessage != null) { output.Add(bmpMessage); } } catch (DecoderException) { input.SkipBytes(input.ReadableBytes); throw; } }
public static string ToJson(this BmpMessage msg) { string json; switch (msg.BmpHeader.MessageType) { case BmpMessageType.PeerUp: var peerUpNotificationModel = ConvertToModel(msg as PeerUpNotification); json = JsonConvert.SerializeObject(peerUpNotificationModel); break; case BmpMessageType.RouteMonitoring: var routeMonitoringModel = ConvertToModel(msg as RouteMonitoring); json = JsonConvert.SerializeObject(routeMonitoringModel); break; default: return(null); } return(json); }