-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc-cidr.php
36 lines (29 loc) · 845 Bytes
/
func-cidr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require __DIR__ . '/func-proxy.php';
/**
* Generate a random IP address within a CIDR range.
*
* @param string $cidr The CIDR range (e.g., "192.168.1.0/24").
* @return string The random IP address.
*/
function generateRandomIP(string $cidr): string
{
list($ip, $subnet) = explode('/', $cidr);
// Convert IP to binary format
$ipBinary = ip2long($ip);
// Calculate the number of available IP addresses in the subnet
$subnetSize = pow(2, (32 - $subnet));
// Generate a random offset within the subnet
$offset = mt_rand(1, $subnetSize - 2); // Exclude network address and broadcast address
// Calculate the resulting IP
return long2ip($ipBinary + $offset);
}
/**
* Generate a random port number.
*
* @return int The random port number.
*/
function generateRandomPort(): int
{
return mt_rand(1024, 65535);
}