Skip to content

Commit ab4ee3d

Browse files
committed
test packet send/recv.
1 parent 35e2212 commit ab4ee3d

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

ext/sockets/sockets.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ PHP_FUNCTION(socket_bind)
14021402
struct sockaddr_ll *sa = (struct sockaddr_ll *) sock_type;
14031403
socklen_t sa_len = sizeof(sa);
14041404

1405-
if (getsockname(php_sock->bsd_socket, sock_type, &sa_len) < 0) {
1405+
if (getsockname(php_sock->bsd_socket, (struct sockaddr *)sa, &sa_len) < 0) {
14061406
zend_value_error("invalid AF_PACKET socket");
14071407
RETURN_THROWS();
14081408
}
@@ -1549,8 +1549,8 @@ PHP_FUNCTION(socket_recvfrom)
15491549
#ifdef AF_PACKET
15501550
// ethernet header + payload
15511551
// possibly follow-up PR SOCK_DGRAM
1552-
if (php_sock->type == AF_PACKET && arg3 < 1514) {
1553-
zend_argument_value_error(3, "must be at least 1514 for AF_PACKET");
1552+
if (php_sock->type == AF_PACKET && arg3 < 60) {
1553+
zend_argument_value_error(3, "must be at least 60 for AF_PACKET");
15541554
RETURN_THROWS();
15551555
}
15561556
#endif
@@ -1724,6 +1724,7 @@ PHP_FUNCTION(socket_recvfrom)
17241724
zend_update_property_string(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("macdst"), ether_ntoa((struct ether_addr *)e->h_dest));
17251725
zend_update_property_long(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("ethprotocol"), protocol);
17261726
zend_update_property(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("payload"), &zpayload);
1727+
// TODO fix leaks
17271728

17281729
ZEND_TRY_ASSIGN_REF_COPY(arg2, &obj);
17291730
ZEND_TRY_ASSIGN_REF_STRING(arg5, ifrname);

ext/sockets/tests/socket_afpacket.phpt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
1515
?>
1616
--FILE--
1717
<?php
18-
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_IP);
18+
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
1919
$s_bind = socket_bind($s_c, 'lo');
2020
var_dump($s_bind);
2121

@@ -27,16 +27,17 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
2727

2828
socket_getpeername($s_c, $istr2, $iindex2);
2929

30-
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
30+
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_LOOP);
3131
$v_bind = socket_bind($s_s, 'lo');
3232

33-
$buf = str_repeat("0", ETH_FRAME_LEN) .
34-
str_repeat("\xFF", 6) .
35-
str_repeat("\x11", 6) .
36-
"\x08\x00" .
37-
str_pad("TEST", 46, "\x00");
33+
$buf = pack("H12H12n", "ffffffffffff", "000000000000", ETH_P_LOOP);
34+
$buf .= str_repeat("A", 46);
3835

3936
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
37+
var_dump(socket_recvfrom($s_c, $rsp, strlen($buf), 0, $addr));
38+
39+
var_dump($addr);
40+
var_dump($rsp);
4041

4142
socket_close($s_c);
4243
?>
@@ -47,4 +48,23 @@ string(2) "lo"
4748
int(%i)
4849

4950
Warning: socket_getpeername(): unable to retrieve peer name [95]: %sot supported in %s on line %d
50-
int(1574)
51+
int(60)
52+
int(60)
53+
string(2) "lo"
54+
object(SocketEthernetInfo)#3 (4) {
55+
["socket"]=>
56+
uninitialized(Socket)
57+
["ethprotocol"]=>
58+
int(96)
59+
["macsrc"]=>
60+
string(11) "0:0:0:0:0:0"
61+
["macdst"]=>
62+
string(17) "ff:ff:ff:ff:ff:ff"
63+
["payload"]=>
64+
array(2) {
65+
["ipsrc"]=>
66+
string(11) "%d.%d.%d.%d"
67+
["ipdst"]=>
68+
string(11) "%d.%d.%d.%d"
69+
}
70+
}

0 commit comments

Comments
 (0)