Skip to content

standard/net: even field a bit more with windows by adding interface'… #14624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion ext/standard/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,48 @@
# include <Ws2tcpip.h>
# include <iphlpapi.h>
#else
# ifdef HAVE_SYS_IOCTL_H
# define BSD_COMP 1
# include <sys/ioctl.h>
# include <fcntl.h>
# endif
# include <netdb.h>
#endif

#ifndef PHP_WIN32
static zend_result net_get_mtu(char *ifname, zend_long *mtu)
{
#ifdef SIOCGIFMTU
struct ifreq ifr = {0};
zend_result status = FAILURE;
#ifndef __sun
int local = socket(AF_UNIX, SOCK_DGRAM, 0);
#else
int local = open("/dev/ip", O_RDONLY);
#endif
if (local == -1) {
return FAILURE;
}

strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(local, SIOCGIFMTU, &ifr) < 0) {
goto end;
}

// TODO: a tad harder but mac address could be obtained
// in the same movement.

*mtu = (zend_long)ifr.ifr_mtu;
status = SUCCESS;
end:
close(local);
return status;
#else
return FAILURE;
#endif
}
#endif

PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
socklen_t addrlen = sizeof(struct sockaddr_in);

Expand Down Expand Up @@ -274,7 +313,7 @@ PHP_FUNCTION(net_get_interfaces) {
array_init(return_value);
for (p = addrs; p; p = p->ifa_next) {
zval *iface = zend_hash_str_find(Z_ARR_P(return_value), p->ifa_name, strlen(p->ifa_name));
zval *unicast, *status;
zval *unicast, *status, *mtu;

if (!iface) {
zval newif;
Expand All @@ -298,6 +337,13 @@ PHP_FUNCTION(net_get_interfaces) {
if (!status) {
add_assoc_bool(iface, "up", ((p->ifa_flags & IFF_UP) != 0));
}
mtu = zend_hash_str_find(Z_ARR_P(iface), "mtu", sizeof("mtu") - 1);
if (!mtu) {
zend_long val;
if (net_get_mtu(p->ifa_name, &val) == SUCCESS) {
add_assoc_long(iface, "mtu", val);
}
}
}

freeifaddrs(addrs);
Expand Down
Loading