compare ip adresses

Hello,
at first, sorry for my bad english.
I need a good solution for comparing ip adresses. only c would be nice. but c++ is ok too.
I have ips definied in a config file, which are in style of 123.2-55.*
which means ips from 123.2.0.0 till 123.55.255.255
now i have a ip, for example 123.4.12.5 and i want to know if it does
exist in that ip range.
I had a solution for that wildcard thin. Also when you only have an ip
definied in style of 123.2.*

http://rafb.net/p/agtubz85.html

that is the function. I hope you understand the description for the
struct members, if not, ask ;)

i know there is something with a convert char array to binary solution. but i dont know exactly.

It would be wonderful if someone can help me

Felix
Last edited on
On a Unix system there are a family of functions including inet_addr() which convert between a octet style IP as a char array i.e. 255.255.255.255 or the IPV6 equivalent and a number/structure. Pull the man page or google for details. I don't know if there is a windows equivalent.

Bertha
hey,
thank you, the name for that function is what i've searched.
I don't need a windows equivalent. The program should run on a linux machine.
Thanks

Felix
Last edited on
ok i solved it:
1
2
3
4
5
6
unsigned long ip2long(const char ip[MAX_IP_LENGTH])
{
    struct sockaddr_in n;
    inet_aton(ip,&n.sin_addr);
    return ntohl(n.sin_addr.s_addr);
}
Topic archived. No new replies allowed.