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

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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值