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 | |
| 40 | #include "php.h" |
| 41 | #include "php_ini.h" |
| 42 | #include "ext/standard/info.h" |
| 43 | #include "ext/spl/spl_exceptions.h" |
| 44 | #include "php_grpc.h" |
| 45 | |
| 46 | #include "zend_exceptions.h" |
| 47 | |
| 48 | #include <stdbool.h> |
| 49 | |
| 50 | #include "grpc/grpc.h" |
| 51 | #include "grpc/support/log.h" |
| 52 | #include "grpc/grpc_security.h" |
| 53 | |
| 54 | #include "completion_queue.h" |
| 55 | #include "server.h" |
| 56 | #include "credentials.h" |
| 57 | |
| 58 | /* Frees and destroys an instance of wrapped_grpc_channel */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 59 | void free_wrapped_grpc_channel(void *object TSRMLS_DC) { |
| 60 | wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object; |
| 61 | if (channel->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 62 | grpc_channel_destroy(channel->wrapped); |
| 63 | } |
| 64 | efree(channel); |
| 65 | } |
| 66 | |
| 67 | /* Initializes an instance of wrapped_grpc_channel to be associated with an |
| 68 | * object of a class specified by class_type */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 69 | zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type |
| 70 | TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 71 | zend_object_value retval; |
| 72 | wrapped_grpc_channel *intern; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 73 | intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 74 | memset(intern, 0, sizeof(wrapped_grpc_channel)); |
| 75 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 76 | object_properties_init(&intern->std, class_type); |
| 77 | retval.handle = zend_objects_store_put( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 78 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 79 | free_wrapped_grpc_channel, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 80 | retval.handlers = zend_get_std_object_handlers(); |
| 81 | return retval; |
| 82 | } |
| 83 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 84 | void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 85 | HashTable *array_hash; |
| 86 | HashPosition array_pointer; |
| 87 | int args_index; |
| 88 | zval **data; |
| 89 | char *key; |
| 90 | uint key_len; |
| 91 | ulong index; |
| 92 | array_hash = Z_ARRVAL_P(args_array); |
| 93 | args->num_args = zend_hash_num_elements(array_hash); |
| 94 | args->args = ecalloc(args->num_args, sizeof(grpc_arg)); |
| 95 | args_index = 0; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 96 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 97 | zend_hash_get_current_data_ex(array_hash, (void **)&data, |
| 98 | &array_pointer) == SUCCESS; |
| 99 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 100 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 101 | &array_pointer) != HASH_KEY_IS_STRING) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 102 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 103 | "args keys must be strings", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | args->args[args_index].key = key; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 107 | switch (Z_TYPE_P(*data)) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 108 | case IS_LONG: |
| 109 | args->args[args_index].value.integer = (int)Z_LVAL_P(*data); |
| 110 | break; |
| 111 | case IS_STRING: |
| 112 | args->args[args_index].value.string = Z_STRVAL_P(*data); |
| 113 | break; |
| 114 | default: |
| 115 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 116 | "args values must be int or string", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | args_index++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Construct an instance of the Channel class. If the $args array contains a |
| 125 | * "credentials" key mapping to a Credentials object, a secure channel will be |
| 126 | * created with those credentials. |
| 127 | * @param string $target The hostname to associate with this channel |
| 128 | * @param array $args The arguments to pass to the Channel (optional) |
| 129 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 130 | PHP_METHOD(Channel, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 131 | wrapped_grpc_channel *channel = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 132 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 133 | char *target; |
| 134 | int target_length; |
| 135 | zval *args_array = NULL; |
| 136 | grpc_channel_args args; |
| 137 | HashTable *array_hash; |
| 138 | zval **creds_obj = NULL; |
| 139 | wrapped_grpc_credentials *creds = NULL; |
| 140 | /* "s|a" == 1 string, 1 optional array */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 141 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target, |
| 142 | &target_length, &args_array) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 143 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 144 | "Channel expects a string and an array", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | if (args_array == NULL) { |
| 148 | channel->wrapped = grpc_channel_create(target, NULL); |
| 149 | } else { |
| 150 | array_hash = Z_ARRVAL_P(args_array); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 151 | if (zend_hash_find(array_hash, "credentials", sizeof("credentials"), |
| 152 | (void **)&creds_obj) == SUCCESS) { |
| 153 | if (zend_get_class_entry(*creds_obj TSRMLS_CC) != grpc_ce_credentials) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 154 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 155 | "credentials must be a Credentials object", |
| 156 | 1 TSRMLS_CC); |
| 157 | return; |
| 158 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 159 | creds = (wrapped_grpc_credentials *)zend_object_store_get_object( |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 160 | *creds_obj TSRMLS_CC); |
| 161 | zend_hash_del(array_hash, "credentials", 12); |
| 162 | } |
| 163 | php_grpc_read_args_array(args_array, &args); |
| 164 | if (creds == NULL) { |
| 165 | channel->wrapped = grpc_channel_create(target, &args); |
| 166 | } else { |
| 167 | gpr_log(GPR_DEBUG, "Initialized secure channel"); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 168 | channel->wrapped = |
| 169 | grpc_secure_channel_create(creds->wrapped, target, &args); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 170 | } |
| 171 | efree(args.args); |
| 172 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 173 | channel->target = ecalloc(target_length + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 174 | memcpy(channel->target, target, target_length); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Close the channel |
| 179 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 180 | PHP_METHOD(Channel, close) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 181 | wrapped_grpc_channel *channel = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 182 | (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 183 | if (channel->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 184 | grpc_channel_destroy(channel->wrapped); |
| 185 | channel->wrapped = NULL; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static zend_function_entry channel_methods[] = { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 190 | PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 191 | PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 192 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 193 | void grpc_init_channel(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 194 | zend_class_entry ce; |
| 195 | INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods); |
| 196 | ce.create_object = create_wrapped_grpc_channel; |
| 197 | grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC); |
| 198 | } |