首页 > 其他 > 详细

TCP初始化序列号ISN的程序实现

时间:2015-03-25 09:05:28      阅读:189      评论:0      收藏:0      [点我收藏+]

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;
}


TCP初始化序列号ISN的程序实现

原文:http://blog.csdn.net/xumesang/article/details/44599705

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!