private static void SetFirstPortToTry(ParsedLocalAddr la, int lastPort) { if (la.key == null) { la.key = la.startPort.ToString() + ":" + la.endPort.ToString(); if (la.hostname != null) { la.key = la.key + ":" + la.hostname; } } localAddrPorts[la.key] = lastPort; }
private static int GetFirstPortToTry(ParsedLocalAddr la) { if (la.key == null) { la.key = la.startPort.ToString() + ":" + la.endPort.ToString(); if (la.hostname != null) { la.key = la.key + ":" + la.hostname; } } int startPort = la.startPort; object obj2 = localAddrPorts[la.key]; if (obj2 == null) { return(startPort); } startPort = (int)obj2; if ((startPort >= la.startPort) && (startPort <= la.endPort)) { return(startPort); } return(la.startPort); }
private Socket ConnectSocket(string localAddr, string connectionName, int options) { uint method = 0x2c8; this.TrEntry(method, new object[] { localAddr, connectionName, options }); Socket socket = null; IPAddress[] hostAddresses = null; try { string str; int num2; int length = connectionName.Length; int index = connectionName.IndexOf('(', 0); if (index >= 0) { string str2; str = connectionName.Substring(0, index); int num4 = connectionName.IndexOf(')', index); if (num4 < 0) { str2 = connectionName.Substring(index + 1); } else { str2 = connectionName.Substring(index + 1, (num4 - index) - 1); } num2 = Convert.ToInt32(str2); if ((num2 < 0) || (num2 > 0xffff)) { throw new MQManagedClientException(0x20009203, (uint)num2, (uint)num2, "'" + connectionName + "'", "TCP/IP", " (Convert.ToInt32)", 2, 0x893); } } else { str = connectionName.Trim(); num2 = 0x586; } try { ParsedLocalAddr addr = this.ParseLocalAddress(localAddr); hostAddresses = Dns.GetHostAddresses(str); Exception exception = null; bool flag = "MQIPADDR_IPV6".Equals(Convert.ToString(base.env.Cfg.GetStringValue(MQClientCfg.ENV_MQIPADDRV)).ToUpper()); IPAddress address = null; foreach (IPAddress address2 in hostAddresses) { if ((flag && address2.AddressFamily.Equals(AddressFamily.InterNetworkV6)) || (!flag && address2.AddressFamily.Equals(AddressFamily.InterNetwork))) { address = address2; try { socket = this.ConnectUsingLocalAddr(addr, address2, num2); } catch (Exception exception2) { base.TrException(method, exception2, 1); exception = exception2; } break; } } foreach (IPAddress address3 in hostAddresses) { if (address3 != address) { try { socket = this.ConnectUsingLocalAddr(addr, address3, num2); break; } catch (Exception exception3) { base.TrException(method, exception3, 2); exception = exception3; } } } if (socket == null) { throw exception; } if ((base.clientConn.SSLCipherSpec != null) && (base.clientConn.SSLCipherSpec.Length > 0)) { this.network = new MQEncryptedSocket(base.env, this, socket, this.mqcd, this.sslConfigOptions); } else { this.network = new MQPlainSocket(base.env, this, socket); } this.connected = true; } catch (Exception exception4) { uint errorCode; base.TrException(method, exception4, 3); if (exception4 is SocketException) { errorCode = (uint)((SocketException)exception4).ErrorCode; } else { errorCode = 0; } throw new MQManagedClientException(0x20009202, errorCode, errorCode, connectionName, "TCP/IP", " (Exception)", 2, 0x9ea); } } finally { base.TrExit(method); } return(socket); }
private ParsedLocalAddr ParseLocalAddress(string localAddr) { uint method = 0x2c9; this.TrEntry(method, new object[] { localAddr }); ParsedLocalAddr result = null; try { int intValue = base.env.Cfg.GetIntValue(MQClientCfg.TCP_STRPORT); int num3 = base.env.Cfg.GetIntValue(MQClientCfg.TCP_ENDPORT); string str = null; if ((localAddr == null) || (localAddr.Length == 0)) { localAddr = base.env.Cfg.GetStringValue(MQClientCfg.ENV_MQ_LCLADDR); } if ((localAddr != null) && (localAddr.Length > 0)) { localAddr = localAddr.Trim(); int length = localAddr.LastIndexOf('('); int num5 = localAddr.LastIndexOf(')'); if ((length == -1) && (num5 == -1)) { str = localAddr; } else { if (((length == -1) || (num5 == -1)) || (num5 != (localAddr.Length - 1))) { NmqiException ex = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, ex); throw ex; } str = localAddr.Substring(0, length); if ((str.IndexOf('(') != -1) || (str.IndexOf(')') != -1)) { NmqiException exception2 = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, exception2); throw exception2; } int index = localAddr.IndexOf(',', length + 1); int num7 = ((index >= 0) ? index : num5) - 1; try { int num8 = num7 - length; intValue = int.Parse(localAddr.Substring(length + 1, num8)); if (intValue < 0) { NmqiException exception3 = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, exception3); throw exception3; } } catch (Exception) { NmqiException exception4 = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, exception4); throw exception4; } if (index >= 0) { try { int num9 = (num5 - index) - 1; num3 = int.Parse(localAddr.Substring(index + 1, num9)); if (num3 < 0) { NmqiException exception5 = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, exception5); throw exception5; } goto Label_021C; } catch (Exception exception6) { NmqiException exception7 = new NmqiException(base.env, -1, null, 2, 0x80b, exception6); base.TrException(method, exception7); throw exception7; } } num3 = intValue; } } Label_021C: result = new ParsedLocalAddr(); result.hostname = str; result.startPort = intValue; result.endPort = num3; } finally { base.TrExit(method, result); } return(result); }
private Socket ConnectUsingLocalAddr(ParsedLocalAddr localAddr, IPAddress ipad, int port) { uint method = 0x2ca; this.TrEntry(method, new object[] { localAddr, ipad, port }); Socket socket = null; try { IPEndPoint remoteEP = new IPEndPoint(ipad, port); base.TrText("Remote Address:" + remoteEP.ToString()); IPAddress any = null; if (localAddr.hostname != null) { foreach (IPAddress address2 in Dns.GetHostEntry(localAddr.hostname).AddressList) { if (address2.AddressFamily == ipad.AddressFamily) { any = address2; } } if (any == null) { NmqiException ex = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, ex); throw ex; } } else if (remoteEP.AddressFamily == AddressFamily.InterNetworkV6) { any = IPAddress.IPv6Any; } else { any = IPAddress.Any; } bool flag = false; try { int startPort = localAddr.startPort; if (startPort != 0) { startPort = GetFirstPortToTry(localAddr); } bool connected = false; int num3 = localAddr.endPort - localAddr.startPort; for (int i = 0; (i <= num3) && !connected; i++) { try { socket = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEP = new IPEndPoint(any, startPort); base.TrText("Local Address:" + localEP.ToString()); base.TrText(method, "Bind"); socket.Bind(localEP); base.TrText(method, "Bind returned " + socket.LocalEndPoint.ToString()); base.TrText(method, "Connect"); socket.Connect(remoteEP); base.TrText(method, "Connect returned " + socket.Connected); connected = socket.Connected; } catch (SocketException exception2) { base.TrException(method, exception2); if (socket != null) { socket.Close(); } socket = null; if (exception2.ErrorCode != 0x2740) { break; } } startPort++; if (startPort > localAddr.endPort) { startPort = localAddr.startPort; } SetFirstPortToTry(localAddr, startPort); } if ((socket != null) && socket.Connected) { if ((base.connectOptions & 0x10) != 0) { LingerOption option = new LingerOption(true, 10); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, option); base.TrText(method, "TCP/IP LINGER active"); } if (((base.connectOptions & 0x20) != 0) && ((base.connectOptions & 0x40) == 0)) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); base.TrText(method, "TCP/IP KEEPALIVE active"); } int optionValue = 0x8000; int num6 = 0x8000; string environmentVariable = Environment.GetEnvironmentVariable("MQ_COMMS_IP_SDRBUF"); if (environmentVariable != null) { base.TrText(method, "MQ_COMMS_IP_SDRBUF set on evironment"); optionValue = Convert.ToInt32(environmentVariable.Trim()); if (optionValue <= 0) { base.TrText(method, "MQ_COMMS_IP_SDRBUF set with invalid value " + optionValue); } } string str2 = Environment.GetEnvironmentVariable("MQ_COMMS_IP_RCVBUF"); if (str2 != null) { base.TrText(method, "MQ_COMMS_IP_RCVBUF set on evironment"); num6 = Convert.ToInt32(str2.Trim()); if (num6 <= 0) { base.TrText(method, "MQ_COMMS_IP_RCVBUF set with invalid value " + num6); } } if ((optionValue <= 0) || (num6 <= 0)) { base.TrText(method, "Not valid values on Send/Receive buffer. making connection to fail"); flag = false; } else { base.TrText(method, "Using socket send buffer size " + optionValue); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, optionValue); base.TrText(method, "Using socket receive buffer size " + num6); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, num6); flag = true; } } if (!flag) { NmqiException exception3 = new NmqiException(base.env, -1, null, 2, 0x80b, null); base.TrException(method, exception3); throw exception3; } } finally { if ((socket != null) && !flag) { socket.Close(); } } } finally { base.TrExit(method); } return(socket); }