Skip to content

Commit da14f3e

Browse files
committed
forbids non SOCK_RAW socket with AF_PACKET for the moment.
1 parent eb41f3f commit da14f3e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

ext/sockets/sockets.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,8 @@ PHP_FUNCTION(socket_recvfrom)
15161516
#endif
15171517
#ifdef AF_PACKET
15181518
struct sockaddr_ll sll;
1519+
int protoid;
1520+
socklen_t protoidlen = sizeof(protoid);
15191521
#endif
15201522
char addrbuf[INET6_ADDRSTRLEN];
15211523
socklen_t slen;
@@ -1632,6 +1634,13 @@ PHP_FUNCTION(socket_recvfrom)
16321634
#endif
16331635
#ifdef AF_PACKET
16341636
case AF_PACKET:
1637+
getsockopt(php_sock->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
1638+
1639+
// TODO: SOCK_DGRAM support
1640+
if (protoid != SOCK_RAW) {
1641+
zend_argument_value_error(1, "must be SOCK_RAW socket type");
1642+
RETURN_THROWS();
1643+
}
16351644
slen = sizeof(sll);
16361645
memset(&sll, 0, sizeof(sll));
16371646
sll.sll_family = AF_PACKET;
@@ -1755,6 +1764,8 @@ PHP_FUNCTION(socket_sendto)
17551764
#ifdef AF_PACKET
17561765
struct sockaddr_ll sll;
17571766
unsigned char halen;
1767+
int protoid;
1768+
socklen_t protoidlen = sizeof(protoid);
17581769
#endif
17591770
int retval;
17601771
size_t buf_len;
@@ -1841,12 +1852,19 @@ PHP_FUNCTION(socket_sendto)
18411852
#endif
18421853
#ifdef AF_PACKET
18431854
case AF_PACKET:
1855+
getsockopt(php_sock->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &protoid, &protoidlen);
1856+
1857+
// TODO: SOCK_DGRAM support
1858+
if (protoid != SOCK_RAW) {
1859+
zend_argument_value_error(1, "must be SOCK_RAW socket type");
1860+
RETURN_THROWS();
1861+
}
18441862
if (port_is_null) {
18451863
zend_argument_value_error(6, "cannot be null when the socket type is AF_PACKET");
18461864
RETURN_THROWS();
18471865
}
18481866

1849-
halen = addr_len > ETH_ALEN ? ETH_ALEN : (unsigned char)addr_len;
1867+
halen = ZSTR_LEN(addr) > ETH_ALEN ? ETH_ALEN : (unsigned char)ZSTR_LEN(addr);
18501868

18511869
memset(&sll, 0, sizeof(sll));
18521870
memcpy(sll.sll_addr, addr, halen);

0 commit comments

Comments
 (0)