?
Hab was im Internet gefunden:
LPCSTR CSock::ParseIPRange(LPCSTR start_addr,LPCSTR end_addr)
{
char* pHostAddr = NULL;
static BOOL first_call = TRUE;
static IP_RANGE ip_range = {0};
if(first_call)
{
unsigned int a,b,c,d;
char ip_start_addr[IP_ADDRESS_SIZE+1],ip_end_addr[IP_ADDRESS_SIZE+1];
if(ValidateIPAddr(start_addr))
strncpy(ip_start_addr,start_addr,IP_ADDRESS_SIZE);
else
{
if((pHostAddr = (char*)GetHostByName(start_addr))!=NULL)
strncpy(ip_start_addr,pHostAddr,IP_ADDRESS_SIZE);
else
goto done;
}
if(ValidateIPAddr(end_addr))
strncpy(ip_end_addr,end_addr,IP_ADDRESS_SIZE);
else
{
if((pHostAddr = (char*)GetHostByName(end_addr))!=NULL)
strncpy(ip_end_addr,pHostAddr,IP_ADDRESS_SIZE);
else
goto done;
}
strcpy(ip_range.ip_start,ip_start_addr);
strcpy(ip_range.ip_end,ip_end_addr);
TokenizeIPAddr(ip_start_addr,a,b,c,d);
ip_range.a_start = a;
ip_range.b_start = b;
ip_range.c_start = c;
ip_range.d_start = d;
TokenizeIPAddr(ip_end_addr,a,b,c,d);
ip_range.a_end = a;
ip_range.b_end = b;
ip_range.c_end = c;
ip_range.d_end = d;
first_call = FALSE;
}
pHostAddr = (char*)GetIPFromRange(&ip_range);
if(!pHostAddr)
{
first_call = TRUE;
memset(&ip_range,'\0',sizeof(IP_RANGE));
}
done:
return(pHostAddr);
}
const char* CSock::GetIPFromRange(IP_RANGE *ip)
{
#define CHECK_IP(f,a,b,c,d) {if(a==b) f = (c < d); else f = (c < (int)256);}
#define CHECK_CLASS(a) {if(a > (int)255) a = 0;}
BOOL next_class = FALSE;
static char ip_addr[IP_ADDRESS_SIZE + 1];
static BOOL first_call = TRUE;
if(first_call)
{
first_call = FALSE;
CHECK_CLASS(ip->a_start);
CHECK_CLASS(ip->b_start);
CHECK_CLASS(ip->c_start);
CHECK_CLASS(ip->d_start);
_snprintf(ip_addr,sizeof(ip_addr)-1,"%d.%d.%d.%d",ip->a_start,ip->b_start,ip->c_start,ip->d_start);
return(ip_addr);
}
d_1:
CHECK_IP(next_class,ip->c_start,ip->c_end,ip->d_start,ip->d_end);
if(next_class)
{
ip->d_start++;
CHECK_CLASS(ip->a_start);
CHECK_CLASS(ip->b_start);
CHECK_CLASS(ip->c_start);
CHECK_CLASS(ip->d_start);
_snprintf(ip_addr,sizeof(ip_addr)-1,"%d.%d.%d.%d",ip->a_start,ip->b_start,ip->c_start,ip->d_start);
return(ip_addr);
}
// c_1:
CHECK_IP(next_class,ip->b_start,ip->b_end,ip->c_start,ip->c_end);
if(next_class)
{
ip->d_start = -1;
ip->c_start++;
goto d_1;
}
// b_1:
CHECK_IP(next_class,ip->a_start,ip->a_end,ip->b_start,ip->b_end);
if(next_class)
{
ip->d_start = -1;
ip->c_start = 0;
ip->b_start++;
goto d_1;
}
// a_1:
if(ip->a_start < ip->a_end)
{
ip->d_start = -1;
ip->c_start = 0;
ip->b_start = 0;
ip->a_start++;
goto d_1;
}
first_call = TRUE;
return(NULL);
}
BOOL CSock::ValidateIPAddr(const char* ip_address)
{
char* token;
char* ip;
int ip_number,ip_len;
char ip_addr[IP_ADDRESS_SIZE + 1];
int i = -1;
strncpy(ip_addr,ip_address,IP_ADDRESS_SIZE);
for(token = strtok(ip_addr,"."); token!=NULL; token = strtok(NULL,"."))
{
ip = token;
ip_len = strlen(ip);
for(i = 0; i < ip_len; i++)
{
if(!isdigit(ip[i]))
{
i = -1;
break;
}
}
if(i < 0)
break;
ip_number = atoi(ip);
if(ip_number < 0 || ip_number > 255)
{
i = -1;
break;
}
}
return(i >= 0);
}
void CSock::TokenizeIPAddr(const char* ip_address,unsigned int& a,unsigned int& b,unsigned int& c,unsigned int& d)
{
int i;
char* token;
char ip_addr[IP_ADDRESS_SIZE + 1];
strncpy(ip_addr,ip_address,IP_ADDRESS_SIZE);
token = strtok(ip_addr,".");
for(i = 0; token!=NULL; i++)
{
switch(i)
{
case 0:
a = atoi(token);
break;
case 1:
b = atoi(token);
break;
case 2:
c = atoi(token);
break;
case 3:
d = atoi(token);
break;
}
token = strtok(NULL,".");
}
}