Is 0.0.0.0 a valid IP address?

后端 未结 10 1103
遇见更好的自我
遇见更好的自我 2020-11-29 04:14

Is 0.0.0.0 a valid IP address? I want my program to be able to store it as an indication that no address is in use, but this won\'t work if it\'s actually valid.

相关标签:
10条回答
  • 2020-11-29 04:43

    of course it is. it will not be valid for a single host on a network however. it is in the broadcast range for the local network. read here: http://tools.ietf.org/html/rfc1700

    0 讨论(0)
  • 2020-11-29 04:45

    Yes, it is an IP address but it is reserved.

    0.0.0.0/8 - Addresses in this block refer to source hosts on "this" network. Address 0.0.0.0/32 may be used as a source address for this host on this network; other addresses within 0.0.0.0/8 may be used to refer to specified hosts on this network

    0 讨论(0)
  • 2020-11-29 04:50

    It's valid inasmuch as it contains four octets, each within the range 0 through 255 inclusive. However, it's not usable as a real IP address.

    RFC1700 (a) states that 0.0.0.0/8 (0.<anything>.<anything>.<anything>) is reserved as a source address only. You can get into situation where it appears you have this address but that's normally because no address has been assigned to you (by DHCP, for example).

    See also Wikipedia entry on IPv4.


    (a) Although this RFC is now considered obsolete, it is still correct in terms of the given behaviour. Its replacement, https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml, still has the same text detailing use of the 0.0.0.0 address.

    0 讨论(0)
  • 2020-11-29 04:50

    Lets look at the Question being asked here by the OP.

    Is 0.0.0.0 a valid IP address?

    Yes. This is technically a valid IP address, and the other answers describe many various uses of it (I am not going to repost wikipedia links here ... or maybe I am).

    As such I believe paxdiablo's answer above is the most correct, but lets look at the context of your question as well.

    I want my program to be able to store it as an indication that no address is in use, but this won't work if it's actually valid.

    This entirely depends on your use case. Given that this is a programmers forum, lets consider that perspective.

    If your software is storing actual internet addresses - server locations, visitors to your website, replication/mirror or backup sites, web service or database servers etc. - then this will be perfectly valid. No machine on the internet will ever have this address assigned, nor will it ever resolve to a valid connection.

    If on the other hand you are writing firewall or router firmware, then this address does take on special meaning; default route, accept any IP source/destination, block all IP source/destination, fall-trough catch-all, etc. as outlined by everyone else. However, let me point out that if you are coding on this level you should have a good enough understanding of network protocols so as to not need to ask this question in the first place.

    I am therefore going to assume that most people viewing this question fall into the first category, and suggest that this is a perfectly valid way of storing a null, empty or missing IP address, if there is some reason that an actual null value cannot be used. Even if you neglect validation checking and your software does try to connect to this IP address, it will simply not be able to make a connection.

    0 讨论(0)
  • 2020-11-29 04:51

    As other answers have covered, 0.0.0.0 is a legal and valid for some purposes IP address.

    If all values in a range are legal values, then any flag items you want to define must come from somewhere else. Otherwise you will overload a legal value with the meaning of a flag, and when using the overloaded value it won't be 100% possible to determine whether the legal value or the flag meaning was intended. This will lead to bugs which must be solved with a re-factor which can be an expensive endeavour.

    Overloading legal values happens all the time, IP address and MAC addresses (yes, 00:00:00:00:00:00 is legal and allocated) are some of the most common victims.

    Coincidentally, I am working on a system (not ethernet/IP based) now where the length of a frame is capped at about 40 bytes. Since a byte can represent lengths of 0 - 255 bytes, and the max length is 40 bytes, I can use some of the unused aka non-legal values (252 to 255) to represent flag items. These in-band flags are okay because there is no overloading.

    0 讨论(0)
  • 2020-11-29 04:56

    You can use it in your application to represent that it does not have an IP address, Microsoft also uses 0.0.0.0 when the machine has no IP address.

    the "Valid" scenarios talked about above are dependent on the specific scenarios where they have nothing to do with your application.

    0 讨论(0)
提交回复
热议问题