public void scan(string queryString, mDNSType queryType) { byte[] data; byte[] query; mDNSHeader header; byte[] headerBytes; IPEndPoint endpoint; header = new mDNSHeader() { transactionID = 0, flags = 0, questions = NetworkUtils.NetworkToHostOrder16(1), answerRRs = 0, authorityRRs = 0, additionalRRs = 0 }; query = buildQuery(queryString, queryType); headerBytes = header.GetBytes(); data = new byte[headerBytes.Length + query.Length]; headerBytes.CopyTo(data, 0); query.CopyTo(data, headerBytes.Length); endpoint = new IPEndPoint(IPAddress.Parse(multicastIP), port); if (globalListener.inUse) { try { Logger.getInstance().WriteNet(Logger.DebugLevel.Debug, globalListener.endPoint, endpoint, ProtocolType.Udp, data); globalListener.udp.Send(data, data.Length, endpoint); } catch { Logger.getInstance().WriteLine(Logger.DebugLevel.Error, String.Format("Error: mDNS.scan(): Unable to send data to {0}!", endpoint.ToString())); } } if (interfacesListerner != null) { foreach (networkBundle net in interfacesListerner) { if (net.inUse) { try { Logger.getInstance().WriteNet(Logger.DebugLevel.Debug, net.endPoint, endpoint, ProtocolType.Udp, data); net.udp.Send(data, data.Length, endpoint); } catch { Logger.getInstance().WriteLine(Logger.DebugLevel.Error, String.Format("Error: mDNS.scan(): Unable to send data to {0}!", endpoint.ToString())); } } } } }
private byte[] buildQuery(string queryString, mDNSType queryType) { byte[] result; string[] subDomains; int querySize, i; IdnMapping idn = new IdnMapping(); subDomains = idn.GetAscii(queryString).Split('.'); // compute size of byte needed querySize = 0; foreach (var d in subDomains) { querySize += 1 + Encoding.ASCII.GetBytes(d).Length; } querySize++; // NULL string termination; querySize += 4; // type + questionClass; result = new byte[querySize]; // generate query i = 0; foreach (var d in subDomains) { byte[] currentSubDomain; result[i] = (byte)d.Length; i++; currentSubDomain = Encoding.ASCII.GetBytes(d); currentSubDomain.CopyTo(result, i); i += currentSubDomain.Length; } result[i++] = 0x00; // NULL string termination; result[i] = (byte)((((UInt16)queryType) << 8) & 0xff); result[i + 1] = (byte)(((UInt16)queryType) & 0xff); i += 2; result[i++] = (byte)((mDNSQuestionClass << 8) & 0xff); // mDNSQuestionClass result[i++] = (byte)((mDNSQuestionClass) & 0xff); return(result); }