/// <summary> /// Tries to parse a Netbios Name Service packet from a buffer of bytes /// </summary> /// <param name="buffer">Byte array containing the NbNs packet</param> /// <param name="packet">Parsed NbNs packet if successful, else null</param> /// <returns><c>true</c> if parsing was successful, else <c>false</c></returns> /// <remarks> /// This method is the entry point for parsing Netbios Name Service packets. It returns an object of a class /// derived from <see cref="NbNsPacketBase"/>, which can then be casted based on <see cref="PacketType"/> to /// the respective derived class. /// </remarks> public static bool TryParse(byte[] buffer, out NbNsPacketBase packet) { packet = null; if (buffer == null || buffer.Length < NbNsHeader.NETBIOS_HEADER_LENGTH) { return(false); } NbNsHeader header; if (!NbNsHeader.TryParse(buffer, out header)) { return(false); } if (header.Opcode == NbNsHeader.OpcodeSpecifier.Query && header.IsResponse == false && header.IsRecursionDesired == false) { // Must be a Netbios Node Status Request NbNsNodeStatusRequest result; if (!NbNsNodeStatusRequest.TryParse(header, buffer, out result)) { return(false); } packet = result; } else if (header.Opcode == NbNsHeader.OpcodeSpecifier.Query && header.IsRecursionDesired == false) { // Must be a Netbios Node Status Response NbNsNodeStatusResponse result; if (!NbNsNodeStatusResponse.TryParse(header, buffer, out result)) { return(false); } packet = result; } // ToDo: Parse Further Netbios Name Service packets return(true); }
/// <summary> /// Conveience method for sending a <see cref="NbNsNodeStatusRequest"/> and receiving a <see cref="NbNsNodeStatusResponse"/> /// </summary> /// <param name="packet"><see cref="NbNsNodeStatusRequest"/> to send</param> /// <param name="address">Target address to send the <see cref="NbNsNodeStatusRequest"/> to</param> /// <returns> /// <see cref="Task"/> that completes when either a <see cref="NbNsNodeStatusResponse"/> was received (which is then the /// result value of the task) or when - given the default timeout and retry values - no response has been received /// (in which case the Task's result value is null). /// </returns> public async Task <NbNsNodeStatusResponse> SendUnicastNodeStatusRequestAsync(NbNsNodeStatusRequest packet, IPAddress address) { var response = await SendUnicastRequestAsync(packet, address); return(response as NbNsNodeStatusResponse); }