Skip to content

Commit bd580db

Browse files
committed
Build fixes; accept names for if_index
1 parent 4414b33 commit bd580db

File tree

6 files changed

+48
-45
lines changed

6 files changed

+48
-45
lines changed

ext/sockets/conversions.c

+33-28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <netinet/in.h>
1313
#include <sys/un.h>
1414

15+
#include <sys/ioctl.h>
16+
#include <net/if.h>
17+
1518
#include <limits.h>
1619
#include <stdarg.h>
1720
#include <stddef.h>
@@ -53,6 +56,8 @@ typedef struct {
5356
#define KEY_RECVMSG_RET "recvmsg_ret"
5457
#define KEY_CMSG_LEN "cmsg_len"
5558

59+
const struct key_value empty_key_value_list[] = {{0}};
60+
5661
/* PARAMETERS */
5762
static int param_get_bool(void *ctx, const char *key, int def)
5863
{
@@ -331,25 +336,6 @@ void from_zval_write_int(const zval *arr_value, char *field, ser_context *ctx)
331336
ival = (int)lval;
332337
memcpy(field, &ival, sizeof(ival));
333338
}
334-
static void from_zval_write_unsigned(const zval *arr_value, char *field, ser_context *ctx)
335-
{
336-
long lval;
337-
unsigned ival;
338-
339-
lval = from_zval_integer_common(arr_value, ctx);
340-
if (ctx->err.has_error) {
341-
return;
342-
}
343-
344-
if (sizeof(long) > sizeof(ival) && (lval < 0 || lval > UINT_MAX)) {
345-
do_from_zval_err(ctx, "%s", "given PHP integer is out of bounds "
346-
"for a native unsigned int");
347-
return;
348-
}
349-
350-
ival = (unsigned)lval;
351-
memcpy(field, &ival, sizeof(ival));
352-
}
353339
static void from_zval_write_uint32(const zval *arr_value, char *field, ser_context *ctx)
354340
{
355341
long lval;
@@ -1192,32 +1178,50 @@ void to_zval_read_msghdr(const char *msghdr_c, zval *zv, res_context *ctx)
11921178
/* CONVERSIONS for if_index */
11931179
static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context *ctx)
11941180
{
1195-
zval *va; unsigned *out;
1196-
unsigned ret;
1197-
zval lzval = zval_used_for_init;
1181+
unsigned ret;
1182+
zval lzval = zval_used_for_init;
11981183

11991184
if (Z_TYPE_P(zv) == IS_LONG) {
1200-
if (Z_LVAL_P(zv) < 0 || Z_LVAL_P(zv) > UINT_MAX) {
1185+
if (Z_LVAL_P(zv) < 0 || Z_LVAL_P(zv) > UINT_MAX) { /* allow 0 (unspecified interface) */
12011186
do_from_zval_err(ctx, "the interface index cannot be negative or "
12021187
"larger than %u; given %ld", UINT_MAX, Z_LVAL_P(zv));
12031188
} else {
12041189
ret = (unsigned)Z_LVAL_P(zv);
12051190
}
12061191
} else {
1207-
#if HAVE_IF_NAMETOINDEX
1208-
12091192
if (Z_TYPE_P(zv) != IS_STRING) {
12101193
ZVAL_COPY_VALUE(&lzval, zv);
12111194
zval_copy_ctor(&lzval);
12121195
convert_to_string(&lzval);
12131196
zv = &lzval;
12141197
}
12151198

1199+
#if HAVE_IF_NAMETOINDEX
12161200
ret = if_nametoindex(Z_STRVAL_P(zv));
12171201
if (ret == 0) {
12181202
do_from_zval_err(ctx, "no interface with name \"%s\" could be "
12191203
"found", Z_STRVAL_P(zv));
12201204
}
1205+
#elif defined(SIOCGIFINDEX)
1206+
{
1207+
struct ifreq ifr;
1208+
if (strlcpy(ifr.ifr_name, Z_STRVAL_P(zv), sizeof(ifr.ifr_name))
1209+
>= sizeof(ifr.ifr_name)) {
1210+
do_from_zval_err(ctx, "the interface name \"%s\" is too large ",
1211+
Z_STRVAL_P(zv));
1212+
} else if (ioctl(ctx->sock->bsd_socket, SIOCGIFINDEX, &ifr) < 0) {
1213+
if (errno == ENODEV) {
1214+
do_from_zval_err(ctx, "no interface with name \"%s\" could be "
1215+
"found", Z_STRVAL_P(zv));
1216+
} else {
1217+
do_from_zval_err(ctx, "error fetching interface index for "
1218+
"interface with name \"%s\" (errno %d)",
1219+
Z_STRVAL_P(zv), errno);
1220+
}
1221+
} else {
1222+
ret = (unsigned)ifr.ifr_ifindex;
1223+
}
1224+
}
12211225
#else
12221226
do_from_zval_err(ctx,
12231227
"this platform does not support looking up an interface by "
@@ -1236,7 +1240,7 @@ static void from_zval_write_ifindex(const zval *zv, char *uinteger, ser_context
12361240
#ifdef IPV6_PKTINFO
12371241
static const field_descriptor descriptors_in6_pktinfo[] = {
12381242
{"addr", sizeof("addr"), 1, offsetof(struct in6_pktinfo, ipi6_addr), from_zval_write_sin6_addr, to_zval_read_sin6_addr},
1239-
{"ifindex", sizeof("ifindex"), 1, offsetof(struct in6_pktinfo, ipi6_ifindex), from_zval_write_unsigned, to_zval_read_unsigned},
1243+
{"ifindex", sizeof("ifindex"), 1, offsetof(struct in6_pktinfo, ipi6_ifindex), from_zval_write_ifindex, to_zval_read_unsigned},
12401244
{0}
12411245
};
12421246
void from_zval_write_in6_pktinfo(const zval *container, char *in6_pktinfo_c, ser_context *ctx)
@@ -1294,6 +1298,7 @@ size_t calculate_scm_rights_space(const zval *arr, ser_context *ctx)
12941298
static void from_zval_write_fd_array_aux(zval **elem, unsigned i, void **args, ser_context *ctx)
12951299
{
12961300
int *iarr = args[0];
1301+
TSRMLS_FETCH();
12971302

12981303
if (Z_TYPE_PP(elem) == IS_RESOURCE) {
12991304
php_stream *stream;
@@ -1375,8 +1380,8 @@ void to_zval_read_fd_array(const char *data, zval *zv, res_context *ctx)
13751380
return;
13761381
}
13771382
if (S_ISSOCK(statbuf.st_mode)) {
1378-
php_socket *sock = socket_import_file_descriptor(fd);
1379-
zend_register_resource(elem, sock, php_sockets_le_socket());
1383+
php_socket *sock = socket_import_file_descriptor(fd TSRMLS_CC);
1384+
zend_register_resource(elem, sock, php_sockets_le_socket() TSRMLS_CC);
13801385
} else {
13811386
php_stream *stream = php_stream_fopen_from_fd(fd, "rw", NULL);
13821387
php_stream_to_zval(stream, elem);

ext/sockets/multicast.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static int php_do_mcast_opt(php_socket *php_sock, int level, int optname, zval *
250250
int php_do_setsockopt_ip_mcast(php_socket *php_sock,
251251
int level,
252252
int optname,
253-
zval **arg4)
253+
zval **arg4 TSRMLS_DC)
254254
{
255255
unsigned int if_index;
256256
struct in_addr if_addr;
@@ -319,7 +319,7 @@ int php_do_setsockopt_ip_mcast(php_socket *php_sock,
319319
int php_do_setsockopt_ipv6_mcast(php_socket *php_sock,
320320
int level,
321321
int optname,
322-
zval **arg4)
322+
zval **arg4 TSRMLS_DC)
323323
{
324324
unsigned int if_index;
325325
void *opt_ptr;

ext/sockets/multicast.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
int php_do_setsockopt_ip_mcast(php_socket *php_sock,
3131
int level,
3232
int optname,
33-
zval **arg4);
33+
zval **arg4 TSRMLS_DC);
3434

3535
int php_do_setsockopt_ipv6_mcast(php_socket *php_sock,
3636
int level,
3737
int optname,
38-
zval **arg4);
38+
zval **arg4 TSRMLS_DC);
3939

4040
int php_if_index_to_addr4(
4141
unsigned if_index,

ext/sockets/sendrecvmsg.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ PHP_FUNCTION(socket_cmsg_space)
272272
RETURN_LONG((long)CMSG_SPACE(entry->size + n * entry->var_el_size));
273273
}
274274

275-
int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval **arg4)
275+
int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval **arg4 TSRMLS_DC)
276276
{
277277
struct err_s err = {0};
278278
zend_llist *allocations = NULL;
@@ -311,7 +311,7 @@ int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname,
311311
return retval != 0 ? FAILURE : SUCCESS;
312312
}
313313

314-
int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result)
314+
int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result TSRMLS_DC)
315315
{
316316
struct err_s err = {0};
317317
void *buffer;
@@ -340,7 +340,7 @@ int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname,
340340
zval *zv = to_zval_run_conversions(buffer, reader, "in6_pktinfo",
341341
empty_key_value_list, &err);
342342
if (err.has_error) {
343-
err_msg_dispose(&err);
343+
err_msg_dispose(&err TSRMLS_CC);
344344
res = -1;
345345
} else {
346346
ZVAL_COPY_VALUE(result, zv);
@@ -354,9 +354,7 @@ int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname,
354354

355355
void php_socket_sendrecvmsg_init(INIT_FUNC_ARGS)
356356
{
357-
/* IPv6 ancillary data
358-
* Note that support for sticky options via setsockopt() is not implemented
359-
* yet (where special support is needed, i.e., the optval is not an int). */
357+
/* IPv6 ancillary data */
360358
#ifdef IPV6_RECVPKTINFO
361359
REGISTER_LONG_CONSTANT("IPV6_RECVPKTINFO", IPV6_RECVPKTINFO, CONST_CS | CONST_PERSISTENT);
362360
REGISTER_LONG_CONSTANT("IPV6_PKTINFO", IPV6_PKTINFO, CONST_CS | CONST_PERSISTENT);

ext/sockets/sendrecvmsg.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ PHP_FUNCTION(socket_cmsg_space);
1212
void php_socket_sendrecvmsg_init(INIT_FUNC_ARGS);
1313
void php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS);
1414

15-
int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval **arg4);
16-
int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result);
15+
int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval **arg4 TSRMLS_DC);
16+
int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result TSRMLS_DC);
1717

1818
/* for conversions.c */
1919
typedef struct {

ext/sockets/sockets.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ PHP_FUNCTION(socket_get_option)
18781878
}
18791879
}
18801880
} else if (level == IPPROTO_IPV6) {
1881-
int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
1881+
int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value TSRMLS_CC);
18821882
if (ret == SUCCESS) {
18831883
return;
18841884
} else if (ret == FAILURE) {
@@ -1981,15 +1981,15 @@ PHP_FUNCTION(socket_set_option)
19811981

19821982

19831983
if (level == IPPROTO_IP) {
1984-
int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
1984+
int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4 TSRMLS_CC);
19851985
HANDLE_SUBCALL(res);
19861986
}
19871987

19881988
#if HAVE_IPV6
19891989
else if (level == IPPROTO_IPV6) {
1990-
int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
1990+
int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4 TSRMLS_CC);
19911991
if (res == 1) {
1992-
res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
1992+
res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4 TSRMLS_CC);
19931993
}
19941994
HANDLE_SUBCALL(res);
19951995
}
@@ -2273,7 +2273,7 @@ PHP_FUNCTION(socket_import_stream)
22732273
RETURN_FALSE;
22742274
}
22752275

2276-
retsock = socket_import_file_descriptor(socket);
2276+
retsock = socket_import_file_descriptor(socket TSRMLS_CC);
22772277
if (retsock == NULL) {
22782278
RETURN_FALSE;
22792279
}

0 commit comments

Comments
 (0)