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