Skip to content

Commit 35372a1

Browse files
committed
Authenticate in redis_sock_server_open
1 parent e37f38a commit 35372a1

File tree

9 files changed

+53
-75
lines changed

9 files changed

+53
-75
lines changed

cluster_library.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,6 @@ cluster_read_sock_resp(RedisSock *redis_sock, REDIS_REPLY_TYPE type,
249249
return r;
250250
}
251251

252-
/* Helper to open connection and send AUTH if necessary */
253-
static zend_always_inline int
254-
cluster_sock_open(RedisSock *redis_sock)
255-
{
256-
zend_bool need_auth = (redis_sock->auth && redis_sock->status != REDIS_SOCK_STATUS_CONNECTED);
257-
if (!redis_sock_server_open(redis_sock) && (!need_auth || !redis_sock_auth(redis_sock ))) {
258-
return SUCCESS;
259-
}
260-
return FAILURE;
261-
}
262-
263252
/*
264253
* Helpers to send various 'control type commands to a specific node, e.g.
265254
* MULTI, ASKING, READONLY, READWRITE, etc
@@ -1127,7 +1116,7 @@ PHP_REDIS_API int cluster_map_keyspace(redisCluster *c) {
11271116
// Iterate over seeds until we can get slots
11281117
ZEND_HASH_FOREACH_PTR(c->seeds, seed) {
11291118
// Attempt to connect to this seed node
1130-
if (seed == NULL || cluster_sock_open(seed) != 0) {
1119+
if (seed == NULL || redis_sock_server_open(seed) != SUCCESS) {
11311120
continue;
11321121
}
11331122

cluster_library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
/* Protected sending of data down the wire to a RedisSock->stream */
6464
#define CLUSTER_SEND_PAYLOAD(sock, buf, len) \
65-
(sock && !cluster_sock_open(sock) && sock->stream && !redis_check_eof(sock, 1 ) && \
65+
(sock && !redis_sock_server_open(sock) && sock->stream && !redis_check_eof(sock, 1 ) && \
6666
php_stream_write(sock->stream, buf, len)==len)
6767

6868
/* Macro to read our reply type character */

common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
typedef enum {
2525
REDIS_SOCK_STATUS_FAILED = -1,
2626
REDIS_SOCK_STATUS_DISCONNECTED,
27-
REDIS_SOCK_STATUS_CONNECTED
27+
REDIS_SOCK_STATUS_CONNECTED,
28+
REDIS_SOCK_STATUS_READY
2829
} redis_sock_status;
2930

3031
#define _NL "\r\n"

library.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ redis_check_eof(RedisSock *redis_sock, int no_throw)
234234
errmsg = "AUTH failed while reconnecting";
235235
break;
236236
}
237+
redis_sock->status = REDIS_SOCK_STATUS_READY;
237238
/* If we're using a non-zero db, reselect it */
238239
if (redis_sock->dbNumber && reselect_db(redis_sock) != 0) {
239240
errmsg = "SELECT failed while reconnecting";
@@ -1890,7 +1891,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
18901891
zend_llist_remove_tail(&p->list);
18911892

18921893
if (redis_sock_check_liveness(redis_sock) == SUCCESS) {
1893-
redis_sock->status = REDIS_SOCK_STATUS_CONNECTED;
1894+
redis_sock->status = REDIS_SOCK_STATUS_READY;
18941895
return SUCCESS;
18951896
} else if (redis_sock->stream) {
18961897
php_stream_pclose(redis_sock->stream);
@@ -1975,12 +1976,23 @@ redis_sock_server_open(RedisSock *redis_sock)
19751976
{
19761977
if (redis_sock) {
19771978
switch (redis_sock->status) {
1978-
case REDIS_SOCK_STATUS_FAILED:
1979-
return FAILURE;
19801979
case REDIS_SOCK_STATUS_DISCONNECTED:
1981-
return redis_sock_connect(redis_sock);
1982-
default:
1980+
if (redis_sock_connect(redis_sock) != SUCCESS) {
1981+
break;
1982+
} else if (redis_sock->status == REDIS_SOCK_STATUS_READY) {
1983+
return SUCCESS;
1984+
}
1985+
// fall through
1986+
case REDIS_SOCK_STATUS_CONNECTED:
1987+
if (redis_sock->auth && redis_sock_auth(redis_sock) != SUCCESS) {
1988+
break;
1989+
}
1990+
redis_sock->status = REDIS_SOCK_STATUS_READY;
1991+
// fall through
1992+
case REDIS_SOCK_STATUS_READY:
19831993
return SUCCESS;
1994+
default:
1995+
return FAILURE;
19841996
}
19851997
}
19861998
return FAILURE;

redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ PHP_REDIS_API RedisSock *redis_sock_get_connected(INTERNAL_FUNCTION_PARAMETERS)
654654
if((zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
655655
&object, redis_ce) == FAILURE) ||
656656
(redis_sock = redis_sock_get(object, 1)) == NULL ||
657-
redis_sock->status != REDIS_SOCK_STATUS_CONNECTED)
657+
redis_sock->status < REDIS_SOCK_STATUS_CONNECTED)
658658
{
659659
return NULL;
660660
}

redis_array.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,6 @@ redis_array_get(zval *id)
220220
return NULL;
221221
}
222222

223-
PHP_REDIS_API int
224-
ra_call_user_function(HashTable *function_table, zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[])
225-
{
226-
if (object) {
227-
redis_object *redis = PHPREDIS_ZVAL_GET_OBJECT(redis_object, object);
228-
if (redis->sock->auth &&
229-
redis->sock->status != REDIS_SOCK_STATUS_CONNECTED &&
230-
redis_sock_server_open(redis->sock) == SUCCESS
231-
) {
232-
redis_sock_auth(redis->sock);
233-
}
234-
}
235-
return call_user_function(function_table, object, function_name, retval_ptr, param_count, params);
236-
}
237-
238223
/* {{{ proto RedisArray RedisArray::__construct()
239224
Public constructor */
240225
PHP_METHOD(RedisArray, __construct)
@@ -417,7 +402,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
417402

418403
/* multi/exec */
419404
if(ra->z_multi_exec) {
420-
ra_call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
405+
call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
421406
zval_dtor(return_value);
422407
zval_dtor(&z_fun);
423408
for (i = 0; i < argc; ++i) {
@@ -435,7 +420,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
435420
/* add MULTI + SADD */
436421
ra_index_multi(redis_inst, MULTI);
437422
/* call using discarded temp value and extract exec results after. */
438-
ra_call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
423+
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
439424
zval_dtor(return_value);
440425

441426
/* add keys to index. */
@@ -444,7 +429,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
444429
/* call EXEC */
445430
ra_index_exec(redis_inst, return_value, 0);
446431
} else { /* call directly through. */
447-
ra_call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
432+
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
448433

449434
if (!b_write_cmd) {
450435
/* check if we have an error. */
@@ -662,7 +647,7 @@ multihost_distribute_call(RedisArray *ra, zval *return_value, zval *z_fun, int a
662647
/* Iterate our RedisArray nodes */
663648
for (i = 0; i < ra->count; ++i) {
664649
/* Call each node in turn */
665-
ra_call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, &z_tmp, argc, argv);
650+
call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, &z_tmp, argc, argv);
666651

667652
/* Add the result for this host */
668653
add_assoc_zval_ex(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]), &z_tmp);
@@ -975,7 +960,7 @@ PHP_METHOD(RedisArray, mget)
975960
/* prepare call */
976961
ZVAL_STRINGL(&z_fun, "MGET", 4);
977962
/* call MGET on the node */
978-
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
963+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
979964
zval_dtor(&z_fun);
980965

981966
/* cleanup args array */
@@ -1119,7 +1104,7 @@ PHP_METHOD(RedisArray, mset)
11191104
ZVAL_STRINGL(&z_fun, "MSET", 4);
11201105

11211106
/* call */
1122-
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
1107+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
11231108
zval_dtor(&z_fun);
11241109
zval_dtor(&z_ret);
11251110

@@ -1251,7 +1236,7 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
12511236
}
12521237

12531238
/* call */
1254-
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
1239+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
12551240

12561241
if(ra->index) {
12571242
zval_dtor(&z_ret);

redis_array.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,4 @@ typedef struct RedisArray_ {
6969
zend_object *create_redis_array_object(zend_class_entry *ce);
7070
void free_redis_array_object(zend_object *object);
7171

72-
PHP_REDIS_API int ra_call_user_function(HashTable *function_table, zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[]);
73-
7472
#endif

0 commit comments

Comments
 (0)