1 using java
.nio
.channels
;
5 namespace System
.Net
.Sockets
8 /// Summary description for GHStreamSocket.
10 internal class GHStreamSocket
: GHSocket
12 java
.net
.ServerSocket jServerSocket
;
13 java
.net
.Socket jSocket
;
14 java
.nio
.channels
.ServerSocketChannel jServerSocketChannel
;
15 java
.nio
.channels
.SocketChannel jSocketChannel
;
17 // This field I need because a bug in the java.nio.channels.SocketAdapter, which
18 // returns local port 0 if the socket is not connected (even if the socket is bound)
19 // so I need temporary use regular socket (not channel socket) to bind it to the
20 // local address and use this address in the LocalPoint property and to create the
21 // actual client/server channel sockets
22 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
23 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
24 java
.net
.InetSocketAddress jTempLocalSocketAddress
;
26 public GHStreamSocket()
28 jSocketChannel
= java
.nio
.channels
.SocketChannel
.open();
29 jSocket
= jSocketChannel
.socket();
32 public GHStreamSocket(java
.nio
.channels
.SocketChannel socketChannel
)
34 jSocketChannel
= socketChannel
;
35 jSocket
= jSocketChannel
.socket();
38 public override int GetHashCode ()
40 if (jSocket
== null && jServerSocket
== null)
43 if (jServerSocket
!= null) {
44 return jServerSocket
.ToString ().GetHashCode ();
47 return jSocket
.ToString ().GetHashCode ();
50 public int Available_internal(out int error
)
55 if (jSocket
== null || !jSocket
.isConnected())
62 r
= jSocket
.getInputStream().available();
66 error
= 10054; //WSAECONNRESET (Connection reset by peer)
69 Console
.WriteLine("Caught exception during Available_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
76 public void Blocking_internal(bool block
, out int error
)
80 if (jSocket
== null && jServerSocket
== null)
82 error
= 10022; //WSAEINVAL (Invalid argument)
88 if (jServerSocket
!= null)
90 jServerSocketChannel
.configureBlocking(block
);
94 jSocketChannel
.configureBlocking(block
);
99 error
= 10022; //WSAEINVAL (Invalid argument)
101 Console
.WriteLine("Caught exception during Blocking_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
106 public EndPoint
LocalEndPoint_internal(out int error
)
109 java
.net
.InetSocketAddress localAddr
= null;
113 if (jTempLocalSocketAddress
!= null)
115 localAddr
= jTempLocalSocketAddress
;
117 else if (jServerSocket
!= null)
119 localAddr
= (java
.net
.InetSocketAddress
)jServerSocket
.getLocalSocketAddress();
123 localAddr
= (java
.net
.InetSocketAddress
)jSocket
.getLocalSocketAddress();
130 Console
.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
134 if (localAddr
== null || localAddr
.getAddress() == null || localAddr
.getPort() < 0)
139 IPHostEntry lipa
= Dns
.Resolve(localAddr
.getHostName());
140 IPEndPoint ret
= new IPEndPoint(lipa
.AddressList
[0], localAddr
.getPort());
144 public EndPoint
RemoteEndPoint_internal(out int error
)
147 java
.net
.InetSocketAddress remoteAddr
= null;
149 if (jSocket
== null || !jSocket
.isBound())
156 remoteAddr
= (java
.net
.InetSocketAddress
)jSocket
.getRemoteSocketAddress();
162 Console
.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
166 if (remoteAddr
== null || remoteAddr
.getAddress() == null || remoteAddr
.getPort() <= 0)
168 error
= 10057; //WSAENOTCONN (Socket is not connected)
172 IPHostEntry lipa
= Dns
.Resolve(remoteAddr
.getHostName());
173 IPEndPoint ret
= new IPEndPoint(lipa
.AddressList
[0], remoteAddr
.getPort());
177 public GHSocket
Accept_internal(out int error
)
181 if (jServerSocket
== null)
183 throw new InvalidOperationException("You must call Bind and Listen before calling Accept.");
189 If this channel is in non-blocking mode then this method will immediately
190 return null if there are no pending connections.
191 Otherwise it will block indefinitely until a new connection is
192 available or an I/O error occurs.
194 java
.nio
.channels
.SocketChannel acceptedSocket
= jServerSocketChannel
.accept();
195 if (acceptedSocket
== null)
197 error
= 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
199 Console
.WriteLine("The Accept_internal is in non-blocking mode and no pending connections are available");
204 return new GHStreamSocket(acceptedSocket
);
206 catch (AsynchronousCloseException
) {
211 error
= 10061; //WSAECONNREFUSED (Connection refused)
213 Console
.WriteLine("Caught exception during Accept_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
220 public void Bind_internal(EndPoint sa
, out int error
)
223 IPEndPoint addr
= sa
as IPEndPoint
;
226 error
= 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
230 if (jSocket
== null || jSocket
.isBound() || jSocket
.isConnected() || jSocketChannel
.isConnectionPending())
232 error
= 10022; //WSAEINVAL (Invalid argument)
238 // This code I need because a bug in the java.nio.channels.SocketAdapter, which
239 // returns local port 0 if the socket is not connected (even if the socket is bound)
240 // so I need temporary use regular socket (not channel socket) to bind it to the
241 // local address and use this address in the LocalPoint property and to create the
242 // actual client/server channel sockets
243 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
244 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
245 java
.net
.Socket jTempSocket
= new java
.net
.Socket();
246 jTempSocket
.bind(new java
.net
.InetSocketAddress(java
.net
.InetAddress
.getByName(addr
.Address
.ToString()),
248 jTempLocalSocketAddress
= (java
.net
.InetSocketAddress
)jTempSocket
.getLocalSocketAddress();
250 jSocket
.bind(jTempLocalSocketAddress
);
254 error
= 10048; //WSAEADDRINUSE (Address already in use)
256 Console
.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
261 public void Close_internal(out int error
)
265 if (jServerSocket
!= null)
269 jServerSocket
.close();
273 error
= 10022; //WSAEINVAL (Invalid argument)
275 Console
.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
280 jServerSocketChannel
.close();
284 error
= 10022; //WSAEINVAL (Invalid argument)
286 Console
.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
289 jServerSocket
= null;
290 jServerSocketChannel
= null;
292 else if (jSocket
!= null)
300 error
= 10022; //WSAEINVAL (Invalid argument)
302 Console
.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
307 jSocketChannel
.close();
311 error
= 10022; //WSAEINVAL (Invalid argument)
313 Console
.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
317 jSocketChannel
= null;
321 public void Connect_internal(EndPoint sa
, out int error
)
325 IPEndPoint addr
= sa
as IPEndPoint
;
328 error
= 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
334 error
= 10022; //WSAEINVAL (Invalid argument)
338 if (jSocket
.isConnected() || jSocketChannel
.isConnectionPending())
340 error
= 10056; //WSAEISCONN (Socket is already connected)
347 If this channel is in non-blocking mode then an invocation of this method
348 initiates a non-blocking connection operation. If the connection is
349 established immediately, as can happen with a local connection, then this
350 method returns true. Otherwise this method returns false.
351 If this channel is in blocking mode then an invocation of this method
352 will block until the connection is established or an I/O error occurs.
354 bool status
= jSocketChannel
.connect(new java
.net
.InetSocketAddress(
355 java
.net
.InetAddress
.getByName(addr
.Address
.ToString()),
359 error
= 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
362 catch (java
.nio
.channels
.AlreadyConnectedException ae
)
364 error
= 10056; //WSAEISCONN (Socket is already connected)
366 catch (java
.nio
.channels
.ConnectionPendingException cpe
)
368 error
= 10036; //WSAEINPROGRESS (Operation now in progress)
370 catch (java
.nio
.channels
.UnresolvedAddressException uae
)
372 error
= 10039; //WSAEDESTADDRREQ (Destination address required)
374 catch (java
.nio
.channels
.UnsupportedAddressTypeException uate
)
376 error
= 10041; //WSAEPROTOTYPE (Protocol wrong type for socket)
378 catch (AsynchronousCloseException
) {
383 error
= 10061; //WSAECONNREFUSED (Connection refused)
385 Console
.WriteLine("Caught exception during Connect_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
390 public void Listen_internal(int backlog
, out int error
)
394 if (jSocket
== null || !jSocket
.isBound())
396 error
= 10022; //WSAEINVAL (Invalid argument)
400 if (jSocket
.isConnected() || jSocketChannel
.isConnectionPending())
402 error
= 10056; //WSAEISCONN (Socket is already connected)
406 bool blockMode
= jSocketChannel
.isBlocking();
407 bool reuseAddr
= jSocket
.getReuseAddress();
416 Console
.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
422 jSocketChannel
.close();
427 Console
.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
432 jSocketChannel
= null;
436 jServerSocketChannel
= java
.nio
.channels
.ServerSocketChannel
.open();
437 jServerSocket
= jServerSocketChannel
.socket();
438 jServerSocket
.bind(jTempLocalSocketAddress
, backlog
);
439 jServerSocketChannel
.configureBlocking(blockMode
);
440 jServerSocket
.setReuseAddress(reuseAddr
);
444 error
= 10048; //WSAEADDRINUSE (Address already in use)
446 Console
.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
451 public bool Poll_internal (SelectMode mode
, int timeout
, Socket source
, out int error
)
455 if (mode
== SelectMode
.SelectError
&& !jSocketChannel
.isConnectionPending())
460 java
.nio
.channels
.Selector selector
= java
.nio
.channels
.Selector
.open();
461 RegisterSelector(selector
, ((mode
== SelectMode
.SelectRead
)?0:1), source
, out error
);
466 GHSocketFactory
.CloseSelector(selector
);
467 return (mode
== SelectMode
.SelectError
);
472 long timeOutMillis
= 1;
477 else if (timeout
> 999)
479 timeOutMillis
= (long)(timeout
/ 1000);
485 readyCount
= selector
.select(timeOutMillis
);
489 error
= 10022; //WSAEINVAL (Invalid argument)
491 Console
.WriteLine("Caught exception during Poll_internal selector.select - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
497 if (jSocket
!= null && jSocketChannel
.isConnectionPending())
502 status
= jSocketChannel
.finishConnect();
507 Console
.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
512 retVal
= (mode
!= SelectMode
.SelectError
);
516 retVal
= (mode
== SelectMode
.SelectError
);
525 GHSocketFactory
.CloseSelector(selector
);
530 public void RegisterSelector(java
.nio
.channels
.Selector selector
, int mode
, Socket source
, out int error
)
533 if (jServerSocket
!= null)
535 // only accept operation, which included to the read list, is allowed for server sockets
538 // error = 10038; //WSAENOTSOCK (Socket operation on nonsocket)
540 Console
.WriteLine("RegisterSelector, invalid mode {0} for the server socket", mode
);
547 if (jServerSocketChannel
.isBlocking())
550 A channel must be placed into non-blocking mode before being registered
551 with a selector, and may not be returned to blocking mode until it has been
554 jServerSocketChannel
.configureBlocking(false);
557 jServerSocketChannel
.register(selector
, java
.nio
.channels
.SelectionKey
.OP_ACCEPT
, source
);
561 error
= 10022; //WSAEINVAL (Invalid argument)
563 Console
.WriteLine("Caught exception during RegisterSelector, register server socket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
571 int ops
= java
.nio
.channels
.SelectionKey
.OP_READ
;
574 if (jSocketChannel
.isConnectionPending())
576 ops
= java
.nio
.channels
.SelectionKey
.OP_CONNECT
;
580 ops
= java
.nio
.channels
.SelectionKey
.OP_WRITE
;
584 if (jSocketChannel
.isBlocking())
587 A channel must be placed into non-blocking mode before being registered
588 with a selector, and may not be returned to blocking mode until it has been
591 jSocketChannel
.configureBlocking(false);
594 jSocketChannel
.register(selector
, ops
, source
);
598 error
= 10022; //WSAEINVAL (Invalid argument)
600 Console
.WriteLine("Caught exception during RegisterSelector, register client socket - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
606 public bool CheckConnectionFinished()
609 if (jSocket
!= null && jSocketChannel
.isConnectionPending())
613 status
= jSocketChannel
.finishConnect();
619 Console
.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
627 public int Receive_internal(byte[] buffer
, int offset
, int count
, SocketFlags flags
,
635 error
= 10057; //WSAENOTCONN (Socket is not connected)
641 if (jSocketChannel
.isConnectionPending())
643 bool status
= jSocketChannel
.finishConnect();
646 error
= 10022; //WSAEINVAL (Invalid argument)
648 Console
.WriteLine("Receive_internal, jSocketChannel.finishConnect return false");
653 else if (!jSocketChannel
.isConnected())
655 error
= 10057; //WSAENOTCONN (Socket is not connected)
659 java
.nio
.ByteBuffer readBuff
= java
.nio
.ByteBuffer
.wrap(vmw
.common
.TypeUtils
.ToSByteArray(buffer
), offset
, count
);
660 ret
= jSocketChannel
.read(readBuff
);
661 if (ret
< 0) ret
= 0;
665 error
= 10054; //WSAECONNRESET (Connection reset by peer)
668 Console
.WriteLine("Caught exception during Receive_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
672 if (ret
== 0 && !jSocketChannel
.isBlocking())
674 error
= 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
679 public int RecvFrom_internal(byte[] buffer
, int offset
, int count
, SocketFlags flags
,
680 ref SocketAddress sockaddr
, out int error
)
682 return Receive_internal(buffer
, offset
, count
, flags
, out error
);
685 public int Send_internal(byte[] buf
, int offset
, int count
, SocketFlags flags
,
693 error
= 10057; //WSAENOTCONN (Socket is not connected)
699 if (jSocketChannel
.isConnectionPending())
701 bool status
= jSocketChannel
.finishConnect();
704 error
= 10022; //WSAEINVAL (Invalid argument)
706 Console
.WriteLine("Send_internal, jSocketChannel.finishConnect return false");
711 else if (!jSocketChannel
.isConnected())
713 error
= 10057; //WSAENOTCONN (Socket is not connected)
717 java
.nio
.ByteBuffer writeBuff
= java
.nio
.ByteBuffer
.wrap(vmw
.common
.TypeUtils
.ToSByteArray(buf
), offset
, count
);
718 ret
= jSocketChannel
.write(writeBuff
);
719 if (ret
< 0) ret
= 0;
723 error
= 10054; //WSAECONNRESET (Connection reset by peer)
726 Console
.WriteLine("Caught exception during Send_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
730 if (ret
== 0 && !jSocketChannel
.isBlocking())
732 error
= 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
737 public int SendTo_internal(byte[] buffer
, int offset
, int count
,
738 SocketFlags flags
, SocketAddress sa
, out int error
)
740 return Send_internal(buffer
, offset
, count
, flags
, out error
);
743 public void SetSocketOption_internal (SocketOptionLevel level
,
744 SocketOptionName name
, object obj_val
,
745 byte [] byte_val
, int int_val
, out int error
)
749 if (byte_val
!= null)
752 throw new NotImplementedException();
755 if (jSocket
== null && jServerSocket
== null)
757 error
= 10022; //WSAEINVAL (Invalid argument)
763 case SocketOptionLevel
.IPv6
:
764 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
766 case SocketOptionLevel
.IP
:
767 if (name
!= SocketOptionName
.NoDelay
)
769 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
773 case SocketOptionLevel
.Udp
:
774 if (name
== SocketOptionName
.NoDelay
)
776 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
780 error
= 10022; //WSAEINVAL (Invalid argument)
783 case SocketOptionLevel
.Tcp
:
784 if (name
!= SocketOptionName
.NoDelay
)
786 error
= 10022; //WSAEINVAL (Invalid argument)
798 case SocketOptionName
.DontLinger
:
799 jSocket
.setSoLinger(false, 0);
801 case SocketOptionName
.Linger
:
802 LingerOption lval
= obj_val
as LingerOption
;
805 jSocket
.setSoLinger(lval
.Enabled
, lval
.LingerTime
);
809 error
= 10022; //WSAEINVAL (Invalid argument)
812 case SocketOptionName
.KeepAlive
:
815 bval
= ((int)obj_val
== 0)?false:true;
819 bval
= (int_val
== 0)?false:true;
821 jSocket
.setKeepAlive(bval
);
823 case SocketOptionName
.NoDelay
:
826 bval
= ((int)obj_val
== 0)?false:true;
830 bval
= (int_val
== 0)?false:true;
832 jSocket
.setTcpNoDelay(bval
);
834 case SocketOptionName
.ReceiveBuffer
:
838 ival
= (int) obj_val
;
840 if (jServerSocket
!= null)
842 jServerSocket
.setReceiveBufferSize(ival
);
846 jSocket
.setReceiveBufferSize(ival
);
849 case SocketOptionName
.ReceiveTimeout
:
853 ival
= (int) obj_val
;
855 if (jServerSocket
!= null)
857 jServerSocket
.setSoTimeout(ival
);
861 jSocket
.setSoTimeout(ival
);
864 case SocketOptionName
.ReuseAddress
:
867 bval
= ((int)obj_val
== 0)?false:true;
871 bval
= (int_val
== 0)?false:true;
873 if (jServerSocket
!= null)
875 jServerSocket
.setReuseAddress(bval
);
879 jSocket
.setReuseAddress(bval
);
882 case SocketOptionName
.SendBuffer
:
886 ival
= (int) obj_val
;
888 jSocket
.setSendBufferSize(ival
);
890 case SocketOptionName
.OutOfBandInline
:
893 bval
= ((int)obj_val
== 0)?false:true;
897 bval
= (int_val
== 0)?false:true;
899 jSocket
.setOOBInline(bval
);
902 error
= 10022; //WSAEINVAL (Invalid argument)
908 error
= 10022; //WSAEINVAL (Invalid argument)
913 public void GetSocketOption_obj_internal(SocketOptionLevel level
, SocketOptionName name
,
914 out object obj_val
, out int error
)
919 if (jSocket
== null && jServerSocket
== null)
921 error
= 10022; //WSAEINVAL (Invalid argument)
927 case SocketOptionLevel
.IPv6
:
928 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
930 case SocketOptionLevel
.IP
:
931 if (name
!= SocketOptionName
.NoDelay
)
933 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
937 case SocketOptionLevel
.Udp
:
938 if (name
== SocketOptionName
.NoDelay
)
940 error
= 10042; //WSAENOPROTOOPT (Bad protocol option)
944 error
= 10022; //WSAEINVAL (Invalid argument)
947 case SocketOptionLevel
.Tcp
:
948 if (name
!= SocketOptionName
.NoDelay
)
950 error
= 10022; //WSAEINVAL (Invalid argument)
962 case SocketOptionName
.DontLinger
:
963 ival
= jSocket
.getSoLinger();
973 case SocketOptionName
.Linger
:
974 ival
= jSocket
.getSoLinger();
979 LingerOption ret
= new LingerOption((ival
!= 0), ival
);
982 case SocketOptionName
.KeepAlive
:
983 bval
= jSocket
.getKeepAlive();
984 obj_val
= ((bval
)?1:0);
986 case SocketOptionName
.NoDelay
:
987 bval
= jSocket
.getTcpNoDelay();
988 obj_val
= ((bval
)?1:0);
990 case SocketOptionName
.ReceiveBuffer
:
991 if (jServerSocket
!= null)
993 ival
= jServerSocket
.getReceiveBufferSize();
997 ival
= jSocket
.getReceiveBufferSize();
1001 case SocketOptionName
.ReceiveTimeout
:
1002 if (jServerSocket
!= null)
1004 ival
= jServerSocket
.getSoTimeout();
1008 ival
= jSocket
.getSoTimeout();
1012 case SocketOptionName
.ReuseAddress
:
1013 if (jServerSocket
!= null)
1015 bval
= jServerSocket
.getReuseAddress();
1019 bval
= jSocket
.getReuseAddress();
1021 obj_val
= ((bval
)?1:0);
1023 case SocketOptionName
.SendBuffer
:
1024 ival
= jSocket
.getSendBufferSize();
1027 case SocketOptionName
.OutOfBandInline
:
1028 bval
= jSocket
.getOOBInline();
1029 obj_val
= ((bval
)?1:0);
1032 error
= 10022; //WSAEINVAL (Invalid argument)
1038 error
= 10022; //WSAEINVAL (Invalid argument)
1043 public void GetSocketOption_arr_internal(SocketOptionLevel level
, SocketOptionName name
,
1044 ref byte[] byte_val
, out int error
)
1047 throw new NotImplementedException();
1050 public int WSAIoctl (int ioctl_code
, byte [] input
, byte [] output
, out int error
)
1053 throw new NotImplementedException();
1056 public void Shutdown_internal(SocketShutdown how
, out int error
)
1060 if (jServerSocket
!= null || jSocket
== null || !jSocket
.isConnected())
1062 error
= 10057; //WSAENOTCONN (Socket is not connected)
1070 case SocketShutdown
.Receive
:
1071 jSocket
.shutdownInput();
1073 case SocketShutdown
.Send
:
1074 jSocket
.shutdownOutput();
1076 case SocketShutdown
.Both
:
1077 jSocket
.shutdownInput();
1078 jSocket
.shutdownOutput();
1084 error
= 10022; //WSAEINVAL (Invalid argument)
1086 Console
.WriteLine("Caught exception during Shutdown_internal - {0}: {1}\n{2}", e
.GetType(), e
.Message
, e
.StackTrace
);
1091 private java
.io
.FileInputStream
searchDefaultCacerts()
1095 string javaHome
= java
.lang
.System
.getProperty("java.home");
1096 if(javaHome
== null)
1099 string keyStorePath
= javaHome
+ "/lib/security/cacerts";
1100 //Console.WriteLine("keyStorePath = {0}", keyStorePath);
1102 java
.io
.File f
= new java
.io
.File(keyStorePath
);
1105 return new java
.io
.FileInputStream(f
);
1111 Console
.WriteLine(e
.GetType() + ":" + e
.Message
+ "\n" + e
.StackTrace
);
1117 private SSLSocketFactory
getSSLSocketFactory()
1119 SSLSocketFactory factory
= null;
1123 //reading the keyStore path and password from the environment properties
1124 string keyStorePath
= java
.lang
.System
.getProperty("javax.net.ssl.keyStore");
1125 java
.io
.FileInputStream keyStoreStream
= null;
1126 if (keyStorePath
!= null)
1128 java
.io
.File file
= new java
.io
.File(keyStorePath
);
1130 keyStoreStream
= new java
.io
.FileInputStream(file
);
1132 keyStoreStream
= searchDefaultCacerts();
1135 keyStoreStream
= searchDefaultCacerts();
1137 string keyStorePassWord
= java
.lang
.System
.getProperty("javax.net.ssl.keyStorePassword");
1138 if (keyStorePassWord
== null)
1139 keyStorePassWord
= "changeit";
1140 char[] passphrase
= keyStorePassWord
.ToCharArray();
1142 //initiating SSLContext
1143 SSLContext ctx
= SSLContext
.getInstance("TLS");
1144 KeyManagerFactory kmf
= KeyManagerFactory
.getInstance(KeyManagerFactory
.getDefaultAlgorithm());
1145 TrustManagerFactory tmf
= TrustManagerFactory
.getInstance(TrustManagerFactory
.getDefaultAlgorithm());
1146 KeyStore ks
= KeyStore
.getInstance("JKS");
1147 if (keyStoreStream
!= null)
1148 ks
.load(keyStoreStream
,passphrase
);
1151 kmf
.init(ks
, passphrase
);
1153 ctx
.init(kmf
.getKeyManagers(), tmf
.getTrustManagers(), null);
1155 factory
= ctx
.getSocketFactory();
1161 Console
.WriteLine("Can't get SSL Socket Factory, the exception is {0}, {1}", e
.GetType(), e
.Message
);
1168 public GHSocket
ChangeToSSL(EndPoint remote_end
)
1170 if (jSocket
== null)
1172 throw new InvalidOperationException("The underlying socket is null");
1175 if (!jSocketChannel
.isBlocking())
1177 throw new NotImplementedException("The SSL Socket for non-blocking mode is not supported");
1180 SSLSocketFactory factory
= getSSLSocketFactory();
1181 if (factory
== null)
1183 throw new ApplicationException("Can't get SSL Socket Factory");
1188 // The problem with local address, when I closed the socket and try to create the new one
1189 // bounded to the given local address, I receive exception "Address already in use"
1190 IPEndPoint localEndPoint
= null;
1191 // IPEndPoint localEndPoint = (IPEndPoint) LocalEndPoint_internal(out err);
1193 // localEndPoint = null;
1195 IPEndPoint remoteEndPoint
= remote_end
as IPEndPoint
;
1196 if (remoteEndPoint
== null)
1198 remoteEndPoint
= (IPEndPoint
) RemoteEndPoint_internal(out err
);
1200 remoteEndPoint
= null;
1203 java
.net
.Socket sslSocket
= null;
1206 if (remoteEndPoint
!= null)
1208 if (localEndPoint
!= null)
1210 sslSocket
= factory
.createSocket(
1211 java
.net
.InetAddress
.getByName(remoteEndPoint
.Address
.ToString()),
1212 remoteEndPoint
.Port
,
1213 java
.net
.InetAddress
.getByName(localEndPoint
.Address
.ToString()),
1214 localEndPoint
.Port
);
1218 sslSocket
= factory
.createSocket(
1220 remoteEndPoint
.Address
.ToString(),
1221 remoteEndPoint
.Port
,
1225 if (sslSocket
!= null)
1227 String
[] protocols
= { "TLSv1", "SSLv3" }
;
1228 ((SSLSocket
)sslSocket
).setUseClientMode(true);
1229 ((SSLSocket
)sslSocket
).startHandshake();
1235 sslSocket
= factory
.createSocket();
1242 Console
.WriteLine("Can't create SSL Socket, the exception is {0}, {1}", e
.GetType(), e
.Message
);
1246 if (sslSocket
== null)
1248 // throw new ApplicationException("Can't create SSL Socket");
1249 // it is important to the Socket class to distinguish if the underlying
1250 // handle (GHSocket) is still valid and can be used as non-SSL, or it is already
1251 // closed by this function and can't be used any more.
1256 string[] arr = ((SSLSocket)sslSocket).getEnabledProtocols();
1259 foreach (string s in arr)
1260 Console.WriteLine("s:"+s);
1262 string [] arr1 = ((SSLSocket)sslSocket).getEnabledCipherSuites();
1265 foreach (string s in arr1)
1266 Console.WriteLine("s:"+s);
1270 return new GHStreamSocketSSL(sslSocket
);