DbPortResolver.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.Data.ProviderBase.PDbPortResolver
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using java.net;
  32. namespace System.Data.ProviderBase
  33. {
  34. internal sealed class DbPortResolver
  35. {
  36. public static int getMSSqlPort(AbstractDBConnection connection)
  37. {
  38. String instanceName = connection.InstanceName;
  39. int timeout = connection.ConnectionTimeout;
  40. int port = -1;
  41. try
  42. {
  43. DatagramSocket socket = new DatagramSocket();
  44. // send request
  45. sbyte[] buf = new sbyte[] {2};
  46. InetAddress address = InetAddress.getByName(connection.DataSource);
  47. DatagramPacket packet = new DatagramPacket(buf, buf.Length, address, 1434);
  48. socket.send(packet);
  49. sbyte[] recbuf = new sbyte[1024];
  50. packet = new DatagramPacket(recbuf, recbuf.Length, packet.getAddress(), packet.getPort());
  51. // try to receive from socket while increasing timeouts in geometric progression
  52. int iterationTimeout = 1;
  53. int totalTimeout = 0;
  54. for(;;) {
  55. socket.setSoTimeout(iterationTimeout);
  56. try
  57. {
  58. socket.receive(packet);
  59. break;
  60. }
  61. catch (SocketTimeoutException e)
  62. {
  63. totalTimeout += iterationTimeout;
  64. iterationTimeout *= 2;
  65. if (totalTimeout >= timeout*1000) {
  66. throw new java.sql.SQLException(
  67. String.Format ("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1433).", connection.DataSource)
  68. );
  69. }
  70. }
  71. }
  72. sbyte[] rcvdSbytes = packet.getData();
  73. char[] rcvdChars = new char[rcvdSbytes.Length];
  74. for(int i=0; i < rcvdSbytes.Length; i++)
  75. {
  76. rcvdChars[i] = (char)rcvdSbytes[i];
  77. }
  78. String received = new String(rcvdChars);
  79. java.util.StringTokenizer st = new java.util.StringTokenizer(received, ";");
  80. String prev = "";
  81. bool instanceReached = instanceName == null || instanceName.Length == 0;
  82. while (st.hasMoreTokens())
  83. {
  84. if (!instanceReached)
  85. {
  86. if (prev.Trim().Equals("InstanceName"))
  87. {
  88. if (String.Compare(instanceName,st.nextToken().Trim(),true) == 0)
  89. {
  90. instanceReached = true;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (prev.Trim().Equals("tcp"))
  97. {
  98. port = java.lang.Integer.parseInt(st.nextToken().Trim());
  99. break;
  100. }
  101. }
  102. prev = st.nextToken();
  103. }
  104. socket.close();
  105. if (!instanceReached)
  106. throw new java.sql.SQLException(
  107. String.Format ("Specified SQL Server '{0}\\{1}' not found.", connection.DataSource, instanceName)
  108. );
  109. return port;
  110. }
  111. catch (java.sql.SQLException) {
  112. throw;
  113. }
  114. catch (Exception e) {
  115. throw new java.sql.SQLException(e.Message);
  116. }
  117. }
  118. }
  119. }