/// <summary> /// The NetBIOS naming convention allows for 16 characters in a NetBIOS name. /// Microsoft, however, limits NetBIOS names to 15 characters and uses the 16th character as a NetBIOS suffix /// See http://support.microsoft.com/kb/163409/en-us /// </summary> public static string GetMSNetBiosName(string name, NetBiosSuffix suffix) { if (name.Length > 15) { name = name.Substring(0, 15); } else if (name.Length < 15) { name = name.PadRight(15); } return(name + (char)suffix); }
public string GetServerName() { NodeStatusRequest request = new NodeStatusRequest(); request.Header.QDCount = 1; request.Question.Name = "*".PadRight(16, '\0'); NodeStatusResponse response = SendNodeStatusRequest(request); foreach (KeyValuePair <string, NameFlags> entry in response.Names) { NetBiosSuffix suffix = NetBiosUtils.GetSuffixFromMSNetBiosName(entry.Key); if (suffix == NetBiosSuffix.FileServiceService) { return(entry.Key); } } return(null); }
private void ReceiveCallback(IAsyncResult result) { if (!m_listening) { return; } IPEndPoint remoteEP = null; byte[] buffer; try { buffer = m_client.EndReceive(result, ref remoteEP); } catch (ObjectDisposedException) { return; } catch (SocketException) { return; } // Process buffer if (buffer.Length > NameServicePacketHeader.Length) { NameServicePacketHeader header = new NameServicePacketHeader(buffer, 0); if (header.OpCode == NameServiceOperation.QueryRequest) { NameQueryRequest request = null; try { request = new NameQueryRequest(buffer, 0); } catch { } if (request != null) { if (request.Question.Type == NameRecordType.NB) { string name = NetBiosUtils.GetNameFromMSNetBiosName(request.Question.Name); NetBiosSuffix suffix = (NetBiosSuffix)request.Question.Name[15]; bool nameMatch = String.Equals(name, Environment.MachineName, StringComparison.OrdinalIgnoreCase); if (nameMatch && ((suffix == NetBiosSuffix.WorkstationService) || (suffix == NetBiosSuffix.FileServiceService))) { PositiveNameQueryResponse response = new PositiveNameQueryResponse(); response.Header.TransactionID = request.Header.TransactionID; response.Resource.Name = request.Question.Name; NameFlags nameFlags = new NameFlags(); response.Addresses.Add(m_serverAddress.GetAddressBytes(), nameFlags); byte[] responseBytes = response.GetBytes(); m_client.Send(responseBytes, responseBytes.Length, remoteEP); } } else // NBStat { NodeStatusResponse response = new NodeStatusResponse(); response.Header.TransactionID = request.Header.TransactionID; response.Resource.Name = request.Question.Name; NameFlags nameFlags = new NameFlags(); string name1 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService); string name2 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.FileServiceService); NameFlags nameFlags3 = new NameFlags(); nameFlags3.WorkGroup = true; string name3 = NetBiosUtils.GetMSNetBiosName(WorkgroupName, NetBiosSuffix.WorkstationService); response.Names.Add(name1, nameFlags); response.Names.Add(name2, nameFlags); response.Names.Add(name3, nameFlags3); byte[] responseBytes = response.GetBytes(); try { m_client.Send(responseBytes, responseBytes.Length, remoteEP); } catch (ObjectDisposedException) { } } } } } try { m_client.BeginReceive(ReceiveCallback, null); } catch (ObjectDisposedException) { } catch (SocketException) { } }
public NameRegistrationRequest(string machineName, NetBiosSuffix suffix, IPAddress address) : this() { Question.Name = NetBiosUtils.GetMSNetBiosName(machineName, suffix); Address = address.GetAddressBytes(); }
public static byte[] EncodeName(string name, NetBiosSuffix suffix, string scopeID) { string netBiosName = GetMSNetBiosName(name, suffix); return(EncodeName(netBiosName, scopeID)); }
/// <summary> /// The NetBIOS naming convention allows for 16 characters in a NetBIOS name. /// Microsoft, however, limits NetBIOS names to 15 characters and uses the 16th character as a NetBIOS suffix /// See http://support.microsoft.com/kb/163409/en-us /// </summary> public static string GetMSNetBiosName(string name, NetBiosSuffix suffix) { if (name.Length > 15) { name = name[..15];