1
<?php

require_once('PEAR.php');

$GLOBALS['Net_EPP_Client_Version'] = '0.0.3';

class Net_EPP_Client {

    var $socket;

    function connect($host, $port=700, $timeout=1, $ssl=true, $sslcertpath, $sslpassphrase) {
        $context = @stream_context_create(); 
        $result = stream_context_set_option($context, 'ssl', 'allow_self_signed', 'true');
        $result = stream_context_set_option($context, 'ssl', 'local_cert', $sslcertpath); 
        $result = stream_context_set_option($context, 'ssl', 'passphrase', $sslpassphrase);

        $target = sprintf('%s://%s:%d', ($ssl === true ? 'ssl' : 'tcp'), $host, $port);
        if (!$this->socket = @stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context)) {
            return new PEAR_Error("Error connecting to $target - $errstr (code $errno)");
        } else {
            return $this->getFrame();
        }
    }

    function getFrame() {
        //stream_set_blocking($this->socket, 1);
        //stream_set_timeout($this->socket, 30);
        if (@feof($this->socket)) return new PEAR_Error('connection closed by remote server');

        //$hdr = @fread($this->socket, 4);
        $hdr = @fgets($this->socket, 5);
        //print_r($hdr); 
        //return new PEAR_Error($hdr);

        if (empty($hdr) && feof($this->socket)) {
            return new PEAR_Error('connection closed by remote server');

        } elseif (empty($hdr)) {
            //return new PEAR_Error('Error reading from server: '.$php_errormsg);
            $php_errormsg = isset($php_errormsg) ? $php_errormsg : "";
            return new PEAR_Error('Error reading from server: '. $php_errormsg);
        } else {
            /*
            $unpacked = unpack('N', $hdr);
            $answer = fread($this->socket, ($unpacked[1] - 4));
            return $answer;
            */
            $unpacked = unpack('N', $hdr);
            $length = $unpacked[1];

            if ($length < 5) {
                return new PEAR_Error(sprintf('Got a bad frame header length of %d bytes from server', $length));
            } else {

                //'|'.fread($this->socket, ($length - 4)).'|'; //not sure why, but this fixed some part here..
                //return '<'.fread($this->socket, ($length));
                return fread($this->socket, ($length));
            }
        }
    }


    function sendFrame($xml) {
        fwrite($this->socket, pack('N', (strlen($xml)+4)).$xml);
    }


    function request($xml) {
        $this->sendFrame($xml);
        return $this->getFrame();
    }

    function disconnect() {
        return @fclose($this->socket);
    }

}

?>

I met a issue that i will take memory leak from this line

return fread($this->socket, ($length));

Will you tell me what is wrong with this line

if(!$this->socket = @stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context))

I have searched issue that a connection opened by stream_socket_client() wasn't closed by the server, the socket will keep data from server. Is there any solution? Another software engineer have replaced this line

return fread($this->socket, ($length));

to the below code. They said they don't know why this error will happen.

'|'.fread($this->socket, ($length - 4)).'|';
return '<'.fread($this->socket, ($length));

I would like to know is it any alternative solution?

yannis
  • 6,215
  • 5
  • 39
  • 49
Jun
  • 41
  • 3
  • 10
  • Why do you think it leaks in that particular line? – zerkms Sep 18 '12 at 03:40
  • Hi, i have solved the issue. I refer the new code from this link https://github.com/centralnic/php-epp/blob/master/Net/EPP/Protocol.php. The reason is that socket will be buffered. So the memory will leak. Thanks for viewing. :) – Jun Sep 18 '12 at 09:37

0 Answers0