Net_SSH2::_send_binary_packet PHP Method

_send_binary_packet() public method

See '6. Binary Packet Protocol' of rfc4253 for more info.
See also: self::_get_binary_packet()
public _send_binary_packet ( string $data, string $logged = null ) : boolean
$data string
$logged string
return boolean
    function _send_binary_packet($data, $logged = null)
    {
        if (!is_resource($this->fsock) || feof($this->fsock)) {
            user_error('Connection closed prematurely');
            $this->bitmap = 0;
            return false;
        }
        //if ($this->compress) {
        //    // the -4 removes the checksum:
        //    // http://php.net/function.gzcompress#57710
        //    $data = substr(gzcompress($data), 0, -4);
        //}
        // 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9
        $packet_length = strlen($data) + 9;
        // round up to the nearest $this->encrypt_block_size
        $packet_length += ($this->encrypt_block_size - 1) * $packet_length % $this->encrypt_block_size;
        // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length
        $padding_length = $packet_length - strlen($data) - 5;
        $padding = crypt_random_string($padding_length);
        // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself
        $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);
        $hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : '';
        $this->send_seq_no++;
        if ($this->encrypt !== false) {
            $packet = $this->encrypt->encrypt($packet);
        }
        $packet .= $hmac;
        $start = strtok(microtime(), ' ') + strtok('');
        // http://php.net/microtime#61838
        $result = strlen($packet) == fputs($this->fsock, $packet);
        $stop = strtok(microtime(), ' ') + strtok('');
        if (defined('NET_SSH2_LOGGING')) {
            $current = strtok(microtime(), ' ') + strtok('');
            $message_number = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')';
            $message_number = '-> ' . $message_number . ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
            $this->_append_log($message_number, isset($logged) ? $logged : $data);
            $this->last_packet = $current;
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * Request agent forwarding of remote server
  *
  * @param Net_SSH2 $ssh
  * @return bool
  * @access private
  */
 function _request_forwarding($ssh)
 {
     $request_channel = $ssh->_get_open_channel();
     if ($request_channel === false) {
         return false;
     }
     $packet = pack('CNNa*C', NET_SSH2_MSG_CHANNEL_REQUEST, $ssh->server_channels[$request_channel], strlen('*****@*****.**'), '*****@*****.**', 1);
     $ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_REQUEST;
     if (!$ssh->_send_binary_packet($packet)) {
         return false;
     }
     $response = $ssh->_get_channel_packet($request_channel);
     if ($response === false) {
         return false;
     }
     $ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN;
     $this->forward_status = SYSTEM_SSH_AGENT_FORWARD_ACTIVE;
     return true;
 }