php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16020 long2ip Returns Incorrect Results
Submitted: 2002-03-12 08:47 UTC Modified: 2002-08-15 23:21 UTC
From: lee at nerds dot org dot uk Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.1.2 OS: FreeBSD 4.4-STABLE i386
Private report: No CVE-ID: None
 [2002-03-12 08:47 UTC] lee at nerds dot org dot uk
I have found some problems where long2ip (and I would presume ip2long by the same token) seems to return an IP address offset by one.  I'm not sure if it is the implmentation of inet_ntoa on the platform I am using or something else.

Even if this is a problem with a particular version of a library on my machine, maybe it might be worth using a method other than inet_ntoa for ease of platform independance?

Perhaps something along there lines....  ?


struct inet4addr {
  unsigned int a:8;
  unsigned int b:8;
  unsigned int c:8;
  unsigned int d:8;
};

typedef union {
  unsigned int inet4dec;
  struct inet4addr inet4oct;
} inet4oct;
  
  blah.inet4dec = <SOME LONG IP HERE>;
  printf("%i.%i.%i.%i\n", blah.inet4oct.a, blah.inet4oct.b,blah.inet4oct.c,blah.inet4oct.d);


 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-12 08:58 UTC] sander@php.net
Can you provide a simple sample script with data that shows the problem?
 [2002-03-12 09:04 UTC] lee at nerds dot org dot uk
Sorry, I missed out a bit:

inet4oct blah;

;)
 [2002-03-12 10:41 UTC] lee at nerds dot org dot uk
I've done a little more digging to see why the problem occured.  I'm not 100% sure if it is a problem with php, or misuse of php really.  However it might be worth adding in something to make this work.

My script was getting the decimal address as a string, an integer.  The following example should illustrate it.

<?

$moo = "167772161";
echo long2ip($moo);

echo "<BR>";

$moo = 167772161;
echo long2ip($moo);

?>


As a side thing you also need to add the following to get larger numbers to work when using strings:

if ($moo > 0) $moo += pow(2,32);
 [2002-03-13 14:38 UTC] sander@php.net
Strange... you script works fine for me...
 [2002-03-13 15:25 UTC] lee at nerds dot org dot uk
In which case I would suspect that this is a platform dependent bug, as I have reproduced this on more than one FreeBSD machine, of different versions (all 4.x) on differing processors.

Presumably as this works fine when the address is presented as an int and not as a string, the problem lies in converting the string to an integer value, before it is tranformed into dotted octet notation?

If this is the case then it is probably some .h file being misdetected by the configure script??? </GUESS>

Also another pointer to problems is that if you use a number that requires more than 16 bits, then the returned address is always 127.255.255.255, which is quite a significant number.

My guess (although I am by no means an expert) is that in using some function like strtoul we are loosing the long or the unsigned attributes of the integer and so accidentally passing incorrect (16 bit number instead of 32??) information about??
 [2002-03-19 07:02 UTC] lee at nerds dot org dot uk
Another thing that might help narrow down what is happening is that is you use ip2long to convert an address above 127.255.255.255 the integer you get back is negative.

I presume that this means that somewhere in the code this is stored as a signed integer as opposed to an unsigned long?
 [2002-07-07 20:54 UTC] sniper@php.net
I was unable to reproduce this with latest CVS.
Please try this snapshot:

http://snaps.php.net/php4-latest.tar.gz

 [2002-08-10 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2002-08-15 23:21 UTC] nohn@php.net
Duplicate of http://bugs.php.net/bug.php?id=18816 ?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 11:01:27 2024 UTC