Skip to content

Commit 77280ab

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

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

ext/standard/net.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,46 @@
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+
76+
*mtu = (zend_long)ifr.ifr_mtu;
77+
status = SUCCESS;
78+
end:
79+
close(local);
80+
return status;
81+
#else
82+
return FAILURE;
83+
#endif
84+
}
85+
#endif
86+
5087
PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
5188
socklen_t addrlen = sizeof(struct sockaddr_in);
5289

@@ -274,7 +311,7 @@ PHP_FUNCTION(net_get_interfaces) {
274311
array_init(return_value);
275312
for (p = addrs; p; p = p->ifa_next) {
276313
zval *iface = zend_hash_str_find(Z_ARR_P(return_value), p->ifa_name, strlen(p->ifa_name));
277-
zval *unicast, *status;
314+
zval *unicast, *status, *mtu;
278315

279316
if (!iface) {
280317
zval newif;
@@ -298,6 +335,13 @@ PHP_FUNCTION(net_get_interfaces) {
298335
if (!status) {
299336
add_assoc_bool(iface, "up", ((p->ifa_flags & IFF_UP) != 0));
300337
}
338+
mtu = zend_hash_str_find(Z_ARR_P(iface), "mtu", sizeof("mtu") - 1);
339+
if (!mtu) {
340+
zend_long val;
341+
if (net_get_mtu(p->ifa_name, &val) == SUCCESS) {
342+
add_assoc_long(iface, "mtu", val);
343+
}
344+
}
301345
}
302346

303347
freeifaddrs(addrs);

0 commit comments

Comments
 (0)