Skip to content

Commit fc54cc9

Browse files
committed
add more ethernet frame protocols support on reception
1 parent 6ed9e9b commit fc54cc9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

ext/sockets/sockets.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
# ifdef HAVE_NETINET_ETHER_H
5959
# include <netinet/ether.h>
6060
# include <netinet/ip.h>
61+
# include <linux/ipv6.h>
6162
# endif
6263
# if defined(HAVE_LINUX_SOCK_DIAG_H)
6364
# include <linux/sock_diag.h>
@@ -1628,8 +1629,6 @@ PHP_FUNCTION(socket_recvfrom)
16281629
#endif
16291630
#ifdef AF_PACKET
16301631
case AF_PACKET:
1631-
// TODO expose and use proper ethernet frame type instead i.e. src mac, dst mac and payload to userland
1632-
// ditto for socket_sendto
16331632
slen = sizeof(sll);
16341633
memset(&sll, 0, sizeof(sll));
16351634
sll.sll_family = AF_PACKET;
@@ -1688,6 +1687,24 @@ PHP_FUNCTION(socket_recvfrom)
16881687
}
16891688
break;
16901689
}
1690+
case ETH_P_IPV6: {
1691+
struct ipv6hdr *ip = (struct ipv6hdr *)payload;
1692+
char s[INET6_ADDRSTRLEN], d[INET6_ADDRSTRLEN];
1693+
inet_ntop(AF_INET6, &ip->saddr, s, sizeof(s));
1694+
inet_ntop(AF_INET6, &ip->daddr, d, sizeof(d));
1695+
add_assoc_string(&zpayload, "ipsrc", s);
1696+
add_assoc_string(&zpayload, "ipdst", d);
1697+
break;
1698+
}
1699+
case ETH_P_LOOP: {
1700+
struct iphdr *ip = (struct iphdr *)payload;
1701+
struct in_addr s, d;
1702+
s.s_addr = ip->saddr;
1703+
d.s_addr = ip->daddr;
1704+
add_assoc_string(&zpayload, "ipsrc", inet_ntoa(s));
1705+
add_assoc_string(&zpayload, "ipdst", inet_ntoa(d));
1706+
break;
1707+
}
16911708
default:
16921709
zend_value_error("unsupported ethernet protocol");
16931710
RETURN_THROWS();
@@ -1719,7 +1736,7 @@ PHP_FUNCTION(socket_sendto)
17191736
struct sockaddr_in6 sin6;
17201737
#endif
17211738
#ifdef AF_PACKET
1722-
//struct sockaddr_ll sll;
1739+
struct sockaddr_ll sll;
17231740
#endif
17241741
int retval;
17251742
size_t buf_len;
@@ -1796,7 +1813,6 @@ PHP_FUNCTION(socket_sendto)
17961813
break;
17971814
#endif
17981815
#ifdef AF_PACKET
1799-
/*
18001816
case AF_PACKET:
18011817
if (port_is_null) {
18021818
zend_argument_value_error(6, "cannot be null when the socket type is AF_PACKET");
@@ -1806,10 +1822,11 @@ PHP_FUNCTION(socket_sendto)
18061822
memset(&sll, 0, sizeof(sll));
18071823
sll.sll_family = AF_PACKET;
18081824
sll.sll_ifindex = port;
1825+
sll.sll_halen = ETH_ALEN;
18091826

1827+
// TODO allows to use more user friendly type to replace raw buffer usage
18101828
retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
18111829
break;
1812-
*/
18131830
#endif
18141831
default:
18151832
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");

ext/sockets/tests/socket_afpacket.phpt

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)