ActionItemPtr ActionLog::AddRemoteAction(shared_ptr<Data> actionData) { Name name = actionData->getName(); // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> uint64_t seqno = name.get(-1).toNumber(); std::string sharedFolder = name.get(-2).toUri(); if (sharedFolder != m_sharedFolderName) { _LOG_ERROR("Action doesn't belong to this shared folder"); return ActionItemPtr(); } if (name.get(-3).toUri() != "action") { _LOG_ERROR("not an action"); return ActionItemPtr(); } if (name.get(-4) != m_appName) { _LOG_ERROR("Action doesn't belong to this application"); return ActionItemPtr(); } Name deviceName = name.getSubName(0, name.size() - 4); _LOG_DEBUG("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno); return AddRemoteAction(deviceName, seqno, actionData); }
bool MySQLConnection::_Query(PreparedStatement* stmt, MYSQL_RES **pResult, uint64* pRowCount, uint32* pFieldCount) { if (!m_Mysql) return false; uint32 index = stmt->m_index; { MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index); ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query m_mStmt->m_stmt = stmt; // Cross reference them for debug output stmt->m_stmt = m_mStmt; /// @todo Cleaner way stmt->BindParameters(); MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT(); MYSQL_BIND* msql_BIND = m_mStmt->GetBind(); boost::timer _s; if (mysql_stmt_bind_param(msql_STMT, msql_BIND)) { uint32 lErrno = mysql_errno(m_Mysql); _LOG_ERROR(LOG_FILTER_SQL, "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT)); if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection) return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again m_mStmt->ClearParameters(); return false; } if (mysql_stmt_execute(msql_STMT)) { uint32 lErrno = mysql_errno(m_Mysql); _LOG_ERROR(LOG_FILTER_SQL, "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT)); if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection) return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again m_mStmt->ClearParameters(); return false; } _LOG_DEBUG(LOG_FILTER_SQL, "[%u ms] SQL(p): %s",(uint32)_s.elapsed(), m_mStmt->getQueryString(m_queries[index].first).c_str()); m_mStmt->ClearParameters(); *pResult = mysql_stmt_result_metadata(msql_STMT); *pRowCount = mysql_stmt_num_rows(msql_STMT); *pFieldCount = mysql_stmt_field_count(msql_STMT); return true; } }
bool MySQLConnection::_HandleMySQLErrno(uint32 errNo) { switch (errNo) { case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: case CR_INVALID_CONN_HANDLE: case CR_SERVER_LOST_EXTENDED: { m_reconnecting = true; uint64 oldThreadId = mysql_thread_id(GetHandle()); mysql_close(GetHandle()); if (this->Open()) // Don't remove 'this' pointer unless you want to skip loading all prepared statements.... { _LOG_INFO(LOG_FILTER_SQL, "Connection to the MySQL server is active."); if (oldThreadId != mysql_thread_id(GetHandle())) _LOG_INFO(LOG_FILTER_SQL, "Successfully reconnected to %s @%s:%s (%s).", m_connectionInfo.database.c_str(), m_connectionInfo.host.c_str(), m_connectionInfo.port_or_socket.c_str(), (m_connectionFlags & CONNECTION_ASYNC) ? "asynchronous" : "synchronous"); m_reconnecting = false; return true; } uint32 lErrno = mysql_errno(GetHandle()); // It's possible this attempted reconnect throws 2006 at us. To prevent crazy recursive calls, sleep here. boost::thread::sleep(boost::get_system_time()+boost::posix_time::seconds(3)); // Sleep 3 seconds return _HandleMySQLErrno(lErrno); // Call self (recursive) } case ER_LOCK_DEADLOCK: return false; // Implemented in TransactionTask::Execute and DatabaseWorkerPool<T>::DirectCommitTransaction // Query related errors - skip query case ER_WRONG_VALUE_COUNT: case ER_DUP_ENTRY: return false; // Outdated table or database structure - terminate core case ER_BAD_FIELD_ERROR: case ER_NO_SUCH_TABLE: _LOG_ERROR(LOG_FILTER_SQL, "Your database structure is not up to date. Please make sure you've executed all queries in the sql/updates folders."); boost::thread::sleep(boost::get_system_time()+boost::posix_time::seconds(10)); std::abort(); return false; case ER_PARSE_ERROR: _LOG_ERROR(LOG_FILTER_SQL, "Error while parsing SQL. Core fix required."); boost::thread::sleep(boost::get_system_time()+boost::posix_time::seconds(10)); std::abort(); return false; default: _LOG_ERROR(LOG_FILTER_SQL, "Unhandled MySQL errno %u. Unexpected behaviour possible.", errNo); return false; } }
void InterestValidationState::fail(const ValidationError& error) { _LOG_TRACE(error); try { failureCallback_(interest_, error); } catch (const std::exception& ex) { _LOG_ERROR("InterestValidationState::fail: Error in failureCallback: " << ex.what()); } catch (...) { _LOG_ERROR("InterestValidationState::fail: Error in failureCallback."); } setOutcome(false); }
void DataValidationState::bypassValidation() { _LOG_TRACE("Signature verification bypassed for data `" << data_.getName() << "`"); try { successCallback_(data_); } catch (const std::exception& ex) { _LOG_ERROR("DataValidationState::fail: Error in successCallback: " << ex.what()); } catch (...) { _LOG_ERROR("DataValidationState::fail: Error in successCallback."); } setOutcome(true); }
bool MySQLConnection::Execute(const char* sql) { if (!m_Mysql) return false; { boost::timer _s; if (mysql_query(m_Mysql, sql)) { uint32 lErrno = mysql_errno(m_Mysql); _LOG_INFO(LOG_FILTER_SQL, "SQL: %s", sql); _LOG_ERROR(LOG_FILTER_SQL, "[%u] %s", lErrno, mysql_error(m_Mysql)); if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection) return Execute(sql); // Try again return false; } else _LOG_DEBUG(LOG_FILTER_SQL, "[%u ms] SQL: %s",(uint32) _s.elapsed(), sql); } return true; }
void InterestValidationState::bypassValidation() { _LOG_TRACE("Signature verification bypassed for interest `" << interest_.getName() << "`"); for (size_t i = 0; i < successCallbacks_.size(); ++i) { try { successCallbacks_[i](interest_); } catch (const std::exception& ex) { _LOG_ERROR("InterestValidationState::fail: Error in successCallback: " << ex.what()); } catch (...) { _LOG_ERROR("InterestValidationState::fail: Error in successCallback."); } } setOutcome(true); }
void DataValidationState::verifyOriginalPacket (const CertificateV2& trustedCertificate) { if (VerificationHelpers::verifyDataSignature(data_, trustedCertificate)) { _LOG_TRACE("OK signature for data `" << data_.getName() << "`"); try { successCallback_(data_); } catch (const std::exception& ex) { _LOG_ERROR("DataValidationState::fail: Error in successCallback: " << ex.what()); } catch (...) { _LOG_ERROR("DataValidationState::fail: Error in successCallback."); } setOutcome(true); } else fail(ValidationError(ValidationError::INVALID_SIGNATURE, "Invalid signature of data `" + data_.getName().toUri() + "`")); }
MySQLPreparedStatement* MySQLConnection::GetPreparedStatement(uint32 index) { ASSERT(index < m_stmts.size()); MySQLPreparedStatement* ret = m_stmts[index]; if (!ret) _LOG_ERROR(LOG_FILTER_SQL, "Could not fetch prepared statement %u on database `%s`, connection type: %s.", index, m_connectionInfo.database.c_str(), (m_connectionFlags & CONNECTION_ASYNC) ? "asynchronous" : "synchronous"); return ret; }
void InterestValidationState::verifyOriginalPacket (const CertificateV2& trustedCertificate) { if (VerificationHelpers::verifyInterestSignature(interest_, trustedCertificate)) { _LOG_TRACE("OK signature for interest `" << interest_.getName() << "`"); for (size_t i = 0; i < successCallbacks_.size(); ++i) { try { successCallbacks_[i](interest_); } catch (const std::exception& ex) { _LOG_ERROR("InterestValidationState::fail: Error in successCallback: " << ex.what()); } catch (...) { _LOG_ERROR("InterestValidationState::fail: Error in successCallback."); } } setOutcome(true); } else fail(ValidationError(ValidationError::INVALID_SIGNATURE, "Invalid signature of interest `" + interest_.getName().toUri() + "`")); }
void MySQLConnection::PrepareStatement(uint32 index, const char* sql, ConnectionFlags flags) { m_queries.insert(PreparedStatementMap::value_type(index, std::make_pair(sql, flags))); // For reconnection case if (m_reconnecting) delete m_stmts[index]; // Check if specified query should be prepared on this connection // i.e. don't prepare async statements on synchronous connections // to save memory that will not be used. if (!(m_connectionFlags & flags)) { m_stmts[index] = NULL; return; } MYSQL_STMT* stmt = mysql_stmt_init(m_Mysql); if (!stmt) { _LOG_ERROR(LOG_FILTER_SQL, "In mysql_stmt_init() id: %u, sql: \"%s\"", index, sql); _LOG_ERROR(LOG_FILTER_SQL, "%s", mysql_error(m_Mysql)); m_prepareError = true; } else { if (mysql_stmt_prepare(stmt, sql, static_cast<unsigned long>(strlen(sql)))) { _LOG_ERROR(LOG_FILTER_SQL, "In mysql_stmt_prepare() id: %u, sql: \"%s\"", index, sql); _LOG_ERROR(LOG_FILTER_SQL, "%s", mysql_stmt_error(stmt)); mysql_stmt_close(stmt); m_prepareError = true; } else { MySQLPreparedStatement* mStmt = new MySQLPreparedStatement(stmt); m_stmts[index] = mStmt; } } }
uint64_t NamePrefixTableEntry::removeRoutingTableEntry(shared_ptr<RoutingTablePoolEntry> rtpePtr) { auto rtpeItr = std::find(m_rteList.begin(), m_rteList.end(), rtpePtr); if (rtpeItr != m_rteList.end()) { (*rtpeItr)->decrementUseCount(); m_rteList.erase(rtpeItr); } else { _LOG_ERROR("Routing entry for: " << rtpePtr->getDestination() << " not found in NPT entry: " << getNamePrefix()); } return (*rtpeItr)->getUseCount(); }
Data* Factory::create(const Config& args) { if (args.size() < 2) { _LOG_ERROR("Create worker failed.[Reason: arguement less than 2]"); return 0; } DataList::const_iterator iter = m_dataList.begin(); for ( ; iter != m_dataList.end(); ++iter) { if ((iter->m_compatibleFunc)(args)) { return (iter->m_createFunc)(args); } } return 0; }
bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount) { if (!m_Mysql) return false; { boost::timer _s; if (mysql_query(m_Mysql, sql)) { uint32 lErrno = mysql_errno(m_Mysql); _LOG_INFO(LOG_FILTER_SQL, "SQL: %s", sql); _LOG_ERROR(LOG_FILTER_SQL, "[%u] %s", lErrno, mysql_error(m_Mysql)); if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection) return _Query(sql, pResult, pFields, pRowCount, pFieldCount); // We try again return false; } else _LOG_DEBUG(LOG_FILTER_SQL, "[%u ms] SQL: %s",(uint32)_s.elapsed(), sql); *pResult = mysql_store_result(m_Mysql); *pRowCount = mysql_affected_rows(m_Mysql); *pFieldCount = mysql_field_count(m_Mysql); } if (!*pResult ) return false; if (!*pRowCount) { mysql_free_result(*pResult); return false; } *pFields = mysql_fetch_fields(*pResult); return true; }
void FsWatcher::DidFileChanged(QString filePath) { if (!filePath.startsWith(m_dirPath)) { _LOG_ERROR( "Got notification about a file not from the monitored directory: " << filePath.toStdString()); return; } QString absFilePath = filePath; fs::path absPathTriggeredFile(filePath.toStdString()); filePath.remove(0, m_dirPath.size()); fs::path triggeredFile(filePath.toStdString()); if (fs::exists(fs::path(absPathTriggeredFile))) { _LOG_DEBUG("Triggered UPDATE of file: " << triggeredFile.relative_path().generic_string()); // m_onChange(triggeredFile.relative_path()); m_watcher->removePath(absFilePath); m_watcher->addPath(absFilePath); rescheduleEvent("", triggeredFile.relative_path().string(), time::milliseconds(500), bind(m_onChange, triggeredFile.relative_path())); } else { _LOG_DEBUG("Triggered DELETE of file: " << triggeredFile.relative_path().generic_string()); // m_onDelete(triggeredFile.relative_path()); m_watcher->removePath(absFilePath); deleteFile(triggeredFile.relative_path()); rescheduleEvent("r", triggeredFile.relative_path().string(), time::milliseconds(500), bind(m_onDelete, triggeredFile.relative_path())); } }
bool MySQLConnection::Open() { MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { _LOG_ERROR(LOG_FILTER_SQL, "Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str()); return false; } int port; char const* unix_socket; //unsigned int timeout = 10; mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8"); //mysql_options(mysqlInit, MYSQL_OPT_READ_TIMEOUT, (char const*)&timeout); #ifdef _WIN32 if (m_connectionInfo.host == ".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = 0; } #else if (m_connectionInfo.host == ".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); m_connectionInfo.host = "localhost"; port = 0; unix_socket = m_connectionInfo.port_or_socket.c_str(); } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = 0; } #endif m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(), m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0); if (m_Mysql) { if (!m_reconnecting) { _LOG_INFO(LOG_FILTER_SQL, "MySQL client library: %s", mysql_get_client_info()); _LOG_INFO(LOG_FILTER_SQL, "MySQL server ver: %s ", mysql_get_server_info(m_Mysql)); // MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1 // if (mysql_get_server_version(m_Mysql) != mysql_get_client_version()) // _LOG_INFO(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements."); } _LOG_INFO(LOG_FILTER_SQL, "Connected to MySQL database at %s", m_connectionInfo.host.c_str()); mysql_autocommit(m_Mysql, 1); // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too mysql_set_character_set(m_Mysql, "utf8"); return PrepareStatements(); } else { _LOG_ERROR(LOG_FILTER_SQL, "Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }
ActionItemPtr ActionLog::AddRemoteAction(const Name& deviceName, sqlite3_int64 seqno, shared_ptr<Data> actionData) { if (!actionData) { _LOG_ERROR("actionData is not valid"); return ActionItemPtr(); } ActionItemPtr action = deserializeMsg<ActionItem>( Buffer(actionData->getContent().value(), actionData->getContent().value_size())); if (!action) { _LOG_ERROR("action cannot be decoded"); return ActionItemPtr(); } _LOG_DEBUG("AddRemoteAction: [" << deviceName.toUri() << "] seqno: " << seqno); sqlite3_stmt* stmt; sqlite3_prepare_v2(m_db, "INSERT INTO ActionLog " "(device_name, seq_no, action, filename, version, action_timestamp, " "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " "parent_device_name, parent_seq_no, " "action_name, action_content_object) " "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " " ?, ?, " " ?, ?);", -1, &stmt, 0); _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 2, seqno); sqlite3_bind_int(stmt, 3, action->action()); sqlite3_bind_text(stmt, 4, action->filename().c_str(), action->filename().size(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 5, action->version()); sqlite3_bind_int64(stmt, 6, action->timestamp()); if (action->action() == ActionItem::UPDATE) { sqlite3_bind_blob(stmt, 7, action->file_hash().c_str(), action->file_hash().size(), SQLITE_STATIC); // sqlite3_bind_int64(stmt, 8, atime); // NULL sqlite3_bind_int64(stmt, 9, action->mtime()); // sqlite3_bind_int64(stmt, 10, ctime); // NULL sqlite3_bind_int(stmt, 11, action->mode()); sqlite3_bind_int(stmt, 12, action->seg_num()); } if (action->has_parent_device_name()) { sqlite3_bind_blob(stmt, 13, action->parent_device_name().c_str(), action->parent_device_name().size(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 14, action->parent_seq_no()); } Name actionName = Name(deviceName); actionName.append("action").append(m_sharedFolderName).appendNumber(seqno); sqlite3_bind_blob(stmt, 15, actionName.wireEncode().wire(), actionName.wireEncode().size(), SQLITE_STATIC); sqlite3_bind_blob(stmt, 16, actionData->wireEncode().wire(), actionData->wireEncode().size(), SQLITE_STATIC); sqlite3_step(stmt); // if action needs to be applied to file state, the trigger will take care of it _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); sqlite3_finalize(stmt); // I had a problem including directory_name assignment as part of the initial insert. sqlite3_prepare_v2(m_db, "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", -1, &stmt, 0); _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 2, seqno); sqlite3_step(stmt); _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); sqlite3_finalize(stmt); return action; }
void ClientController::onSocket_ReadEvent(coconut_socket_t fd) { if(protocolFactory_) { //#define PROTOCOL_READ_FROM_SOCKET #ifdef PROTOCOL_READ_FROM_SOCKET do { if(!protocol_ || protocol_->isReadComplete()) { _LOG_TRACE("New Protocol make #1 in %p\n", this); protocol_ = protocolFactory_->makeProtocol(); } if(protocol_->processRead(socket()) == true) { onReceivedProtocol(protocol_); } else { break; } } while(1); #else char chunk[IOBUF_LEN]; int nread = socket()->read(chunk, IOBUF_LEN); if(nread <= 0) return; //logger::hexdump( (unsigned char*)chunk, nread, stdout); if(!protocol_ || protocol_->isReadComplete()) { _LOG_TRACE("New Protocol make #1 in this = %p\n", this); protocol_ = protocolFactory_->makeProtocol(); } _LOG_DEBUG("ClientController read socket fd = %d, readSize = %d in %p\n", socket()->socketFD(), nread, this); protocol_->addToReadingBuffer(chunk, nread); int index = -1; while(true) { index++; if(protocol_->processReadFromReadingBuffer() == true) { // fire! eventGotProtocol()->fireObservers(shared_from_this(), protocol_); onReceivedProtocol(protocol_); _LOG_TRACE("ClientController Protocol receved completed. readSize = %d, remainBufferSize = %d in %p, index = %d\n", nread, protocol_->remainingBufferSize(), this, index); if(protocol_->remainingBufferSize() > 0) { #define ALWAS_MAKE_PROTOCOL #ifdef ALWAS_MAKE_PROTOCOL // new protocol _LOG_TRACE("New Protocol make #2 in this = %p\n", this); boost::shared_ptr<protocol::BaseProtocol> protocolTemp = protocolFactory_->makeProtocol(); protocolTemp->addToReadingBuffer(protocol_->remainingBufferPtr(), protocol_->remainingBufferSize()); protocol_ = protocolTemp; #else protocol_->resetReadingBufferToRemainingBuffer(); #endif continue; // one more processing.. } } // --> parsing failed.. if(protocol_->isInvalidPacketReceived()) { // this session close! _LOG_ERROR("Invalid Packet Recved.. this = %p, size = \n", this, protocol_->payloadBuffer()->totalSize()); socket()->close(); } break; } #endif } else { char buffer[IOBUF_LEN]; int res = socket()->read(buffer, IOBUF_LEN); if(res > 0) onReceivedData(buffer, res); } }