RFC1948中提出了一个较好的初始化序列号ISN随机生成算法,简单描述就是:
ISN = C +H(sourceIP, sourcePort, destIP, destPort)
H中的4个参数分别是:源IP,源端口号,目的IP,目的端口号。
闲话少说,看代码。
#include<iostream>
#include<string>
#include<fstream>
#include<unistd.h> //unsleep()
using namespace std;
unsigned int SDBMHash(char* str)
{
unsigned int hash = 0;
while(*str)
{
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
unsigned int JSHash(char* str)
{
unsigned int hash = 1315423911;
while(*str)
hash = (hash << 5) + (*str++) + (hash >> 2);
return (hash & 0x7FFFFFFF);
}
unsigned int ELFHash(char* str)
{
unsigned int hash = 0;
unsigned int x = 0;
while(*str)
{
hash = (hash << 4) + (*str++);
if((x= (hash & 0xF0000000L)) != 0)
{
hash = (x >> 24);
hash &=~x;
}
}
return (hash & 0x7FFFFFFF);
}
unsigned int DJBHash(char* str)
{
unsigned int hash = 5381;
while(*str)
hash += (hash << 5) + (*str++);
return (hash & 0x7FFFFFFF);
}
clock_t getTime()
{
return clock();
}
int main(int argc, char* argv[])
{
unsigned int hash;
//输入5个参数,格式为:exe名 源IP地址 源端口号 目的IP地址 目的端口号
hash = SDBMHash(argv[1]) + JSHash(argv[2]) + ELFHash(argv[3]) + DJBHash(argv[4]);
ofstream outfile("result.txt");
if(!outfile)
cout << "Open result.txt error!" << endl;
while(!usleep(4))
{
outfile << hash + getTime() << endl;
}
outfile.close();
return 0;
}