[ IP validation math... ]

S

sean

I need to validate IP address.

Do you have any idea to validate IP address in C language ??
I checked previous lists, but can't find C source code...
 
G

Gregory Toomey

sean said:
I need to validate IP address.

Do you have any idea to validate IP address in C language ??
I checked previous lists, but can't find C source code...

Always of the format X1.X2.X3.X4 whre 0<=Xi<=255.
A parser for it would be trivial.

gtoomey
 
N

nanolucifer

#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="192.168.1.10";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,".");
while (pch != NULL)
{
printf ("%s\n",pch);
/* Add your string validation code here*/
pch = strtok (NULL, ".");
}
return 0;
}

EnTee

P.S.: Plagiarised in part from sample for strtok.
 
R

Richard Bos

nanolucifer said:
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="192.168.1.10";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,".");
while (pch != NULL)
{
printf ("%s\n",pch);
/* Add your string validation code here*/
pch = strtok (NULL, ".");
}
return 0;
}

This doesn't check that there are exactly the right number of periods in
the IP number, and it has the disadvantages that all uses of strtok()
have: you cannot pass a const string or a string literal to it, and if
you're using strtok() to separate a series of IP numbers from a string,
forget about using this, because you'll mess up the higher-level
function.

Oh, btw: if <http://192.168.1.10/> is valid where you are, then so is
<http://3232235786/>. Are you going to allow for that?

Richard
 
N

nanolucifer

Richard said:
nanolucifer said:
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="192.168.1.10";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,".");
while (pch != NULL)
{
printf ("%s\n",pch);
/* Add your string validation code here*/
pch = strtok (NULL, ".");
}
return 0;
}

This doesn't check that there are exactly the right number of periods in
the IP number,

I just gave him an example of how strtok works. Checking for the right
number of periods shouldn't be too hard :)
and it has the disadvantages that all uses of strtok()
have: you cannot pass a const string or a string literal to it,

I agree with the disadvantages of strtok with respect to const strings.

and if you're using strtok() to separate a series of IP numbers from a
string, forget about using this, because you'll mess up the higher-level
function.

I did not quite understand what you meant here.
Oh, btw: if <http://192.168.1.10/> is valid where you are, then so is
<http://3232235786/>. Are you going to allow for that?
It is a legal way of representing an IP address and should be allowed.

- EnTee
 
R

Richard Bos

nanolucifer said:
I just gave him an example of how strtok works. Checking for the right
number of periods shouldn't be too hard :)

strtok() is not the right tool for this problem.
I did not quite understand what you meant here.

Suppose you do this:

int get_ips(char *option, const char *name)
{
int count;
char *ptr;

count=0;
ptr=strtok(option, ";,");
while (ptr) {
if (check_ip(ptr)) {
add_ip_to_some_list(ptr);
count++;
} else
log_error("Invalid IP number in option %s: %s", name, ptr);
ptr=strtok(0, ";,");
}

return count;
}

All very nice and proper, right? It separates a string in the form of
"192.168.1.10;192.168.1.11,127.0.0.1;kablooie;192.168.42.42" into the
separate IP numbers; adds the valid ones to a list; adds an entry to the
error log for "kablooie"; and returns the number of valid IPs, in this
case 4.

Except when check_ip() is based on your example, and uses strtok() as
well...

Richard
 
B

Ben Pfaff

nanolucifer said:
I just gave him an example of how strtok works. Checking for the right
number of periods shouldn't be too hard :)

strtok() isn't a great choice for checking for the right number
of delimiters, because it considers multiple consecutive
delimiters to be the same as single delimiters. That is, it will
tokenize "1.2.3.4" and ".1..2...3....4....." the same way.

strtok() has at least these other problems:

* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.

* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.
 
M

Mark McIntyre

Always of the format X1.X2.X3.X4 whre 0<=Xi<=255.

even IPV6? :)

But this is OT here, its an algorithms question, or possibly a question
about how to do a reverse DNS lookup. Either way, offtopic.
 
K

Keith Thompson

I need to validate IP address.

Do you have any idea to validate IP address in C language ??
I checked previous lists, but can't find C source code...

The format of a valid IP address is off-topic here (and the question
of whether a given syntactically valid IP address refers to an actual
system is even more so). But given a defined syntax (say, N.N.N.N,
where each N is a decimal integer with a value in the range 0..255),
it's not a bad question.

<OT>
IP address can be represented in other forms, for exapmle as a single
32-bit integer. You may or may not want to worry about IPv6 as well.
</OT>

Given the N.N.N.N format, sscanf() is probably going to be useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top