Skip to content

Commit 1c7ea4d

Browse files
committed
reminder for phase 2
1 parent d1090a5 commit 1c7ea4d

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

ext/sockets/sockets.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ PHP_FUNCTION(socket_bind)
14031403
}
14041404
#endif
14051405
default:
1406-
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1406+
zend_argument_value_error(1, "must be one of AF_UNIX, AF_PACKET, AF_INET, or AF_INET6");
14071407
RETURN_THROWS();
14081408
}
14091409

@@ -1504,6 +1504,9 @@ PHP_FUNCTION(socket_recvfrom)
15041504
struct sockaddr_in sin;
15051505
#ifdef HAVE_IPV6
15061506
struct sockaddr_in6 sin6;
1507+
#endif
1508+
#ifdef AF_PACKET
1509+
struct sockaddr_ll sll;
15071510
#endif
15081511
char addrbuf[INET6_ADDRSTRLEN];
15091512
socklen_t slen;
@@ -1608,9 +1611,39 @@ PHP_FUNCTION(socket_recvfrom)
16081611
ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
16091612
ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
16101613
break;
1614+
#endif
1615+
#ifdef AF_PACKET
1616+
case AF_PACKET:
1617+
// TODO expose and use proper ethernet frame type instead i.e. src mac, dst mac and payload to userland
1618+
// ditto for socket_sendto
1619+
slen = sizeof(sll);
1620+
memset(&sll, 0, sizeof(sll));
1621+
sll.sll_family = AF_PACKET;
1622+
char ifrname[IFNAMSIZ];
1623+
1624+
retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sll, (socklen_t *)&slen);
1625+
1626+
if (retval < 0) {
1627+
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
1628+
zend_string_efree(recv_buf);
1629+
RETURN_FALSE;
1630+
}
1631+
ZSTR_LEN(recv_buf) = retval;
1632+
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1633+
1634+
if (UNEXPECTED(!if_indextoname(sll.sll_ifindex, ifrname))) {
1635+
PHP_SOCKET_ERROR(php_sock, "unable to get the interface name", errno);
1636+
zend_string_efree(recv_buf);
1637+
RETURN_FALSE;
1638+
}
1639+
1640+
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1641+
ZEND_TRY_ASSIGN_REF_STRING(arg5, ifrname);
1642+
ZEND_TRY_ASSIGN_REF_LONG(arg6, sll.sll_ifindex);
1643+
break;
16111644
#endif
16121645
default:
1613-
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1646+
zend_argument_value_error(1, "must be one of AF_UNIX, AF_PACKET, AF_INET, or AF_INET6");
16141647
RETURN_THROWS();
16151648
}
16161649

0 commit comments

Comments
 (0)