Skip to content

Commit 20fa796

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

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

ext/standard/net.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,40 @@
4444
# include <Ws2tcpip.h>
4545
# include <iphlpapi.h>
4646
#else
47+
# ifdef HAVE_SYS_IOCTL_H
48+
# include <sys/ioctl.h>
49+
# endif
4750
# include <netdb.h>
4851
#endif
4952

53+
#ifndef PHP_WIN32
54+
static zend_result net_get_mtu(char *ifname, zend_long *mtu)
55+
{
56+
#ifdef SIOCGIFMTU
57+
struct ifreq ifr = {0};
58+
zend_result status = FAILURE;
59+
int local = socket(AF_UNIX, SOCK_DGRAM, 0);
60+
if (local == -1) {
61+
return FAILURE;
62+
}
63+
64+
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
65+
if (ioctl(local, SIOCGIFMTU, &ifr) < 0) {
66+
goto end;
67+
}
68+
69+
70+
*mtu = (zend_long)ifr.ifr_mtu;
71+
status = SUCCESS;
72+
end:
73+
close(local);
74+
return status;
75+
#else
76+
return FAILURE;
77+
#endif
78+
}
79+
#endif
80+
5081
PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
5182
socklen_t addrlen = sizeof(struct sockaddr_in);
5283

@@ -274,7 +305,7 @@ PHP_FUNCTION(net_get_interfaces) {
274305
array_init(return_value);
275306
for (p = addrs; p; p = p->ifa_next) {
276307
zval *iface = zend_hash_str_find(Z_ARR_P(return_value), p->ifa_name, strlen(p->ifa_name));
277-
zval *unicast, *status;
308+
zval *unicast, *status, *mtu;
278309

279310
if (!iface) {
280311
zval newif;
@@ -298,6 +329,13 @@ PHP_FUNCTION(net_get_interfaces) {
298329
if (!status) {
299330
add_assoc_bool(iface, "up", ((p->ifa_flags & IFF_UP) != 0));
300331
}
332+
mtu = zend_hash_str_find(Z_ARR_P(iface), "mtu", sizeof("mtu") - 1);
333+
if (!mtu) {
334+
zend_long val;
335+
if (net_get_mtu(p->ifa_name, &val) == SUCCESS) {
336+
add_assoc_long(iface, "mtu", val);
337+
}
338+
}
301339
}
302340

303341
freeifaddrs(addrs);

0 commit comments

Comments
 (0)