Skip to content

Commit 9b15a69

Browse files
committed
ext/sockets: follow-up on AF_PACKET support.
support from socket_recvfrom and exposing basic PHP userland type.
1 parent b757fa8 commit 9b15a69

File tree

5 files changed

+122
-9
lines changed

5 files changed

+122
-9
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])
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/php_sockets.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ typedef struct {
7777

7878
extern PHP_SOCKETS_API zend_class_entry *socket_ce;
7979

80+
#ifdef AF_PACKET
81+
extern PHP_SOCKETS_API zend_class_entry *socket_ethinfo_ce;
82+
#endif
83+
8084
static inline php_socket *socket_from_obj(zend_object *obj) {
8185
return (php_socket *)((char *)(obj) - XtOffsetOf(php_socket, std));
8286
}

ext/sockets/sockets.c

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
# include <netdb.h>
4242
# include <netinet/in.h>
4343
# include <netinet/tcp.h>
44+
# include <netinet/udp.h>
4445
# include <sys/un.h>
4546
# include <arpa/inet.h>
4647
# include <sys/time.h>
@@ -54,6 +55,10 @@
5455
# ifdef HAVE_IF_NAMETOINDEX
5556
# include <net/if.h>
5657
# endif
58+
# ifdef HAVE_NETINET_ETHER_H
59+
# include <netinet/ether.h>
60+
# include <netinet/ip.h>
61+
# endif
5762
# if defined(HAVE_LINUX_SOCK_DIAG_H)
5863
# include <linux/sock_diag.h>
5964
# else
@@ -120,6 +125,9 @@ static PHP_RSHUTDOWN_FUNCTION(sockets);
120125

121126
zend_class_entry *socket_ce;
122127
static zend_object_handlers socket_object_handlers;
128+
#ifdef AF_PACKET
129+
zend_class_entry *socket_ethinfo_ce;
130+
#endif
123131

124132
static zend_object *socket_create_object(zend_class_entry *class_type) {
125133
php_socket *intern = zend_object_alloc(sizeof(php_socket), class_type);
@@ -482,6 +490,9 @@ static PHP_MINIT_FUNCTION(sockets)
482490
socket_object_handlers.get_gc = socket_get_gc;
483491
socket_object_handlers.compare = zend_objects_not_comparable;
484492

493+
#if defined(AF_PACKET)
494+
socket_ethinfo_ce = register_class_SocketEthernetInfo();
495+
#endif
485496
address_info_ce = register_class_AddressInfo();
486497
address_info_ce->create_object = address_info_create_object;
487498
address_info_ce->default_object_handlers = &address_info_object_handlers;
@@ -1505,7 +1516,7 @@ PHP_FUNCTION(socket_recvfrom)
15051516
struct sockaddr_in6 sin6;
15061517
#endif
15071518
#ifdef AF_PACKET
1508-
//struct sockaddr_ll sll;
1519+
struct sockaddr_ll sll;
15091520
#endif
15101521
char addrbuf[INET6_ADDRSTRLEN];
15111522
socklen_t slen;
@@ -1534,6 +1545,12 @@ PHP_FUNCTION(socket_recvfrom)
15341545
RETURN_FALSE;
15351546
}
15361547

1548+
#ifdef AF_PACKET
1549+
if (php_sock->type == AF_PACKET && arg3 < 2048) {
1550+
RETURN_FALSE;
1551+
}
1552+
#endif
1553+
15371554
recv_buf = zend_string_alloc(arg3 + 1, 0);
15381555

15391556
switch (php_sock->type) {
@@ -1612,14 +1629,14 @@ PHP_FUNCTION(socket_recvfrom)
16121629
break;
16131630
#endif
16141631
#ifdef AF_PACKET
1615-
/*
16161632
case AF_PACKET:
16171633
// TODO expose and use proper ethernet frame type instead i.e. src mac, dst mac and payload to userland
16181634
// ditto for socket_sendto
16191635
slen = sizeof(sll);
16201636
memset(&sll, 0, sizeof(sll));
16211637
sll.sll_family = AF_PACKET;
16221638
char ifrname[IFNAMSIZ];
1639+
zval zpayload;
16231640

16241641
retval = recvfrom(php_sock->bsd_socket, ZSTR_VAL(recv_buf), arg3, arg4, (struct sockaddr *)&sll, (socklen_t *)&slen);
16251642

@@ -1628,20 +1645,61 @@ PHP_FUNCTION(socket_recvfrom)
16281645
zend_string_efree(recv_buf);
16291646
RETURN_FALSE;
16301647
}
1631-
ZSTR_LEN(recv_buf) = retval;
1632-
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
16331648

16341649
if (UNEXPECTED(!if_indextoname(sll.sll_ifindex, ifrname))) {
16351650
PHP_SOCKET_ERROR(php_sock, "unable to get the interface name", errno);
16361651
zend_string_efree(recv_buf);
16371652
RETURN_FALSE;
16381653
}
16391654

1640-
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1655+
struct ethhdr *e = (struct ethhdr *)ZSTR_VAL(recv_buf);
1656+
unsigned short protocol = ntohs(e->h_proto);
1657+
unsigned char *payload = ((unsigned char *)e + sizeof(struct ethhdr));
1658+
1659+
object_init_ex(arg2, socket_ethinfo_ce);
1660+
zend_update_property_string(Z_OBJCE_P(arg2), Z_OBJ_P(arg2), ZEND_STRL("macsrc"), ether_ntoa((struct ether_addr *)e->h_source));
1661+
zend_update_property_string(Z_OBJCE_P(arg2), Z_OBJ_P(arg2), ZEND_STRL("macdst"), ether_ntoa((struct ether_addr *)e->h_dest));
1662+
array_init(&zpayload);
1663+
1664+
switch (protocol) {
1665+
case ETH_P_IP: {
1666+
struct iphdr *ip = (struct iphdr *)payload;
1667+
unsigned char *ipdata = payload + (ip->ihl * 4);
1668+
struct in_addr s, d;
1669+
s.s_addr = ip->saddr;
1670+
d.s_addr = ip->daddr;
1671+
add_assoc_string(&zpayload, "ipsrc", inet_ntoa(s));
1672+
add_assoc_string(&zpayload, "ipdst", inet_ntoa(d));
1673+
1674+
switch (ip->protocol) {
1675+
case IPPROTO_TCP: {
1676+
struct tcphdr *tcp = (struct tcphdr *)ipdata;
1677+
add_assoc_long(&zpayload, "portsrc", ntohs(tcp->th_sport));
1678+
add_assoc_long(&zpayload, "portdst", ntohs(tcp->th_dport));
1679+
break;
1680+
}
1681+
case IPPROTO_UDP: {
1682+
struct udphdr *udp = (struct udphdr *)ipdata;
1683+
add_assoc_long(&zpayload, "portsrc", ntohs(udp->uh_sport));
1684+
add_assoc_long(&zpayload, "portdst", ntohs(udp->uh_dport));
1685+
break;
1686+
}
1687+
default:
1688+
zend_value_error("unsupported ip header protocol");
1689+
RETURN_THROWS();
1690+
}
1691+
break;
1692+
}
1693+
default:
1694+
zend_value_error("unsupported ethernet protocol");
1695+
RETURN_THROWS();
1696+
}
1697+
1698+
zend_update_property(Z_OBJCE_P(arg2), Z_OBJ_P(arg2), ZEND_STRL("payload"), &zpayload);
1699+
16411700
ZEND_TRY_ASSIGN_REF_STRING(arg5, ifrname);
16421701
ZEND_TRY_ASSIGN_REF_LONG(arg6, sll.sll_ifindex);
16431702
break;
1644-
*/
16451703
#endif
16461704
default:
16471705
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
@@ -1752,7 +1810,7 @@ PHP_FUNCTION(socket_sendto)
17521810
17531811
retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
17541812
break;
1755-
*/
1813+
*/
17561814
#endif
17571815
default:
17581816
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");

ext/sockets/sockets.stub.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,3 +2149,17 @@ function socket_wsaprotocol_info_import(string $info_id): Socket|false {}
21492149

21502150
function socket_wsaprotocol_info_release(string $info_id): bool {}
21512151
#endif
2152+
2153+
#ifdef AF_PACKET
2154+
final class SocketEthernetInfo
2155+
{
2156+
/** @readonly **/
2157+
public Socket $socket;
2158+
/** @readonly **/
2159+
public string $macsrc;
2160+
/** @readonly **/
2161+
public string $macdst;
2162+
/** @readonly **/
2163+
public array $payload;
2164+
}
2165+
#endif

ext/sockets/sockets_arginfo.h

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)