Skip to content

Commit d2cdf04

Browse files
committed
add more ethernet frame protocols support on reception
1 parent 9b15a69 commit d2cdf04

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>
@@ -1630,8 +1631,6 @@ PHP_FUNCTION(socket_recvfrom)
16301631
#endif
16311632
#ifdef AF_PACKET
16321633
case AF_PACKET:
1633-
// TODO expose and use proper ethernet frame type instead i.e. src mac, dst mac and payload to userland
1634-
// ditto for socket_sendto
16351634
slen = sizeof(sll);
16361635
memset(&sll, 0, sizeof(sll));
16371636
sll.sll_family = AF_PACKET;
@@ -1690,6 +1689,24 @@ PHP_FUNCTION(socket_recvfrom)
16901689
}
16911690
break;
16921691
}
1692+
case ETH_P_IPV6: {
1693+
struct ipv6hdr *ip = (struct ipv6hdr *)payload;
1694+
char s[INET6_ADDRSTRLEN], d[INET6_ADDRSTRLEN];
1695+
inet_ntop(AF_INET6, &ip->saddr, s, sizeof(s));
1696+
inet_ntop(AF_INET6, &ip->daddr, d, sizeof(d));
1697+
add_assoc_string(&zpayload, "ipsrc", s);
1698+
add_assoc_string(&zpayload, "ipdst", d);
1699+
break;
1700+
}
1701+
case ETH_P_LOOP: {
1702+
struct iphdr *ip = (struct iphdr *)payload;
1703+
struct in_addr s, d;
1704+
s.s_addr = ip->saddr;
1705+
d.s_addr = ip->daddr;
1706+
add_assoc_string(&zpayload, "ipsrc", inet_ntoa(s));
1707+
add_assoc_string(&zpayload, "ipdst", inet_ntoa(d));
1708+
break;
1709+
}
16931710
default:
16941711
zend_value_error("unsupported ethernet protocol");
16951712
RETURN_THROWS();
@@ -1721,7 +1738,7 @@ PHP_FUNCTION(socket_sendto)
17211738
struct sockaddr_in6 sin6;
17221739
#endif
17231740
#ifdef AF_PACKET
1724-
//struct sockaddr_ll sll;
1741+
struct sockaddr_ll sll;
17251742
#endif
17261743
int retval;
17271744
size_t buf_len, addr_len;
@@ -1797,7 +1814,6 @@ PHP_FUNCTION(socket_sendto)
17971814
break;
17981815
#endif
17991816
#ifdef AF_PACKET
1800-
/*
18011817
case AF_PACKET:
18021818
if (port_is_null) {
18031819
zend_argument_value_error(6, "cannot be null when the socket type is AF_PACKET");
@@ -1807,10 +1823,11 @@ PHP_FUNCTION(socket_sendto)
18071823
memset(&sll, 0, sizeof(sll));
18081824
sll.sll_family = AF_PACKET;
18091825
sll.sll_ifindex = port;
1826+
sll.sll_halen = ETH_ALEN;
18101827

1828+
// TODO allows to use more user friendly type to replace raw buffer usage
18111829
retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sin, sizeof(sin));
18121830
break;
1813-
*/
18141831
#endif
18151832
default:
18161833
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)