Hello,
Is it possible to determine is a string is an ip address or a hostname? I need to be able to differentiate between the two.
Thanks!
Printable View
Hello,
Is it possible to determine is a string is an ip address or a hostname? I need to be able to differentiate between the two.
Thanks!
An IP address is numer . number . number . number. That should be easy enough to check for.
You could do it this way:
Sorry dont know how to use code blocks, can some one tell us
char *str="12.34.34.34:0.0";
if(str[0] > '9')
cout<<"not ip address"<<endl;
else
cout<<"ip address"<<endl;
Hope this helps
I was assuming that either his ip address was in this format:
10.2.1.10:0.0 or a total string format ie asmith:0.0
yes i know i shouldnt assume :)
This code calls up a lot of custom stuff that won't compile but may be of help to know how to go about it:
Code:bool ValidateIP(string ip)
{
xVector<string> exploded = Parser::Explode(ip,".");
if(exploded.GetSize()!=4)return false;
string tmp;
for(int i=0;exploded.Get(tmp,i);i++)
{
int octet = BitStream::Str2Int(tmp);
if(octet<0 || octet>255)return false;
}
return true;
}
Basically you want to explode the string by ".", then make sure that all you have is 4 ints, and that they are between 0 and 255.
Just check if the first character is a digit
isdigit(ipaddress[0])
In post #4, GCDEF asked: "What happens if the host name begins with a number?"Quote:
Originally Posted by _Superman_
The following code snippet is from MSDN sample code for the function gethostbyaddr
if (isalpha(host_name[0])) { /* host address is a name */
printf("Calling gethostbyname with %s\n", host_name);
remoteHost = gethostbyname(host_name);
} else {
http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx
In that case the sample code has a bug since GCDEF's point seems valid to me.Quote:
Originally Posted by _Superman_
If the hostname is given something like 123greetings.com, then it would be a problem.
Names beginning with http:// or www would be fine.
You could always check the string to see if it contains letters... there is probably a more efficient and/or concise way of doing this, but something like the following should work:
Code:#include <iostream>
#include <string>
#include <cctype> //for tolower
#include <algorithm> //for std::find_if
#include <functional> //for std::unary_function
class IsAlpha : public std::unary_function<char,void>
{
public:
bool operator()(char c)
{
int val = tolower(c);
return (val>='a' && val<='z');
}
};
bool ContainsAlpha(const std::string& s)
{
return(s.end() != std::find_if(s.begin(), s.end(), IsAlpha()));
}
int main()
{
std::cout << ContainsAlpha("192.168.0.1:8080") << std::endl;
std::cout << ContainsAlpha("123greetings.com") << std::endl;
system("PAUSE");
}
When there´s no parsing include you can assume the following:
If it´s a IPv4 address the string must consist of three dots ("."), everything else must be digits (0-9). If this rule is violated the address is either a logical host name or corrupt ;)
Edit:
Way too late
Can you do this without making IsAlpha a class?
Thanks
IsAlpha() alone wont work for IPv6 addresses, the OP didn't state whether he was supporting IPv6. I think regex maybe a better solution?
Code:bool ContainsAlpha(const std::string& s)
{
return(std::string::npos != s.find_first_not_of("0123456789.:");
}
well,
there are functions in ctype.h called isxdigit() and ispunct() that would come to use here.,
outputCode:
//c language code, but you get the point right?
#include<ctype.h>
#include<stdio.h>
int check_ip(char *p)
{
while(*p != '\0')
{
if(isxdigit(*p)==0)
{
if(ispunct(*p)==0)
{
return 0;
}
}
p++;
}
return 1;
}
int main()
{
char ip[16]="127.0.0.1";
char name[16]="123google.com";
char ipport[22]="127.0.0.1:8080";
char ipv6[50]="2001:db8:85a3:0:0:8a2e:370:7334";
/*Check if the string "ip" is an ip address or not*/
if(check_ip(ip)==1)
{
printf("%s is ip",ip);
}
/*Check if the string "name" is an ip address or not*/
if(check_ip(name)==1)
{
printf("%s is ip",name);
}
/*Check if the string "ipport" is an ip address or not*/
if(check_ip(ipport)==1)
{
printf("%s is ip\n",ipport);
}
/*Check if the string "ipv6" is an ip address or not*/
if(check_ip(ipv6)==1)
{
printf("%s is ip\n",ipv6);
}
return 0;
}
me thinks that satisfies the OPs requirements....Code:$ gcc isdigit.c
$ ./a.out
127.0.0.1 is ip
127.0.0.1:8080 is ip
2001:db8:85a3:0:0:8a2e:370:7334 is ip
:)
I think you're starting on the right track, but you need to do a lot more checking to verify a valid IP address: the number of fields and the numeric value in each field.
Hints: keep track of the location of the lastDot and the currentDot, as well as the total dotCount. If you hit a colon ':' or a non-numeric before you reach 3 dots, return the failure condition. If you encounter a dot set the currentDot to the current position, then use the two to find the field width, then check the field width, then check for a substring that is an integer between 0 and 255 (or 0 and 65535 for the port number).
Also, keep in mind that the max size of a valid IPv4 address c-string is 22 characters (including the null-terminator) i.e. "255.255.255.255:65535"
There's probably a couple conditions I missed in there, but that's a general rundown.
Its also valid to append :port_number after a hostname, so that case would need to report hostname.
If I was trying to solve this I would probably do it like this.
Assuming you have a function like this...
std::vector<std::string> Split(const std::string &text, char delimiter)
Split the string at every ':'
For an IP address you should have either one or two elements.
Split the first element at every '.'
For an IP address you should have four elements.
Check that all elements in both vectors are integers between 0 and 255.
EDIT: Except for the port number of course.