/** * Parse binary data * * @param string $binaryData The binary data */ public function parseBinary($binaryData) { if (!is_null($binaryData) && Util::binaryLength($binaryData) >= TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE) { $reply = unpack('C1status/C1flags/n1server_msgLenght/n1dataLenght', $binaryData); $this->status = $reply['status']; $this->flags = $reply['flags']; $this->msgLenght = $reply['server_msgLenght']; $this->dataLenght = $reply['dataLenght']; $checkLen = TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE + $this->msgLenght + $this->dataLenght; if (Util::binaryLength($binaryData) == $checkLen) { $unpackMask = 'a' . TAC_AUTHEN_REPLY_FIXED_FIELDS_SIZE . 'header'; if ($this->msgLenght > 0) { $unpackMask .= '/a' . $this->msgLenght . 'msg'; } if ($this->dataLenght > 0) { $unpackMask .= '/a' . $this->dataLenght . 'data'; } $unpack = unpack($unpackMask, $binaryData); if ($this->msgLenght > 0) { $this->msg = $unpack['msg']; } if ($this->dataLenght > 0) { $this->data = $unpack['data']; } } } }
/** * Returns the binary representation * * @return string */ public function toBinary() { $bin = pack('CCCCCCCC', $this->action, $this->privilegeLevel, $this->authenticationType, $this->service, Util::binaryLength($this->user), Util::binaryLength($this->port), Util::binaryLength($this->remoteAddress), Util::binaryLength($this->data)); if (Util::binaryLength($this->user) > 0) { $bin .= pack('a*', $this->user); } if (Util::binaryLength($this->port) > 0) { $bin .= pack('a*', $this->port); } if (Util::binaryLength($this->remoteAddress) > 0) { $bin .= pack('a*', $this->remoteAddress); } if (Util::binaryLength($this->data) > 0) { $bin .= pack('a*', $this->data); } return $bin; }
/** * Send * * @param Packet $packet The packet * * @return void */ protected function send($packet) { $this->log("Sending TACACS+ message... "); $data = $packet->toBinary(); @socket_write($this->socket, $data, Util::binaryLength($data)); $this->log("DONE (wrote " . Util::binaryLength($data) . " bytes)!"); $unpackMask = 'H' . TAC_PLUS_HDR_SIZE . 'header/H*body'; $unpack = unpack($unpackMask, $data); $unpackHeader = $unpack['header']; $unpackBody = $unpack['body']; $this->log("SENT: " . implode($unpack)); $this->log("SENT (Header): " . $unpackHeader); $this->log("SENT (Body): " . $unpackBody); }