Skip to content

Commit

Permalink
reminder for phase 2
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Jan 11, 2025
1 parent d1090a5 commit 1c7ea4d
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ PHP_FUNCTION(socket_bind)
}
#endif
default:
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
zend_argument_value_error(1, "must be one of AF_UNIX, AF_PACKET, AF_INET, or AF_INET6");
RETURN_THROWS();
}

Expand Down Expand Up @@ -1504,6 +1504,9 @@ PHP_FUNCTION(socket_recvfrom)
struct sockaddr_in sin;
#ifdef HAVE_IPV6
struct sockaddr_in6 sin6;
#endif
#ifdef AF_PACKET
struct sockaddr_ll sll;
#endif
char addrbuf[INET6_ADDRSTRLEN];
socklen_t slen;
Expand Down Expand Up @@ -1608,9 +1611,39 @@ PHP_FUNCTION(socket_recvfrom)
ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
break;
#endif
#ifdef AF_PACKET
case AF_PACKET:
// TODO expose and use proper ethernet frame type instead i.e. src mac, dst mac and payload to userland
// ditto for socket_sendto
slen = sizeof(sll);
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
char ifrname[IFNAMSIZ];

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

if (retval < 0) {
PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
zend_string_efree(recv_buf);
RETURN_FALSE;
}
ZSTR_LEN(recv_buf) = retval;
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';

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

ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
ZEND_TRY_ASSIGN_REF_STRING(arg5, ifrname);
ZEND_TRY_ASSIGN_REF_LONG(arg6, sll.sll_ifindex);
break;
#endif
default:
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
zend_argument_value_error(1, "must be one of AF_UNIX, AF_PACKET, AF_INET, or AF_INET6");
RETURN_THROWS();
}

Expand Down

0 comments on commit 1c7ea4d

Please sign in to comment.