/// <summary> /// Prepares and sends the disconnect message to the specified remote endpoint. /// </summary> /// <param name="endpoint">Remote client.</param> /// <param name="code">Disconnection reason.</param> /// <param name="message">Optional text message for the user of the remote client.</param> /// <param name="data">Optional payload to further explain the disconnection reason.</param> /// <param name="removeAddressEntry">If true, the remote address will be removed from the `PresenceList`.</param> public static void sendBye(RemoteEndpoint endpoint, ProtocolByeCode code, string message, string data, bool removeAddressEntry = true) { using (MemoryStream m2 = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(m2)) { writer.Write((int)code); writer.Write(message); writer.Write(data); #if TRACE_MEMSTREAM_SIZES Logging.info(String.Format("CoreProtocolMessage::sendBye: {0}", m2.Length)); #endif endpoint.sendData(ProtocolMessageCode.bye, m2.ToArray()); Logging.info("Sending bye to {0} with message '{1}' and data '{2}'", endpoint.getFullAddress(), message, data); } } if (removeAddressEntry) { if (endpoint.presence != null && endpoint.presence.wallet != null && endpoint.presenceAddress != null) { PresenceList.removeAddressEntry(endpoint.presence.wallet, endpoint.presenceAddress); } //PeerStorage.removePeer(endpoint.getFullAddress(true)); } }
// Sends perioding keepalive network messages private static void keepAlive() { forceSendKeepAlive = true; while (autoKeepalive) { TLC.Report(); int keepalive_interval = CoreConfig.serverKeepAliveInterval; if (curNodePresenceAddress.type == 'C') { keepalive_interval = CoreConfig.clientKeepAliveInterval; } // Wait x seconds before rechecking for (int i = 0; i < keepalive_interval; i++) { if (autoKeepalive == false) { return; } if (IxianHandler.publicIP == "") { // do not send KA i = 0; } else { if (forceSendKeepAlive) { Thread.Sleep(1000); forceSendKeepAlive = false; break; } } // Sleep for one second Thread.Sleep(1000); } if (curNodePresenceAddress.type == 'W') { continue; // no need to send PL for worker nodes } try { byte[] ka_bytes = null; ka_bytes = keepAlive_v1(); byte[] address = null; // Update self presence PresenceList.receiveKeepAlive(ka_bytes, out address, null); // Send this keepalive to all connected non-clients CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M', 'H', 'W' }, ProtocolMessageCode.keepAlivePresence, ka_bytes, address); // Send this keepalive message to all connected clients CoreProtocolMessage.broadcastEventDataMessage(NetworkEvents.Type.keepAlive, address, ProtocolMessageCode.keepAlivePresence, ka_bytes, address); } catch (Exception e) { Logging.error("Exception occured while generating keepalive: " + e); } } }
/// <summary> /// Processes a Hello Ixian protocol message and updates the `PresenceList` as appropriate. /// </summary> /// <remarks> /// This function should normally be called from `NetworkProtocol.parseProtocolMessage()` /// </remarks> /// <param name="endpoint">Remote endpoint from which the message was received.</param> /// <param name="reader">Reader object placed at the beginning of the hello message data.</param> /// <returns>True if the message was formatted properly and accepted.</returns> public static bool processHelloMessage(RemoteEndpoint endpoint, BinaryReader reader) { // Node already has a presence if (endpoint.presence != null) { // Ignore the hello message in this case return(false); } // Another layer to catch any incompatible node exceptions for the hello message try { int protocol_version = reader.ReadInt32(); Logging.info(string.Format("Received Hello: Node version {0}", protocol_version)); // Check for incompatible nodes if (protocol_version < CoreConfig.protocolVersion) { Logging.warn(String.Format("Hello: Connected node version ({0}) is too old! Upgrade the node.", protocol_version)); sendBye(endpoint, ProtocolByeCode.deprecated, string.Format("Your node version is too old. Should be at least {0} is {1}", CoreConfig.protocolVersion, protocol_version), CoreConfig.protocolVersion.ToString(), true); return(false); } int addrLen = reader.ReadInt32(); byte[] addr = reader.ReadBytes(addrLen); bool test_net = reader.ReadBoolean(); char node_type = reader.ReadChar(); string node_version = reader.ReadString(); string device_id = reader.ReadString(); int pkLen = reader.ReadInt32(); byte[] pubkey = null; if (pkLen > 0) { pubkey = reader.ReadBytes(pkLen); } endpoint.serverPubKey = pubkey; int port = reader.ReadInt32(); long timestamp = reader.ReadInt64(); int sigLen = reader.ReadInt32(); byte[] signature = reader.ReadBytes(sigLen); // Check the testnet designator and disconnect on mismatch if (test_net != CoreConfig.isTestNet) { Logging.warn(string.Format("Rejected node {0} due to incorrect testnet designator: {1}", endpoint.fullAddress, test_net)); sendBye(endpoint, ProtocolByeCode.incorrectNetworkType, string.Format("Incorrect testnet designator: {0}. Should be {1}", test_net, CoreConfig.isTestNet), test_net.ToString(), true); return(false); } // Check the address and pubkey and disconnect on mismatch if (!addr.SequenceEqual((new Address(pubkey)).address)) { Logging.warn(string.Format("Pubkey and address do not match.")); sendBye(endpoint, ProtocolByeCode.authFailed, "Pubkey and address do not match.", "", true); return(false); } endpoint.incomingPort = port; // Verify the signature if (node_type == 'C') { // TODO: verify if the client is connectable, then if connectable, check if signature verifies /*if (CryptoManager.lib.verifySignature(Encoding.UTF8.GetBytes(ConsensusConfig.ixianChecksumLockString + "-" + device_id + "-" + timestamp + "-" + endpoint.getFullAddress(true)), pubkey, signature) == false) * { * CoreProtocolMessage.sendBye(endpoint, ProtocolByeCode.incorrectIp, "Verify signature failed in hello message, likely an incorrect IP was specified. Detected IP:", endpoint.address); * Logging.warn(string.Format("Connected node used an incorrect signature in hello message, likely an incorrect IP was specified. Detected IP: {0}", endpoint.address)); * return false; * }*/ // TODO store the full address if connectable // Store the presence address for this remote endpoint endpoint.presenceAddress = new PresenceAddress(device_id, "", node_type, node_version, Clock.getNetworkTimestamp() - CoreConfig.clientKeepAliveInterval, null); } else { if (!CryptoManager.lib.verifySignature(Encoding.UTF8.GetBytes(ConsensusConfig.ixianChecksumLockString + "-" + device_id + "-" + timestamp + "-" + endpoint.getFullAddress(true)), pubkey, signature)) { CoreProtocolMessage.sendBye(endpoint, ProtocolByeCode.incorrectIp, "Verify signature failed in hello message, likely an incorrect IP was specified. Detected IP:", endpoint.address); Logging.warn(string.Format("Connected node used an incorrect signature in hello message, likely an incorrect IP was specified. Detected IP: {0}", endpoint.address)); return(false); } // Store the presence address for this remote endpoint endpoint.presenceAddress = new PresenceAddress(device_id, endpoint.getFullAddress(true), node_type, node_version, Clock.getNetworkTimestamp() - CoreConfig.serverKeepAliveInterval, null); } // if we're a client update the network time difference if (endpoint.GetType() == typeof(NetworkClient)) { long timeDiff = endpoint.calculateTimeDifference(); // amortize +- 2 seconds if (timeDiff >= -2 && timeDiff <= 2) { timeDiff = 0; } ((NetworkClient)endpoint).timeDifference = timeDiff; // Check the address and local address and disconnect on mismatch if (endpoint.serverWalletAddress != null && !addr.SequenceEqual(endpoint.serverWalletAddress)) { Logging.warn(string.Format("Local address mismatch, possible Man-in-the-middle attack.")); sendBye(endpoint, ProtocolByeCode.addressMismatch, "Local address mismatch.", "", true); return(false); } } // Create a temporary presence with the client's address and device id Presence presence = new Presence(addr, pubkey, null, endpoint.presenceAddress); if (endpoint.GetType() != typeof(NetworkClient)) { // we're the server if (node_type == 'M' || node_type == 'H' || node_type == 'R') { if (node_type != 'R') { // Check the wallet balance for the minimum amount of coins IxiNumber balance = IxianHandler.getWalletBalance(addr); if (balance < ConsensusConfig.minimumMasterNodeFunds) { Logging.warn(string.Format("Rejected master node {0} due to insufficient funds: {1}", endpoint.getFullAddress(), balance.ToString())); sendBye(endpoint, ProtocolByeCode.insufficientFunds, string.Format("Insufficient funds. Minimum is {0}", ConsensusConfig.minimumMasterNodeFunds), balance.ToString(), true); return(false); } } // Limit to one IP per masternode // TODO TODO TODO - think about this and do it properly /*string[] hostname_split = hostname.Split(':'); * if (PresenceList.containsIP(hostname_split[0], 'M')) * { * using (MemoryStream m2 = new MemoryStream()) * { * using (BinaryWriter writer = new BinaryWriter(m2)) * { * writer.Write(string.Format("This IP address ( {0} ) already has a masternode connected.", hostname_split[0])); * Logging.info(string.Format("Rejected master node {0} due to duplicate IP address", hostname)); * socket.Send(prepareProtocolMessage(ProtocolMessageCode.bye, m2.ToArray()), SocketFlags.None); * socket.Disconnect(true); * return; * } * } * }*/ if (!checkNodeConnectivity(endpoint)) { return(false); } } } // Retrieve the final presence entry from the list (or create a fresh one) endpoint.presence = PresenceList.updateEntry(presence); } catch (Exception e) { // Disconnect the node in case of any reading errors Logging.warn(string.Format("Exception occured in Hello Message {0}", e.ToString())); sendBye(endpoint, ProtocolByeCode.deprecated, "Something went wrong during hello, make sure you're running the latest version of Ixian DLT.", ""); return(false); } if (NetworkClientManager.getConnectedClients().Count() == 1) { PresenceList.forceSendKeepAlive = true; } return(true); }