Changeset View
Changeset View
Standalone View
Standalone View
src/app/IP4Net.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Support\Facades\DB; | |||||
class IP4Net extends Model | class IP4Net extends Model | ||||
{ | { | ||||
protected $table = "ip4nets"; | protected $table = "ip4nets"; | ||||
/** @var array<int, string> The attributes that are mass assignable */ | /** @var array<int, string> The attributes that are mass assignable */ | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'rir_name', | 'rir_name', | ||||
'net_number', | 'net_number', | ||||
'net_mask', | 'net_mask', | ||||
'net_broadcast', | 'net_broadcast', | ||||
'country', | 'country', | ||||
'serial', | 'serial', | ||||
'created_at', | 'created_at', | ||||
'updated_at' | 'updated_at' | ||||
]; | ]; | ||||
/** | |||||
* Get IP network by IP address | |||||
* | |||||
* @param string $ip IPv4 address | |||||
* | |||||
* @return ?\App\IP4Net IPv4 network record, Null if not found | |||||
*/ | |||||
public static function getNet($ip) | public static function getNet($ip) | ||||
{ | { | ||||
$where = 'INET_ATON(net_number) <= INET_ATON(?) and INET_ATON(net_broadcast) >= INET_ATON(?)'; | $where = 'INET_ATON(net_number) <= INET_ATON(?) and INET_ATON(net_broadcast) >= INET_ATON(?)'; | ||||
return IP4Net::whereRaw($where, [$ip, $ip]) | |||||
return self::whereRaw($where, [$ip, $ip]) | |||||
->orderByRaw('INET_ATON(net_number), net_mask DESC') | ->orderByRaw('INET_ATON(net_number), net_mask DESC') | ||||
->first(); | ->first(); | ||||
} | } | ||||
} | } |