Does Wireshark use a well-known hash function to store TCP streams? (They use GHashTable for those interested.) Or is it something Wireshark developers have come up by themselves? Also, is there any data on the uniformness of their hash function for the input data it is used for (i.e. addresses and ports)?
Just for reference, here is the conversation_key structure definition:
typedef struct conversation_key {
struct conversation_key *next;
address addr1;
address addr2;
port_type ptype;
guint32 port1;
guint32 port2;
} conversation_key;
And here is the hash function itself:
static guint
conversation_hash_exact(gconstpointer v)
{
const conversation_key *key = (const conversation_key *)v;
guint hash_val;
address tmp_addr;
hash_val = 0;
tmp_addr.len = 4;
ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
tmp_addr.data = &key->port1;
ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
tmp_addr.data = &key->port2;
ADD_ADDRESS_TO_HASH(hash_val, &tmp_addr);
hash_val += ( hash_val << 3 );
hash_val ^= ( hash_val >> 11 );
hash_val += ( hash_val << 15 );
return hash_val;
}
That ADD_ADDRESS_TO_HASH macro expands to a function call:
static inline guint
add_address_to_hash(guint hash_val, const address *addr) {
const guint8 *hash_data = (const guint8 *)(addr)->data;
int idx;
for (idx = 0; idx < (addr)->len; idx++) {
hash_val += hash_data[idx];
hash_val += ( hash_val << 10 );
hash_val ^= ( hash_val >> 6 );
}
return hash_val;
}
#define ADD_ADDRESS_TO_HASH(hash_val, addr) do { hash_val = add_address_to_hash(hash_val, (addr)); } while (0)
It's Jenkin's One-at-a-time: https://en.wikipedia.org/wiki/Jenkins_hash_function
I forget why we chose it in particular, but it's relatively fast and we haven't had collision problems.
It used to be a lot worse (https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob;f=epan/address.h;h=f1848adfe104237f52ff02bc40fb9116e5efbc86;hb=7f57fe33573d04cb94598942d807abd3d2d85ebc#l188) and got updated at https://code.wireshark.org/review/gitweb?p=wireshark.git;a=commit;h=7f57fe33573d04cb94598942d807abd3d2d85ebc
Related
Write a assign_obj.h file so that the assign_driver file will output the
following. When implementing assign_obj.h think about how efficient your program will run. You must
implement your assign_obj class using a dynamic array.
assign_obj:: assign_obj() function will just set size to zero
assign_obj:: assign_obj(std::string s) function will go through each character of string and convert it to uppercase and push it the last of A
std::ostream& operator<<(std::ostream & out, assign_obj & obj) function will go through each data in obj.A vector and convert integer into string and
than output the data in this format [value:count value:count ....]
class assign_obj{
private:
struct item{
char value;
int count;
};
item *A;
int size;
public:
assign_obj();
assign_obj(std::string);
friend std::ostream& operator<<(std::ostream & out, assign_obj & obj);
}
int main(){
assign_obj ao1("dsdfdf");
std::cout << ao1 << std::endl;
}
This is the output that I want
[ D:1 S:1 D:1 F:1 D:1 F:1 ]
assign_obj:: assign_obj(){
size = 0;
}
assign_obj:: assign_obj(std::string s){
size = s.size();
for(int i = 0; i < s.size(); i++){
char c = std::toupper(s[i]);
A = new item{c,1};
}
}
# std::ostream& operator<<(std::ostream & out, assign_obj & obj){
std::cout<< "size: " << obj.size <<std::endl;
out << "[ ";
for(int i = 0; i < obj.size; i++){
out << obj.A[i].value << ":" << std::to_string(obj.A[i].count) << " ";
}
out << "]" << "\n";
return out;
}
I would like to use prepared statements to insert thousands of rows at once into my postgres database. The data to insert is stored in a vector of structs.
By reading the answers to How to prepare statements and bind parameters in Postgresql for C++ I thought I found the way how to do it.
Unfortunately, the current version of libpqxx I am using doesn't support pqxx::prepare::invocation anymore and I wasn't able to find any alternative in the docs/internet.
The code I tried out for my purpose is the following:
//pqxx header
#include "pqxx/connection.hxx"
#include "pqxx/transaction.hxx"
#include "pqxx/nontransaction.hxx"
#include "pqxx/compiler-public.hxx"
#include "pqxx/result.hxx"
#include "pqxx/prepared_statement.hxx"
#include "pqxx/transaction_base.hxx"
#include "pqxx/internal/statement_parameters.hxx"
...
int main(){
struct testData {
string uuid;
float rndNo;
string timestamp;
};
vector<testData> dataBuffer;
testData dbdata;
string queryStr;
const char* query;
stringstream connStream;
stringstream queryStream;
string DB_NAME = "dbname";
string DB_USER = "postgres";
string DB_PASSWORD = "password";
string DB_HOSTADDR = "127.0.0.1";
string DB_PORT = "1234";
string DB_SCHEMA = "test_schema";
string tableName = "test_table";
//Create Random data
for (int i = 0; i < 100000; i++) {
dbdata.uuid = "fe2b22ba-6ce7-4bbc-8226-d7696fe7a047";
dbdata.rndNo = i / 3.123;
dbdata.timestamp = systemDateTimeSQLFormat(0);
dataBuffer.push_back(dbdata);
}
stringstream connStream;
connStream << "dbname = " << DB_NAME << " user = " << DB_USER << " password = " << DB_PASSWORD << " hostaddr = " << DB_HOSTADDR << " port = " << DB_PORT;
connection dbConn(connStream.str());
if (dbConn.is_open()) {
cout << "Opened database successfully: " << dbConn.dbname() << endl;
}
else {
cout << "Can't open database" << endl;
return;
}
pqxx::nontransaction W(dbConn);
std::string m_insertCommand = "INSERT INTO test_schema.test_table (column1, column2, column3) VALUES";
int buffSize = dataBuffer.size();
for (size_t i = 0; i < buffSize; i++)
{
unsigned int countOf$ = i * 3;
for (unsigned int i = 0; i < 3; ++i)
{
if (i == 0)
{
m_insertCommand += "(";
}
else
{
m_insertCommand += ", ";
}
m_insertCommand += "$";
std::stringstream ss;
ss << countOf$ + i + 1;
m_insertCommand += ss.str();
}
if (i < buffSize - 1)
m_insertCommand += ") ,";
}
m_insertCommand += ")";
//FOLLOWING CODE NOT POSSIBLE WITH libpqxx v12
dbConn.prepare("insert_into_db", m_insertCommand);
pqxx::prepare::invocation = W.exec_prepared("insert_into_db"); //prepare doesn't have a member "invocation"
for (size_t i = 0; i < buffSize; i++)
{
inv(dataBuffer.at(i).rndNo)(dataBuffer.at(i).timestamp)(dataBuffer.at(i).uuid); //inv not defined
}
inv.exec();
return 1;
}
pqxx::prepare::make_dynamic_params will probably solve your problem. It's solved my problem. Use this way:
for (size_t i = 0; i < buffSize; ++i)
{
auto element = dataBuffer.at(i);
vector<string> vect;
vect.reserve(3);
vect.push_back(pqxx::to_string(element.rndNo));
vect.push_back(element.timestamp);
vect.push_back(element.uuid);
work.exec_params(m_insertCommand, pqxx::prepare::make_dynamic_params(vect));
}
From the version 7.6.0 dynamic_params are deprecated. params can be used instead. Here is the new solution:
for (size_t i = 0; i < buffSize; ++i)
{
auto element = dataBuffer.at(i);
pqxx::params;
params.reserve(4);
params.append(pqxx::to_string(element.rndNo));
params.append(element.timestamp);
params.append(element.uuid);
params.append(); // For example insert null variable
work.exec_params(m_insertCommand, params);
}
I am doing a project for a class writing C-String-editing functions. 3/5 of the functions I have to write change the size of the char arrays I have to use, and they are being read through an ifstream input. Here is the program:
#include <iostream>
#include <fstream>
using namespace std;
void stringCopy(char *A, char *B);
bool stringCompare(char *A, char *B);
void stringConcatenation(char *A, char *B); //added const to make sure b is never changed
int stringPosition(char *A, char B);
int stringLength(char *A);
//-------------------MY-FUNCTIONS----------------------
int cStringLen(const char*); //finds string length, but doesn't account for null char
void reSize(char*&, int len, int newLen);
void input(char*& A, istream& is);
void printMessage(const char* word1, const char* word2, const char* message);
int main()
{
ifstream ifs{"input.txt"};
ofstream ofs{"output.txt"};
char* word1 = "";
char* word2 = "";
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringCopy()");
stringCopy(word1, word2);
printMessage(word1, word2, "after stringCopy()");
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringCompare()");
if(stringCompare(word1, word2))
{
cout << "They match!" << endl;
}
else
{
cout << "They don't match!" << endl;
}
stringCopy(word1, word2);
printMessage(word1, word2, "comparing after stringCopy()");
if(stringCompare(word1, word2))
{
cout << "They match!" << endl;
}
else
{
cout << "They don't match!" << endl;
}
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringConcatenation()");
stringConcatenation(word1, word2);
printMessage(word1, word2, "after stringConcatenation()");
cout << endl;
input(word1, ifs);
input(word2, ifs);
printMessage(word1, word2, "stringPosition()");
cout << "Searching for 'm' in word1..." << endl << "position returned is: " << stringPosition(word1, 'm') << endl;
cout << "Searching for 'n' in word2..." << endl << "position returned is: " << stringPosition(word2, 'n') << endl;
cout << endl;
input(word1, ifs);
cout << "stringLength()" << endl;
cout << "word1: " << word1 << endl;
cout << "The length of word1 is: " << stringLength(word1) << endl;
cout << "after stringLength()" << endl;
cout << "word1: " << word1 << endl;
return 0;
}
void stringCopy(char *A, char *B)
{
///GETTING THE SIZES OF BOTH ARRAYS
int counterA = cStringLen(A) + 1;
int counterB = cStringLen(B) + 1;
///MAKES SURE BOTH ARE THE SAME SIZE BEFORE COPYING
if(counterA < counterB)
{
reSize(A, counterA, counterB);
}
else
{
reSize(A, counterB, counterA);
}
///THE COPY
for(int i = 0; i < counterB; i++) *(A + i) = *(B + i); //each character is copied to A from B
}
bool stringCompare(char *A, char *B)
{
///getting length of one string
int counter = cStringLen(A);
///will move through string until diff char found
for(int i = 0; i < counter + 1; i++)
{
if(*(A + i) != *(B + i))
{
return false;
}
}
return true;
}
void stringConcatenation(char *A, char *B) //added const to make sure b is never changed
{
///getting length of both strings
int counterA = cStringLen(A)+1;
int counterB = cStringLen(B)+1;
///putting the length of both together for new string
const int COUNTERS = counterA + counterB - 1;
///making A the size of both strings - 1
reSize(A, counterA, COUNTERS);
///copying b to the parts of a past the original
for(int i = 0; i < counterB; i++)
{
*(A + (counterA - 1) + i) = *(B + i); //will override the '/0' char of A
}
}
int stringPosition(char *A, char B)
{
int counter = cStringLen(A) + 1;
///searching through string for char
for(int i = 0; i < counter; i++)
{
if(*(A + i) == B)
{
return i; //found!
}
}
///checking if b == '\0' and a '\0' isn't found somewhere before last spot of A
if(B == '\0')
{
return counter;
}
return -1; //not found
}
int stringLength(char *A)
{
int counter = cStringLen(A) + 1;
char* car = new char[counter + 1];
for(int i = 0; i < counter; i++)
{
*(car + 1 + i) = *(A + i);
}
*(car + 0) = counter;
delete[] A;
A = car;
/**
* Will take string as param.
* Shifts all characters to the right by one and store the length of the string in position 0.
- Length doesn't include position 0.
*/
return counter; //temp
}
//-----------------------------------------MY FUNCTIONS---------------------------------------------------------------------------
int cStringLen(const char* A) //finds string length, but doesn't account for null char
{
int counter = 0;
while(*(A + counter) != '\0')
{
counter++;
}
return counter;
}
void reSize(char*& A, int len, int newLen)
{
char* car = new char[newLen];
for(int i = 0; i < newLen; i++)
{
if(i < len)
{
*(car + i) = *(A + i);
}
else if(i >= len && i < newLen)
{
*(car + i) = '\0';
}
}
delete[] A;
A = car;
}
void input(char*& A, istream& is)
{
int wordSize = 0;
int arrSize = 1;
char c = 'o'; //checking char
char* car = new char[arrSize];
while((!(is.eof())) && (c != ' ' && c != '\t' && c != '\n'))
{
is.unsetf(ios_base::skipws);
is >> c;
if(is.eof())
{
delete[] A;
A = car;
return;
}
if(c != ' ' && c != '\t' && c != '\n')
{
if(wordSize == arrSize)
{
reSize(car, arrSize, arrSize * 2);
}
*(car + wordSize) = c;
}
wordSize++;
}
is.setf(ios_base::skipws);
delete[] A;
A = car;
}
void printMessage(const char* word1, const char* word2, const char* message)
{
cout << message << endl;
cout << "word1: " << word1 << endl << "word2: " << word2 << endl;
}
I thought I got it all done just fine. Keep in mind that I added the "&" operator after each of the pointer parameters already. Here is how they were before:
void stringCopy(char *&A, char *B);
bool stringCompare(char *A, char *B);
void stringConcatenation(char *&A, char *B); //added const to make sure b
is never changed
int stringPosition(char *A, char B);
int stringLength(char *&A);
But, when I got to class, my teacher said we weren't allowed to change the function headers in any way. So, I am stuck passing by value for the assignment. The problem is that I have no way of changing the c-strings outside the editing functions now. Any changes I do to them stay inside there.
It all compiles just fine, and, if I make the pointers pass-by-reference, the program runs flawlessly. I am just wondering how I could change the values of the c-strings outside of the editing functions. This assignment is starting to become a pain (so many f***ing restrictions).
I think what your teacher wants you to do is to change the value at the character pointer instead of creating a new string.
So instead trying to reassigning parameter A to a new char* you change the value that A points to in memory. That way the method that called your function still points to that same memory and when they access that location the get the value you changed from within your function.
While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list.
Here is the function that I'm calling in main:
template<class Engine, typename Type, template<typename = Type> class Distribution, class... DistParams>
Type randomGenerator( RE::SeedType seedType, std::size_t seedValue, std::initializer_list<std::size_t> list, DistParams... params ) {
static Type retVal = 0;
static Engine engine = RE::getEngine<Engine>( seedType, seedValue, list );
static Distribution<Type> dist = RD::getDistribution<Type, Distribution>( params... );
retVal = dist( engine );
return retVal;
}
A little bit about the function above:
The Type represents the return value of the distribution such as int, unsigned, char, etc for Integral Types that work with distributions such as uniform_int_distribution<IntType> or Real Types such as float, double for distributions like uniform_real_distribution<RealType>.
The terms RE & RD are typedefs of two classes.
typedef RandomEngine RE;
typedef RandomDistribution RD;
Both classes follow the same pattern as they have delete constructors and all of their methods are declared a static.
The 2nd line in the function is using the template parameter <class Engine> to represent what kind of engine from the <random> header file we want to use from the static methods in class RandomEngine{...}. Each of the engine types has it's own function to create an engine, seed it by the seeding type and seed values and then returns a reference of the engine. All of the functions in the RandomEngine class are non template functions. So I then went ahead and made generalized function template RE::getEngine<Engine>( parameters ); in this RandomEngine class that you can see in this function template randomGenerator(). Then I specialized this function for each engine type. I had no problems with doing that.
This brings me to the next line with the RandomDistribution class I'm trying to follow a similar pattern as I did with the RandomEngine I made a generalized function template RD::getDistribution<Type, Distribution>( params... );
Before I get to the RD::getDistribution<...>(...) function both of the classes above are non templates. The first class RandomEngine has zero function templates for its engines except the generalized getEngine() The difference with this class as opposed to the RandomDistribution is that every function in this class is a function template, because the <random> library's distribution functions require it. So now I have to not only template this generalized function as I did for getEngine() I have to also use a variadic parameter pack as different distributions takes a different amount of arguments.
Here is my declaration of my generalized function in the RandomDistribution class that is found in the header file:
template<typename Type, template<typename = Type> class Distribution, class... DistParams>
static Distribution<Type>& getDistribution( DistParams... params ) {
return getUniformIntDistribution( params... );
}
Then I have this attempt of a specialization for this function in the cpp file for just one of the other distributions:
template<>
static std::uniform_real_distribution<>& RandomDistribution::getDistribution() {
return RandomDistribution::getUniformRealDistribution();
}
I would like to do this for all the other distributions that I'm supporting.
I am using the stand alone function template randomGenerator() in my main function like this:
{
std::initializer_list<std::size_t> list{};
unsigned val = randomGenerator<std::mt19937, unsigned, std::uniform_int_distribution>
( RE::USE_CHRONO_CLOCK, std::size_t( 12 ), list, 1, 100 );
std::cout << val << std::endl;
}
When I compile RandomGenerator.cpp file it compiles without error;
however, when I compile main.cpp I am getting a compiler error stating that it can not convert from std::uniform_int_distribution<int>& to std::uniform_int_distribution<Type>&
and it is pointing to my class's generalized function template that is declared in RandomGenerator.hfile.
For some reason; Type is not being assigned or casted to the type that is passed into randomGenerator's template parameter list.
I'm stuck at this point. I know what the compiler message is saying; I don't what to do to fix. What can be done to resolve this conversion failure?
Okay scrap that whole idea above: I went and entirely rewritten my classes into a single class. The class itself is now a class template. And it looks like this:
#ifndef GENERATOR_H
#define GENERATOR_H
#include <limits>
#include <chrono>
#include <random>
#include <type_traits>
enum SeedType { USE_CHRONO_CLOCK, USE_RANDOM_DEVICE, USE_SEED_VALUE, USE_SEED_SEQ };
template<class Engine, class Type, template<typename> class Distribution>
class Generator {
public:
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
private:
Engine _engine;
Distribution<Type> _distribution;
Type _value;
public:
template<class... Params>
explicit Generator( Engine engine, Params... params ) : _engine( engine ) {
_distribution = Distribution<Type>( params... );
}
void seed( SeedType type = USE_RANDOM_DEVICE, std::size_t seedValue = 0, std::initializer_list<std::size_t> list = {} ) {
switch( type ) {
case USE_CHRONO_CLOCK: { _engine.seed( getTimeNow() ); break; }
case USE_RANDOM_DEVICE: { std::random_device device{};
_engine.seed( device() ); break; }
case USE_SEED_VALUE: { _engine.seed( seedValue ); break; }
case USE_SEED_SEQ: { std::seed_seq seq( list );
_engine.seed( seq ); break; }
}
}
void generate() {
_value = _distribution( _engine );
}
Type getGeneratedValue() const {
return _value;
}
Distribution<Type> getDistribution() const {
return _distribution;
}
std::size_t getTimeNow() {
std::size_t now = static_cast<std::size_t>(Clock::now().time_since_epoch().count());
return now;
}
};
#endif // !GENERATOR_H
And to use it is as simple as:
#include <iostream>
#include <iomanip>
#include <vector>
#include "generator.h"
int main() {
// Engine, Seeding Type, & Distribution Combo 1
std::mt19937 engine1;
Generator<std::mt19937, short, std::uniform_int_distribution> g1( engine1, 1, 100 );
g1.seed( USE_RANDOM_DEVICE );
std::vector<short> vals1;
for( unsigned int i = 0; i < 200; i++ ) {
g1.generate();
auto v = g1.getGeneratedValue();
vals1.push_back( v );
}
int i = 0;
for( auto& v : vals1 ) {
if( (i % 10) != 0 ) {
std::cout << std::setw( 3 ) << v << " ";
} else {
std::cout << '\n' << std::setw( 3 ) << v << " ";
}
i++;
}
std::cout << "\n\n";
// Engine, Seeding Type, & Distribution Combo 2
std::ranlux48 engine2;
std::initializer_list<std::size_t> list2{ 3, 7, 13, 17, 27, 31, 43 };
Generator<std::ranlux48, unsigned, std::binomial_distribution> g2( engine2, 50, 0.75 );
g2.seed( USE_SEED_SEQ, std::size_t(7), list2 );
std::vector<unsigned> vals2;
for( int i = 0; i < 200; i++ ) {
g2.generate();
auto v = g2.getGeneratedValue();
vals2.push_back( v );
}
for( auto& v : vals2 ) {
if( (i % 10) != 0 ) {
std::cout << std::setw( 3 ) << v << " ";
} else {
std::cout << '\n' << std::setw( 3 ) << v << " ";
}
i++;
}
std::cout << "\n\n";
// Engine, Seeding Type, & Distribution Combo 3
std::minstd_rand engine3;
Generator<std::minstd_rand, float, std::gamma_distribution> g3( engine3, 0.22222f, 0.7959753f );
g3.seed( USE_CHRONO_CLOCK );
std::vector<float> vals3;
for( int i = 0; i < 200; i++ ) {
g3.generate();
auto v = g3.getGeneratedValue();
vals3.push_back( v );
}
for( auto& v : vals3 ) {
if( (i % 5 ) != 0 ) {
std::cout << std::setw( 12 ) << v << " ";
} else {
std::cout << '\n' << std::setw( 12 ) << v << " ";
}
i++;
}
std::cout << "\n\n";
std::cout << "\nPress any key and enter to quit.\n";
std::cin.get();
return 0;
}
class ship {
public:
int location;
int length;
};
void createship(ship ship1, int gridsize) { //function to set ship1's length and location
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
void insertship(ship ship1, vector <char> grid) { //insert ship into grid, change grid's elements from 'e' to 'f'
for (int temp2= 0; temp2 < ship1.length; temp2++) {
grid[ship1.location + temp2] = 'f';
}
}
int main()
{
int gridsize;
cout << "Gridsize: ";
cin >> gridsize;
cout << "\n";
vector <char> grid(gridsize, 'e');
for (int temp3 = 0; temp3 < grid.size(); temp3++) { //cout vector grid
cout << grid[temp3];
}
cout << "\n";
ship* ship1 = new ship(); //create ship1
createship(*ship1, gridsize);
insertship(*ship1, grid);
cout << (*ship1).length << "\n";
cout << (*ship1).location << "\n";
for (int temp4 = 0; temp4 < grid.size(); temp4++) { //cout vector grid again (with ship)
cout << grid[temp4];
}
return 0;
}
My ship1.length and ship1.location always remain as zero, even though the createship() function is supposed to change it to a random figure? Is there any mistake I made?
Just to add more words below because stackexchange doesn't allow me to add mostly code in my question
You were passing a parameter by value which will create and change a local variable in the function instead of the source variable.
You should pass the parameter by reference.
void createship(ship &ship1, int gridsize) {
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
Anyway,in you case, using a member function maybe a better solution.
class ship {
public:
int location;
int length;
void createship(int gridsize) {
srand(time(NULL));
this->length = (int)((rand()) % gridsize) / 4 + 1;
this->location = (int)(rand()) % gridsize;
}
};
call:
ship1->createship(100);
You pass the ship by value, so the parameter createship gets is a copy of the original object and changing it won't change the original.
Pass a reference/pointer to ship and then changes to the parameter will change the original object.
Or much better, use a constructor.