//将主机名或IP转换成sa.sin_addr.S_un.S_addr需要的值
unsigned long resolv(char*host)
{
struct hostent *hp;
unsigned long host_ip;
host_ip = inet_addr(host);
if( host_ip == INADDR_NONE )
{
hp = gethostbyname(host);
if(!hp)
{
//printf("\nError: Unable to resolve hostname (%s)\n",host);
exit(1);
}
else
host_ip = *(u_long*)hp->h_addr ;
}
return(host_ip);
}
//校验和算法
unsigned short checksum(unsigned short*buffer,int size)
{
unsigned long cksum=0 ;
while(size>1)
{
cksum+=*buffer++;
size-=sizeof(unsigned short);
}
if(size)
cksum+=*(UCHAR*)buffer ;
cksum=(cksum>>16)+(cksum&0xffff);
cksum+=(cksum>>16);
return(unsigned short)(~cksum);
}
//校验和算法:汇编
USHORT checksum(USHORT *_lpsz, int _dwSize)
{
__asm
{
mov ecx,_dwSize
shr ecx,1
xor ebx,ebx
mov esi,_lpsz
cld
aa:
lodsw
movzx eax,ax
add ebx,eax
loop aa
test _dwSize,1
jz bb
lodsb
movzx eax,al
add ebx,eax
bb:
mov eax,ebx
and eax,0ffffh
shr ebx,16
add eax,ebx
not ax
mov rerere,eax
}
return(rerere);
}
/*++
函数说明:
打印16进制函数
参数:
data - 要打印的缓冲区
amount - 要打印的大小
address - 显示的初始地址
--*/
void hexdump(unsigned char *data, unsigned int amount, DWORD address=-1)
{
unsigned int dp, p;
const char trans[] =
"................................ !\"#$%&'()*+,-./0123456789"
":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
"nopqrstuvwxyz{|}~...................................."
"....................................................."
"........................................";
if(address!=-1)
printf("%08x : ", address);
for (dp = 1; dp <= amount; dp++)
{
printf ("%02x ", data[dp-1]);
if ((dp % 8) == 0)
printf (" ");
if ((dp % 16) == 0)
{
printf ("| ");
p = dp;
for (dp -= 16; dp < p; dp++)
printf ("%c", trans[data[dp]]);
printf ("\n");
if(address!=-1)
printf("%08x : ", address+=16);
}
}
if ((amount % 16) != 0)
{
p = dp = 16 - (amount % 16);
for (dp = p; dp > 0; dp--)
{
printf (" ");
if (((dp % 8) == 0) && (p != 8))
printf (" ");
}
printf (" | ");
for (dp = (amount - (16 - p)); dp < amount; dp++)
printf ("%c", trans[data[dp]]);
}
printf ("\n");
return ;
}
/**
函数说明:
获取到达指定IP的下一跳地址
参数:
ip- 目的ip
附: 需要使用iphlpapi头文件和库
#include "iphlpapi.h"
#pragma comment(lib,"iphlpapi")
调用时:
in_addr ia;
ia.S_un.S_addr = GetNextHopFormIp("192.168.1.1");
printf("NextHop: %s\n", inet_ntoa(ia));
**/
ULONG GetNextHopFormIp(char *ip)
{
MIB_IPFORWARDROW BestRoute;
DWORD dwRes = GetBestRoute(inet_addr(ip), 0, &BestRoute);
if(dwRes == ERROR_SUCCESS)
{
return BestRoute.dwForwardNextHop;
}
return 0;
}
/**
BCB中用的函数, 在string中获取两段字符中间的字符
如有字符串"HOST : ZWELL.3322.ORG\r\n",
调用函数GetTextBetween("HOST : ZWELL.3322.ORG\r\n", "HOST : ", "\r\n")
则返回ZWELL.3322.ORG
**/
AnsiString GetTextBetween(AnsiString string, AnsiString startstr, AnsiString endstr)
{
//如果参数长度有任意一个为0则跳出
if(string.Length()<0 || startstr.Length()<0 || endstr.Length()<0)
return FALSE;
//如果未找到起始字符, 则跳出
int postion = string.Pos(startstr);
if(postion<1)
return FALSE;
string = string.SubString(postion+startstr.Length(), string.Length());
//如果未找到终止字符, 则跳出
postion = string.Pos(endstr);
if(postion<1)
return FALSE;
string = string.SubString(1, postion);
return string;
}
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=86209