From: [EMAIL PROTECTED]
Operating system: Linux
PHP version: 4.2.2
PHP Bug Type: Zend Engine 2 problem
Bug description: Socket_recvfrom call will truncate returning string if data had a 0
in it.
When using the socket_recvfrom call with a UDP socket - if you sent a
string to the socket that was
$length = socket_recvfrom($sock, $buf, 2048, 0 , $name, $port );
pppppppp0pppppppp - the $length would be the full amount read off the
socket, but the $buf will only contain the data up to the first 0 (value
is zero, i.e. NULL) and nothing beyond it. It seems that the string
handling routines of the engine truncate the memory/length if the network
read data contains a 0.
Attached are the server side php code that will show this and a C program
to generate the packets.
The server program will printout the chars in the packet seperated by a ~
<**** begin server php script ****>
<?php
//*****
// Server Connection Script
// Receive : UDP packets from clients and act based on
control bytes.
//*****
set_time_limit(0);//***** Don't let the script stop
$addr = "192.168.1.2";//***** Server info
$port = 9000;
$sock = socket_create(AF_INET, SOCK_DGRAM, 0); //*****Create socket for
UDP
if ($sock < 0 )
die(print $sock);
if (($ret = socket_bind($sock, $addr, $port)) < 0) //*****
Complete socket connection
die(print $ret);
$read = socket_recvfrom($sock, $buf, 2048, 0 , $name, $port );
//process information in buffer
for ($k=0;$k < $read ;$k++)
{
echo $buf[$k];
echo "~";
}
print "\r\nRead $read bytes from Network\r\n";
print "IP Addy: $name\r\n";
print "Port: $port\r\n";
socket_close($sock);
?>
<****** End server php script******>
<****** begin client C program ******>
/* Generate Some udp packet for network. */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/inet.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
unsigned int ConvertHostname(char *hostname);
void main(void)
{
int connSocket;
sockaddr_in sin_connect;
sin_connect.sin_port = htons(9001);
sin_connect.sin_addr.s_addr = INADDR_ANY;
sin_connect.sin_family = AF_INET;
connSocket = socket(AF_INET, SOCK_DGRAM, 0);
if(bind(connSocket,(sockaddr*)&sin_connect,sizeof(sockaddr_in)) ==
-1)
{
connSocket = 0;
printf("Socket Bind error");
return 0;
}
sin_connect.sin_addr.s_addr = ConvertHostname("192.168.1.2");
sin_connect.sin_port = htons(9000);
int dataLength = 64
char *data = new char[dataLength];
memset(data,80,dataLength); // put a P in the data packet
data[10] = 0; // put a zero in the data stream
int a = sendto(connSocket,(char *)data,dataLength,0,(sockaddr *)
&sin_connect,sizeof(struct sockaddr)$
printf("Sendto did %d\n",a);
delete [] data;
close(connSocket);
}
unsigned int ConvertHostname(char *hostname)
{
struct hostent *phost;
struct in_addr addr;
addr.s_addr = inet_addr(hostname);
if(addr.s_addr == 0)
return 0;
if(addr.s_addr == -1)
{
phost = gethostbyname(hostname);
if(phost == NULL)
{
printf("Error: Couldn't resolve hostname.");
return 0;
}
memcpy((char *)&addr.s_addr,
phost->h_addr,phost->h_length);
}
return addr.s_addr;
}
<****** end client C program ******>
--
Edit bug report at http://bugs.php.net/?id=21033&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=21033&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=21033&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=21033&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=21033&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=21033&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=21033&r=support
Expected behavior: http://bugs.php.net/fix.php?id=21033&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=21033&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=21033&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=21033&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21033&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=21033&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=21033&r=isapi