[DISCUSSION] Native UUID support in PHP

php.internals

Aran Reeks

6 years ago
Hi Internals, I'd like to open up a discussion around the implementation of a new function within PHP for generating a UUID. Whilst there are libraries out there already for PHP which can generate a UUID, all of those libraries have the possibility to generate IDs that *could* have a collision. The specification for UUID versions 1 and 2 allow for collision-free ID generation by incorporating the unique MAC addresses from network cards - something which can't be accessed from PHP code at present. I can see two possible methods to enable this; - New function added to PHP which exposes the MAC address (allowing external implementations for UUIDs, as well as other possible purposes a MAC address could be used). - A new PHP function which introduced a uuid() function which will generate and return version-compliant UUIDs. - Both of the above. Thoughts and feedback welcome to kickstart conversations and if there's a positive consensus, I'd like to put forward an RFC for this to be introduced in PHP 8. Best, Aran

Christoph Becker

6 years ago
On 16.03.2020 at 17:06, Aran Reeks wrote:
> I'd like to open up a discussion around the implementation of a new > function within PHP for generating a UUID. > > Whilst there are libraries out there already for PHP which can generate a > UUID, all of those libraries have the possibility to generate IDs that > *could* have a collision. > > The specification for UUID versions 1 and 2 allow for collision-free ID > generation by incorporating the unique MAC addresses from network cards - > something which can't be accessed from PHP code at present. > > I can see two possible methods to enable this; > > - New function added to PHP which exposes the MAC address (allowing > external implementations for UUIDs, as well as other possible purposes a > MAC address could be used). > > - A new PHP function which introduced a uuid() function which will > generate and return version-compliant UUIDs. > > - Both of the above. > > Thoughts and feedback welcome to kickstart conversations and if there's a > positive consensus, I'd like to put forward an RFC for this to be > introduced in PHP 8.
See also <https://wiki.php.net/rfc/uuid>.
-- Christoph M. Becker

Ben Ramsey

6 years ago
> On Mar 16, 2020, at 11:06, Aran Reeks <cdtreeks@gmail.com> wrote: > > Hi Internals, > > I'd like to open up a discussion around the implementation of a new > function within PHP for generating a UUID. > > Whilst there are libraries out there already for PHP which can generate a > UUID, all of those libraries have the possibility to generate IDs that > *could* have a collision. > > The specification for UUID versions 1 and 2 allow for collision-free ID > generation by incorporating the unique MAC addresses from network cards - > something which can't be accessed from PHP code at present. > > I can see two possible methods to enable this; > > - New function added to PHP which exposes the MAC address (allowing > external implementations for UUIDs, as well as other possible purposes a > MAC address could be used). > > - A new PHP function which introduced a uuid() function which will > generate and return version-compliant UUIDs. > > - Both of the above. > > Thoughts and feedback welcome to kickstart conversations and if there's a > positive consensus, I'd like to put forward an RFC for this to be > introduced in PHP 8. >
While undocumented, PHP has the method `net_get_interfaces()` (since PHP 7.3.0). It uses `getifaddrs()` on Linux and `GetAdaptersAddresses()` on Windows to create an array of network interface addresses. See https://github.com/php/php-src/commit/7ca5a7d84ebdc1b97f49cb460f200db093b96d9d Right now, the address is only added when it is an IPv4 or IPv6 address. It just needs to be modified to look for the correct “family” and set that as the “mac” array value for the interface. I’m currently working on a patch for this, so this might help with one aspect of your proposal. Cheers, Ben

Aran Reeks

6 years ago
Hi Ben, Thanks for the feedback, I wasn't aware of net_get_interfaces, but that's really helpful and agree that post your patch, implementing a get_network_mac_addr() function (or similar name) would be amazing. Many thanks Aran On Mon, 16 Mar 2020 at 16:16, Ben Ramsey <ben@benramsey.com> wrote:

Ben Ramsey

6 years ago
> On Mar 16, 2020, at 11:22, Aran Reeks <cdtreeks@gmail.com> wrote: > > Hi Ben, > > Thanks for the feedback, I wasn't aware of net_get_interfaces, but that's really helpful and agree that post your patch, implementing a get_network_mac_addr() function (or similar name) would be amazing. >
net_get_interfaces() returns an array that looks like this, so patching this wouldn’t introduce a new method but, rather, modify this array to include a key named “mac,” which (I think) it already does on Windows but not on Linux. [ "lo" => [ "unicast" => [ [ "flags" => 65609, "family" => 17, ], [ "flags" => 65609, "family" => 2, "address" => "127.0.0.1", "netmask" => "255.0.0.0", ], ], "up" => true, ], "tunl0" => [ "unicast" => [ [ "flags" => 128, "family" => 17, ], ], "up" => false, ], "ip6tnl0" => [ "unicast" => [ [ "flags" => 128, "family" => 17, ], ], "up" => false, ], "eth0" => [ "unicast" => [ [ "flags" => 69699, "family" => 17, ], [ "flags" => 69699, "family" => 2, "address" => "172.17.0.2", "netmask" => "255.255.0.0", "broadcast" => "172.17.255.255", ], ], "up" => true, ], ]

Sara Golemon

6 years ago
On Mon, Mar 16, 2020 at 11:17 AM Ben Ramsey <ben@benramsey.com> wrote:
> I’m currently working on a patch for this, so this might help with one > aspect of your proposal. > > Here's a quick&dirty for AF_PACKET, looks like OSX/BSD might need AF_LINK
though... https://github.com/php/php-src/compare/master...sgolemon:sgolemon.ngi-mac

Ben Ramsey

6 years ago
> On Mar 16, 2020, at 12:48, Sara Golemon <pollita@php.net> wrote: > > On Mon, Mar 16, 2020 at 11:17 AM Ben Ramsey <ben@benramsey.com> wrote: > I’m currently working on a patch for this, so this might help with one aspect of your proposal. > > Here's a quick&dirty for AF_PACKET, looks like OSX/BSD might need AF_LINK though... > > https://github.com/php/php-src/compare/master...sgolemon:sgolemon.ngi-mac
Yep. OSX/BSD needs AF_LINK.

Sara Golemon

6 years ago
On Mon, Mar 16, 2020 at 12:49 PM Ben Ramsey <ben@benramsey.com> wrote:

Ben Ramsey

6 years ago
> On Mar 16, 2020, at 13:48, Sara Golemon <pollita@php.net> wrote: > >  >> On Mon, Mar 16, 2020 at 12:49 PM Ben Ramsey <ben@benramsey.com> wrote: > >> > On Mar 16, 2020, at 12:48, Sara Golemon <pollita@php.net> wrote: >> > >> > On Mon, Mar 16, 2020 at 11:17 AM Ben Ramsey <ben@benramsey.com> wrote: >> > I’m currently working on a patch for this, so this might help with one aspect of your proposal. >> > >> > Here's a quick&dirty for AF_PACKET, looks like OSX/BSD might need AF_LINK though... >> > >> > https://github.com/php/php-src/compare/master...sgolemon:sgolemon.ngi-mac >> >> >> Yep. OSX/BSD needs AF_LINK. >> > Updated for AF_LINK
Thanks, Sara! That’s what I call “mailing list driven development.” Cheers, Ben