Skip to content

Commit 1d0ff8a

Browse files
committed
Fix portability issues around Socket class
1 parent 287cb50 commit 1d0ff8a

11 files changed

+93
-31
lines changed

Portable.BouncyCastle.nuspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<dependency id="System.IO.FileSystem" version="4.0.0" />
4343
<dependency id="System.IO.FileSystem.Primitives" version="4.0.0" />
4444
<dependency id="System.Linq" version="4.0.0" />
45+
<dependency id="System.Net.Primitives" version="4.0.10" />
4546
<dependency id="System.Reflection" version="4.0.10" />
4647
<dependency id="System.Reflection.Extensions" version="4.0.0" />
4748
<dependency id="System.Runtime" version="4.0.20" />
@@ -58,6 +59,7 @@
5859
<dependency id="System.IO.FileSystem" version="4.0.0" />
5960
<dependency id="System.IO.FileSystem.Primitives" version="4.0.0" />
6061
<dependency id="System.Linq" version="4.0.0" />
62+
<dependency id="System.Net.Primitives" version="4.0.10" />
6163
<dependency id="System.Reflection" version="4.0.10" />
6264
<dependency id="System.Reflection.Extensions" version="4.0.0" />
6365
<dependency id="System.Runtime" version="4.0.20" />
@@ -74,6 +76,7 @@
7476
<dependency id="System.IO.FileSystem" version="4.0.0" />
7577
<dependency id="System.IO.FileSystem.Primitives" version="4.0.0" />
7678
<dependency id="System.Linq" version="4.0.0" />
79+
<dependency id="System.Net.Primitives" version="4.0.10" />
7780
<dependency id="System.Reflection" version="4.0.10" />
7881
<dependency id="System.Reflection.Extensions" version="4.0.0" />
7982
<dependency id="System.Runtime" version="4.0.20" />

crypto/BouncyCastle.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@
12331233
<Compile Include="src\crypto\tls\TlsSrtpUtilities.cs" />
12341234
<Compile Include="src\crypto\tls\TlsStream.cs" />
12351235
<Compile Include="src\crypto\tls\TlsStreamCipher.cs" />
1236+
<Compile Include="src\crypto\tls\TlsTimeoutException.cs" />
12361237
<Compile Include="src\crypto\tls\TlsUtilities.cs" />
12371238
<Compile Include="src\crypto\tls\UrlAndHash.cs" />
12381239
<Compile Include="src\crypto\tls\UseSrtpData.cs" />

crypto/BouncyCastle.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@
12271227
<Compile Include="src\crypto\tls\TlsSrtpUtilities.cs" />
12281228
<Compile Include="src\crypto\tls\TlsStream.cs" />
12291229
<Compile Include="src\crypto\tls\TlsStreamCipher.cs" />
1230+
<Compile Include="src\crypto\tls\TlsTimeoutException.cs" />
12301231
<Compile Include="src\crypto\tls\TlsUtilities.cs" />
12311232
<Compile Include="src\crypto\tls\UrlAndHash.cs" />
12321233
<Compile Include="src\crypto\tls\UseSrtpData.cs" />
@@ -1683,4 +1684,4 @@
16831684
<None Include="checklist.txt" />
16841685
</ItemGroup>
16851686
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
1686-
</Project>
1687+
</Project>

crypto/BouncyCastle.iOS.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@
12281228
<Compile Include="src\crypto\tls\TlsSrtpUtilities.cs" />
12291229
<Compile Include="src\crypto\tls\TlsStream.cs" />
12301230
<Compile Include="src\crypto\tls\TlsStreamCipher.cs" />
1231+
<Compile Include="src\crypto\tls\TlsTimeoutException.cs" />
12311232
<Compile Include="src\crypto\tls\TlsUtilities.cs" />
12321233
<Compile Include="src\crypto\tls\UrlAndHash.cs" />
12331234
<Compile Include="src\crypto\tls\UseSrtpData.cs" />

crypto/crypto.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6023,6 +6023,11 @@
60236023
SubType = "Code"
60246024
BuildAction = "Compile"
60256025
/>
6026+
<File
6027+
RelPath = "src\crypto\tls\TlsTimeoutException.cs"
6028+
SubType = "Code"
6029+
BuildAction = "Compile"
6030+
/>
60266031
<File
60276032
RelPath = "src\crypto\tls\TlsUtilities.cs"
60286033
SubType = "Code"

crypto/src/crypto/tls/DtlsRecordLayer.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
3+
#if !PORTABLE || DOTNET
34
using System.Net.Sockets;
5+
#endif
46

57
using Org.BouncyCastle.Utilities.Date;
68

@@ -312,31 +314,28 @@ private void RaiseAlert(byte alertLevel, byte alertDescription, string message,
312314

313315
private int ReceiveDatagram(byte[] buf, int off, int len, int waitMillis)
314316
{
315-
//try
316-
//{
317-
// return mTransport.Receive(buf, off, len, waitMillis);
318-
//}
319-
//catch (SocketTimeoutException e)
320-
//{
321-
// return -1;
322-
//}
323-
//catch (InterruptedIOException e)
324-
//{
325-
// e.bytesTransferred = 0;
326-
// throw e;
327-
//}
328-
329317
try
330318
{
331319
return mTransport.Receive(buf, off, len, waitMillis);
332320
}
321+
catch (TlsTimeoutException)
322+
{
323+
return -1;
324+
}
325+
#if !PORTABLE || DOTNET
333326
catch (SocketException e)
334327
{
335328
if (TlsUtilities.IsTimeout(e))
336329
return -1;
337330

338331
throw e;
339332
}
333+
#endif
334+
//catch (InterruptedIOException e)
335+
//{
336+
// e.bytesTransferred = 0;
337+
// throw e;
338+
//}
340339
}
341340

342341
private int ProcessRecord(int received, byte[] record, byte[] buf, int off)

crypto/src/crypto/tls/DtlsTransport.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
3+
#if !PORTABLE || DOTNET
34
using System.Net.Sockets;
5+
#endif
46

57
namespace Org.BouncyCastle.Crypto.Tls
68
{
@@ -44,15 +46,11 @@ public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
4446
mRecordLayer.Fail(fatalAlert.AlertDescription);
4547
throw fatalAlert;
4648
}
47-
//catch (InterruptedIOException e)
48-
//{
49-
// throw e;
50-
//}
51-
catch (IOException e)
49+
catch (TlsTimeoutException e)
5250
{
53-
mRecordLayer.Fail(AlertDescription.internal_error);
5451
throw e;
5552
}
53+
#if !PORTABLE || DOTNET
5654
catch (SocketException e)
5755
{
5856
if (TlsUtilities.IsTimeout(e))
@@ -61,6 +59,16 @@ public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
6159
mRecordLayer.Fail(AlertDescription.internal_error);
6260
throw new TlsFatalAlert(AlertDescription.internal_error, e);
6361
}
62+
#endif
63+
//catch (InterruptedIOException e)
64+
//{
65+
// throw e;
66+
//}
67+
catch (IOException e)
68+
{
69+
mRecordLayer.Fail(AlertDescription.internal_error);
70+
throw e;
71+
}
6472
catch (Exception e)
6573
{
6674
mRecordLayer.Fail(AlertDescription.internal_error);
@@ -86,15 +94,11 @@ public virtual void Send(byte[] buf, int off, int len)
8694
mRecordLayer.Fail(fatalAlert.AlertDescription);
8795
throw fatalAlert;
8896
}
89-
//catch (InterruptedIOException e)
90-
//{
91-
// throw e;
92-
//}
93-
catch (IOException e)
97+
catch (TlsTimeoutException e)
9498
{
95-
mRecordLayer.Fail(AlertDescription.internal_error);
9699
throw e;
97100
}
101+
#if !PORTABLE || DOTNET
98102
catch (SocketException e)
99103
{
100104
if (TlsUtilities.IsTimeout(e))
@@ -103,6 +107,16 @@ public virtual void Send(byte[] buf, int off, int len)
103107
mRecordLayer.Fail(AlertDescription.internal_error);
104108
throw new TlsFatalAlert(AlertDescription.internal_error, e);
105109
}
110+
#endif
111+
//catch (InterruptedIOException e)
112+
//{
113+
// throw e;
114+
//}
115+
catch (IOException e)
116+
{
117+
mRecordLayer.Fail(AlertDescription.internal_error);
118+
throw e;
119+
}
106120
catch (Exception e)
107121
{
108122
mRecordLayer.Fail(AlertDescription.internal_error);

crypto/src/crypto/tls/TlsException.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace Org.BouncyCastle.Crypto.Tls
66
public class TlsException
77
: IOException
88
{
9+
public TlsException()
10+
: base()
11+
{
12+
}
13+
14+
public TlsException(string message)
15+
: base(message)
16+
{
17+
}
18+
919
public TlsException(string message, Exception cause)
1020
: base(message, cause)
1121
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace Org.BouncyCastle.Crypto.Tls
4+
{
5+
public class TlsTimeoutException
6+
: TlsException
7+
{
8+
public TlsTimeoutException()
9+
: base()
10+
{
11+
}
12+
13+
public TlsTimeoutException(string message)
14+
: base(message)
15+
{
16+
}
17+
18+
public TlsTimeoutException(string message, Exception cause)
19+
: base(message, cause)
20+
{
21+
}
22+
}
23+
}

crypto/src/crypto/tls/TlsUtilities.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
22
using System.Collections;
3+
#if !PORTABLE || DOTNET
34
using System.Net.Sockets;
5+
#endif
46
using System.IO;
5-
using System.Text;
67

78
using Org.BouncyCastle.Asn1;
89
using Org.BouncyCastle.Asn1.Nist;
@@ -2347,11 +2348,15 @@ public static IList GetUsableSignatureAlgorithms(IList sigHashAlgs)
23472348
return v;
23482349
}
23492350

2351+
#if !PORTABLE || DOTNET
23502352
public static bool IsTimeout(SocketException e)
23512353
{
2352-
// TODO Net 2.0+
2353-
//return SocketError.TimedOut == e.SocketErrorCode;
2354+
#if NET_1_1
23542355
return 10060 == e.ErrorCode;
2356+
#else
2357+
return SocketError.TimedOut == e.SocketErrorCode;
2358+
#endif
23552359
}
2360+
#endif
23562361
}
23572362
}

0 commit comments

Comments
 (0)