Click here to Skip to main content
15,891,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 CString  ip_start = ctlList.GetItemText(row, s_cnIpFilterRangeStart);
     
 CString ip_end = ctlList.GetItemText(row, s_cnIpFilterRangeEnd);
here i am getting ip address and storing into strings.
when i am giving like below i should through some waringing
ip_start = 11.1.1.1
ip_end  = 1.1.1.1
could you please suggest me here


What I have tried:

rightnow i have checked only ip validation it means when user gives IP address as 111111 then i am thorwing as error
but now my request is need compare both

for validation i did below one
ValidateIP(CString input)
{
    int retVal = FALSE;
    CString m_LocalIPaddress("0.0.0.0");
    CString m_BroadCastIPaddress("255.255.255.255");
    std::string s_input(CW2A(input.GetString()));
    std::regex pattern("[0-9][0-9a-zA-Z_!@#$%^&*()+=,.<>':;]*");
    if ((input.Compare(m_LocalIPaddress) == 0) || (input.Compare(m_BroadCastIPaddress) == 0))
    {
        return EParseResult::ENUM_NOT_SUPPORTED;
    }
    else if (std::regex_match(s_input, pattern) || (input.Find(_T(":"), 0) != RET_FAILURE))
    {
        if (input.Find(_T(".")) != RET_FAILURE)
        {
            struct sockaddr_in sa;
            retVal = InetPton(AF_INET, input, &(sa.sin_addr));
            if (retVal == RET_VALID_IP)
            {
                return EParseResult::ENUM_VALID_IPV4;
            }
        }
        else if (input.Find(_T(":")) != RET_FAILURE)
        {
            struct sockaddr_in6 sa;
            retVal = InetPton(AF_INET6, input, &(sa.sin6_addr));
            if (retVal == RET_VALID_IP)
            {
                return EParseResult::ENUM_VALID_IPV6;
            }
        }
    }
    else
    {
       return EParseResult::ENUM_IS_FQDN;
    }


so same way i ave to cck start sould is less tan end ip
Posted
Updated 29-Aug-17 20:58pm
v2
Comments
Afzaal Ahmad Zeeshan 13-Jul-17 7:42am    
How are you comparing, or for what purpose? Plus 11.1.1.1 will only be stored in a string, you cannot store this in float (it can hold one decimal place, right?). Can you be more specific than this?
Member 13089825 13-Jul-17 7:48am    
it will stored as "11.1.1.1" because ctlList.GetItemText(row, s_cnIpFilterRangeEnd);will be rturn cstring
i want to compare for less than or greater than purpose
Richard MacCutchan 13-Jul-17 8:44am    
Parse the string into its four component values and then compare them one by one.
Member 13089825 14-Jul-17 6:47am    
if possible could you please provide some example for ip address parsing

It can be compared with dword type.
For example, you can use follow macros.
C++
#define str2dword(sip, ip) { \
	DWORD _2, _3, _4; \
	_stscanf_s(sip, _T("%d.%d.%d.%d"), &ip, &_2, &_3, &_4);	\
	ip = ip | (_2 << 8);			\
	ip = ip | (_3 << 16);			\
	ip = ip | (_4 << 24);			\
}
#define I4V2IP(a, b, c, d) ((DWORD)(a) | ((DWORD)(b)<<8) | ((DWORD)(c)<<16) | ((DWORD)(d)<<24))
 
Share this answer
 
v2
Extract the octects (bytes) from the IP strings (you may find many examples just Googling[^]) and then compare them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900