Skip to content

Commit 4a4441f

Browse files
committed
adding more tests
1 parent 6cc916e commit 4a4441f

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

ext/sockets/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PHP_ARG_ENABLE([sockets],
55

66
if test "$PHP_SOCKETS" != "no"; then
77
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname sockatmark])
8-
AC_CHECK_HEADERS([sys/sockio.h linux/filter.h linux/if_packet.h linux/if_ether.h linux/udp.h netinet/ether.h])
8+
AC_CHECK_HEADERS([sys/sockio.h linux/filter.h linux/if_packet.h linux/if_ether.h netinet/ether.h])
99
AC_DEFINE([HAVE_SOCKETS], [1],
1010
[Define to 1 if the PHP extension 'sockets' is available.])
1111

ext/sockets/sockets.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@
7676
# if defined(HAVE_LINUX_IF_ETHER_H)
7777
# include <linux/if_ether.h>
7878
# endif
79-
# if defined(HAVE_LINUX_UDP_H)
80-
# include <linux/udp.h>
81-
# endif
8279
#endif
8380

8481
#include <stddef.h>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
AF_PACKET sockets handling errors
3+
--EXTENSIONS--
4+
sockets
5+
posix
6+
--SKIPIF--
7+
<?php
8+
9+
if (!defined("AF_PACKET")) {
10+
die('SKIP AF_PACKET not supported on this platform.');
11+
}
12+
if (!function_exists("posix_getuid") || posix_getuid() != 0) {
13+
die('SKIP AF_PACKET requires root permissions.');
14+
}
15+
?>
16+
--FILE--
17+
<?php
18+
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
19+
$s_bind = socket_bind($s_c, 'lo');
20+
21+
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
22+
$v_bind = socket_bind($s_s, 'lo');
23+
24+
$payload = random_bytes(1024);
25+
$payload .= str_repeat("A", 46);
26+
27+
$buf = pack("H12H12n", "ffffffffffff", "000000000000", 0x0806);
28+
$buf .= $payload;
29+
30+
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
31+
32+
try {
33+
socket_recvfrom($s_c, $rsp, strlen($buf), 0, $addr);
34+
} catch (\ValueError $e) {
35+
echo $e->getMessage(), PHP_EOL;
36+
}
37+
38+
socket_close($s_s);
39+
socket_close($s_c);
40+
41+
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
42+
$s_bind = socket_bind($s_c, 'lo');
43+
44+
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
45+
$v_bind = socket_bind($s_s, 'lo');
46+
47+
$buf = pack("H12H12n", "ffffffffffff", "000000000000", ETH_P_IP);
48+
$buf .= str_repeat("A", 46);
49+
50+
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
51+
52+
try {
53+
socket_recvfrom($s_c, $rsp, strlen($buf), 0, $addr);
54+
} catch (\ValueError $e) {
55+
echo $e->getMessage(), PHP_EOL;
56+
}
57+
58+
socket_close($s_s);
59+
socket_close($s_c);
60+
?>
61+
--EXPECTF--
62+
int(1084)
63+
unsupported ethernet protocol
64+
int(60)
65+
invalid transport header length

0 commit comments

Comments
 (0)