15declare(strict_types=1);
17namespace raklib\protocol;
25use
function inet_ntop;
26use
function inet_pton;
43 $version = $this->getByte();
45 $addr = ((~$this->getByte()) & 0xff) .
"." . ((~$this->getByte()) & 0xff) .
"." . ((~$this->getByte()) & 0xff) .
"." . ((~$this->getByte()) & 0xff);
46 $port = $this->getShort();
48 }elseif($version === 6){
51 $port = $this->getShort();
53 $addr = inet_ntop($this->
get(16));
58 return new InternetAddress($addr, $port, $version);
60 throw new BinaryDataException(
"Unknown IP address version $version");
64 public function putString(
string $v) : void{
65 $this->putShort(strlen($v));
69 public function putAddress(InternetAddress $address) : void{
70 $version = $address->getVersion();
71 $this->putByte($version);
73 $parts = explode(
".", $address->getIp());
74 assert(count($parts) === 4,
"Wrong number of parts in IPv4 IP, expected 4, got " . count($parts));
75 foreach($parts as $b){
76 $this->putByte((~((
int) $b)) & 0xff);
78 $this->putShort($address->getPort());
79 }elseif($version === 6){
80 $this->putLShort(AF_INET6);
81 $this->putShort($address->getPort());
83 $rawIp = inet_pton($address->getIp());
85 throw new \InvalidArgumentException(
"Invalid IPv6 address could not be encoded");
90 throw new \InvalidArgumentException(
"IP version $version is not supported");