Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Re: How can I validate and store IP address that user enter from command line?

 
Thread Tools Search this Thread
Old 07-09-2003, 06:51 AM   #1
Default Re: How can I validate and store IP address that user enter from command line?


Hi,

In order to get the inputs from command line, you can us gets () API.

After you get the number, use strchr () to see if the input value contains
"-". if it is, then use strstr to get the substring (range value) starting
from "-".

In this way, you will get two diff strings that will be two IP
address...

In the strings you've got, again use strchr to see that there's atleast
one dot (.). If neither strings contains dot, print invalid string and exit..

otherwise. determine which is the last byte in both the string..
Calculate the difference and allocate array and start STORINg.


(Abby) wrote in message news:<. com>...
> My program will need to get ip address from command line. User can
> enter a single IP or multiple IPs(enter in range).
>
> For examples,
>
> c:\>programname 192.168.0.1 --> user enter a single ip
>
> c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
> ip
>
> My question is:
>
> 1. How to get a range IP addresses and store them into an array? I'm
> not sure if my example is the easiest way to get it or there're other
> ways easier than this one. I need the program to automatically fill in
> an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?
>
> 2. How can I validate if user has entered valid IP address? Any
> function or easy way to do it?
>
> Thank you so much.
>
> Abb.



sumit.sharma@wipro.com
  Reply With Quote
Old 07-09-2003, 07:56 AM   #2
Richard Bos
 
Posts: n/a
Default Re: How can I validate and store IP address that user enter from command line?

wrote:

[ Do not top-post. Learn to snip. ]

> (Abby) wrote in message news:<. com>...
> > My program will need to get ip address from command line. User can
> > enter a single IP or multiple IPs(enter in range).
> >
> > For examples,
> >
> > c:\>programname 192.168.0.1 --> user enter a single ip
> >
> > c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
> > ip

>
> In order to get the inputs from command line, you can us gets () API.


NO. Not ever. gets() is the single most dangerous function in C, because
it cannot be used safely except with the use of outside appliances, such
as chains and straitjackets. There is no way to prevent it from reading
too much input into its buffer, because there is no way for it to know
how large that buffer is. NEVER use gets(). Use fgets() instead.

Besides, neither gets() nor fgets() reads from the command line. They
read from standard input, which is a different matter entirely. To read
from the command line, use the arguments to main():

int main(int argc, char **argv)

> After you get the number, use strchr () to see if the input value contains
> "-". if it is, then use strstr to get the substring (range value) starting
> from "-".
>
> In this way, you will get two diff strings that will be two IP
> address...


You hope. Make sure that they actually _are_ IP addresses

> In the strings you've got, again use strchr to see that there's atleast
> one dot (.). If neither strings contains dot, print invalid string and exit..


That's not enough. It's a start, but it's not enough.

> > 1. How to get a range IP addresses and store them into an array? I'm
> > not sure if my example is the easiest way to get it or there're other
> > ways easier than this one. I need the program to automatically fill in
> > an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?


Question: how do you want to handle 192.168.1.0 - 192.168.100.0? Subnet
masks of 255.255.0.128 are valid, AFAICT. Unwise, of course, but valid.

> > 2. How can I validate if user has entered valid IP address? Any
> > function or easy way to do it?


Do it by hand, as demonstrated by earlier replies. Note: use strtoul(),
not atoi(), unless you do some pre-checking. You do not want to invoke
undefined behaviour if some joker types "yourprogram 34278459780489034".

Richard
  Reply With Quote
Old 07-09-2003, 08:57 AM   #3
Zoran Cutura
 
Posts: n/a
Default Re: How can I validate and store IP address that user enter from command line?

wrote:
> Hi,
>
> In order to get the inputs from command line, you can us gets () API.


You are kidding?! Never, never use gets(), unless you are Dan Pop and
know which platform you are using.

Also the OP obviously meant to get arguments passed to his program
instead of reading from the standard input stream. There we want to use
the parameters from one of mains legal defintions/declarations:
int main(int argc, char **argv)
where argv will hold pointers to the strings representing the arguments
given at invocation of the program.

>
> After you get the number, use strchr () to see if the input value contains
> "-". if it is, then use strstr to get the substring (range value) starting
> from "-".


If the arguments are read from argv there will be no need to do this.

>
> In this way, you will get two diff strings that will be two IP
> address...


argv[1] and argv[3] should be the two strings.

>
> In the strings you've got, again use strchr to see that there's atleast
> one dot (.). If neither strings contains dot, print invalid string and exit..


Verifying that a string is a ip-address will probably need mor than
this.

>
> otherwise. determine which is the last byte in both the string..
> Calculate the difference and allocate array and start STORINg.
>
>
> (Abby) wrote in message news:<. com>...
>> My program will need to get ip address from command line. User can
>> enter a single IP or multiple IPs(enter in range).
>>
>> For examples,
>>
>> c:\>programname 192.168.0.1 --> user enter a single ip
>>
>> c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
>> ip
>>
>> My question is:
>>
>> 1. How to get a range IP addresses and store them into an array? I'm
>> not sure if my example is the easiest way to get it or there're other
>> ways easier than this one. I need the program to automatically fill in
>> an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?
>>
>> 2. How can I validate if user has entered valid IP address? Any
>> function or easy way to do it?
>>
>> Thank you so much.
>>
>> Abb.


--
Z ()
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
  Reply With Quote
Old 07-10-2003, 03:50 PM   #4
Micah Cowan
 
Posts: n/a
Default Re: How can I validate and store IP address that user enter from command line?

(Richard Bos) writes:

> wrote:
>
> > In order to get the inputs from command line, you can us gets () API.

>
> NO. Not ever. gets() is the single most dangerous function in C, because
> it cannot be used safely except with the use of outside appliances, such
> as chains and straitjackets.


Think chains and straitjackets would be enough to make this usage safe
on the DS9000?

....straightjacket for the programmer, maybe

-Micah
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump