Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 19 | #include "channel.h" |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 25 | #include <php.h> |
| 26 | #include <php_ini.h> |
| 27 | #include <ext/standard/info.h> |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 28 | #include <ext/standard/php_var.h> |
| 29 | #include <ext/standard/sha1.h> |
| 30 | #if PHP_MAJOR_VERSION < 7 |
| 31 | #include <ext/standard/php_smart_str.h> |
| 32 | #else |
| 33 | #include <zend_smart_str.h> |
| 34 | #endif |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 35 | #include <ext/spl/spl_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 36 | #include "php_grpc.h" |
| 37 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 38 | #include <zend_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 39 | |
| 40 | #include <stdbool.h> |
| 41 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 42 | #include <grpc/grpc.h> |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 43 | #include <grpc/grpc_security.h> |
Zhouyihai Ding | d3b5524 | 2018-01-22 12:52:56 -0800 | [diff] [blame] | 44 | #include <grpc/support/alloc.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 45 | |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 46 | #include "completion_queue.h" |
Stanley Cheung | 9c0b35e | 2015-10-21 17:07:56 -0700 | [diff] [blame] | 47 | #include "channel_credentials.h" |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 48 | #include "server.h" |
| 49 | #include "timeval.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 50 | |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 51 | zend_class_entry *grpc_ce_channel; |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 52 | #if PHP_MAJOR_VERSION >= 7 |
| 53 | static zend_object_handlers channel_ce_handlers; |
| 54 | #endif |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 55 | static gpr_mu global_persistent_list_mu; |
| 56 | int le_plink; |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 57 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 58 | /* Frees and destroys an instance of wrapped_grpc_channel */ |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 59 | PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel) |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 60 | if (p->wrapper != NULL) { |
| 61 | gpr_mu_lock(&p->wrapper->mu); |
| 62 | if (p->wrapper->wrapped != NULL) { |
| 63 | php_grpc_zend_resource *rsrc; |
| 64 | php_grpc_int key_len = strlen(p->wrapper->key); |
| 65 | // only destroy the channel here if not found in the persistent list |
| 66 | gpr_mu_lock(&global_persistent_list_mu); |
| 67 | if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), p->wrapper->key, |
| 68 | key_len, rsrc))) { |
| 69 | grpc_channel_destroy(p->wrapper->wrapped); |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 70 | free(p->wrapper->target); |
| 71 | free(p->wrapper->args_hashstr); |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 72 | if (p->wrapper->creds_hashstr != NULL) { |
| 73 | free(p->wrapper->creds_hashstr); |
| 74 | p->wrapper->creds_hashstr = NULL; |
| 75 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 76 | } |
| 77 | gpr_mu_unlock(&global_persistent_list_mu); |
| 78 | } |
| 79 | gpr_mu_unlock(&p->wrapper->mu); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 80 | } |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 81 | PHP_GRPC_FREE_WRAPPED_FUNC_END() |
| 82 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 83 | /* Initializes an instance of wrapped_grpc_channel to be associated with an |
| 84 | * object of a class specified by class_type */ |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 85 | php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type |
| 86 | TSRMLS_DC) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 87 | PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 89 | object_properties_init(&intern->std, class_type); |
thinkerou | dc673c5 | 2016-07-28 09:49:38 +0800 | [diff] [blame] | 90 | PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers); |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 91 | } |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 92 | |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 93 | int php_grpc_read_args_array(zval *args_array, |
| 94 | grpc_channel_args *args TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 95 | HashTable *array_hash; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 96 | int args_index; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 97 | array_hash = Z_ARRVAL_P(args_array); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 98 | if (!array_hash) { |
| 99 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Stanley Cheung | c1f25fb | 2016-07-29 13:41:22 -0700 | [diff] [blame] | 100 | "array_hash is NULL", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 101 | return FAILURE; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 102 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 103 | args->num_args = zend_hash_num_elements(array_hash); |
| 104 | args->args = ecalloc(args->num_args, sizeof(grpc_arg)); |
| 105 | args_index = 0; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 106 | |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 107 | char *key = NULL; |
| 108 | zval *data; |
| 109 | int key_type; |
| 110 | |
| 111 | PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data) |
| 112 | if (key_type != HASH_KEY_IS_STRING) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 113 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 114 | "args keys must be strings", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 115 | return FAILURE; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 116 | } |
| 117 | args->args[args_index].key = key; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 118 | switch (Z_TYPE_P(data)) { |
| 119 | case IS_LONG: |
| 120 | args->args[args_index].value.integer = (int)Z_LVAL_P(data); |
| 121 | args->args[args_index].type = GRPC_ARG_INTEGER; |
| 122 | break; |
| 123 | case IS_STRING: |
| 124 | args->args[args_index].value.string = Z_STRVAL_P(data); |
| 125 | args->args[args_index].type = GRPC_ARG_STRING; |
| 126 | break; |
| 127 | default: |
| 128 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 129 | "args values must be int or string", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 130 | return FAILURE; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 131 | } |
| 132 | args_index++; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 133 | PHP_GRPC_HASH_FOREACH_END() |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 134 | return SUCCESS; |
| 135 | } |
| 136 | |
| 137 | void generate_sha1_str(char *sha1str, char *str, php_grpc_int len) { |
| 138 | PHP_SHA1_CTX context; |
| 139 | unsigned char digest[20]; |
| 140 | sha1str[0] = '\0'; |
| 141 | PHP_SHA1Init(&context); |
| 142 | PHP_GRPC_SHA1Update(&context, str, len); |
| 143 | PHP_SHA1Final(digest, &context); |
| 144 | make_sha1_digest(sha1str, digest); |
| 145 | } |
| 146 | |
| 147 | void create_channel( |
| 148 | wrapped_grpc_channel *channel, |
| 149 | char *target, |
| 150 | grpc_channel_args args, |
| 151 | wrapped_grpc_channel_credentials *creds) { |
| 152 | if (creds == NULL) { |
| 153 | channel->wrapper->wrapped = grpc_insecure_channel_create(target, &args, |
| 154 | NULL); |
| 155 | } else { |
| 156 | channel->wrapper->wrapped = |
| 157 | grpc_secure_channel_create(creds->wrapped, target, &args, NULL); |
| 158 | } |
| 159 | efree(args.args); |
| 160 | } |
| 161 | |
| 162 | void create_and_add_channel_to_persistent_list( |
| 163 | wrapped_grpc_channel *channel, |
| 164 | char *target, |
| 165 | grpc_channel_args args, |
| 166 | wrapped_grpc_channel_credentials *creds, |
| 167 | char *key, |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 168 | php_grpc_int key_len TSRMLS_DC) { |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 169 | php_grpc_zend_resource new_rsrc; |
| 170 | channel_persistent_le_t *le; |
| 171 | // this links each persistent list entry to a destructor |
| 172 | new_rsrc.type = le_plink; |
| 173 | le = malloc(sizeof(channel_persistent_le_t)); |
| 174 | |
| 175 | create_channel(channel, target, args, creds); |
| 176 | |
| 177 | le->channel = channel->wrapper; |
| 178 | new_rsrc.ptr = le; |
| 179 | gpr_mu_lock(&global_persistent_list_mu); |
| 180 | PHP_GRPC_PERSISTENT_LIST_UPDATE(&EG(persistent_list), key, key_len, |
| 181 | (void *)&new_rsrc); |
| 182 | gpr_mu_unlock(&global_persistent_list_mu); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 183 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 184 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 185 | /** |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 186 | * Construct an instance of the Channel class. |
| 187 | * |
| 188 | * By default, the underlying grpc_channel is "persistent". That is, given |
| 189 | * the same set of parameters passed to the constructor, the same underlying |
| 190 | * grpc_channel will be returned. |
| 191 | * |
| 192 | * If the $args array contains a "credentials" key mapping to a |
| 193 | * ChannelCredentials object, a secure channel will be created with those |
| 194 | * credentials. |
| 195 | * |
| 196 | * If the $args array contains a "force_new" key mapping to a boolean value |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 197 | * of "true", a new and separate underlying grpc_channel will be created |
| 198 | * and returned. This will not affect existing channels. |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 199 | * |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 200 | * @param string $target The hostname to associate with this channel |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 201 | * @param array $args_array The arguments to pass to the Channel |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 202 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 203 | PHP_METHOD(Channel, __construct) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 204 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
| 205 | zval *creds_obj = NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 206 | char *target; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 207 | php_grpc_int target_length; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 208 | zval *args_array = NULL; |
| 209 | grpc_channel_args args; |
| 210 | HashTable *array_hash; |
Stanley Cheung | aeea102 | 2015-10-21 17:00:49 -0700 | [diff] [blame] | 211 | wrapped_grpc_channel_credentials *creds = NULL; |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 212 | php_grpc_zend_resource *rsrc; |
| 213 | bool force_new = false; |
| 214 | zval *force_new_obj = NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 215 | |
Stanley Cheung | f77a4ad | 2016-02-16 09:45:51 -0800 | [diff] [blame] | 216 | /* "sa" == 1 string, 1 array */ |
| 217 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 218 | &target_length, &args_array) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 219 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 220 | "Channel expects a string and an array", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 223 | array_hash = Z_ARRVAL_P(args_array); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 224 | if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"), |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 225 | (void **)&creds_obj) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 226 | if (Z_TYPE_P(creds_obj) == IS_NULL) { |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 227 | creds = NULL; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 228 | php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials")); |
| 229 | } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) != |
| 230 | grpc_ce_channel_credentials) { |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 231 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 232 | "credentials must be a ChannelCredentials object", |
| 233 | 1 TSRMLS_CC); |
| 234 | return; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 235 | } else { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 236 | creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 237 | php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials")); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 238 | } |
| 239 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 240 | if (php_grpc_zend_hash_find(array_hash, "force_new", sizeof("force_new"), |
| 241 | (void **)&force_new_obj) == SUCCESS) { |
| 242 | if (PHP_GRPC_BVAL_IS_TRUE(force_new_obj)) { |
| 243 | force_new = true; |
| 244 | } |
| 245 | php_grpc_zend_hash_del(array_hash, "force_new", sizeof("force_new")); |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 246 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 247 | |
| 248 | // parse the rest of the channel args array |
| 249 | if (php_grpc_read_args_array(args_array, &args TSRMLS_CC) == FAILURE) { |
Zhouyihai Ding | b6cdfca | 2018-01-13 16:25:28 -0800 | [diff] [blame] | 250 | efree(args.args); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
| 254 | // Construct a hashkey for the persistent channel |
| 255 | // Currently, the hashkey contains 3 parts: |
| 256 | // 1. hostname |
| 257 | // 2. hash value of the channel args array (excluding "credentials" |
| 258 | // and "force_new") |
| 259 | // 3. (optional) hash value of the ChannelCredentials object |
| 260 | php_serialize_data_t var_hash; |
| 261 | smart_str buf = {0}; |
| 262 | PHP_VAR_SERIALIZE_INIT(var_hash); |
| 263 | PHP_GRPC_VAR_SERIALIZE(&buf, args_array, &var_hash); |
| 264 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
| 265 | |
| 266 | char sha1str[41]; |
| 267 | generate_sha1_str(sha1str, PHP_GRPC_SERIALIZED_BUF_STR(buf), |
| 268 | PHP_GRPC_SERIALIZED_BUF_LEN(buf)); |
| 269 | |
| 270 | php_grpc_int key_len = target_length + strlen(sha1str); |
| 271 | if (creds != NULL && creds->hashstr != NULL) { |
| 272 | key_len += strlen(creds->hashstr); |
| 273 | } |
| 274 | char *key = malloc(key_len + 1); |
| 275 | strcpy(key, target); |
| 276 | strcat(key, sha1str); |
| 277 | if (creds != NULL && creds->hashstr != NULL) { |
| 278 | strcat(key, creds->hashstr); |
| 279 | } |
| 280 | channel->wrapper = malloc(sizeof(grpc_channel_wrapper)); |
| 281 | channel->wrapper->key = key; |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 282 | channel->wrapper->target = strdup(target); |
| 283 | channel->wrapper->args_hashstr = strdup(sha1str); |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 284 | channel->wrapper->creds_hashstr = NULL; |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 285 | if (creds != NULL && creds->hashstr != NULL) { |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 286 | php_grpc_int creds_hashstr_len = strlen(creds->hashstr); |
| 287 | char *channel_creds_hashstr = malloc(creds_hashstr_len + 1); |
| 288 | strcpy(channel_creds_hashstr, creds->hashstr); |
| 289 | channel->wrapper->creds_hashstr = channel_creds_hashstr; |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 290 | } |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 291 | |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 292 | gpr_mu_init(&channel->wrapper->mu); |
| 293 | smart_str_free(&buf); |
| 294 | |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 295 | if (force_new || (creds != NULL && creds->has_call_creds)) { |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 296 | // If the ChannelCredentials object was composed with a CallCredentials |
| 297 | // object, there is no way we can tell them apart. Do NOT persist |
| 298 | // them. They should be individually destroyed. |
| 299 | create_channel(channel, target, args, creds); |
| 300 | } else if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key, |
| 301 | key_len, rsrc))) { |
| 302 | create_and_add_channel_to_persistent_list( |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 303 | channel, target, args, creds, key, key_len TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 304 | } else { |
| 305 | // Found a previously stored channel in the persistent list |
| 306 | channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr; |
| 307 | if (strcmp(target, le->channel->target) != 0 || |
| 308 | strcmp(sha1str, le->channel->args_hashstr) != 0 || |
| 309 | (creds != NULL && creds->hashstr != NULL && |
| 310 | strcmp(creds->hashstr, le->channel->creds_hashstr) != 0)) { |
| 311 | // somehow hash collision |
| 312 | create_and_add_channel_to_persistent_list( |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 313 | channel, target, args, creds, key, key_len TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 314 | } else { |
Zhouyihai Ding | b6cdfca | 2018-01-13 16:25:28 -0800 | [diff] [blame] | 315 | efree(args.args); |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 316 | if (channel->wrapper->creds_hashstr != NULL){ |
| 317 | free(channel->wrapper->creds_hashstr); |
| 318 | channel->wrapper->creds_hashstr = NULL; |
| 319 | } |
| 320 | free(channel->wrapper->creds_hashstr); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 321 | channel->wrapper = le->channel; |
| 322 | } |
| 323 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 327 | * Get the endpoint this call/stream is connected to |
| 328 | * @return string The URI of the endpoint |
| 329 | */ |
| 330 | PHP_METHOD(Channel, getTarget) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 331 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 332 | gpr_mu_lock(&channel->wrapper->mu); |
| 333 | if (channel->wrapper->wrapped == NULL) { |
| 334 | zend_throw_exception(spl_ce_RuntimeException, |
| 335 | "Channel already closed", 1 TSRMLS_CC); |
| 336 | gpr_mu_unlock(&channel->wrapper->mu); |
| 337 | return; |
| 338 | } |
| 339 | char *target = grpc_channel_get_target(channel->wrapper->wrapped); |
| 340 | gpr_mu_unlock(&channel->wrapper->mu); |
Zhouyihai Ding | d3b5524 | 2018-01-22 12:52:56 -0800 | [diff] [blame] | 341 | PHP_GRPC_RETVAL_STRING(target, 1); |
| 342 | gpr_free(target); |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 346 | * Get the connectivity state of the channel |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 347 | * @param bool $try_to_connect Try to connect on the channel (optional) |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 348 | * @return long The grpc connectivity state |
| 349 | */ |
| 350 | PHP_METHOD(Channel, getConnectivityState) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 351 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 352 | gpr_mu_lock(&channel->wrapper->mu); |
| 353 | if (channel->wrapper->wrapped == NULL) { |
| 354 | zend_throw_exception(spl_ce_RuntimeException, |
| 355 | "Channel already closed", 1 TSRMLS_CC); |
| 356 | gpr_mu_unlock(&channel->wrapper->mu); |
| 357 | return; |
| 358 | } |
| 359 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 360 | bool try_to_connect = false; |
| 361 | |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 362 | /* "|b" == 1 optional bool */ |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 363 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) |
| 364 | == FAILURE) { |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 365 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 366 | "getConnectivityState expects a bool", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 367 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 370 | int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped, |
| 371 | (int)try_to_connect); |
| 372 | // this can happen if another shared Channel object close the underlying |
| 373 | // channel |
| 374 | if (state == GRPC_CHANNEL_SHUTDOWN) { |
| 375 | channel->wrapper->wrapped = NULL; |
| 376 | } |
| 377 | gpr_mu_unlock(&channel->wrapper->mu); |
| 378 | RETURN_LONG(state); |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 382 | * Watch the connectivity state of the channel until it changed |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 383 | * @param long $last_state The previous connectivity state of the channel |
| 384 | * @param Timeval $deadline_obj The deadline this function should wait until |
Stanley Cheung | 4c5c7b8 | 2015-08-12 16:28:58 -0700 | [diff] [blame] | 385 | * @return bool If the connectivity state changes from last_state |
| 386 | * before deadline |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 387 | */ |
| 388 | PHP_METHOD(Channel, watchConnectivityState) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 389 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 390 | gpr_mu_lock(&channel->wrapper->mu); |
| 391 | if (channel->wrapper->wrapped == NULL) { |
| 392 | zend_throw_exception(spl_ce_RuntimeException, |
| 393 | "Channel already closed", 1 TSRMLS_CC); |
| 394 | gpr_mu_unlock(&channel->wrapper->mu); |
| 395 | return; |
| 396 | } |
| 397 | |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 398 | php_grpc_long last_state; |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 399 | zval *deadline_obj; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 400 | |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 401 | /* "lO" == 1 long 1 object */ |
| 402 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 403 | &last_state, &deadline_obj, |
| 404 | grpc_ce_timeval) == FAILURE) { |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 405 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 406 | "watchConnectivityState expects 1 long 1 timeval", |
| 407 | 1 TSRMLS_CC); |
| 408 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 412 | wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 413 | grpc_channel_watch_connectivity_state(channel->wrapper->wrapped, |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 414 | (grpc_connectivity_state)last_state, |
| 415 | deadline->wrapped, completion_queue, |
| 416 | NULL); |
| 417 | grpc_event event = |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 418 | grpc_completion_queue_pluck(completion_queue, NULL, |
| 419 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 420 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 421 | RETURN_BOOL(event.success); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 425 | * Close the channel |
thinkerou | 03dc219 | 2016-08-16 19:31:44 +0800 | [diff] [blame] | 426 | * @return void |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 427 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 428 | PHP_METHOD(Channel, close) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 429 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 430 | gpr_mu_lock(&channel->wrapper->mu); |
| 431 | if (channel->wrapper->wrapped != NULL) { |
| 432 | grpc_channel_destroy(channel->wrapper->wrapped); |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 433 | free(channel->wrapper->target); |
| 434 | free(channel->wrapper->args_hashstr); |
Zhouyihai Ding | 4b9f8d8 | 2018-01-22 12:58:21 -0800 | [diff] [blame] | 435 | if (channel->wrapper->creds_hashstr != NULL) { |
| 436 | free(channel->wrapper->creds_hashstr); |
| 437 | channel->wrapper->creds_hashstr = NULL; |
| 438 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 439 | channel->wrapper->wrapped = NULL; |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 440 | |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 441 | php_grpc_delete_persistent_list_entry(channel->wrapper->key, |
| 442 | strlen(channel->wrapper->key) |
| 443 | TSRMLS_CC); |
| 444 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 445 | gpr_mu_unlock(&channel->wrapper->mu); |
| 446 | } |
| 447 | |
| 448 | // Delete an entry from the persistent list |
| 449 | // Note: this does not destroy or close the underlying grpc_channel |
| 450 | void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len |
| 451 | TSRMLS_DC) { |
| 452 | php_grpc_zend_resource *rsrc; |
| 453 | gpr_mu_lock(&global_persistent_list_mu); |
| 454 | if (PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key, |
| 455 | key_len, rsrc)) { |
| 456 | channel_persistent_le_t *le; |
| 457 | le = (channel_persistent_le_t *)rsrc->ptr; |
| 458 | le->channel = NULL; |
| 459 | php_grpc_zend_hash_del(&EG(persistent_list), key, key_len+1); |
Zhouyihai Ding | 33af9aa | 2018-01-22 12:54:42 -0800 | [diff] [blame] | 460 | free(le); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 461 | } |
| 462 | gpr_mu_unlock(&global_persistent_list_mu); |
| 463 | } |
| 464 | |
| 465 | // A destructor associated with each list entry from the persistent list |
| 466 | static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc |
| 467 | TSRMLS_DC) { |
| 468 | channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr; |
| 469 | if (le->channel != NULL) { |
| 470 | gpr_mu_lock(&le->channel->mu); |
| 471 | if (le->channel->wrapped != NULL) { |
| 472 | grpc_channel_destroy(le->channel->wrapped); |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 473 | free(le->channel->target); |
| 474 | free(le->channel->args_hashstr); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 475 | } |
| 476 | gpr_mu_unlock(&le->channel->mu); |
| 477 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Stanley Cheung | 6a5c83d | 2017-02-16 12:25:32 -0800 | [diff] [blame] | 480 | ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2) |
| 481 | ZEND_ARG_INFO(0, target) |
| 482 | ZEND_ARG_INFO(0, args) |
| 483 | ZEND_END_ARG_INFO() |
| 484 | |
| 485 | ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0) |
| 486 | ZEND_END_ARG_INFO() |
| 487 | |
| 488 | ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0) |
| 489 | ZEND_ARG_INFO(0, try_to_connect) |
| 490 | ZEND_END_ARG_INFO() |
| 491 | |
| 492 | ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2) |
| 493 | ZEND_ARG_INFO(0, last_state) |
| 494 | ZEND_ARG_INFO(0, deadline) |
| 495 | ZEND_END_ARG_INFO() |
| 496 | |
| 497 | ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0) |
| 498 | ZEND_END_ARG_INFO() |
| 499 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 500 | static zend_function_entry channel_methods[] = { |
Stanley Cheung | 6a5c83d | 2017-02-16 12:25:32 -0800 | [diff] [blame] | 501 | PHP_ME(Channel, __construct, arginfo_construct, |
| 502 | ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 503 | PHP_ME(Channel, getTarget, arginfo_getTarget, |
| 504 | ZEND_ACC_PUBLIC) |
| 505 | PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState, |
| 506 | ZEND_ACC_PUBLIC) |
| 507 | PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState, |
| 508 | ZEND_ACC_PUBLIC) |
| 509 | PHP_ME(Channel, close, arginfo_close, |
| 510 | ZEND_ACC_PUBLIC) |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 511 | PHP_FE_END |
| 512 | }; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 513 | |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 514 | GRPC_STARTUP_FUNCTION(channel) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 515 | zend_class_entry ce; |
| 516 | INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods); |
| 517 | ce.create_object = create_wrapped_grpc_channel; |
| 518 | grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC); |
Stanley Cheung | 5d55948 | 2017-08-22 13:15:01 -0700 | [diff] [blame] | 519 | gpr_mu_init(&global_persistent_list_mu); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 520 | le_plink = zend_register_list_destructors_ex( |
| 521 | NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number); |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 522 | PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame] | 523 | return SUCCESS; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 524 | } |