Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Nov 25, 2013
1 parent 5912ffe commit 011af0a
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 333 deletions.
1 change: 0 additions & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
38 changes: 17 additions & 21 deletions Tmds/MDns/DnsMessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

Expand Down Expand Up @@ -81,15 +80,15 @@ public DnsMessageReader(Stream stream)

public Header ReadHeader()
{
ushort transactionID = ReadUInt16();
ushort transactionId = ReadUInt16();
ushort flags = ReadUInt16();
ushort questionCount = ReadUInt16();
ushort answerCount = ReadUInt16();
ushort authorityCount = ReadUInt16();
ushort additionalCount = ReadUInt16();

return new Header {
TransactionID = transactionID,
TransactionID = transactionId,
Flags = flags,
QuestionCount = questionCount,
AnswerCount = answerCount,
Expand All @@ -114,7 +113,7 @@ public Question ReadQuestion()

public RecordHeader ReadRecordHeader()
{
if (NextRecord > 0)
if (_nextRecord > 0)
{
SkipRecordBytes();
}
Expand All @@ -124,10 +123,10 @@ public RecordHeader ReadRecordHeader()
ushort _class = ReadUInt16();
uint ttl = ReadUInt32();
ushort rdLength = ReadUInt16();
RecordLength = rdLength;
NextRecord = (int)(BaseStream.Position + rdLength);
_recordLength = rdLength;
_nextRecord = (int)(BaseStream.Position + rdLength);

return new RecordHeader()
return new RecordHeader
{
Name = name,
Type = (RecordType)type,
Expand All @@ -139,24 +138,24 @@ public RecordHeader ReadRecordHeader()

public byte[] ReadRecordBytes()
{
return ReadBytes(RecordLength);
return ReadBytes(_recordLength);
}

public void SkipRecordBytes()
{
BaseStream.Seek(NextRecord, SeekOrigin.Begin);
BaseStream.Seek(_nextRecord, SeekOrigin.Begin);
}

public IPAddress ReadARecord()
{
return new IPAddress(ReadBytes(RecordLength));
return new IPAddress(ReadBytes(_recordLength));
}

public List<string> ReadTxtRecord()
{
List<string> txts = new List<string>();
byte txtLength = 0;
int rdLength = RecordLength;
var txts = new List<string>();
byte txtLength;
int rdLength = _recordLength;

while ((rdLength > 0) && ((txtLength = ReadByte()) != 0))
{
Expand All @@ -175,7 +174,7 @@ public SrvRecord ReadSrvRecord()
ushort port = ReadUInt16();
Name target = ReadName();

return new SrvRecord()
return new SrvRecord
{
Priority = priority,
Weight = weight,
Expand All @@ -198,15 +197,12 @@ private byte ReadByte()
{
return (byte)i;
}
else
{
throw new Exception();
}
throw new Exception();
}

private byte[] ReadBytes(int length)
{
byte[] buffer = new byte[length];
var buffer = new byte[length];
int offset = 0;

while (length > 0)
Expand Down Expand Up @@ -272,7 +268,7 @@ private void ReadName(Name name)
}
}

private int RecordLength = -1;
private int NextRecord = 0;
private int _recordLength = -1;
private int _nextRecord;
}
}
110 changes: 54 additions & 56 deletions Tmds/MDns/DnsMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

Expand All @@ -28,22 +27,22 @@ class DnsMessageWriter
{
public DnsMessageWriter()
{
Stream = new MemoryStream(Buffer);
_stream = new MemoryStream(_buffer);
}

public void Reset()
{
Stream.Seek(0, SeekOrigin.Begin);
QuestionCount = 0;
AnswerCount = 0;
AuthorityCount = 0;
AdditionalCount = 0;
RecordStartPosition = 0;
_stream.Seek(0, SeekOrigin.Begin);
_questionCount = 0;
_answerCount = 0;
_authorityCount = 0;
_additionalCount = 0;
_recordStartPosition = 0;
}

public void WriteQueryHeader(ushort transactionId)
{
Debug.Assert(Stream.Position == 0);
Debug.Assert(_stream.Position == 0);
WriteUInt16(transactionId);
WriteUInt16(0); // flags
WriteUInt16(0); // questionCount
Expand All @@ -52,24 +51,24 @@ public void WriteQueryHeader(ushort transactionId)
WriteUInt16(0); // additionalCount
}

public void WriteQuestion(Name name, RecordType qtype, RecordClass qclass = RecordClass.IN)
public void WriteQuestion(Name name, RecordType qtype, RecordClass qclass = RecordClass.Internet)
{
WriteName(name);
WriteUInt16((ushort)qtype);
WriteUInt16((ushort)qclass);
QuestionCount++;
_questionCount++;
}

public void WritePtrRecord(RecordSection recordType, Name name, Name ptrName, uint ttl, RecordClass _class = RecordClass.IN)
public void WritePtrRecord(RecordSection recordType, Name name, Name ptrName, uint ttl, RecordClass _class = RecordClass.Internet)
{
WriteRecordStart(recordType, name, RecordType.PTR, ttl, _class);
WriteRecordData(name);
WriteRecordEnd();
}

public void WriteRecordStart(RecordSection recordType, Name name, RecordType type, uint ttl, RecordClass _class = RecordClass.IN)
public void WriteRecordStart(RecordSection recordType, Name name, RecordType type, uint ttl, RecordClass _class = RecordClass.Internet)
{
Debug.Assert(RecordStartPosition == 0);
Debug.Assert(_recordStartPosition == 0);
WriteName(name);
WriteUInt16((ushort)type);
WriteUInt16((ushort)_class);
Expand All @@ -78,32 +77,32 @@ public void WriteRecordStart(RecordSection recordType, Name name, RecordType typ
switch (recordType)
{
case RecordSection.Answer:
AnswerCount++;
_answerCount++;
break;
case RecordSection.Additional:
AdditionalCount++;
_additionalCount++;
break;
case RecordSection.Authority:
AuthorityCount++;
_authorityCount++;
break;
}
RecordStartPosition = Stream.Position;
_recordStartPosition = _stream.Position;
}

public void WriteRecordData(byte[] buffer, int offset, int length)
{
Debug.Assert(RecordStartPosition != 0);
Stream.Write(buffer, offset, length);
Debug.Assert(_recordStartPosition != 0);
_stream.Write(buffer, offset, length);
}

public void WriteRecordData(byte[] buffer)
{
Stream.Write(buffer, 0, buffer.Length);
_stream.Write(buffer, 0, buffer.Length);
}

public void WriteRecordData(Name name)
{
Debug.Assert(RecordStartPosition != 0);
Debug.Assert(_recordStartPosition != 0);
WriteName(name);
}

Expand All @@ -115,39 +114,38 @@ public void WriteRecordData(IPAddress address)

public void WriteRecordEnd()
{
Debug.Assert(RecordStartPosition != 0);
long currentPosition = Stream.Position;
ushort length = (ushort)(currentPosition - RecordStartPosition);
Stream.Seek(RecordStartPosition - 2, SeekOrigin.Begin);
long currentPosition = _stream.Position;
var length = (ushort)(currentPosition - _recordStartPosition);
_stream.Seek(_recordStartPosition - 2, SeekOrigin.Begin);
WriteUInt16(length);
Stream.Seek(currentPosition, SeekOrigin.Begin);
RecordStartPosition = 0;
_stream.Seek(currentPosition, SeekOrigin.Begin);
_recordStartPosition = 0;
}

public IList<ArraySegment<byte>> Packets
{
get
{
finish();
return new List<ArraySegment<byte>>()
Finish();
return new List<ArraySegment<byte>>
{
new ArraySegment<byte>(Buffer, 0, (int)Stream.Position)
new ArraySegment<byte>(_buffer, 0, (int)_stream.Position)
};
}
}

private void WriteUInt16(ushort value)
{
Stream.WriteByte((byte)(value >> 8));
Stream.WriteByte((byte)(value & 0xff));
_stream.WriteByte((byte)(value >> 8));
_stream.WriteByte((byte)(value & 0xff));
}

private void WriteUInt32(uint value)
{
Stream.WriteByte((byte)((value & 0xff000000) >> 24));
Stream.WriteByte((byte)((value & 0x00ff0000) >> 16));
Stream.WriteByte((byte)((value & 0x0000ff00) >> 8));
Stream.WriteByte((byte)((value & 0x000000ff) >> 0));
_stream.WriteByte((byte)((value & 0xff000000) >> 24));
_stream.WriteByte((byte)((value & 0x00ff0000) >> 16));
_stream.WriteByte((byte)((value & 0x0000ff00) >> 8));
_stream.WriteByte((byte)((value & 0x000000ff) >> 0));
}

private void WriteName(Name name)
Expand All @@ -157,33 +155,33 @@ private void WriteName(Name name)
{
int length = label.Length;
finished = (length == 0);
Stream.WriteByte((byte)length);
Encoding.UTF8.GetBytes(label, 0, label.Length, Buffer, (int)Stream.Position);
Stream.Seek(length, SeekOrigin.Current);
_stream.WriteByte((byte)length);
Encoding.UTF8.GetBytes(label, 0, label.Length, _buffer, (int)_stream.Position);
_stream.Seek(length, SeekOrigin.Current);
}
if (!finished)
{
Stream.WriteByte(0);
_stream.WriteByte(0);
}
}

private void finish()
private void Finish()
{
long currentPosition = Stream.Position;
Stream.Seek(4, SeekOrigin.Begin);
WriteUInt16(QuestionCount);
WriteUInt16(AnswerCount);
WriteUInt16(AuthorityCount);
WriteUInt16(AdditionalCount);
Stream.Seek(currentPosition, SeekOrigin.Begin);
long currentPosition = _stream.Position;
_stream.Seek(4, SeekOrigin.Begin);
WriteUInt16(_questionCount);
WriteUInt16(_answerCount);
WriteUInt16(_authorityCount);
WriteUInt16(_additionalCount);
_stream.Seek(currentPosition, SeekOrigin.Begin);
}

private byte[] Buffer = new byte[9000];
private MemoryStream Stream;
private ushort QuestionCount;
private ushort AnswerCount;
private ushort AuthorityCount;
private ushort AdditionalCount;
private long RecordStartPosition;
private readonly byte[] _buffer = new byte[9000];
private readonly MemoryStream _stream;
private ushort _questionCount;
private ushort _answerCount;
private ushort _authorityCount;
private ushort _additionalCount;
private long _recordStartPosition;
}
}
3 changes: 0 additions & 3 deletions Tmds/MDns/HostInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Tmds.MDns
{
Expand Down
3 changes: 0 additions & 3 deletions Tmds/MDns/InterfaceDetectEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tmds.MDns
{
Expand Down
Loading

0 comments on commit 011af0a

Please sign in to comment.