Skip to content

Commit b0dd94b

Browse files
committed
standard/net: even field a bit more with windows by adding interface's MTU data.
1 parent 5d359cd commit b0dd94b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

ext/standard/net.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,48 @@
4444
# include <Ws2tcpip.h>
4545
# include <iphlpapi.h>
4646
#else
47+
# ifdef HAVE_SYS_IOCTL_H
48+
# define BSD_COMP 1
49+
# include <sys/ioctl.h>
50+
# include <fcntl.h>
51+
# endif
4752
# include <netdb.h>
4853
#endif
4954

55+
#ifndef PHP_WIN32
56+
static zend_result net_get_mtu(char *ifname, zend_long *mtu)
57+
{
58+
#ifdef SIOCGIFMTU
59+
struct ifreq ifr = {0};
60+
zend_result status = FAILURE;
61+
#ifndef __sun
62+
int local = socket(AF_UNIX, SOCK_DGRAM, 0);
63+
#else
64+
int local = open("/dev/ip", O_RDONLY);
65+
#endif
66+
if (local == -1) {
67+
return FAILURE;
68+
}
69+
70+
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
71+
if (ioctl(local, SIOCGIFMTU, &ifr) < 0) {
72+
goto end;
73+
}
74+
75+
// TODO: a tad harder but mac address could be obtained
76+
// in the same movement.
77+
78+
*mtu = (zend_long)ifr.ifr_mtu;
79+
status = SUCCESS;
80+
end:
81+
close(local);
82+
return status;
83+
#else
84+
return FAILURE;
85+
#endif
86+
}
87+
#endif
88+
5089
PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
5190
socklen_t addrlen = sizeof(struct sockaddr_in);
5291

@@ -274,7 +313,7 @@ PHP_FUNCTION(net_get_interfaces) {
274313
array_init(return_value);
275314
for (p = addrs; p; p = p->ifa_next) {
276315
zval *iface = zend_hash_str_find(Z_ARR_P(return_value), p->ifa_name, strlen(p->ifa_name));
277-
zval *unicast, *status;
316+
zval *unicast, *status, *mtu;
278317

279318
if (!iface) {
280319
zval newif;
@@ -298,6 +337,13 @@ PHP_FUNCTION(net_get_interfaces) {
298337
if (!status) {
299338
add_assoc_bool(iface, "up", ((p->ifa_flags & IFF_UP) != 0));
300339
}
340+
mtu = zend_hash_str_find(Z_ARR_P(iface), "mtu", sizeof("mtu") - 1);
341+
if (!mtu) {
342+
zend_long val;
343+
if (net_get_mtu(p->ifa_name, &val) == SUCCESS) {
344+
add_assoc_long(iface, "mtu", val);
345+
}
346+
}
301347
}
302348

303349
freeifaddrs(addrs);

0 commit comments

Comments
 (0)