Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions CoAP.NET/CoAP.NET40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\NET40\</OutputPath>
<DefineConstants>TRACE;DEBUG;COAPALL</DefineConstants>
<DefineConstants>TRACE;DEBUG;COAPALL;INCLUDE_OSCOAP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\NET40\</OutputPath>
<DefineConstants>TRACE;COAPALL</DefineConstants>
<DefineConstants>TRACE;COAPALL;INCLUDE_OSCOAP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\NET40\CoAP.NET.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>coapnet.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Common.Logging">
<HintPath>..\packages\Common.Logging.3.0.0\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="Common.Logging.Core">
<HintPath>..\packages\Common.Logging.Core.3.0.0\lib\net40\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -102,6 +96,12 @@
<Compile Include="Observe\ReregisterEventArgs.cs" />
<Compile Include="Option.cs" />
<Compile Include="OptionType.cs" />
<Compile Include="OSCOAP\HKDF.cs" />
<Compile Include="OSCOAP\OscoapLayer.cs" />
<Compile Include="OSCOAP\OscoapOption.cs" />
<Compile Include="OSCOAP\SecureBlockwiseLayer.cs" />
<Compile Include="OSCOAP\SecurityContext.cs" />
<Compile Include="OSCOAP\SecurityContextSet.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Request.cs" />
<Compile Include="Response.cs" />
Expand Down Expand Up @@ -146,6 +146,16 @@
<None Include="packages.config" />
<None Include="README" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Bouncy-Castle\bc-csharp\crypto\BouncyCastle.csproj">
<Project>{4c235092-820c-4deb-9074-d356fb797d8b}</Project>
<Name>BouncyCastle</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\COSE\COSE-csharp\COSE\COSE.Net40.csproj">
<Project>{d02f476f-bc2c-4d01-bede-eb8e79dd050e}</Project>
<Name>COSE.Net40</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
70 changes: 70 additions & 0 deletions CoAP.NET/CoapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public static ICoapConfig Default
private Int32 _channelReceiveBufferSize;
private Int32 _channelSendBufferSize;
private Int32 _channelReceivePacketSize = 2048;
#if INCLUDE_OSCOAP
private Int32 _oscoap_maxMessageSize = 1024;
private Int32 _oscoap_defaultBlockSize = CoapConstants.DefaultBlockSize;
private Int32 _oscoap_blockwiseStatusLifetime = 10 *60 * 1000; // ms
private bool _oscoap_ReplayWindow = true;
#endif

/// <summary>
/// Instantiate.
Expand Down Expand Up @@ -399,6 +405,60 @@ public Int32 ChannelReceivePacketSize
}
}

#if INCLUDE_OSCOAP
/// <inheritdoc/>
public Int32 OSCOAP_MaxMessageSize
{
get { return _oscoap_maxMessageSize; }
set
{
if (_oscoap_maxMessageSize != value)
{
_oscoap_maxMessageSize = value;
NotifyPropertyChanged("OSCOAP_MaxMessageSize");
}
}
}

/// <inheritdoc/>
public Int32 OSCOAP_DefaultBlockSize
{
get { return _oscoap_defaultBlockSize; }
set
{
if (_oscoap_defaultBlockSize != value)
{
_oscoap_defaultBlockSize = value;
NotifyPropertyChanged("OSCOAP_DefaultBlockSize");
}
}
}

/// <inheritdoc/>
public Int32 OSCOAP_BlockwiseStatusLifetime
{
get { return _oscoap_blockwiseStatusLifetime; }
set
{
if (_oscoap_blockwiseStatusLifetime != value)
{
_oscoap_blockwiseStatusLifetime = value;
NotifyPropertyChanged("OSCOAP_BlockwiseStatusLifetime");
}
}
}

public bool OSCOAP_ReplayWindow {
get { return _oscoap_ReplayWindow; }
set {
if (_oscoap_ReplayWindow != value) {
_oscoap_ReplayWindow = value;
NotifyPropertyChanged("OSCOAP_ReplayWindow");
}
}
}
#endif

/// <inheritdoc/>
public void Load(String configFile)
{
Expand Down Expand Up @@ -433,6 +493,11 @@ public void Load(String configFile)
ChannelReceiveBufferSize = GetInt32(nvc, "ChannelReceiveBufferSize", "UDP_CONNECTOR_RECEIVE_BUFFER", ChannelReceiveBufferSize);
ChannelSendBufferSize = GetInt32(nvc, "ChannelSendBufferSize", "UDP_CONNECTOR_SEND_BUFFER", ChannelSendBufferSize);
ChannelReceivePacketSize = GetInt32(nvc, "ChannelReceivePacketSize", "UDP_CONNECTOR_DATAGRAM_SIZE", ChannelReceivePacketSize);
#if INCLUDE_OSCOAP
OSCOAP_MaxMessageSize = GetInt32(nvc, "OSCOAP_MaxMessageSize", "MAX_MESSAGE_SIZE", OSCOAP_MaxMessageSize);
OSCOAP_DefaultBlockSize = GetInt32(nvc, "OSCOAP_DefaultBlockSize", "DEFAULT_BLOCK_SIZE", OSCOAP_DefaultBlockSize);
OSCOAP_ReplayWindow = GetBoolean(nvc, "OSCOAP_ReplayWindow", "REPLAY_WINDOW", OSCOAP_ReplayWindow);
#endif
}

/// <inheritdoc/>
Expand Down Expand Up @@ -462,6 +527,11 @@ public void Store(String configFile)
w.Write("ChannelReceiveBufferSize="); w.WriteLine(ChannelReceiveBufferSize);
w.Write("ChannelSendBufferSize="); w.WriteLine(ChannelSendBufferSize);
w.Write("ChannelReceivePacketSize="); w.WriteLine(ChannelReceivePacketSize);
#if INCLUDE_OSCOAP
w.Write("OSCOAP_MaxMessageSize="); w.WriteLine(OSCOAP_MaxMessageSize);
w.Write("OSCOAP_DefaultBlockSize="); w.WriteLine(OSCOAP_DefaultBlockSize);
w.Write("OSCOAP_ReplayWindow="); w.WriteLine(OSCOAP_ReplayWindow);
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions CoAP.NET/ICoapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public partial interface ICoapConfig : System.ComponentModel.INotifyPropertyChan
Int32 ChannelSendBufferSize { get; }
Int32 ChannelReceivePacketSize { get; }

#if INCLUDE_OSCOAP
Int32 OSCOAP_MaxMessageSize { get; }
Int32 OSCOAP_DefaultBlockSize { get; }
Int32 OSCOAP_BlockwiseStatusLifetime { get; }

bool OSCOAP_ReplayWindow { get; }
#endif

/// <summary>
/// Loads configuration from a config properties file.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions CoAP.NET/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,18 @@ public void SetBlock2(Int32 szx, Boolean m, Int32 num)
SetOption(new BlockOption(OptionType.Block2, num, szx, m));
}

#if INCLUDE_OSCOAP
public OSCOAP.OscoapOption Oscoap
{
get { return GetFirstOption(OptionType.Oscoap) as OSCOAP.OscoapOption; }
set
{
if (value == null) RemoveOptions(OptionType.Oscoap);
else SetOption(value);
}
}
#endif

private IEnumerable<T> SelectOptions<T>(OptionType optionType, Func<Option, T> func)
{
IEnumerable<Option> opts = GetOptions(optionType);
Expand Down
86 changes: 86 additions & 0 deletions CoAP.NET/Net/Exchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using CoAP.Observe;
using CoAP.Stack;
using CoAP.Util;
#if INCLUDE_OSCOAP
using CoAP.OSCOAP;
#endif

namespace CoAP.Net
{
Expand Down Expand Up @@ -43,6 +46,12 @@ public class Exchange
private IEndPoint _endpoint;
private IOutbox _outbox;
private IMessageDeliverer _deliverer;
#if INCLUDE_OSCOAP
private BlockwiseStatus _oscoap_requestBlockStatus;
private BlockwiseStatus _oscoap_responseBlockStatus;
private SecurityContext _oscoap_securityContext;
private byte[] _oscoap_sequenceNumber;
#endif

public event EventHandler Completed;

Expand Down Expand Up @@ -138,6 +147,45 @@ public BlockOption Block1ToAck
set { _block1ToAck = value; }
}

#if INCLUDE_OSCOAP
/// <summary>
/// Gets or sets the status of the security blockwise transfer of the request,
/// or null in case of a normal transfer,
/// </summary>
public BlockwiseStatus OSCOAP_RequestBlockStatus
{
get { return _oscoap_requestBlockStatus; }
set { _oscoap_requestBlockStatus = value; }
}

/// <summary>
/// Gets or sets the status of the security blockwise transfer of the response,
/// or null in case of a normal transfer,
/// </summary>
public BlockwiseStatus OSCOAP_ResponseBlockStatus
{
get { return _oscoap_responseBlockStatus; }
set { _oscoap_responseBlockStatus = value; }
}

/// <summary>
/// Gets or sets the OSCOAP security context for the exchange
/// </summary>
public SecurityContext OscoapContext {
get { return _oscoap_securityContext; }
set { _oscoap_securityContext = value; }
}

/// <summary>
/// Gets or sets the sequence number used to link requests and responses
/// in OSCOAP authentication
/// </summary>
public byte[] OscoapSequenceNumber {
get { return _oscoap_sequenceNumber; }
set { _oscoap_sequenceNumber = value; }
}
#endif

/// <summary>
/// Gets the time when this exchange was created.
/// </summary>
Expand Down Expand Up @@ -337,13 +385,32 @@ public class KeyUri
private readonly Uri _uri;
private readonly System.Net.EndPoint _endpoint;
private readonly Int32 _hash;
#if INCLUDE_OSCOAP
private readonly byte[] _oscoap;
#endif

#if INCLUDE_OSCOAP
public KeyUri(Uri uri, byte[] oscoap, System.Net.EndPoint ep)
{
_uri = uri;
_endpoint = ep;
_oscoap = oscoap;
_hash = _uri.GetHashCode() * 31 + ep.GetHashCode();

if (oscoap != null) {
Int32 hash2 = 0;
for (int i = 0; i < oscoap.Length; i++) hash2 = hash2 * 7 + oscoap[i];
_hash += hash2 * 71;
}
}
#else
public KeyUri(Uri uri, System.Net.EndPoint ep)
{
_uri = uri;
_endpoint = ep;
_hash = _uri.GetHashCode() * 31 + ep.GetHashCode();
}
#endif

/// <inheritdoc/>
public override Int32 GetHashCode()
Expand All @@ -357,13 +424,32 @@ public override Boolean Equals(Object obj)
KeyUri other = obj as KeyUri;
if (other == null)
return false;
#if INCLUDE_OSCOAP
if (Object.Equals(_uri, other._uri) && Object.Equals(_endpoint, other._endpoint)) {
if (_oscoap != null) {
if (other._oscoap == null) return false;
if (_oscoap.Length != other._oscoap.Length) return false;
for (int i = 0; i < _oscoap.Length; i++) if (_oscoap[i] != other._oscoap[i]) return false;
return true;
}
return other._oscoap == null;
}
return false;
#else
return Object.Equals(_uri, other._uri) && Object.Equals(_endpoint, other._endpoint);
#endif
}

/// <inheritdoc/>
public override String ToString()
{
#if INCLUDE_OSCOAP
if (_oscoap == null) return "KeyUri[" + _uri + " for " + _endpoint + "]";

return "KeyUri[" + _uri + "for " + _endpoint + " encryption is " + BitConverter.ToString(_oscoap) + "]";
#else
return "KeyUri[" + _uri + " for " + _endpoint + "]";
#endif
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions CoAP.NET/Net/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ public void SendResponse(Exchange exchange, Response response)
if (response.HasOption(OptionType.Block2))
{
Request request = exchange.CurrentRequest;
#if INCLUDE_OSCOAP
byte[] oscoapKey = null;
if (request.HasOption(OptionType.Oscoap)) {
oscoapKey = request.Oscoap.RawValue;
}
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, oscoapKey, response.Destination);
#else // !INCLUDE_OSCOAP
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, response.Destination);
#endif
// Observe notifications only send the first block, hence do not store them as ongoing
if (exchange.ResponseBlockStatus != null && !response.HasOption(OptionType.Observe))
{
Expand Down Expand Up @@ -223,7 +231,15 @@ public Exchange ReceiveRequest(Request request)
}
else
{
#if INCLUDE_OSCOAP
byte[] oscoapValue = null;
if (request.HasOption(OptionType.Oscoap)) {
oscoapValue = request.Oscoap.RawValue;
}
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, oscoapValue, request.Source);
#else
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, request.Source);
#endif

if (log.IsDebugEnabled)
log.Debug("Looking up ongoing exchange for " + keyUri);
Expand Down Expand Up @@ -438,7 +454,15 @@ private void OnExchangeCompleted(Object sender, EventArgs e)
Request request = exchange.CurrentRequest;
if (request != null && (request.HasOption(OptionType.Block1) || response.HasOption(OptionType.Block2)))
{
#if INCLUDE_OSCOAP
byte[] oscoapValue = null;
if (request.HasOption(OptionType.Oscoap)) {
oscoapValue = request.Oscoap.RawValue;
}
Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, oscoapValue, request.Source);
#else
Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, request.Source);
#endif
if (log.IsDebugEnabled)
log.Debug("Remote ongoing completed, cleaning up " + uriKey);
Exchange exc;
Expand Down
Loading