blob: fe69fa5e29ae4ede314b3eef297e2e21b18f35fb [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
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
mlumishb892a272014-12-09 16:28:23 -080034#include "channel.h"
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
murgatroid998242ba72015-04-01 15:29:44 -070040#include <php.h>
41#include <php_ini.h>
42#include <ext/standard/info.h>
43#include <ext/spl/spl_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080044#include "php_grpc.h"
45
murgatroid998242ba72015-04-01 15:29:44 -070046#include <zend_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080047
48#include <stdbool.h>
49
murgatroid998242ba72015-04-01 15:29:44 -070050#include <grpc/grpc.h>
51#include <grpc/support/log.h>
52#include <grpc/grpc_security.h>
mlumishb892a272014-12-09 16:28:23 -080053
mlumishb892a272014-12-09 16:28:23 -080054#include "server.h"
55#include "credentials.h"
56
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080057zend_class_entry *grpc_ce_channel;
58
mlumishb892a272014-12-09 16:28:23 -080059/* Frees and destroys an instance of wrapped_grpc_channel */
Craig Tillerb5dcec52015-01-13 11:13:42 -080060void free_wrapped_grpc_channel(void *object TSRMLS_DC) {
61 wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object;
62 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080063 grpc_channel_destroy(channel->wrapped);
64 }
murgatroid99aa110662015-04-08 16:53:47 -070065 efree(channel->target);
mlumishb892a272014-12-09 16:28:23 -080066 efree(channel);
67}
68
69/* Initializes an instance of wrapped_grpc_channel to be associated with an
70 * object of a class specified by class_type */
Craig Tillerb5dcec52015-01-13 11:13:42 -080071zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type
72 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080073 zend_object_value retval;
74 wrapped_grpc_channel *intern;
Craig Tillerb5dcec52015-01-13 11:13:42 -080075 intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel));
mlumishb892a272014-12-09 16:28:23 -080076 memset(intern, 0, sizeof(wrapped_grpc_channel));
77 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
78 object_properties_init(&intern->std, class_type);
79 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080080 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
81 free_wrapped_grpc_channel, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080082 retval.handlers = zend_get_std_object_handlers();
83 return retval;
84}
85
Craig Tillerb5dcec52015-01-13 11:13:42 -080086void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
mlumishb892a272014-12-09 16:28:23 -080087 HashTable *array_hash;
88 HashPosition array_pointer;
89 int args_index;
90 zval **data;
91 char *key;
92 uint key_len;
93 ulong index;
94 array_hash = Z_ARRVAL_P(args_array);
95 args->num_args = zend_hash_num_elements(array_hash);
96 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
97 args_index = 0;
Craig Tillerb5dcec52015-01-13 11:13:42 -080098 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
99 zend_hash_get_current_data_ex(array_hash, (void **)&data,
100 &array_pointer) == SUCCESS;
101 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
102 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
103 &array_pointer) != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800104 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800105 "args keys must be strings", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800106 return;
107 }
108 args->args[args_index].key = key;
Craig Tillerb5dcec52015-01-13 11:13:42 -0800109 switch (Z_TYPE_P(*data)) {
mlumishb892a272014-12-09 16:28:23 -0800110 case IS_LONG:
111 args->args[args_index].value.integer = (int)Z_LVAL_P(*data);
112 break;
113 case IS_STRING:
114 args->args[args_index].value.string = Z_STRVAL_P(*data);
115 break;
116 default:
117 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800118 "args values must be int or string", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800119 return;
120 }
121 args_index++;
122 }
123}
124
125/**
126 * Construct an instance of the Channel class. If the $args array contains a
127 * "credentials" key mapping to a Credentials object, a secure channel will be
128 * created with those credentials.
129 * @param string $target The hostname to associate with this channel
130 * @param array $args The arguments to pass to the Channel (optional)
131 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800132PHP_METHOD(Channel, __construct) {
mlumishb892a272014-12-09 16:28:23 -0800133 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800134 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800135 char *target;
136 int target_length;
137 zval *args_array = NULL;
138 grpc_channel_args args;
139 HashTable *array_hash;
140 zval **creds_obj = NULL;
141 wrapped_grpc_credentials *creds = NULL;
murgatroid99f2fe1a82015-03-11 15:40:19 -0700142 zval **override_obj;
143 char *override;
144 int override_len;
mlumishb892a272014-12-09 16:28:23 -0800145 /* "s|a" == 1 string, 1 optional array */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800146 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target,
147 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800148 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800149 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800150 return;
151 }
murgatroid99f2fe1a82015-03-11 15:40:19 -0700152 override = target;
153 override_len = target_length;
mlumishb892a272014-12-09 16:28:23 -0800154 if (args_array == NULL) {
155 channel->wrapped = grpc_channel_create(target, NULL);
156 } else {
157 array_hash = Z_ARRVAL_P(args_array);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800158 if (zend_hash_find(array_hash, "credentials", sizeof("credentials"),
159 (void **)&creds_obj) == SUCCESS) {
160 if (zend_get_class_entry(*creds_obj TSRMLS_CC) != grpc_ce_credentials) {
mlumishb892a272014-12-09 16:28:23 -0800161 zend_throw_exception(spl_ce_InvalidArgumentException,
162 "credentials must be a Credentials object",
163 1 TSRMLS_CC);
164 return;
165 }
Craig Tillerb5dcec52015-01-13 11:13:42 -0800166 creds = (wrapped_grpc_credentials *)zend_object_store_get_object(
mlumishb892a272014-12-09 16:28:23 -0800167 *creds_obj TSRMLS_CC);
168 zend_hash_del(array_hash, "credentials", 12);
169 }
murgatroid99f2fe1a82015-03-11 15:40:19 -0700170 if (zend_hash_find(array_hash, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG,
171 sizeof(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
172 (void **)&override_obj) == SUCCESS) {
173 if (Z_TYPE_PP(override_obj) != IS_STRING) {
174 zend_throw_exception(spl_ce_InvalidArgumentException,
175 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
176 " must be a string",
177 1 TSRMLS_CC);
178 return;
179 }
180 override = Z_STRVAL_PP(override_obj);
181 override_len = Z_STRLEN_PP(override_obj);
182 }
mlumishb892a272014-12-09 16:28:23 -0800183 php_grpc_read_args_array(args_array, &args);
184 if (creds == NULL) {
185 channel->wrapped = grpc_channel_create(target, &args);
186 } else {
187 gpr_log(GPR_DEBUG, "Initialized secure channel");
Craig Tillerb5dcec52015-01-13 11:13:42 -0800188 channel->wrapped =
189 grpc_secure_channel_create(creds->wrapped, target, &args);
mlumishb892a272014-12-09 16:28:23 -0800190 }
191 efree(args.args);
192 }
murgatroid99f2fe1a82015-03-11 15:40:19 -0700193 channel->target = ecalloc(override_len + 1, sizeof(char));
194 memcpy(channel->target, override, override_len);
mlumishb892a272014-12-09 16:28:23 -0800195}
196
197/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700198 * Get the endpoint this call/stream is connected to
199 * @return string The URI of the endpoint
200 */
201PHP_METHOD(Channel, getTarget) {
202 wrapped_grpc_channel *channel =
203 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
204 RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1);
205}
206
207/**
mlumishb892a272014-12-09 16:28:23 -0800208 * Close the channel
209 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800210PHP_METHOD(Channel, close) {
mlumishb892a272014-12-09 16:28:23 -0800211 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800212 (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
213 if (channel->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -0800214 grpc_channel_destroy(channel->wrapped);
215 channel->wrapped = NULL;
216 }
217}
218
219static zend_function_entry channel_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800220 PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Stanley Cheungdb98e082015-07-27 10:19:45 -0700221 PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC)
222 PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC)
223 PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800224
Craig Tillerb5dcec52015-01-13 11:13:42 -0800225void grpc_init_channel(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800226 zend_class_entry ce;
227 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
228 ce.create_object = create_wrapped_grpc_channel;
229 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
230}