Skip to content

Commit 696ef35

Browse files
committed
add more tests
1 parent 651440b commit 696ef35

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

ext/sockets/sockets.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ PHP_FUNCTION(socket_recvfrom)
19301930
if (arg6) {
19311931
ZEND_TRY_ASSIGN_REF_LONG(arg6, sll.sll_ifindex);
19321932
}
1933-
zend_value_error("unsupported ip header protocol");
1933+
zend_value_error("unsupported ipv6 header protocol");
19341934
RETURN_THROWS();
19351935
}
19361936
break;

ext/sockets/tests/socket_afpacket.phpt

+105
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
5555
}
5656

5757
socket_close($s_c);
58+
socket_close($s_s);
5859

5960
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
6061
$s_bind = socket_bind($s_c, 'lo');
@@ -163,6 +164,68 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
163164
} catch (\ValueError $e) {
164165
echo $e->getMessage(), PHP_EOL;
165166
}
167+
168+
socket_close($s_s);
169+
socket_close($s_c);
170+
171+
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_IP);
172+
$s_bind = socket_bind($s_c, 'lo');
173+
174+
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_IP);
175+
$v_bind = socket_bind($s_s, 'lo');
176+
177+
$ip = hex2bin(
178+
"4500" .
179+
str_repeat("0028", 16) .
180+
"0000" .
181+
"4000" .
182+
"4006" .
183+
"0000" .
184+
"7f000001" .
185+
"7f000001"
186+
);
187+
$p = str_repeat("A", 20);
188+
189+
$buf = pack("H12H12n", "ffffffffffff", "000000000000", ETH_P_IP);
190+
$buf .= $ip . $p;
191+
192+
$min_frame_size = 60;
193+
$buf .= str_repeat("\x00", max(0, $min_frame_size - strlen($buf)));
194+
195+
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
196+
197+
try {
198+
socket_recvfrom($s_c, $rsp, strlen($buf), 0, $addr);
199+
} catch (\ValueError $e) {
200+
echo $e->getMessage(), PHP_EOL;
201+
}
202+
203+
$ip = hex2bin(
204+
"9999" .
205+
"0028" .
206+
"0000" .
207+
"4000" .
208+
"4006" .
209+
"0000" .
210+
"FFFFFeFF" .
211+
"7f000001"
212+
);
213+
$p = str_repeat("Bb", 80);
214+
215+
$buf = pack("H12H12n", "ffffffffffffh", "aaaaaAAAAAAA", ETH_P_IP);
216+
$buf .= $ip . $p;
217+
218+
$min_frame_size = 60;
219+
$buf .= str_repeat("\x00", max(0, $min_frame_size - strlen($buf)));
220+
221+
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
222+
var_dump(socket_recvfrom($s_c, $rsp, strlen($buf), 0, $addr));
223+
224+
var_dump($addr);
225+
var_dump($rsp);
226+
227+
socket_close($s_s);
228+
socket_close($s_c);
166229
?>
167230
--EXPECTF--
168231
bool(true)
@@ -274,4 +337,46 @@ object(Socket\EthernetPacket)#%d (7) {
274337
}
275338
}
276339
int(%d)
340+
unsupported ipv6 header protocol
341+
int(%d)
277342
unsupported ip header protocol
343+
int(%d)
344+
int(%d)
345+
string(%d) "%s"
346+
object(Socket\EthernetPacket)#9 (7) {
347+
["headerSize"]=>
348+
int(%d)
349+
["rawPacket"]=>
350+
string(%d) "%r(.|\n)*?"%r
351+
["socket"]=>
352+
object(Socket)#7 (0) {
353+
}
354+
["ethProtocol"]=>
355+
int(2048)
356+
["srcMac"]=>
357+
string(17) "aa:aa:aa:aa:aa:aa"
358+
["dstMac"]=>
359+
string(17) "ff:ff:ff:ff:ff:ff"
360+
["payload"]=>
361+
object(Socket\Ipv4Packet)#2 (5) {
362+
["headerSize"]=>
363+
int(40)
364+
["rawPacket"]=>
365+
string(%d) "%sunable to retrieve"
366+
["srcAddr"]=>
367+
string(15) "%s.%s.%s.%s"
368+
["dstAddr"]=>
369+
string(9) "%s.%s.%s.%s"
370+
["payload"]=>
371+
object(Socket\TcpPacket)#10 (4) {
372+
["headerSize"]=>
373+
int(%d)
374+
["rawPacket"]=>
375+
string(%d) "%r(.|\n)*?"%r
376+
["srcPort"]=>
377+
int(%d)
378+
["dstPort"]=>
379+
int(%d)
380+
}
381+
}
382+
}

0 commit comments

Comments
 (0)