DnsQuery* DnsLayer::addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass) { // create new query on temporary buffer uint8_t newQueryRawData[256]; DnsQuery* newQuery = new DnsQuery(newQueryRawData); newQuery->setDnsClass(dnsClass); newQuery->setDnsType(dnsType); // cannot return false since layer shouldn't be extended or shortened in this stage newQuery->setName(name); // find the offset in the layer to insert the new query size_t newQueryOffsetInLayer = sizeof(dnshdr); DnsQuery* curQuery = getFirstQuery(); while (curQuery != NULL) { newQueryOffsetInLayer += curQuery->getSize(); DnsQuery* nextQuery = getNextQuery(curQuery); if (nextQuery == NULL) break; curQuery = nextQuery; } // set next resource for new query. This must happen here for extendLayer to succeed if (curQuery != NULL) newQuery->setNexResource(curQuery->getNextResource()); else newQuery->setNexResource(m_ResourceList); // extend layer to make room for the new query if (!extendLayer(newQueryOffsetInLayer, newQuery->getSize(), newQuery)) { LOG_ERROR("Couldn't extend DNS layer, addQuery failed"); delete newQuery; return NULL; } // connect the new query to layer newQuery->setDnsLayer(this, newQueryOffsetInLayer); // connect the new query to the layer's resource list if (curQuery != NULL) curQuery->setNexResource(newQuery); else // curQuery == NULL, meaning this is the first query { m_ResourceList = newQuery; m_FirstQuery = newQuery; } // increase number of queries getDnsHeader()->numberOfQuestions = htons(getQueryCount() + 1); return newQuery; }
std::string DnsLayer::toString() { std::ostringstream tidAsString; tidAsString << ntohs(getDnsHeader()->transactionID); std::ostringstream queryCount; queryCount << getQueryCount(); std::ostringstream answerCount; answerCount << getAnswerCount(); std::ostringstream authorityCount; authorityCount << getAuthorityCount(); std::ostringstream additionalCount; additionalCount << getAdditionalRecordCount(); if (getAnswerCount() > 0) { return "DNS query response, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str(); } else if (getQueryCount() > 0) { return "DNS query, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str(); } else // not likely - a DNS with no answers and no queries { return "DNS record without queries and answers, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str(); } }
bool DnsLayer::removeQuery(DnsQuery* queryToRemove) { bool res = removeResource(queryToRemove); if (res) { // decrease number of query records getDnsHeader()->numberOfQuestions = htons(getQueryCount() - 1); } return res; }
BlockInputStreams StorageSystemDictionaries::read( const Names & column_names, const ASTPtr & query, const Context & context, QueryProcessingStage::Enum & processed_stage, const size_t max_block_size, const unsigned) { check(column_names); processed_stage = QueryProcessingStage::FetchColumns; ColumnWithTypeAndName col_name{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "name"}; ColumnWithTypeAndName col_origin{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "origin"}; ColumnWithTypeAndName col_type{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "type"}; ColumnWithTypeAndName col_key{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "key"}; ColumnWithTypeAndName col_attribute_names{ std::make_shared<ColumnArray>(std::make_shared<ColumnString>()), std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()), "attribute.names" }; ColumnWithTypeAndName col_attribute_types{ std::make_shared<ColumnArray>(std::make_shared<ColumnString>()), std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()), "attribute.types" }; ColumnWithTypeAndName col_has_hierarchy{std::make_shared<ColumnUInt8>(), std::make_shared<DataTypeUInt8>(), "has_hierarchy"}; ColumnWithTypeAndName col_bytes_allocated{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "bytes_allocated"}; ColumnWithTypeAndName col_query_count{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "query_count"}; ColumnWithTypeAndName col_hit_rate{std::make_shared<ColumnFloat64>(), std::make_shared<DataTypeFloat64>(), "hit_rate"}; ColumnWithTypeAndName col_element_count{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "element_count"}; ColumnWithTypeAndName col_load_factor{std::make_shared<ColumnFloat64>(), std::make_shared<DataTypeFloat64>(), "load_factor"}; ColumnWithTypeAndName col_creation_time{std::make_shared<ColumnUInt32>(), std::make_shared<DataTypeDateTime>(), "creation_time"}; ColumnWithTypeAndName col_last_exception{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "last_exception"}; ColumnWithTypeAndName col_source{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "source"}; const auto & external_dictionaries = context.getExternalDictionaries(); const std::lock_guard<std::mutex> lock{external_dictionaries.dictionaries_mutex}; for (const auto & dict_info : external_dictionaries.dictionaries) { col_name.column->insert(dict_info.first); col_origin.column->insert(dict_info.second.origin); if (dict_info.second.dict) { const auto dict_ptr = dict_info.second.dict->get(); col_type.column->insert(dict_ptr->getTypeName()); const auto & dict_struct = dict_ptr->getStructure(); col_key.column->insert(dict_struct.getKeyDescription()); col_attribute_names.column->insert(ext::map<Array>(dict_struct.attributes, [] (auto & attr) -> decltype(auto) { return attr.name; })); col_attribute_types.column->insert(ext::map<Array>(dict_struct.attributes, [] (auto & attr) -> decltype(auto) { return attr.type->getName(); })); col_bytes_allocated.column->insert(dict_ptr->getBytesAllocated()); col_query_count.column->insert(dict_ptr->getQueryCount()); col_hit_rate.column->insert(dict_ptr->getHitRate()); col_element_count.column->insert(dict_ptr->getElementCount()); col_load_factor.column->insert(dict_ptr->getLoadFactor()); col_creation_time.column->insert(std::chrono::system_clock::to_time_t(dict_ptr->getCreationTime())); col_source.column->insert(dict_ptr->getSource()->toString()); } else { col_type.column->insertDefault(); col_key.column->insertDefault(); col_attribute_names.column->insertDefault(); col_attribute_types.column->insertDefault(); col_bytes_allocated.column->insertDefault(); col_query_count.column->insertDefault(); col_hit_rate.column->insertDefault(); col_element_count.column->insertDefault(); col_load_factor.column->insertDefault(); col_creation_time.column->insertDefault(); col_source.column->insertDefault(); } if (dict_info.second.exception) { try { std::rethrow_exception(dict_info.second.exception); } catch (...) { col_last_exception.column->insert(getCurrentExceptionMessage(false)); } } else col_last_exception.column->insertDefault(); } Block block{ col_name, col_origin, col_type, col_key, col_attribute_names, col_attribute_types, col_bytes_allocated, col_query_count, col_hit_rate, col_element_count, col_load_factor, col_creation_time, col_last_exception, col_source }; return BlockInputStreams{1, std::make_shared<OneBlockInputStream>(block)}; }
int fetch_user_data(char *username, char *password, struct userdata **user) { char *query; struct userdata *result; int col_username = 0; int col_password = 1; sqlite3_stmt *stmt; int retval; sqlite3 *handle; int rowCount = 0; getConnection(DBNAME, &handle); generateUserQuery(username, password, &query); rowCount = getQueryCount(handle, query); if (rowCount > 0) { int rCounter = 0; retval = sqlite3_prepare_v2(handle, query, -1, &stmt, 0); if (retval) { printf("Selecting data from DB Failed\n"); freeConnection(handle); return -1; } result = (struct userdata *) malloc (sizeof (struct userdata *) * rowCount); if (result == NULL) { perror("ERROR: Cannot allocate memory"); freeConnection(handle); return(-1); } // Read the number of rows fetched // int cols = sqlite3_column_count(stmt); while(1) { // fetch a row's status retval = sqlite3_step(stmt); if(retval == SQLITE_ROW) { // SQLITE_ROW means fetched a row strcpy(result[rCounter].username,(const char*)sqlite3_column_text(stmt,col_username)); strcpy(result[rCounter].password,(const char*)sqlite3_column_text(stmt,col_password)); rCounter++; } else if(retval == SQLITE_DONE) { break; } else { // Some error encountered printf("Some error encountered\n"); freeConnection(handle); return -1; } } sqlite3_finalize(stmt); *user = result; } free(query); freeConnection(handle); return(rowCount); }
int fetch_mesg_data(char *username, int mesgId, struct emaildata **mail) { char *query; struct emaildata *result; int col_mesgId = 0; int col_mesgSize = 1; int col_mesgSubject = 2; int col_mesgBody = 3; int col_mesgFrom = 4; int col_mesgTo = 5; char sMesgId[10]; sqlite3_stmt *stmt; int retval; sqlite3 *handle; int rowCount = 0; getConnection(DBNAME, &handle); sprintf(sMesgId, "%d", mesgId); generateMesgQuery(username, sMesgId, &query); rowCount = getQueryCount(handle, query); if (rowCount > 0) { int rCounter = 0; retval = sqlite3_prepare_v2(handle, query, -1, &stmt, 0); if (retval) { printf("Selecting data from DB Failed\n"); freeConnection(handle); return -1; } result = (struct emaildata *) malloc (sizeof (struct emaildata *) * rowCount); if (result == NULL) { perror("ERROR: Cannot allocate memory"); freeConnection(handle); return(-1); } // Read the number of rows fetched // int cols = sqlite3_column_count(stmt); while(1) { // fetch a row's status retval = sqlite3_step(stmt); if(retval == SQLITE_ROW) { // SQLITE_ROW means fetched a row result[rCounter].mesgid = sqlite3_column_int(stmt,col_mesgId); result[rCounter].mesgsize = sqlite3_column_int(stmt,col_mesgSize); strcpy(result[rCounter].subject,(const char*)sqlite3_column_text(stmt,col_mesgSubject)); result[rCounter].body = (char *)malloc(sizeof(char *)*result[rCounter].mesgsize); if (result[rCounter].body == NULL) { perror("ERROR: Cannot allocate memory"); sqlite3_finalize(stmt); free(query); freeConnection(handle); return(-1); } strcpy(result[rCounter].body,(const char*)sqlite3_column_text(stmt,col_mesgBody)); //From result[rCounter].from = (char *)malloc(sizeof(char *)*50); if (result[rCounter].from == NULL) { perror("ERROR: Cannot allocate memory"); sqlite3_finalize(stmt); free(query); freeConnection(handle); return(-1); } strcpy(result[rCounter].from,(const char*)sqlite3_column_text(stmt,col_mesgFrom)); //TO result[rCounter].to = (char *)malloc(sizeof(char *)*50); if (result[rCounter].to == NULL) { perror("ERROR: Cannot allocate memory"); sqlite3_finalize(stmt); free(query); freeConnection(handle); return(-1); } strcpy(result[rCounter].to,(const char*)sqlite3_column_text(stmt,col_mesgTo)); rCounter++; } else if(retval == SQLITE_DONE) { break; } else { // Some error encountered printf("Some error encountered\n"); freeConnection(handle); return -1; } } sqlite3_finalize(stmt); *mail = result; } free(query); freeConnection(handle); return(rowCount); }
int fetch_list_data(char *username, struct listdata **list) { char *query; struct listdata *result; int col_mesgid = 0; int col_size = 1; sqlite3_stmt *stmt; int retval; sqlite3 *handle; int rowCount = 0; int stat_details = 0; int stat_sizesum = 0; generateListQuery(username, &query); getConnection(DBNAME, &handle); rowCount = getQueryCount(handle, query); result = (struct listdata *) malloc (sizeof (struct listdata *) * (rowCount + 1)); if (result == NULL) { perror("ERROR: Cannot allocate memory"); freeConnection(handle); return(-1); } if (rowCount > 0) { int rCounter = 0; retval = sqlite3_prepare_v2(handle, query, -1, &stmt, 0); if (retval) { printf("Selecting data from DB Failed\n"); freeConnection(handle); return -1; } rCounter++; // Read the number of rows fetched // int cols = sqlite3_column_count(stmt); while(1) { // fetch a row's status retval = sqlite3_step(stmt); if(retval == SQLITE_ROW) { // SQLITE_ROW means fetched a row result[rCounter].count = rCounter; result[rCounter].mesgid = sqlite3_column_int(stmt,col_mesgid); result[rCounter].mesgsize = sqlite3_column_int(stmt,col_size); stat_sizesum += result[rCounter].mesgsize; rCounter++; } else if(retval == SQLITE_DONE) { break; } else { // Some error encountered printf("Some error encountered\n"); freeConnection(handle); return -1; } } sqlite3_finalize(stmt); } //SET STAT DETAILS result[stat_details].count = stat_details; result[stat_details].mesgid = rowCount; result[stat_details].mesgsize = stat_sizesum; *list = result; free(query); freeConnection(handle); return(rowCount); }