void WdtSocket::readEncryptionSettingsOnce()

in util/WdtSocket.cpp [34:97]


void WdtSocket::readEncryptionSettingsOnce(int timeoutMs) {
  if (!encryptionParams_.isSet() || encryptionSettingsRead_) {
    return;
  }
  WDT_CHECK(!encryptionParams_.getSecret().empty());

  int numRead = readInternal(buf_, 1, timeoutMs, true);
  if (numRead != 1) {
    WLOG(ERROR) << "Failed to read encryption settings " << numRead << " "
                << port_;
    return;
  }
  if (buf_[0] != Protocol::ENCRYPTION_CMD) {
    WLOG(ERROR) << "Expected to read ENCRYPTION_CMD(e), but got " << buf_[0];
    readErrorCode_ = UNEXPECTED_CMD_ERROR;
    return;
  }
  int toRead = Protocol::kEncryptionCmdLen - 1;  // already read 1 byte for cmd
  numRead = readInternal(buf_, toRead,
                         threadCtx_.getOptions().read_timeout_millis, true);
  if (numRead != toRead) {
    WLOG(ERROR) << "Failed to read encryption settings " << numRead << " "
                << toRead << " " << port_;
    readErrorCode_ = SOCKET_READ_ERROR;
    return;
  }
  int64_t off = 0;
  EncryptionType encryptionType;
  std::string iv;
  if (!Protocol::decodeEncryptionSettings(
          buf_, off, Protocol::kEncryptionCmdLen, encryptionType, iv,
          readTagInterval_)) {
    WLOG(ERROR) << "Failed to decode encryption settings";
    readErrorCode_ = PROTOCOL_ERROR;
    return;
  }
  if (encryptionType != encryptionParams_.getType()) {
    WLOG(ERROR) << "Encryption type mismatch "
                << encryptionTypeToStr(encryptionType) << " "
                << encryptionTypeToStr(encryptionParams_.getType());
    readErrorCode_ = PROTOCOL_ERROR;
    return;
  }
  if (readTagInterval_ < 0) {
    WLOG(ERROR) << "Encryption tag verification interval can't be negative "
                << readTagInterval_;
    readErrorCode_ = PROTOCOL_ERROR;
    return;
  }
  if ((readTagInterval_ > 0) && (encryptionTypeToTagLen(encryptionType) == 0)) {
    WLOG(ERROR) << "Tag verification should not be enabled for "
                << encryptionTypeToStr(encryptionType) << " "
                << readTagInterval_;
    readErrorCode_ = PROTOCOL_ERROR;
    return;
  }
  if (!decryptor_->start(encryptionParams_, iv)) {
    readErrorCode_ = ENCRYPTION_ERROR;
    return;
  }
  WLOG(INFO) << "Successfully read encryption settings " << port_ << " "
             << encryptionTypeToStr(encryptionType);
  encryptionSettingsRead_ = true;
}