Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: (tye)RE: Matching an IP address

by Clownburner (Monk)
on Mar 11, 2001 at 21:36 UTC ( [id://63629]=note: print w/replies, xml ) Need Help??


in reply to (tye)RE: Matching an IP address
in thread Matching an IP address

I think you misunderstood me (and read my flawed code for help), because what I meant was that 0 and 255 are *NOT* valid values for a *host* in the last octet, but you're right, they're valid anywhere else (except of course, a 0 in the first octet unless you're using all-zeros broadcast, which is non-standard by modern convention, or a 255 in the first octet, which is valid only for broadcast addresses). But that's pedantic.

If you want to do a complete test, the NetAddr::IP module from CPAN (or just use the trick of trying to open a socket with the address and see if it works).

But I did make a mistake; the regexp I wrote does needs to account for 0's in the last octet, provided that the last octet does not contain *only* zeros. So a more correct version would be this, if you wanted to ONLY verify *HOST* addresses and not broadcasts or subnets (please note: the regexp below does not prohibit '255' values in the first or last octet, which are technically invalid for *host* addresses):

unless ($get && $get =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9][0-9]{0, +2}$/ && !grep { $_ < 0 || $_ > 255 } split(/\./, $get) ) { print "BAFFLING: invalid target specified\n"; exit(- +1);}

Signature void where prohibited by law.

Replies are listed 'Best First'.
(tye)Re2: Matching an IP address
by tye (Sage) on Mar 11, 2001 at 23:14 UTC

    Actually, what is not allowed for hosts is to have the "host" half of the address (based on the netmask) be all 0 bits or all 1 bits. That still allows for the last octet to be 0 (though not for standard Class C IP addresses, which are the most common for most people).

    So I still think trying to disallow such is a mistake.

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://63629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found