Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | #include "channel.h" |
| 35 | |
| 36 | #ifdef HAVE_CONFIG_H |
| 37 | #include "config.h" |
| 38 | #endif |
| 39 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 40 | #include <php.h> |
| 41 | #include <php_ini.h> |
| 42 | #include <ext/standard/info.h> |
| 43 | #include <ext/spl/spl_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 44 | #include "php_grpc.h" |
| 45 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 46 | #include <zend_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 47 | |
| 48 | #include <stdbool.h> |
| 49 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 50 | #include <grpc/grpc.h> |
| 51 | #include <grpc/support/log.h> |
| 52 | #include <grpc/grpc_security.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 53 | |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 54 | #include "completion_queue.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 55 | #include "credentials.h" |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 56 | #include "server.h" |
| 57 | #include "timeval.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 58 | |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 59 | zend_class_entry *grpc_ce_channel; |
| 60 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 61 | /* Frees and destroys an instance of wrapped_grpc_channel */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 62 | void free_wrapped_grpc_channel(void *object TSRMLS_DC) { |
| 63 | wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object; |
| 64 | if (channel->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 65 | grpc_channel_destroy(channel->wrapped); |
| 66 | } |
murgatroid99 | aa11066 | 2015-04-08 16:53:47 -0700 | [diff] [blame] | 67 | efree(channel->target); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 68 | efree(channel); |
| 69 | } |
| 70 | |
| 71 | /* Initializes an instance of wrapped_grpc_channel to be associated with an |
| 72 | * object of a class specified by class_type */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 73 | zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type |
| 74 | TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 75 | zend_object_value retval; |
| 76 | wrapped_grpc_channel *intern; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 77 | intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 78 | memset(intern, 0, sizeof(wrapped_grpc_channel)); |
| 79 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 80 | object_properties_init(&intern->std, class_type); |
| 81 | retval.handle = zend_objects_store_put( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 82 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 83 | free_wrapped_grpc_channel, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 84 | retval.handlers = zend_get_std_object_handlers(); |
| 85 | return retval; |
| 86 | } |
| 87 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 88 | void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 89 | HashTable *array_hash; |
| 90 | HashPosition array_pointer; |
| 91 | int args_index; |
| 92 | zval **data; |
| 93 | char *key; |
| 94 | uint key_len; |
| 95 | ulong index; |
| 96 | array_hash = Z_ARRVAL_P(args_array); |
| 97 | args->num_args = zend_hash_num_elements(array_hash); |
| 98 | args->args = ecalloc(args->num_args, sizeof(grpc_arg)); |
| 99 | args_index = 0; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 100 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 101 | zend_hash_get_current_data_ex(array_hash, (void **)&data, |
| 102 | &array_pointer) == SUCCESS; |
| 103 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 104 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 105 | &array_pointer) != HASH_KEY_IS_STRING) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 106 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 107 | "args keys must be strings", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | args->args[args_index].key = key; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 111 | switch (Z_TYPE_P(*data)) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 112 | case IS_LONG: |
| 113 | args->args[args_index].value.integer = (int)Z_LVAL_P(*data); |
| 114 | break; |
| 115 | case IS_STRING: |
| 116 | args->args[args_index].value.string = Z_STRVAL_P(*data); |
| 117 | break; |
| 118 | default: |
| 119 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 120 | "args values must be int or string", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | args_index++; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Construct an instance of the Channel class. If the $args array contains a |
| 129 | * "credentials" key mapping to a Credentials object, a secure channel will be |
| 130 | * created with those credentials. |
| 131 | * @param string $target The hostname to associate with this channel |
| 132 | * @param array $args The arguments to pass to the Channel (optional) |
| 133 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 134 | PHP_METHOD(Channel, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 135 | wrapped_grpc_channel *channel = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 136 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 137 | char *target; |
| 138 | int target_length; |
| 139 | zval *args_array = NULL; |
| 140 | grpc_channel_args args; |
| 141 | HashTable *array_hash; |
| 142 | zval **creds_obj = NULL; |
| 143 | wrapped_grpc_credentials *creds = NULL; |
murgatroid99 | f2fe1a8 | 2015-03-11 15:40:19 -0700 | [diff] [blame] | 144 | zval **override_obj; |
| 145 | char *override; |
| 146 | int override_len; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 147 | /* "s|a" == 1 string, 1 optional array */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 148 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target, |
| 149 | &target_length, &args_array) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 150 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 151 | "Channel expects a string and an array", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 152 | return; |
| 153 | } |
murgatroid99 | f2fe1a8 | 2015-03-11 15:40:19 -0700 | [diff] [blame] | 154 | override = target; |
| 155 | override_len = target_length; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 156 | if (args_array == NULL) { |
Craig Tiller | 4a4f149 | 2015-07-21 16:32:29 -0700 | [diff] [blame] | 157 | channel->wrapped = grpc_insecure_channel_create(target, NULL); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 158 | } else { |
| 159 | array_hash = Z_ARRVAL_P(args_array); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 160 | if (zend_hash_find(array_hash, "credentials", sizeof("credentials"), |
| 161 | (void **)&creds_obj) == SUCCESS) { |
| 162 | if (zend_get_class_entry(*creds_obj TSRMLS_CC) != grpc_ce_credentials) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 163 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 164 | "credentials must be a Credentials object", |
| 165 | 1 TSRMLS_CC); |
| 166 | return; |
| 167 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 168 | creds = (wrapped_grpc_credentials *)zend_object_store_get_object( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 169 | *creds_obj TSRMLS_CC); |
| 170 | zend_hash_del(array_hash, "credentials", 12); |
| 171 | } |
murgatroid99 | f2fe1a8 | 2015-03-11 15:40:19 -0700 | [diff] [blame] | 172 | if (zend_hash_find(array_hash, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, |
| 173 | sizeof(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 174 | (void **)&override_obj) == SUCCESS) { |
| 175 | if (Z_TYPE_PP(override_obj) != IS_STRING) { |
| 176 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 177 | GRPC_SSL_TARGET_NAME_OVERRIDE_ARG |
| 178 | " must be a string", |
| 179 | 1 TSRMLS_CC); |
| 180 | return; |
| 181 | } |
| 182 | override = Z_STRVAL_PP(override_obj); |
| 183 | override_len = Z_STRLEN_PP(override_obj); |
| 184 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 185 | php_grpc_read_args_array(args_array, &args); |
| 186 | if (creds == NULL) { |
Craig Tiller | 4a4f149 | 2015-07-21 16:32:29 -0700 | [diff] [blame] | 187 | channel->wrapped = grpc_insecure_channel_create(target, &args); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 188 | } else { |
| 189 | gpr_log(GPR_DEBUG, "Initialized secure channel"); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 190 | channel->wrapped = |
| 191 | grpc_secure_channel_create(creds->wrapped, target, &args); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 192 | } |
| 193 | efree(args.args); |
| 194 | } |
murgatroid99 | f2fe1a8 | 2015-03-11 15:40:19 -0700 | [diff] [blame] | 195 | channel->target = ecalloc(override_len + 1, sizeof(char)); |
| 196 | memcpy(channel->target, override, override_len); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 200 | * Get the endpoint this call/stream is connected to |
| 201 | * @return string The URI of the endpoint |
| 202 | */ |
| 203 | PHP_METHOD(Channel, getTarget) { |
| 204 | wrapped_grpc_channel *channel = |
| 205 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 206 | RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1); |
| 207 | } |
| 208 | |
| 209 | /** |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 210 | * Get the connectivity state of the channel |
| 211 | * @param bool (optional) try to connect on the channel |
| 212 | * @return long The grpc connectivity state |
| 213 | */ |
| 214 | PHP_METHOD(Channel, getConnectivityState) { |
| 215 | wrapped_grpc_channel *channel = |
| 216 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 217 | bool try_to_connect; |
| 218 | /* "|b" == 1 optional bool */ |
| 219 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) == |
| 220 | FAILURE) { |
| 221 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 222 | "getConnectivityState expects a bool", 1 TSRMLS_CC); |
| 223 | return; |
| 224 | } |
| 225 | RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped, |
| 226 | (int)try_to_connect)); |
| 227 | } |
| 228 | |
| 229 | /** |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 230 | * Watch the connectivity state of the channel until it changed |
| 231 | * @param long The previous connectivity state of the channel |
| 232 | * @param Timeval The deadline this function should wait until |
Stanley Cheung | 4c5c7b8 | 2015-08-12 16:28:58 -0700 | [diff] [blame] | 233 | * @return bool If the connectivity state changes from last_state |
| 234 | * before deadline |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 235 | */ |
| 236 | PHP_METHOD(Channel, watchConnectivityState) { |
| 237 | wrapped_grpc_channel *channel = |
| 238 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 239 | long last_state; |
| 240 | zval *deadline_obj; |
| 241 | /* "lO" == 1 long 1 object */ |
| 242 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", |
| 243 | &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) { |
| 244 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 245 | "watchConnectivityState expects 1 long 1 timeval", |
| 246 | 1 TSRMLS_CC); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | wrapped_grpc_timeval *deadline = |
| 251 | (wrapped_grpc_timeval *)zend_object_store_get_object( |
| 252 | deadline_obj TSRMLS_CC); |
| 253 | grpc_channel_watch_connectivity_state( |
| 254 | channel->wrapped, (grpc_connectivity_state)last_state, |
| 255 | deadline->wrapped, completion_queue, NULL); |
| 256 | grpc_event event = grpc_completion_queue_pluck( |
| 257 | completion_queue, NULL, |
| 258 | gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 259 | if (!event.success) { |
Stanley Cheung | 4c5c7b8 | 2015-08-12 16:28:58 -0700 | [diff] [blame] | 260 | RETURN_BOOL(0); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 261 | } |
Stanley Cheung | 4c5c7b8 | 2015-08-12 16:28:58 -0700 | [diff] [blame] | 262 | RETURN_BOOL(1); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 266 | * Close the channel |
| 267 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 268 | PHP_METHOD(Channel, close) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 269 | wrapped_grpc_channel *channel = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 270 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 271 | if (channel->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 272 | grpc_channel_destroy(channel->wrapped); |
| 273 | channel->wrapped = NULL; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static zend_function_entry channel_methods[] = { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 278 | PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 279 | PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 280 | PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 281 | PHP_ME(Channel, watchConnectivityState, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 282 | PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC) |
| 283 | PHP_FE_END}; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 284 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 285 | void grpc_init_channel(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 286 | zend_class_entry ce; |
| 287 | INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods); |
| 288 | ce.create_object = create_wrapped_grpc_channel; |
| 289 | grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC); |
| 290 | } |