PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System/System.Net.Sockets/GHStreamSocketSSL.jvm.cs

https://github.com/iainlane/mono
C# | 620 lines | 556 code | 60 blank | 4 comment | 95 complexity | e5f548140f01e8cca4d92eb268dfeccb MD5 | raw file
  1. using System;
  2. using System.Net;
  3. namespace System.Net.Sockets
  4. {
  5. /// <summary>
  6. /// Summary description for GHStreamSocket.
  7. /// </summary>
  8. internal class GHStreamSocketSSL : GHSocket
  9. {
  10. java.net.Socket jSocket;
  11. public GHStreamSocketSSL(java.net.Socket sslSocket)
  12. {
  13. jSocket = sslSocket;
  14. }
  15. public override int GetHashCode ()
  16. {
  17. if (jSocket == null)
  18. return -1;
  19. return jSocket.ToString().GetHashCode();
  20. }
  21. public int Available_internal(out int error)
  22. {
  23. error = 0;
  24. int r = 0;
  25. if (jSocket == null || !jSocket.isConnected())
  26. {
  27. return r;
  28. }
  29. try
  30. {
  31. r = jSocket.getInputStream().available();
  32. }
  33. catch (Exception e)
  34. {
  35. error = 10054; //WSAECONNRESET (Connection reset by peer)
  36. r = 0;
  37. #if DEBUG
  38. Console.WriteLine("Caught exception during Available_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  39. #endif
  40. }
  41. return r;
  42. }
  43. public void Blocking_internal(bool block, out int error)
  44. {
  45. //SVETA: see in the non-blocking io
  46. error = 0;
  47. if (block == false)
  48. throw new NotSupportedException();
  49. }
  50. public EndPoint LocalEndPoint_internal(out int error)
  51. {
  52. error = 0;
  53. java.net.InetSocketAddress localAddr = null;
  54. try
  55. {
  56. localAddr = (java.net.InetSocketAddress)jSocket.getLocalSocketAddress();
  57. }
  58. catch (Exception e)
  59. {
  60. localAddr = null;
  61. #if DEBUG
  62. Console.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  63. #endif
  64. }
  65. if (localAddr == null || localAddr.getAddress() == null || localAddr.getPort() < 0)
  66. {
  67. return null;
  68. }
  69. IPHostEntry lipa = Dns.Resolve(localAddr.getHostName());
  70. IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], localAddr.getPort());
  71. return ret;
  72. }
  73. public EndPoint RemoteEndPoint_internal(out int error)
  74. {
  75. error = 0;
  76. java.net.InetSocketAddress remoteAddr = null;
  77. if (jSocket == null || !jSocket.isBound())
  78. {
  79. return null;
  80. }
  81. try
  82. {
  83. remoteAddr = (java.net.InetSocketAddress)jSocket.getRemoteSocketAddress();
  84. }
  85. catch (Exception e)
  86. {
  87. remoteAddr = null;
  88. #if DEBUG
  89. Console.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  90. #endif
  91. }
  92. if (remoteAddr == null || remoteAddr.getAddress() == null || remoteAddr.getPort() <= 0)
  93. {
  94. error = 10057; //WSAENOTCONN (Socket is not connected)
  95. return null;
  96. }
  97. IPHostEntry lipa = Dns.Resolve(remoteAddr.getHostName());
  98. IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], remoteAddr.getPort());
  99. return ret;
  100. }
  101. public GHSocket Accept_internal(out int error)
  102. {
  103. error = 10022; //WSAEINVAL (Invalid argument)
  104. return null;
  105. }
  106. public void Bind_internal(EndPoint sa, out int error)
  107. {
  108. error = 0;
  109. IPEndPoint addr = sa as IPEndPoint;
  110. if (addr == null)
  111. {
  112. error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
  113. return;
  114. }
  115. if (jSocket == null || jSocket.isBound() || jSocket.isConnected())
  116. {
  117. error = 10022; //WSAEINVAL (Invalid argument)
  118. return;
  119. }
  120. try
  121. {
  122. jSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),
  123. addr.Port));
  124. }
  125. catch (Exception e)
  126. {
  127. error = 10048; //WSAEADDRINUSE (Address already in use)
  128. #if DEBUG
  129. Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  130. #endif
  131. }
  132. }
  133. public void Close_internal(out int error)
  134. {
  135. error = 0;
  136. try
  137. {
  138. if (jSocket != null)
  139. {
  140. jSocket.close();
  141. jSocket = null;
  142. }
  143. }
  144. catch (Exception e)
  145. {
  146. error = 10022; //WSAEINVAL (Invalid argument)
  147. #if DEBUG
  148. Console.WriteLine("Caught exception during Close_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  149. #endif
  150. }
  151. }
  152. public void Connect_internal(EndPoint sa, out int error)
  153. {
  154. error = 0;
  155. IPEndPoint addr = sa as IPEndPoint;
  156. if (addr == null)
  157. {
  158. error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
  159. return;
  160. }
  161. if (jSocket == null)
  162. {
  163. error = 10022; //WSAEINVAL (Invalid argument)
  164. return;
  165. }
  166. if (jSocket.isConnected())
  167. {
  168. error = 10056; //WSAEISCONN (Socket is already connected)
  169. return;
  170. }
  171. try
  172. {
  173. jSocket.connect(new java.net.InetSocketAddress(
  174. java.net.InetAddress.getByName(addr.Address.ToString()),
  175. addr.Port));
  176. }
  177. catch (Exception e)
  178. {
  179. error = 10061; //WSAECONNREFUSED (Connection refused)
  180. #if DEBUG
  181. Console.WriteLine("Caught exception during Connect_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  182. #endif
  183. }
  184. }
  185. public void Listen_internal(int backlog, out int error)
  186. {
  187. error = 10022; //WSAEINVAL (Invalid argument)
  188. return;
  189. }
  190. public bool Poll_internal (SelectMode mode, int timeout, Socket source, out int error)
  191. {
  192. error = 0;
  193. throw new NotImplementedException();
  194. }
  195. public int Receive_internal(byte[] buffer, int offset, int count, SocketFlags flags,
  196. out int error)
  197. {
  198. error = 0;
  199. int ret = 0;
  200. if (jSocket == null || !jSocket.isConnected())
  201. {
  202. error = 10057; //WSAENOTCONN (Socket is not connected)
  203. return ret;
  204. }
  205. try
  206. {
  207. ret = jSocket.getInputStream().read(vmw.common.TypeUtils.ToSByteArray(buffer), offset, count);
  208. if (ret < 0) ret = 0;
  209. }
  210. catch (Exception e)
  211. {
  212. error = 10054; //WSAECONNRESET (Connection reset by peer)
  213. ret = 0;
  214. #if DEBUG
  215. Console.WriteLine("Caught exception during Receive_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  216. #endif
  217. }
  218. return ret;
  219. }
  220. public int RecvFrom_internal(byte[] buffer, int offset, int count, SocketFlags flags,
  221. ref SocketAddress sockaddr, out int error)
  222. {
  223. return Receive_internal(buffer, offset, count, flags, out error);
  224. }
  225. public int Send_internal(byte[] buf, int offset, int count, SocketFlags flags,
  226. out int error)
  227. {
  228. error = 0;
  229. int ret = 0;
  230. if (jSocket == null || !jSocket.isConnected())
  231. {
  232. error = 10057; //WSAENOTCONN (Socket is not connected)
  233. return ret;
  234. }
  235. try
  236. {
  237. jSocket.getOutputStream().write(vmw.common.TypeUtils.ToSByteArray(buf), offset, count);
  238. ret = count;
  239. }
  240. catch (Exception e)
  241. {
  242. error = 10054; //WSAECONNRESET (Connection reset by peer)
  243. ret = 0;
  244. #if DEBUG
  245. Console.WriteLine("Caught exception during Send_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  246. #endif
  247. }
  248. return ret;
  249. }
  250. public int SendTo_internal(byte[] buffer, int offset, int count,
  251. SocketFlags flags, SocketAddress sa, out int error)
  252. {
  253. return Send_internal(buffer, offset, count, flags, out error);
  254. }
  255. public void SetSocketOption_internal (SocketOptionLevel level,
  256. SocketOptionName name, object obj_val,
  257. byte [] byte_val, int int_val, out int error)
  258. {
  259. error = 0;
  260. if (byte_val != null)
  261. {
  262. error = -1;
  263. throw new NotImplementedException();
  264. }
  265. if (jSocket == null)
  266. {
  267. error = 10022; //WSAEINVAL (Invalid argument)
  268. return;
  269. }
  270. switch (level)
  271. {
  272. case SocketOptionLevel.IPv6:
  273. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  274. return;
  275. case SocketOptionLevel.IP:
  276. if (name != SocketOptionName.NoDelay)
  277. {
  278. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  279. return;
  280. }
  281. break;
  282. case SocketOptionLevel.Udp:
  283. if (name == SocketOptionName.NoDelay)
  284. {
  285. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  286. }
  287. else
  288. {
  289. error = 10022; //WSAEINVAL (Invalid argument)
  290. }
  291. return;
  292. case SocketOptionLevel.Tcp:
  293. if (name != SocketOptionName.NoDelay)
  294. {
  295. error = 10022; //WSAEINVAL (Invalid argument)
  296. return;
  297. }
  298. break;
  299. }
  300. try
  301. {
  302. bool bval = false;
  303. int ival = 0;
  304. switch (name)
  305. {
  306. case SocketOptionName.DontLinger:
  307. jSocket.setSoLinger(false, 0);
  308. break;
  309. case SocketOptionName.Linger:
  310. LingerOption lval = obj_val as LingerOption;
  311. if (lval != null)
  312. {
  313. jSocket.setSoLinger(lval.Enabled, lval.LingerTime);
  314. }
  315. else
  316. {
  317. error = 10022; //WSAEINVAL (Invalid argument)
  318. }
  319. break;
  320. case SocketOptionName.KeepAlive:
  321. if (obj_val != null)
  322. {
  323. bval = ((int)obj_val == 0)?false:true;
  324. }
  325. else
  326. {
  327. bval = (int_val == 0)?false:true;
  328. }
  329. jSocket.setKeepAlive(bval);
  330. break;
  331. case SocketOptionName.NoDelay:
  332. if (obj_val != null)
  333. {
  334. bval = ((int)obj_val == 0)?false:true;
  335. }
  336. else
  337. {
  338. bval = (int_val == 0)?false:true;
  339. }
  340. jSocket.setTcpNoDelay(bval);
  341. break;
  342. case SocketOptionName.ReceiveBuffer:
  343. ival = int_val;
  344. if (obj_val != null)
  345. {
  346. ival = (int) obj_val;
  347. }
  348. jSocket.setReceiveBufferSize(ival);
  349. break;
  350. case SocketOptionName.ReceiveTimeout:
  351. ival = int_val;
  352. if (obj_val != null)
  353. {
  354. ival = (int) obj_val;
  355. }
  356. jSocket.setSoTimeout(ival);
  357. break;
  358. case SocketOptionName.ReuseAddress:
  359. if (obj_val != null)
  360. {
  361. bval = ((int)obj_val == 0)?false:true;
  362. }
  363. else
  364. {
  365. bval = (int_val == 0)?false:true;
  366. }
  367. jSocket.setReuseAddress(bval);
  368. break;
  369. case SocketOptionName.SendBuffer:
  370. ival = int_val;
  371. if (obj_val != null)
  372. {
  373. ival = (int) obj_val;
  374. }
  375. jSocket.setSendBufferSize(ival);
  376. break;
  377. case SocketOptionName.OutOfBandInline:
  378. if (obj_val != null)
  379. {
  380. bval = ((int)obj_val == 0)?false:true;
  381. }
  382. else
  383. {
  384. bval = (int_val == 0)?false:true;
  385. }
  386. jSocket.setOOBInline(bval);
  387. break;
  388. default:
  389. error = 10022; //WSAEINVAL (Invalid argument)
  390. break;
  391. }
  392. }
  393. catch (Exception e)
  394. {
  395. error = 10022; //WSAEINVAL (Invalid argument)
  396. obj_val = null;
  397. }
  398. }
  399. public void GetSocketOption_obj_internal(SocketOptionLevel level, SocketOptionName name,
  400. out object obj_val, out int error)
  401. {
  402. obj_val = null;
  403. error = 0;
  404. if (jSocket == null)
  405. {
  406. error = 10022; //WSAEINVAL (Invalid argument)
  407. return;
  408. }
  409. switch (level)
  410. {
  411. case SocketOptionLevel.IPv6:
  412. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  413. return;
  414. case SocketOptionLevel.IP:
  415. if (name != SocketOptionName.NoDelay)
  416. {
  417. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  418. return;
  419. }
  420. break;
  421. case SocketOptionLevel.Udp:
  422. if (name == SocketOptionName.NoDelay)
  423. {
  424. error = 10042; //WSAENOPROTOOPT (Bad protocol option)
  425. }
  426. else
  427. {
  428. error = 10022; //WSAEINVAL (Invalid argument)
  429. }
  430. return;
  431. case SocketOptionLevel.Tcp:
  432. if (name != SocketOptionName.NoDelay)
  433. {
  434. error = 10022; //WSAEINVAL (Invalid argument)
  435. return;
  436. }
  437. break;
  438. }
  439. try
  440. {
  441. bool bval = false;
  442. int ival = 0;
  443. switch (name)
  444. {
  445. case SocketOptionName.DontLinger:
  446. ival = jSocket.getSoLinger();
  447. if (ival == -1)
  448. {
  449. obj_val = 1;
  450. }
  451. else
  452. {
  453. obj_val = 0;
  454. }
  455. break;
  456. case SocketOptionName.Linger:
  457. ival = jSocket.getSoLinger();
  458. if (ival == -1)
  459. {
  460. ival = 0;
  461. }
  462. LingerOption ret = new LingerOption((ival != 0), ival);
  463. obj_val = ret;
  464. break;
  465. case SocketOptionName.KeepAlive:
  466. bval = jSocket.getKeepAlive();
  467. obj_val = ((bval)?1:0);
  468. break;
  469. case SocketOptionName.NoDelay:
  470. bval = jSocket.getTcpNoDelay();
  471. obj_val = ((bval)?1:0);
  472. break;
  473. case SocketOptionName.ReceiveBuffer:
  474. ival = jSocket.getReceiveBufferSize();
  475. obj_val = ival;
  476. break;
  477. case SocketOptionName.ReceiveTimeout:
  478. ival = jSocket.getSoTimeout();
  479. obj_val = ival;
  480. break;
  481. case SocketOptionName.ReuseAddress:
  482. bval = jSocket.getReuseAddress();
  483. obj_val = ((bval)?1:0);
  484. break;
  485. case SocketOptionName.SendBuffer:
  486. ival = jSocket.getSendBufferSize();
  487. obj_val = ival;
  488. break;
  489. case SocketOptionName.OutOfBandInline:
  490. bval = jSocket.getOOBInline();
  491. obj_val = ((bval)?1:0);
  492. break;
  493. default:
  494. error = 10022; //WSAEINVAL (Invalid argument)
  495. break;
  496. }
  497. }
  498. catch (Exception e)
  499. {
  500. error = 10022; //WSAEINVAL (Invalid argument)
  501. obj_val = null;
  502. }
  503. }
  504. public void GetSocketOption_arr_internal(SocketOptionLevel level, SocketOptionName name,
  505. ref byte[] byte_val, out int error)
  506. {
  507. error = -1;
  508. throw new NotImplementedException();
  509. }
  510. public int WSAIoctl (int ioctl_code, byte [] input, byte [] output, out int error)
  511. {
  512. error = -1;
  513. throw new NotImplementedException();
  514. }
  515. public void Shutdown_internal(SocketShutdown how, out int error)
  516. {
  517. error = 0;
  518. if (jSocket == null || !jSocket.isConnected())
  519. {
  520. error = 10057; //WSAENOTCONN (Socket is not connected)
  521. return;
  522. }
  523. try
  524. {
  525. switch (how)
  526. {
  527. case SocketShutdown.Receive:
  528. jSocket.shutdownInput();
  529. break;
  530. case SocketShutdown.Send:
  531. jSocket.shutdownOutput();
  532. break;
  533. case SocketShutdown.Both:
  534. jSocket.shutdownInput();
  535. jSocket.shutdownOutput();
  536. break;
  537. }
  538. }
  539. catch (Exception e)
  540. {
  541. error = 10022; //WSAEINVAL (Invalid argument)
  542. #if DEBUG
  543. Console.WriteLine("Caught exception during Shutdown_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  544. #endif
  545. }
  546. }
  547. public void RegisterSelector(java.nio.channels.Selector selector, int mode, Socket source, out int error)
  548. {
  549. throw new InvalidOperationException();
  550. }
  551. public bool CheckConnectionFinished()
  552. {
  553. throw new InvalidOperationException();
  554. }
  555. public GHSocket ChangeToSSL(EndPoint remote_end)
  556. {
  557. return this;
  558. }
  559. }
  560. }