Added DNS server
This commit is contained in:
@@ -12,12 +12,15 @@ final class SocketServer {
|
||||
private int $port; // The Port to bind to
|
||||
private int $backlog = 5; // Number of incoming connections queued
|
||||
|
||||
private int $type; // Socket type
|
||||
|
||||
private array $handler; // The class and method that will handle our connection
|
||||
|
||||
public function __construct(int $port,string $bind='0.0.0.0')
|
||||
public function __construct(int $port,string $bind='0.0.0.0',int $type=SOCK_STREAM)
|
||||
{
|
||||
$this->bind = $bind;
|
||||
$this->port = $port;
|
||||
$this->type = $type;
|
||||
|
||||
$this->_init();
|
||||
}
|
||||
@@ -49,7 +52,18 @@ final class SocketServer {
|
||||
if (! extension_loaded('pcntl'))
|
||||
throw new SocketException(SocketException::CANT_ACCEPT,'Missing pcntl extension');
|
||||
|
||||
$this->server = socket_create(AF_INET|AF_INET6,SOCK_STREAM,SOL_TCP);
|
||||
switch ($this->type) {
|
||||
case SOCK_STREAM:
|
||||
$this->server = socket_create(AF_INET|AF_INET6,$this->type,SOL_TCP);
|
||||
break;
|
||||
|
||||
case SOCK_DGRAM:
|
||||
$this->server = socket_create(AF_INET|AF_INET6,$this->type,SOL_UDP);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('Unknown socket_type:'.$this->type);
|
||||
}
|
||||
|
||||
if ($this->server === FALSE)
|
||||
throw new SocketException(SocketException::CANT_CREATE_SOCKET,socket_strerror(socket_last_error()));
|
||||
@@ -78,12 +92,22 @@ final class SocketServer {
|
||||
if (! $this->handler)
|
||||
throw new SocketException(SocketException::CANT_LISTEN,'Handler not set.');
|
||||
|
||||
if (socket_listen($this->server,$this->backlog) === FALSE)
|
||||
throw new SocketException(SocketException::CANT_LISTEN,socket_strerror(socket_last_error($this->server)));
|
||||
if (in_array($this->type,[SOCK_STREAM,SOCK_SEQPACKET]))
|
||||
if (socket_listen($this->server,$this->backlog) === FALSE)
|
||||
throw new SocketException(SocketException::CANT_LISTEN,socket_strerror(socket_last_error($this->server)));
|
||||
|
||||
Log::info(sprintf('%s:- Listening on [%s:%d]',self::LOGKEY,$this->bind,$this->port));
|
||||
|
||||
$this->loop();
|
||||
switch ($this->type) {
|
||||
case SOCK_STREAM:
|
||||
$this->loop_tcp();
|
||||
break;
|
||||
|
||||
case SOCK_DGRAM:
|
||||
$this->loop_udp();
|
||||
break;
|
||||
}
|
||||
|
||||
socket_close($this->server);
|
||||
|
||||
Log::info(sprintf('%s:= Closed [%s:%d]',self::LOGKEY,$this->bind,$this->port));
|
||||
@@ -94,7 +118,7 @@ final class SocketServer {
|
||||
*
|
||||
* @throws SocketException
|
||||
*/
|
||||
private function loop()
|
||||
private function loop_tcp()
|
||||
{
|
||||
while (TRUE) {
|
||||
if (($accept = socket_accept($this->server)) === FALSE)
|
||||
@@ -112,6 +136,19 @@ final class SocketServer {
|
||||
}
|
||||
}
|
||||
|
||||
private function loop_udp()
|
||||
{
|
||||
$buf = '';
|
||||
$remote = [];
|
||||
$remote['ip'] = NULL;
|
||||
$remote['port'] = NULL;
|
||||
|
||||
while (TRUE) {
|
||||
if (socket_recvfrom($this->server,$buf,512,MSG_WAITALL,$remote['ip'],$remote['port']))
|
||||
$this->handler[0]->{$this->handler[1]}($remote,$buf,$this->server);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set our connection handler Class and Method
|
||||
*
|
||||
|
Reference in New Issue
Block a user