7 namespace System.Net.Sockets
\r
10 /// Summary description for GHStreamSocket.
\r
12 internal class GHStreamSocket : GHSocket
\r
14 java.net.ServerSocket jServerSocket;
\r
15 java.net.Socket jSocket;
\r
16 java.nio.channels.ServerSocketChannel jServerSocketChannel;
\r
17 java.nio.channels.SocketChannel jSocketChannel;
\r
19 // This field I need because a bug in the java.nio.channels.SocketAdapter, which
\r
20 // returns local port 0 if the socket is not connected (even if the socket is bound)
\r
21 // so I need temporary use regular socket (not channel socket) to bind it to the
\r
22 // local address and use this address in the LocalPoint property and to create the
\r
23 // actual client/server channel sockets
\r
24 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
\r
25 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
\r
26 java.net.InetSocketAddress jTempLocalSocketAddress;
\r
28 public GHStreamSocket()
\r
30 jSocketChannel = java.nio.channels.SocketChannel.open();
\r
31 jSocket = jSocketChannel.socket();
\r
34 public GHStreamSocket(java.nio.channels.SocketChannel socketChannel)
\r
36 jSocketChannel = socketChannel;
\r
37 jSocket = jSocketChannel.socket();
\r
40 public override int GetHashCode ()
\r
42 if (jSocket == null && jServerSocket == null)
\r
45 if (jServerSocket != null) {
\r
46 return jServerSocket.ToString ().GetHashCode ();
\r
49 return jSocket.ToString ().GetHashCode ();
\r
52 public int Available_internal(out int error)
\r
57 if (jSocket == null || !jSocket.isConnected())
\r
64 r = jSocket.getInputStream().available();
\r
68 error = 10054; //WSAECONNRESET (Connection reset by peer)
\r
71 Console.WriteLine("Caught exception during Available_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
78 public void Blocking_internal(bool block, out int error)
\r
82 if (jSocket == null && jServerSocket == null)
\r
84 error = 10022; //WSAEINVAL (Invalid argument)
\r
90 if (jServerSocket != null)
\r
92 jServerSocketChannel.configureBlocking(block);
\r
96 jSocketChannel.configureBlocking(block);
\r
101 error = 10022; //WSAEINVAL (Invalid argument)
\r
103 Console.WriteLine("Caught exception during Blocking_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
108 public EndPoint LocalEndPoint_internal(out int error)
\r
111 java.net.InetSocketAddress localAddr = null;
\r
115 if (jTempLocalSocketAddress != null)
\r
117 localAddr = jTempLocalSocketAddress;
\r
119 else if (jServerSocket != null)
\r
121 localAddr = (java.net.InetSocketAddress)jServerSocket.getLocalSocketAddress();
\r
125 localAddr = (java.net.InetSocketAddress)jSocket.getLocalSocketAddress();
\r
128 catch (Exception e)
\r
132 Console.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
136 if (localAddr == null || localAddr.getAddress() == null || localAddr.getPort() < 0)
\r
141 IPHostEntry lipa = Dns.Resolve(localAddr.getHostName());
\r
142 IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], localAddr.getPort());
\r
146 public EndPoint RemoteEndPoint_internal(out int error)
\r
149 java.net.InetSocketAddress remoteAddr = null;
\r
151 if (jSocket == null || !jSocket.isBound())
\r
158 remoteAddr = (java.net.InetSocketAddress)jSocket.getRemoteSocketAddress();
\r
160 catch (Exception e)
\r
164 Console.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
168 if (remoteAddr == null || remoteAddr.getAddress() == null || remoteAddr.getPort() <= 0)
\r
170 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
174 IPHostEntry lipa = Dns.Resolve(remoteAddr.getHostName());
\r
175 IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], remoteAddr.getPort());
\r
179 public GHSocket Accept_internal(out int error)
\r
183 if (jServerSocket == null)
\r
185 throw new InvalidOperationException("You must call Bind and Listen before calling Accept.");
\r
191 If this channel is in non-blocking mode then this method will immediately
\r
192 return null if there are no pending connections.
\r
193 Otherwise it will block indefinitely until a new connection is
\r
194 available or an I/O error occurs.
\r
196 java.nio.channels.SocketChannel acceptedSocket = jServerSocketChannel.accept();
\r
197 if (acceptedSocket == null)
\r
199 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
\r
201 Console.WriteLine("The Accept_internal is in non-blocking mode and no pending connections are available");
\r
206 return new GHStreamSocket(acceptedSocket);
\r
208 catch (Exception e)
\r
210 error = 10061; //WSAECONNREFUSED (Connection refused)
\r
212 Console.WriteLine("Caught exception during Accept_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
219 public void Bind_internal(EndPoint sa, out int error)
\r
222 IPEndPoint addr = sa as IPEndPoint;
\r
225 error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
\r
229 if (jSocket == null || jSocket.isBound() || jSocket.isConnected() || jSocketChannel.isConnectionPending())
\r
231 error = 10022; //WSAEINVAL (Invalid argument)
\r
237 // This code I need because a bug in the java.nio.channels.SocketAdapter, which
\r
238 // returns local port 0 if the socket is not connected (even if the socket is bound)
\r
239 // so I need temporary use regular socket (not channel socket) to bind it to the
\r
240 // local address and use this address in the LocalPoint property and to create the
\r
241 // actual client/server channel sockets
\r
242 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
\r
243 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
\r
244 java.net.Socket jTempSocket = new java.net.Socket();
\r
245 jTempSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),
\r
247 jTempLocalSocketAddress = (java.net.InetSocketAddress)jTempSocket.getLocalSocketAddress();
\r
248 jTempSocket.close();
\r
249 jSocket.bind(jTempLocalSocketAddress);
\r
251 catch (Exception e)
\r
253 error = 10048; //WSAEADDRINUSE (Address already in use)
\r
255 Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
260 public void Close_internal(out int error)
\r
264 if (jServerSocket != null)
\r
268 jServerSocket.close();
\r
270 catch (Exception e)
\r
272 error = 10022; //WSAEINVAL (Invalid argument)
\r
274 Console.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
279 jServerSocketChannel.close();
\r
281 catch (Exception e)
\r
283 error = 10022; //WSAEINVAL (Invalid argument)
\r
285 Console.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
288 jServerSocket = null;
\r
289 jServerSocketChannel = null;
\r
291 else if (jSocket != null)
\r
297 catch (Exception e)
\r
299 error = 10022; //WSAEINVAL (Invalid argument)
\r
301 Console.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
306 jSocketChannel.close();
\r
308 catch (Exception e)
\r
310 error = 10022; //WSAEINVAL (Invalid argument)
\r
312 Console.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
316 jSocketChannel = null;
\r
320 public void Connect_internal(EndPoint sa, out int error)
\r
324 IPEndPoint addr = sa as IPEndPoint;
\r
327 error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
\r
331 if (jSocket == null)
\r
333 error = 10022; //WSAEINVAL (Invalid argument)
\r
337 if (jSocket.isConnected() || jSocketChannel.isConnectionPending())
\r
339 error = 10056; //WSAEISCONN (Socket is already connected)
\r
346 If this channel is in non-blocking mode then an invocation of this method
\r
347 initiates a non-blocking connection operation. If the connection is
\r
348 established immediately, as can happen with a local connection, then this
\r
349 method returns true. Otherwise this method returns false.
\r
350 If this channel is in blocking mode then an invocation of this method
\r
351 will block until the connection is established or an I/O error occurs.
\r
353 bool status = jSocketChannel.connect(new java.net.InetSocketAddress(
\r
354 java.net.InetAddress.getByName(addr.Address.ToString()),
\r
358 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
\r
361 catch (java.nio.channels.AlreadyConnectedException ae)
\r
363 error = 10056; //WSAEISCONN (Socket is already connected)
\r
365 catch (java.nio.channels.ConnectionPendingException cpe)
\r
367 error = 10036; //WSAEINPROGRESS (Operation now in progress)
\r
369 catch (java.nio.channels.UnresolvedAddressException uae)
\r
371 error = 10039; //WSAEDESTADDRREQ (Destination address required)
\r
373 catch (java.nio.channels.UnsupportedAddressTypeException uate)
\r
375 error = 10041; //WSAEPROTOTYPE (Protocol wrong type for socket)
\r
377 catch (Exception e)
\r
379 error = 10061; //WSAECONNREFUSED (Connection refused)
\r
381 Console.WriteLine("Caught exception during Connect_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
386 public void Listen_internal(int backlog, out int error)
\r
390 if (jSocket == null || !jSocket.isBound())
\r
392 error = 10022; //WSAEINVAL (Invalid argument)
\r
396 if (jSocket.isConnected() || jSocketChannel.isConnectionPending())
\r
398 error = 10056; //WSAEISCONN (Socket is already connected)
\r
402 bool blockMode = jSocketChannel.isBlocking();
\r
403 bool reuseAddr = jSocket.getReuseAddress();
\r
409 catch (Exception e)
\r
412 Console.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
418 jSocketChannel.close();
\r
420 catch (Exception e)
\r
423 Console.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
428 jSocketChannel = null;
\r
432 jServerSocketChannel = java.nio.channels.ServerSocketChannel.open();
\r
433 jServerSocket = jServerSocketChannel.socket();
\r
434 jServerSocket.bind(jTempLocalSocketAddress, backlog);
\r
435 jServerSocketChannel.configureBlocking(blockMode);
\r
436 jServerSocket.setReuseAddress(reuseAddr);
\r
438 catch (Exception e)
\r
440 error = 10048; //WSAEADDRINUSE (Address already in use)
\r
442 Console.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
447 public bool Poll_internal (SelectMode mode, int timeout, Socket source, out int error)
\r
451 if (mode == SelectMode.SelectError && !jSocketChannel.isConnectionPending())
\r
456 java.nio.channels.Selector selector = java.nio.channels.Selector.open();
\r
457 RegisterSelector(selector, ((mode == SelectMode.SelectRead)?0:1), source, out error);
\r
462 GHSocketFactory.CloseSelector(selector);
\r
463 return (mode == SelectMode.SelectError);
\r
466 bool retVal = false;
\r
468 long timeOutMillis = 1;
\r
473 else if (timeout > 999)
\r
475 timeOutMillis = (long)(timeout / 1000);
\r
478 int readyCount = 0;
\r
481 readyCount = selector.select(timeOutMillis);
\r
483 catch (Exception e)
\r
485 error = 10022; //WSAEINVAL (Invalid argument)
\r
487 Console.WriteLine("Caught exception during Poll_internal selector.select - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
491 if (readyCount > 0)
\r
493 if (jSocket != null && jSocketChannel.isConnectionPending())
\r
495 bool status = false;
\r
498 status = jSocketChannel.finishConnect();
\r
500 catch (Exception e)
\r
503 Console.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
508 retVal = (mode != SelectMode.SelectError);
\r
512 retVal = (mode == SelectMode.SelectError);
\r
521 GHSocketFactory.CloseSelector(selector);
\r
526 public void RegisterSelector(java.nio.channels.Selector selector, int mode, Socket source, out int error)
\r
529 if (jServerSocket != null)
\r
531 // only accept operation, which included to the read list, is allowed for server sockets
\r
534 // error = 10038; //WSAENOTSOCK (Socket operation on nonsocket)
\r
536 Console.WriteLine("RegisterSelector, invalid mode {0} for the server socket", mode);
\r
543 if (jServerSocketChannel.isBlocking())
\r
546 A channel must be placed into non-blocking mode before being registered
\r
547 with a selector, and may not be returned to blocking mode until it has been
\r
550 jServerSocketChannel.configureBlocking(false);
\r
553 jServerSocketChannel.register(selector, java.nio.channels.SelectionKey.OP_ACCEPT, source);
\r
555 catch (Exception e)
\r
557 error = 10022; //WSAEINVAL (Invalid argument)
\r
559 Console.WriteLine("Caught exception during RegisterSelector, register server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
567 int ops = java.nio.channels.SelectionKey.OP_READ;
\r
570 if (jSocketChannel.isConnectionPending())
\r
572 ops = java.nio.channels.SelectionKey.OP_CONNECT;
\r
576 ops = java.nio.channels.SelectionKey.OP_WRITE;
\r
580 if (jSocketChannel.isBlocking())
\r
583 A channel must be placed into non-blocking mode before being registered
\r
584 with a selector, and may not be returned to blocking mode until it has been
\r
587 jSocketChannel.configureBlocking(false);
\r
590 jSocketChannel.register(selector, ops, source);
\r
592 catch (Exception e)
\r
594 error = 10022; //WSAEINVAL (Invalid argument)
\r
596 Console.WriteLine("Caught exception during RegisterSelector, register client socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
602 public bool CheckConnectionFinished()
\r
604 bool status = true;
\r
605 if (jSocket != null && jSocketChannel.isConnectionPending())
\r
609 status = jSocketChannel.finishConnect();
\r
611 catch (Exception e)
\r
615 Console.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
623 public int Receive_internal(byte[] buffer, int offset, int count, SocketFlags flags,
\r
629 if (jSocket == null)
\r
631 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
637 if (jSocketChannel.isConnectionPending())
\r
639 bool status = jSocketChannel.finishConnect();
\r
642 error = 10022; //WSAEINVAL (Invalid argument)
\r
644 Console.WriteLine("Receive_internal, jSocketChannel.finishConnect return false");
\r
649 else if (!jSocketChannel.isConnected())
\r
651 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
655 java.nio.ByteBuffer readBuff = java.nio.ByteBuffer.wrap(vmw.common.TypeUtils.ToSByteArray(buffer), offset, count);
\r
656 ret = jSocketChannel.read(readBuff);
\r
657 if (ret < 0) ret = 0;
\r
659 catch (Exception e)
\r
661 error = 10054; //WSAECONNRESET (Connection reset by peer)
\r
664 Console.WriteLine("Caught exception during Receive_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
668 if (ret == 0 && !jSocketChannel.isBlocking())
\r
670 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
\r
675 public int RecvFrom_internal(byte[] buffer, int offset, int count, SocketFlags flags,
\r
676 ref SocketAddress sockaddr, out int error)
\r
678 return Receive_internal(buffer, offset, count, flags, out error);
\r
681 public int Send_internal(byte[] buf, int offset, int count, SocketFlags flags,
\r
687 if (jSocket == null)
\r
689 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
695 if (jSocketChannel.isConnectionPending())
\r
697 bool status = jSocketChannel.finishConnect();
\r
700 error = 10022; //WSAEINVAL (Invalid argument)
\r
702 Console.WriteLine("Send_internal, jSocketChannel.finishConnect return false");
\r
707 else if (!jSocketChannel.isConnected())
\r
709 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
713 java.nio.ByteBuffer writeBuff = java.nio.ByteBuffer.wrap(vmw.common.TypeUtils.ToSByteArray(buf), offset, count);
\r
714 ret = jSocketChannel.write(writeBuff);
\r
715 if (ret < 0) ret = 0;
\r
717 catch (Exception e)
\r
719 error = 10054; //WSAECONNRESET (Connection reset by peer)
\r
722 Console.WriteLine("Caught exception during Send_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
726 if (ret == 0 && !jSocketChannel.isBlocking())
\r
728 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)
\r
733 public int SendTo_internal(byte[] buffer, int offset, int count,
\r
734 SocketFlags flags, SocketAddress sa, out int error)
\r
736 return Send_internal(buffer, offset, count, flags, out error);
\r
739 public void SetSocketOption_internal (SocketOptionLevel level,
\r
740 SocketOptionName name, object obj_val,
\r
741 byte [] byte_val, int int_val, out int error)
\r
745 if (byte_val != null)
\r
748 throw new NotImplementedException();
\r
751 if (jSocket == null && jServerSocket == null)
\r
753 error = 10022; //WSAEINVAL (Invalid argument)
\r
759 case SocketOptionLevel.IPv6:
\r
760 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
762 case SocketOptionLevel.IP:
\r
763 if (name != SocketOptionName.NoDelay)
\r
765 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
769 case SocketOptionLevel.Udp:
\r
770 if (name == SocketOptionName.NoDelay)
\r
772 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
776 error = 10022; //WSAEINVAL (Invalid argument)
\r
779 case SocketOptionLevel.Tcp:
\r
780 if (name != SocketOptionName.NoDelay)
\r
782 error = 10022; //WSAEINVAL (Invalid argument)
\r
794 case SocketOptionName.DontLinger:
\r
795 jSocket.setSoLinger(false, 0);
\r
797 case SocketOptionName.Linger:
\r
798 LingerOption lval = obj_val as LingerOption;
\r
801 jSocket.setSoLinger(lval.Enabled, lval.LingerTime);
\r
805 error = 10022; //WSAEINVAL (Invalid argument)
\r
808 case SocketOptionName.KeepAlive:
\r
809 if (obj_val != null)
\r
811 bval = ((int)obj_val == 0)?false:true;
\r
815 bval = (int_val == 0)?false:true;
\r
817 jSocket.setKeepAlive(bval);
\r
819 case SocketOptionName.NoDelay:
\r
820 if (obj_val != null)
\r
822 bval = ((int)obj_val == 0)?false:true;
\r
826 bval = (int_val == 0)?false:true;
\r
828 jSocket.setTcpNoDelay(bval);
\r
830 case SocketOptionName.ReceiveBuffer:
\r
832 if (obj_val != null)
\r
834 ival = (int) obj_val;
\r
836 if (jServerSocket != null)
\r
838 jServerSocket.setReceiveBufferSize(ival);
\r
842 jSocket.setReceiveBufferSize(ival);
\r
845 case SocketOptionName.ReceiveTimeout:
\r
847 if (obj_val != null)
\r
849 ival = (int) obj_val;
\r
851 if (jServerSocket != null)
\r
853 jServerSocket.setSoTimeout(ival);
\r
857 jSocket.setSoTimeout(ival);
\r
860 case SocketOptionName.ReuseAddress:
\r
861 if (obj_val != null)
\r
863 bval = ((int)obj_val == 0)?false:true;
\r
867 bval = (int_val == 0)?false:true;
\r
869 if (jServerSocket != null)
\r
871 jServerSocket.setReuseAddress(bval);
\r
875 jSocket.setReuseAddress(bval);
\r
878 case SocketOptionName.SendBuffer:
\r
880 if (obj_val != null)
\r
882 ival = (int) obj_val;
\r
884 jSocket.setSendBufferSize(ival);
\r
886 case SocketOptionName.OutOfBandInline:
\r
887 if (obj_val != null)
\r
889 bval = ((int)obj_val == 0)?false:true;
\r
893 bval = (int_val == 0)?false:true;
\r
895 jSocket.setOOBInline(bval);
\r
898 error = 10022; //WSAEINVAL (Invalid argument)
\r
902 catch (Exception e)
\r
904 error = 10022; //WSAEINVAL (Invalid argument)
\r
909 public void GetSocketOption_obj_internal(SocketOptionLevel level, SocketOptionName name,
\r
910 out object obj_val, out int error)
\r
915 if (jSocket == null && jServerSocket == null)
\r
917 error = 10022; //WSAEINVAL (Invalid argument)
\r
923 case SocketOptionLevel.IPv6:
\r
924 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
926 case SocketOptionLevel.IP:
\r
927 if (name != SocketOptionName.NoDelay)
\r
929 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
933 case SocketOptionLevel.Udp:
\r
934 if (name == SocketOptionName.NoDelay)
\r
936 error = 10042; //WSAENOPROTOOPT (Bad protocol option)
\r
940 error = 10022; //WSAEINVAL (Invalid argument)
\r
943 case SocketOptionLevel.Tcp:
\r
944 if (name != SocketOptionName.NoDelay)
\r
946 error = 10022; //WSAEINVAL (Invalid argument)
\r
958 case SocketOptionName.DontLinger:
\r
959 ival = jSocket.getSoLinger();
\r
969 case SocketOptionName.Linger:
\r
970 ival = jSocket.getSoLinger();
\r
975 LingerOption ret = new LingerOption((ival != 0), ival);
\r
978 case SocketOptionName.KeepAlive:
\r
979 bval = jSocket.getKeepAlive();
\r
980 obj_val = ((bval)?1:0);
\r
982 case SocketOptionName.NoDelay:
\r
983 bval = jSocket.getTcpNoDelay();
\r
984 obj_val = ((bval)?1:0);
\r
986 case SocketOptionName.ReceiveBuffer:
\r
987 if (jServerSocket != null)
\r
989 ival = jServerSocket.getReceiveBufferSize();
\r
993 ival = jSocket.getReceiveBufferSize();
\r
997 case SocketOptionName.ReceiveTimeout:
\r
998 if (jServerSocket != null)
\r
1000 ival = jServerSocket.getSoTimeout();
\r
1004 ival = jSocket.getSoTimeout();
\r
1008 case SocketOptionName.ReuseAddress:
\r
1009 if (jServerSocket != null)
\r
1011 bval = jServerSocket.getReuseAddress();
\r
1015 bval = jSocket.getReuseAddress();
\r
1017 obj_val = ((bval)?1:0);
\r
1019 case SocketOptionName.SendBuffer:
\r
1020 ival = jSocket.getSendBufferSize();
\r
1023 case SocketOptionName.OutOfBandInline:
\r
1024 bval = jSocket.getOOBInline();
\r
1025 obj_val = ((bval)?1:0);
\r
1028 error = 10022; //WSAEINVAL (Invalid argument)
\r
1032 catch (Exception e)
\r
1034 error = 10022; //WSAEINVAL (Invalid argument)
\r
1039 public void GetSocketOption_arr_internal(SocketOptionLevel level, SocketOptionName name,
\r
1040 ref byte[] byte_val, out int error)
\r
1043 throw new NotImplementedException();
\r
1046 public int WSAIoctl (int ioctl_code, byte [] input, byte [] output, out int error)
\r
1049 throw new NotImplementedException();
\r
1052 public void Shutdown_internal(SocketShutdown how, out int error)
\r
1056 if (jServerSocket != null || jSocket == null || !jSocket.isConnected())
\r
1058 error = 10057; //WSAENOTCONN (Socket is not connected)
\r
1066 case SocketShutdown.Receive:
\r
1067 jSocket.shutdownInput();
\r
1069 case SocketShutdown.Send:
\r
1070 jSocket.shutdownOutput();
\r
1072 case SocketShutdown.Both:
\r
1073 jSocket.shutdownInput();
\r
1074 jSocket.shutdownOutput();
\r
1078 catch (Exception e)
\r
1080 error = 10022; //WSAEINVAL (Invalid argument)
\r
1082 Console.WriteLine("Caught exception during Shutdown_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
1087 private java.io.FileInputStream searchDefaultCacerts()
\r
1091 string javaHome = java.lang.System.getProperty("java.home");
\r
1092 if(javaHome == null)
\r
1095 string keyStorePath = javaHome + "/lib/security/cacerts";
\r
1096 //Console.WriteLine("keyStorePath = {0}", keyStorePath);
\r
1098 java.io.File f = new java.io.File(keyStorePath);
\r
1101 return new java.io.FileInputStream(f);
\r
1103 catch(Exception e)
\r
1107 Console.WriteLine(e.GetType() + ":" + e.Message + "\n" + e.StackTrace);
\r
1113 private SSLSocketFactory getSSLSocketFactory()
\r
1115 SSLSocketFactory factory = null;
\r
1119 //reading the keyStore path and password from the environment properties
\r
1120 string keyStorePath = java.lang.System.getProperty("javax.net.ssl.keyStore");
\r
1121 java.io.FileInputStream keyStoreStream = null;
\r
1122 if (keyStorePath != null)
\r
1124 java.io.File file = new java.io.File(keyStorePath);
\r
1126 keyStoreStream = new java.io.FileInputStream(file);
\r
1128 keyStoreStream = searchDefaultCacerts();
\r
1131 keyStoreStream = searchDefaultCacerts();
\r
1133 string keyStorePassWord = java.lang.System.getProperty("javax.net.ssl.keyStorePassword");
\r
1134 if (keyStorePassWord == null)
\r
1135 keyStorePassWord = "changeit";
\r
1136 char[] passphrase = keyStorePassWord.ToCharArray();
\r
1138 //initiating SSLContext
\r
1139 SSLContext ctx = SSLContext.getInstance("TLS");
\r
1140 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
\r
1141 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
\r
1142 KeyStore ks = KeyStore.getInstance("JKS");
\r
1143 if (keyStoreStream != null)
\r
1144 ks.load(keyStoreStream,passphrase);
\r
1146 ks.load(null,null);
\r
1147 kmf.init(ks, passphrase);
\r
1149 ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
\r
1151 factory = ctx.getSocketFactory();
\r
1153 catch (Exception e)
\r
1157 Console.WriteLine("Can't get SSL Socket Factory, the exception is {0}, {1}", e.GetType(), e.Message);
\r
1164 public GHSocket ChangeToSSL(EndPoint remote_end)
\r
1166 if (jSocket == null)
\r
1168 throw new InvalidOperationException("The underlying socket is null");
\r
1171 if (!jSocketChannel.isBlocking())
\r
1173 throw new NotImplementedException("The SSL Socket for non-blocking mode is not supported");
\r
1176 SSLSocketFactory factory = getSSLSocketFactory();
\r
1177 if (factory == null)
\r
1179 throw new ApplicationException("Can't get SSL Socket Factory");
\r
1184 // The problem with local address, when I closed the socket and try to create the new one
\r
1185 // bounded to the given local address, I receive exception "Address already in use"
\r
1186 IPEndPoint localEndPoint = null;
\r
1187 // IPEndPoint localEndPoint = (IPEndPoint) LocalEndPoint_internal(out err);
\r
1189 // localEndPoint = null;
\r
1191 IPEndPoint remoteEndPoint = remote_end as IPEndPoint;
\r
1192 if (remoteEndPoint == null)
\r
1194 remoteEndPoint = (IPEndPoint) RemoteEndPoint_internal(out err);
\r
1196 remoteEndPoint = null;
\r
1199 java.net.Socket sslSocket = null;
\r
1202 if (remoteEndPoint != null)
\r
1204 if (localEndPoint != null)
\r
1206 sslSocket = factory.createSocket(
\r
1207 java.net.InetAddress.getByName(remoteEndPoint.Address.ToString()),
\r
1208 remoteEndPoint.Port,
\r
1209 java.net.InetAddress.getByName(localEndPoint.Address.ToString()),
\r
1210 localEndPoint.Port);
\r
1214 sslSocket = factory.createSocket(
\r
1216 remoteEndPoint.Address.ToString(),
\r
1217 remoteEndPoint.Port,
\r
1221 if (sslSocket != null)
\r
1223 String[] protocols = { "TLSv1", "SSLv3" };
\r
1224 ((SSLSocket)sslSocket).setUseClientMode(true);
\r
1225 ((SSLSocket)sslSocket).startHandshake();
\r
1231 sslSocket = factory.createSocket();
\r
1234 catch (Exception e)
\r
1238 Console.WriteLine("Can't create SSL Socket, the exception is {0}, {1}", e.GetType(), e.Message);
\r
1242 if (sslSocket == null)
\r
1244 // throw new ApplicationException("Can't create SSL Socket");
\r
1245 // it is important to the Socket class to distinguish if the underlying
\r
1246 // handle (GHSocket) is still valid and can be used as non-SSL, or it is already
\r
1247 // closed by this function and can't be used any more.
\r
1252 string[] arr = ((SSLSocket)sslSocket).getEnabledProtocols();
\r
1255 foreach (string s in arr)
\r
1256 Console.WriteLine("s:"+s);
\r
1258 string [] arr1 = ((SSLSocket)sslSocket).getEnabledCipherSuites();
\r
1261 foreach (string s in arr1)
\r
1262 Console.WriteLine("s:"+s);
\r
1266 return new GHStreamSocketSSL(sslSocket);
\r