/// <summary> /// String to MPLSPackage converter /// and then String to bytes. /// </summary> /// <param name="stringPackage">String of encoded MPLSPackage</param> /// <returns>MPLSPackage created from encoded String</returns> public static MPLSPackage fromString(String stringPackage) { String[] data = stringPackage.Split('#'); MPLSPackage package = new MPLSPackage(); package.DestinationEndPoint = new IPEndPoint(IPAddress.Parse(data[0].Split(":")[0]), int.Parse(data[0].Split(":")[1])); package.StartEndPoint = new IPEndPoint(IPAddress.Parse(data[1].Split(":")[0]), int.Parse(data[1].Split(":")[1])); package.Data = data[2]; package.TTL = int.Parse(data[3]); package.StackPtr = int.Parse(data[4]); package.Port = int.Parse(data[5]); int labelCounter = int.Parse(data[6]); List <int> labelList = new List <int>(); string[] labelElements = data[7].Split(":"); for (int i = 0; i < labelCounter; i++) { labelList.Add(int.Parse(labelElements[i])); } package.Label = new List <int>(labelList); return(package); }
/// <summary> /// Converts bytes to <c>MPLSPackage</c> /// </summary> /// <param name="bytes">Received stream of bytes</param> /// <returns>Converted package</returns> /// <exception cref="InvalidMPLSPackageException">Thrown when a stream of bytes is not a valid package</exception> public static MPLSPackage FromBytes(byte[] bytes) { try { MPLSPackage package = new MPLSPackage(); package.LabelStack = LabelStack.FromBytes(bytes); var stackLength = package.LabelStack.GetLength(); package.ID = BitConverter.ToInt32(bytes, stackLength); package.PacketLength = BitConverter.ToInt32(bytes, stackLength + 4); package.TTL = (ushort)((bytes[stackLength + 9] << 8) + bytes[stackLength + 8]); package.SourceAddress = new IPAddress(new byte[] { bytes[stackLength + 10], bytes[stackLength + 11], bytes[stackLength + 12], bytes[stackLength + 13] }); package.DestAddress = new IPAddress(new byte[] { bytes[stackLength + 14], bytes[stackLength + 15], bytes[stackLength + 16], bytes[stackLength + 17] }); package.Port = (ushort)((bytes[stackLength + 19] << 8) + bytes[stackLength + 18]); var usefulPayload = new List <byte>(); usefulPayload.AddRange(bytes.ToList() .GetRange(stackLength + 20, package.PacketLength - HeaderLength)); package.Payload = Encoding.ASCII.GetString(usefulPayload.ToArray()); return(package); } catch (Exception) { throw new InvalidMPLSPackageException(); } }
/// <summary> /// Prepare MPLSPackage to send via UDP/TCP /// To send via UDP, use MPLSPackage_(mpls).toBytes() /// </summary> /// <param name="mpls"></param> /// <returns>Package object ready to send by UDP/TCP after converting to bytes</returns> public static Package MPLSPackage_(MPLSPackage mpls) { Package package = new Package(); package.Key = "MPLS"; package.data = mpls.toString(); return(package); }
public static void LogsCableCloudReceivePackage(MPLSPackage package) { String log; log = $"({getActualTime()}) [MPLS PACKAGE RECEIVE] PORT: {package.GetPort()})"; Console.WriteLine(log); WriteToFile(log, "CableCloud"); }
public static void LogsRouterReceivePackage(MPLSPackage package, String Routername) { String log; log = $"({getActualTime()}) [MPLS PACKAGE RECEIVE] LABELS: {package.ShowAllLabels()} PORT: {package.GetPort()}"; Console.WriteLine(log); WriteToFile(log, Routername); }
public static void LogsHostReceivePackage(MPLSPackage package, String Hostname) { String log; log = $"({getActualTime()}) [MPLS PACKAGE RECEIVE] '{package.GetData()}' FROM: {package.GetStartAddress().ToString()}"; Console.WriteLine(log); WriteToFile(log, Hostname); }
public static void LogsHostSendPackage(MPLSPackage mpls, String destHost, String HostName) { String log; log = $"({getActualTime()}) [MPLS PACKAGE SEND] '{mpls.GetData()}' TO: {destHost} <{mpls.GetDestinationEndPoint().ToString()}> LABEL: {mpls.ShowAllLabels()}"; Console.WriteLine(log); WriteToFile(log, HostName); }
/// <summary> /// Receives stream of bytes from the connected socket /// </summary> /// <returns>Package, that has been received</returns> public MPLSPackage Receive() { var buffer = new byte[256]; int bytes = Receive(buffer); // probably the string KEEPALIVE if (Encoding.ASCII.GetString(buffer, 0, bytes).Substring(0, 9).Equals("KEEPALIVE")) { return(null); } return(MPLSPackage.FromBytes(buffer)); }
/// <summary> /// Converts <c>package</c> to bytes and sends it to connected socket /// </summary> /// <param name="package">Package that is about to be sent</param> /// <returns>The number of bytes sent</returns> public int Send(MPLSPackage package) { return(Send(package.ToBytes())); }
/// <summary> /// Create MPLS package from opend Package /// </summary> /// <param name="package">Package previously opened</param> /// <returns>MPLSPackage object</returns> public static MPLSPackage ReceiveMPLSPackage(Package package) { return(MPLSPackage.fromString(package.data)); }
/// <summary> /// Create MPLS package from bytes received from UDP/TCP client /// </summary> /// <param name="mpls">Bytes containing MPLSPackage</param> /// <returns>MPLSPackage object</returns> public static MPLSPackage ReceiveMPLSPackage(byte[] mpls) { Package package = Package.fromBytes(mpls); return(MPLSPackage.fromString(package.data)); }