/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. */ using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using Utilities; namespace SMBLibrary.NetBios { /// /// [RFC 1002] 4.2.2. NAME REGISTRATION REQUEST /// public class NameRegistrationRequest { public const int DataLength = 6; public NameServicePacketHeader Header; public QuestionSection Question; public ResourceRecord Resource; public NameFlags NameFlags; public byte[] Address; // IPv4 address public NameRegistrationRequest() { Header = new NameServicePacketHeader(); Header.OpCode = NameServiceOperation.RegistrationRequest; Header.QDCount = 1; Header.ARCount = 1; Header.Flags = OperationFlags.Broadcast | OperationFlags.RecursionDesired; Question = new QuestionSection(); Resource = new ResourceRecord(); Address = new byte[4]; } public NameRegistrationRequest(string machineName, NetBiosSuffix suffix, IPAddress address) : this() { Question.Name = NetBiosUtils.GetMSNetBiosName(machineName, suffix); Address = address.GetAddressBytes(); } public byte[] GetBytes() { Resource.Data = GetData(); MemoryStream stream = new MemoryStream(); Header.WriteBytes(stream); Question.WriteBytes(stream); Resource.WriteBytes(stream, NameServicePacketHeader.Length); return stream.ToArray(); } public byte[] GetData() { byte[] data = new byte[DataLength]; BigEndianWriter.WriteUInt16(data, 0, (ushort)NameFlags); ByteWriter.WriteBytes(data, 2, Address, 4); return data; } } }