Ethereal-dev: Re: [Ethereal-dev] Validating IPs?

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <gharris@xxxxxxxxx>
Date: Fri, 28 Jan 2005 00:37:31 -0800
Alex Kirk wrote:
Already had, actually. There's nothing that extracts IP addresses directly (at
least not that I can see), and getting the bytes individually with
tvb_get_guint8(), converting them to a string with dots, and then calling
inet_aton() seemed wasteful.

It's especially wasteful because it won't tell you waht you want to know!

If you convert the 4 bytes to a string with dots, then "inet_aton()" will *ALWAYS* be able to convert that string back to an IP address - it will *never* fail on a string of the form

{integer from 0 to 255}.{integer from 0 to 255}.{integer from 0 to 255}.{integer from 0 to 255}

and converting 4 bytes to a string with dots involves treating each byte as an unsigned integer in the range 0 to 255 and putting dots between them.

I.e., as implied by Michael Tuexen's mail, all 2^32 4-byte integral values are, in some sense, syntactically valid IP addresses, although there are conventions that render some of them not valid as host addresses - but "inet_aton()" doesn't know that it's being asked to convert a valid host IP address, and doesn't check for an address being valid (doing so would require that it know what the network mask for the address is - and it might well be used in applications that are *intentionally* being supplied addresses with zeroes in the host part and that are *expected* to convert them to IP addresses).