Skip to content

Commit

Permalink
DNS-SD, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 7, 2024
1 parent 6ee1769 commit 4b4472c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions Protest/Protocols/Mdns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using static Protest.Protocols.Dns;
Expand All @@ -13,6 +12,9 @@ namespace Protest.Protocols;

internal class Mdns {

private const string MulticastAddress = "224.0.0.251";
private const int MulticastPort = 5353;

public static byte[] Resolve(Dictionary<string, string> parameters) {
if (parameters is null) {
return Data.CODE_INVALID_ARGUMENT.Array;
Expand All @@ -28,7 +30,7 @@ public static byte[] Resolve(Dictionary<string, string> parameters) {

timeout = Math.Max(timeout, 500);

RecordType type = type = Uri.UnescapeDataString(typeString) switch {
RecordType type = Uri.UnescapeDataString(typeString) switch {
"A" => RecordType.A,
"NS" => RecordType.NS,
"CNAME" => RecordType.CNAME,
Expand All @@ -46,19 +48,19 @@ public static byte[] Resolve(Dictionary<string, string> parameters) {
}

public static byte[] Resolve(string queryString, int timeout = 3000, RecordType type = RecordType.A) {
IPAddress multicastAddress = IPAddress.Parse("224.0.0.251");
IPEndPoint remoteEndPoint = new IPEndPoint(multicastAddress, 5353);
IPAddress multicastAddress = IPAddress.Parse(MulticastAddress);
IPEndPoint remoteEndPoint = new IPEndPoint(multicastAddress, MulticastPort);

byte[] query = ConstructQuery(queryString, type);

List<byte[]> receivedData = new List<byte[]>();

IPAddress[] nics = IpTools.GetIpAddresses();
for (int i = 0; i < nics.Length && receivedData.Count == 0; i++) {
if (IPAddress.IsLoopback(nics[i])) { continue; }
if (IPAddress.IsLoopback(nics[i])) { continue; }

IPAddress localAddress = nics[i];
IPEndPoint localEndPoint = new IPEndPoint(localAddress, 5353);
IPEndPoint localEndPoint = new IPEndPoint(localAddress, MulticastPort);

try {
using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Expand All @@ -85,7 +87,7 @@ public static byte[] Resolve(string queryString, int timeout = 3000, RecordType
catch { }
}
}
catch {}
catch { }
}

try {
Expand Down Expand Up @@ -114,7 +116,6 @@ private static byte[] ConstructQuery(string queryString, RecordType type) {
}
len += 5; //1[null] + 2[type] + 2[class]


byte[] query = new byte[len];

//transaction id
Expand All @@ -123,24 +124,24 @@ private static byte[] ConstructQuery(string queryString, RecordType type) {
query[1] = (byte)rand.Next(0, 255);

//flags
query[2] = (byte)0x00;
query[3] = (byte)0x00;
query[2] = 0x00;
query[3] = 0x00;

//questions
query[4] = (byte)0x00;
query[5] = (byte)0x01;
query[4] = 0x00;
query[5] = 0x01;

//answer RRs
query[6] = (byte)0x00;
query[7] = (byte)0x00;
query[6] = 0x00;
query[7] = 0x00;

//authority RRs
query[8] = (byte)0x00;
query[9] = (byte)0x00;
query[8] = 0x00;
query[9] = 0x00;

//additional RRs
query[10] = (byte)0x00;
query[11] = (byte)0x00;
query[10] = 0x00;
query[11] = 0x00;

//question
short index = 12;
Expand Down Expand Up @@ -318,7 +319,6 @@ private static byte[] Serialize(byte[] query, List<byte[]> receivedData, List<An
}
builder.Append("],");


builder.Append("\"answer\":[");
int count = 0;
for (int i = 0; i < answers.Count; i++) {
Expand Down

0 comments on commit 4b4472c

Please sign in to comment.