grpc 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +10 -0
  5. data/.rubocop_todo.yml +52 -0
  6. data/Gemfile +4 -0
  7. data/README.md +82 -0
  8. data/Rakefile +54 -0
  9. data/bin/apis/google/protobuf/empty.rb +44 -0
  10. data/bin/apis/pubsub_demo.rb +267 -0
  11. data/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
  12. data/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
  13. data/bin/interop/README.md +8 -0
  14. data/bin/interop/interop_client.rb +334 -0
  15. data/bin/interop/interop_server.rb +192 -0
  16. data/bin/interop/test/cpp/interop/empty.rb +44 -0
  17. data/bin/interop/test/cpp/interop/messages.rb +89 -0
  18. data/bin/interop/test/cpp/interop/test.rb +43 -0
  19. data/bin/interop/test/cpp/interop/test_services.rb +60 -0
  20. data/bin/math.proto +80 -0
  21. data/bin/math.rb +61 -0
  22. data/bin/math_client.rb +147 -0
  23. data/bin/math_server.rb +190 -0
  24. data/bin/math_services.rb +56 -0
  25. data/bin/noproto_client.rb +108 -0
  26. data/bin/noproto_server.rb +112 -0
  27. data/ext/grpc/extconf.rb +76 -0
  28. data/ext/grpc/rb_byte_buffer.c +241 -0
  29. data/ext/grpc/rb_byte_buffer.h +54 -0
  30. data/ext/grpc/rb_call.c +569 -0
  31. data/ext/grpc/rb_call.h +59 -0
  32. data/ext/grpc/rb_channel.c +264 -0
  33. data/ext/grpc/rb_channel.h +49 -0
  34. data/ext/grpc/rb_channel_args.c +154 -0
  35. data/ext/grpc/rb_channel_args.h +52 -0
  36. data/ext/grpc/rb_completion_queue.c +185 -0
  37. data/ext/grpc/rb_completion_queue.h +50 -0
  38. data/ext/grpc/rb_credentials.c +281 -0
  39. data/ext/grpc/rb_credentials.h +50 -0
  40. data/ext/grpc/rb_event.c +361 -0
  41. data/ext/grpc/rb_event.h +53 -0
  42. data/ext/grpc/rb_grpc.c +274 -0
  43. data/ext/grpc/rb_grpc.h +74 -0
  44. data/ext/grpc/rb_metadata.c +215 -0
  45. data/ext/grpc/rb_metadata.h +53 -0
  46. data/ext/grpc/rb_server.c +278 -0
  47. data/ext/grpc/rb_server.h +50 -0
  48. data/ext/grpc/rb_server_credentials.c +210 -0
  49. data/ext/grpc/rb_server_credentials.h +50 -0
  50. data/grpc.gemspec +41 -0
  51. data/lib/grpc.rb +39 -0
  52. data/lib/grpc/core/event.rb +44 -0
  53. data/lib/grpc/core/time_consts.rb +71 -0
  54. data/lib/grpc/errors.rb +61 -0
  55. data/lib/grpc/generic/active_call.rb +536 -0
  56. data/lib/grpc/generic/bidi_call.rb +221 -0
  57. data/lib/grpc/generic/client_stub.rb +413 -0
  58. data/lib/grpc/generic/rpc_desc.rb +150 -0
  59. data/lib/grpc/generic/rpc_server.rb +404 -0
  60. data/lib/grpc/generic/service.rb +235 -0
  61. data/lib/grpc/logconfig.rb +40 -0
  62. data/lib/grpc/version.rb +33 -0
  63. data/spec/alloc_spec.rb +44 -0
  64. data/spec/byte_buffer_spec.rb +67 -0
  65. data/spec/call_spec.rb +163 -0
  66. data/spec/channel_spec.rb +181 -0
  67. data/spec/client_server_spec.rb +372 -0
  68. data/spec/completion_queue_spec.rb +74 -0
  69. data/spec/credentials_spec.rb +71 -0
  70. data/spec/event_spec.rb +53 -0
  71. data/spec/generic/active_call_spec.rb +373 -0
  72. data/spec/generic/client_stub_spec.rb +519 -0
  73. data/spec/generic/rpc_desc_spec.rb +357 -0
  74. data/spec/generic/rpc_server_pool_spec.rb +139 -0
  75. data/spec/generic/rpc_server_spec.rb +404 -0
  76. data/spec/generic/service_spec.rb +342 -0
  77. data/spec/metadata_spec.rb +64 -0
  78. data/spec/server_credentials_spec.rb +69 -0
  79. data/spec/server_spec.rb +212 -0
  80. data/spec/spec_helper.rb +51 -0
  81. data/spec/testdata/README +1 -0
  82. data/spec/testdata/ca.pem +15 -0
  83. data/spec/testdata/server1.key +16 -0
  84. data/spec/testdata/server1.pem +16 -0
  85. data/spec/time_consts_spec.rb +89 -0
  86. metadata +353 -0
@@ -0,0 +1,53 @@
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
+
34
+ #ifndef GRPC_RB_METADATA_H_
35
+ #define GRPC_RB_METADATA_H_
36
+
37
+ #include <grpc/grpc.h>
38
+ #include <ruby.h>
39
+
40
+ /* rb_cMetadata is the Metadata class whose instances proxy grpc_metadata. */
41
+ extern VALUE rb_cMetadata;
42
+
43
+ /* grpc_rb_metadata_create_with_mark creates a grpc_rb_metadata with a ruby mark
44
+ * object that will be kept alive while the metadata is alive. */
45
+ extern VALUE grpc_rb_metadata_create_with_mark(VALUE mark, grpc_metadata* md);
46
+
47
+ /* Gets the wrapped metadata from the ruby wrapper */
48
+ grpc_metadata* grpc_rb_get_wrapped_metadata(VALUE v);
49
+
50
+ /* Initializes the Metadata class. */
51
+ void Init_grpc_metadata();
52
+
53
+ #endif /* GRPC_RB_METADATA_H_ */
@@ -0,0 +1,278 @@
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
+
34
+ #include "rb_server.h"
35
+
36
+ #include <ruby.h>
37
+
38
+ #include <grpc/grpc.h>
39
+ #include <grpc/grpc_security.h>
40
+ #include "rb_call.h"
41
+ #include "rb_channel_args.h"
42
+ #include "rb_completion_queue.h"
43
+ #include "rb_server_credentials.h"
44
+ #include "rb_grpc.h"
45
+
46
+ /* rb_cServer is the ruby class that proxies grpc_server. */
47
+ VALUE rb_cServer = Qnil;
48
+
49
+ /* grpc_rb_server wraps a grpc_server. It provides a peer ruby object,
50
+ 'mark' to minimize copying when a server is created from ruby. */
51
+ typedef struct grpc_rb_server {
52
+ /* Holder of ruby objects involved in constructing the server */
53
+ VALUE mark;
54
+ /* The actual server */
55
+ grpc_server *wrapped;
56
+ } grpc_rb_server;
57
+
58
+ /* Destroys server instances. */
59
+ static void grpc_rb_server_free(void *p) {
60
+ grpc_rb_server *svr = NULL;
61
+ if (p == NULL) {
62
+ return;
63
+ };
64
+ svr = (grpc_rb_server *)p;
65
+
66
+ /* Deletes the wrapped object if the mark object is Qnil, which indicates
67
+ that no other object is the actual owner. */
68
+ if (svr->wrapped != NULL && svr->mark == Qnil) {
69
+ grpc_server_shutdown(svr->wrapped);
70
+ grpc_server_destroy(svr->wrapped);
71
+ }
72
+
73
+ xfree(p);
74
+ }
75
+
76
+ /* Protects the mark object from GC */
77
+ static void grpc_rb_server_mark(void *p) {
78
+ grpc_rb_server *server = NULL;
79
+ if (p == NULL) {
80
+ return;
81
+ }
82
+ server = (grpc_rb_server *)p;
83
+ if (server->mark != Qnil) {
84
+ rb_gc_mark(server->mark);
85
+ }
86
+ }
87
+
88
+ /* Allocates grpc_rb_server instances. */
89
+ static VALUE grpc_rb_server_alloc(VALUE cls) {
90
+ grpc_rb_server *wrapper = ALLOC(grpc_rb_server);
91
+ wrapper->wrapped = NULL;
92
+ wrapper->mark = Qnil;
93
+ return Data_Wrap_Struct(cls, grpc_rb_server_mark, grpc_rb_server_free,
94
+ wrapper);
95
+ }
96
+
97
+ /*
98
+ call-seq:
99
+ cq = CompletionQueue.new
100
+ insecure_server = Server.new(cq, {'arg1': 'value1'})
101
+ server_creds = ...
102
+ secure_server = Server.new(cq, {'arg1': 'value1'}, server_creds)
103
+
104
+ Initializes server instances. */
105
+ static VALUE grpc_rb_server_init(int argc, VALUE *argv, VALUE self) {
106
+ VALUE cqueue = Qnil;
107
+ VALUE credentials = Qnil;
108
+ VALUE channel_args = Qnil;
109
+ grpc_completion_queue *cq = NULL;
110
+ grpc_server_credentials *creds = NULL;
111
+ grpc_rb_server *wrapper = NULL;
112
+ grpc_server *srv = NULL;
113
+ grpc_channel_args args;
114
+ MEMZERO(&args, grpc_channel_args, 1);
115
+
116
+ /* "21" == 2 mandatory args, 1 (credentials) is optional */
117
+ rb_scan_args(argc, argv, "21", &cqueue, &channel_args, &credentials);
118
+ cq = grpc_rb_get_wrapped_completion_queue(cqueue);
119
+
120
+ Data_Get_Struct(self, grpc_rb_server, wrapper);
121
+ grpc_rb_hash_convert_to_channel_args(channel_args, &args);
122
+ srv = grpc_server_create(cq, &args);
123
+ if (credentials == Qnil) {
124
+ srv = grpc_server_create(cq, &args);
125
+ } else {
126
+ creds = grpc_rb_get_wrapped_server_credentials(credentials);
127
+ srv = grpc_secure_server_create(creds, cq, &args);
128
+ }
129
+
130
+ if (args.args != NULL) {
131
+ xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */
132
+ }
133
+ if (srv == NULL) {
134
+ rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why");
135
+ }
136
+ wrapper->wrapped = srv;
137
+
138
+ /* Add the cq as the server's mark object. This ensures the ruby cq can't be
139
+ GCed before the server */
140
+ wrapper->mark = cqueue;
141
+ return self;
142
+ }
143
+
144
+ /* Clones Server instances.
145
+
146
+ Gives Server a consistent implementation of Ruby's object copy/dup
147
+ protocol. */
148
+ static VALUE grpc_rb_server_init_copy(VALUE copy, VALUE orig) {
149
+ grpc_rb_server *orig_srv = NULL;
150
+ grpc_rb_server *copy_srv = NULL;
151
+
152
+ if (copy == orig) {
153
+ return copy;
154
+ }
155
+
156
+ /* Raise an error if orig is not a server object or a subclass. */
157
+ if (TYPE(orig) != T_DATA ||
158
+ RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_server_free) {
159
+ rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(rb_cServer));
160
+ }
161
+
162
+ Data_Get_Struct(orig, grpc_rb_server, orig_srv);
163
+ Data_Get_Struct(copy, grpc_rb_server, copy_srv);
164
+
165
+ /* use ruby's MEMCPY to make a byte-for-byte copy of the server wrapper
166
+ object. */
167
+ MEMCPY(copy_srv, orig_srv, grpc_rb_server, 1);
168
+ return copy;
169
+ }
170
+
171
+ static VALUE grpc_rb_server_request_call(VALUE self, VALUE tag_new) {
172
+ grpc_call_error err;
173
+ grpc_rb_server *s = NULL;
174
+ Data_Get_Struct(self, grpc_rb_server, s);
175
+ if (s->wrapped == NULL) {
176
+ rb_raise(rb_eRuntimeError, "closed!");
177
+ } else {
178
+ err = grpc_server_request_call_old(s->wrapped, ROBJECT(tag_new));
179
+ if (err != GRPC_CALL_OK) {
180
+ rb_raise(rb_eCallError, "server request failed: %s (code=%d)",
181
+ grpc_call_error_detail_of(err), err);
182
+ }
183
+ }
184
+ return Qnil;
185
+ }
186
+
187
+ static VALUE grpc_rb_server_start(VALUE self) {
188
+ grpc_rb_server *s = NULL;
189
+ Data_Get_Struct(self, grpc_rb_server, s);
190
+ if (s->wrapped == NULL) {
191
+ rb_raise(rb_eRuntimeError, "closed!");
192
+ } else {
193
+ grpc_server_start(s->wrapped);
194
+ }
195
+ return Qnil;
196
+ }
197
+
198
+ static VALUE grpc_rb_server_destroy(VALUE self) {
199
+ grpc_rb_server *s = NULL;
200
+ Data_Get_Struct(self, grpc_rb_server, s);
201
+ if (s->wrapped != NULL) {
202
+ grpc_server_shutdown(s->wrapped);
203
+ grpc_server_destroy(s->wrapped);
204
+ s->wrapped = NULL;
205
+ s->mark = Qnil;
206
+ }
207
+ return Qnil;
208
+ }
209
+
210
+ /*
211
+ call-seq:
212
+ // insecure port
213
+ insecure_server = Server.new(cq, {'arg1': 'value1'})
214
+ insecure_server.add_http2_port('mydomain:7575')
215
+
216
+ // secure port
217
+ server_creds = ...
218
+ secure_server = Server.new(cq, {'arg1': 'value1'}, creds)
219
+ secure_server.add_http_port('mydomain:7575', True)
220
+
221
+ Adds a http2 port to server */
222
+ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) {
223
+ VALUE port = Qnil;
224
+ VALUE is_secure = Qnil;
225
+ grpc_rb_server *s = NULL;
226
+ int recvd_port = 0;
227
+
228
+ /* "11" == 1 mandatory args, 1 (is_secure) is optional */
229
+ rb_scan_args(argc, argv, "11", &port, &is_secure);
230
+
231
+ Data_Get_Struct(self, grpc_rb_server, s);
232
+ if (s->wrapped == NULL) {
233
+ rb_raise(rb_eRuntimeError, "closed!");
234
+ return Qnil;
235
+ } else if (is_secure == Qnil || TYPE(is_secure) != T_TRUE) {
236
+ recvd_port = grpc_server_add_http2_port(s->wrapped, StringValueCStr(port));
237
+ if (recvd_port == 0) {
238
+ rb_raise(rb_eRuntimeError,
239
+ "could not add port %s to server, not sure why",
240
+ StringValueCStr(port));
241
+ }
242
+ } else if (TYPE(is_secure) != T_FALSE) {
243
+ recvd_port =
244
+ grpc_server_add_secure_http2_port(s->wrapped, StringValueCStr(port));
245
+ if (recvd_port == 0) {
246
+ rb_raise(rb_eRuntimeError,
247
+ "could not add secure port %s to server, not sure why",
248
+ StringValueCStr(port));
249
+ }
250
+ }
251
+ return INT2NUM(recvd_port);
252
+ }
253
+
254
+ void Init_grpc_server() {
255
+ rb_cServer = rb_define_class_under(rb_mGrpcCore, "Server", rb_cObject);
256
+
257
+ /* Allocates an object managed by the ruby runtime */
258
+ rb_define_alloc_func(rb_cServer, grpc_rb_server_alloc);
259
+
260
+ /* Provides a ruby constructor and support for dup/clone. */
261
+ rb_define_method(rb_cServer, "initialize", grpc_rb_server_init, -1);
262
+ rb_define_method(rb_cServer, "initialize_copy", grpc_rb_server_init_copy, 1);
263
+
264
+ /* Add the server methods. */
265
+ rb_define_method(rb_cServer, "request_call", grpc_rb_server_request_call, 1);
266
+ rb_define_method(rb_cServer, "start", grpc_rb_server_start, 0);
267
+ rb_define_method(rb_cServer, "destroy", grpc_rb_server_destroy, 0);
268
+ rb_define_alias(rb_cServer, "close", "destroy");
269
+ rb_define_method(rb_cServer, "add_http2_port", grpc_rb_server_add_http2_port,
270
+ -1);
271
+ }
272
+
273
+ /* Gets the wrapped server from the ruby wrapper */
274
+ grpc_server *grpc_rb_get_wrapped_server(VALUE v) {
275
+ grpc_rb_server *wrapper = NULL;
276
+ Data_Get_Struct(v, grpc_rb_server, wrapper);
277
+ return wrapper->wrapped;
278
+ }
@@ -0,0 +1,50 @@
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
+
34
+ #ifndef GRPC_RB_SERVER_H_
35
+ #define GRPC_RB_SERVER_H_
36
+
37
+ #include <ruby.h>
38
+ #include <grpc/grpc.h>
39
+
40
+ /* rb_cServer is the Server class whose instances proxy
41
+ grpc_byte_buffer. */
42
+ extern VALUE rb_cServer;
43
+
44
+ /* Initializes the Server class. */
45
+ void Init_grpc_server();
46
+
47
+ /* Gets the wrapped server from the ruby wrapper */
48
+ grpc_server* grpc_rb_get_wrapped_server(VALUE v);
49
+
50
+ #endif /* GRPC_RB_SERVER_H_ */
@@ -0,0 +1,210 @@
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
+
34
+ #include "rb_server_credentials.h"
35
+
36
+ #include <ruby.h>
37
+
38
+ #include <grpc/grpc.h>
39
+ #include <grpc/grpc_security.h>
40
+
41
+ #include "rb_grpc.h"
42
+
43
+ /* grpc_rb_server_credentials wraps a grpc_server_credentials. It provides a
44
+ peer ruby object, 'mark' to minimize copying when a server credential is
45
+ created from ruby. */
46
+ typedef struct grpc_rb_server_credentials {
47
+ /* Holder of ruby objects involved in constructing the server credentials */
48
+ VALUE mark;
49
+ /* The actual server credentials */
50
+ grpc_server_credentials *wrapped;
51
+ } grpc_rb_server_credentials;
52
+
53
+ /* Destroys the server credentials instances. */
54
+ static void grpc_rb_server_credentials_free(void *p) {
55
+ grpc_rb_server_credentials *wrapper = NULL;
56
+ if (p == NULL) {
57
+ return;
58
+ };
59
+ wrapper = (grpc_rb_server_credentials *)p;
60
+
61
+ /* Delete the wrapped object if the mark object is Qnil, which indicates that
62
+ no other object is the actual owner. */
63
+ if (wrapper->wrapped != NULL && wrapper->mark == Qnil) {
64
+ grpc_server_credentials_release(wrapper->wrapped);
65
+ wrapper->wrapped = NULL;
66
+ }
67
+
68
+ xfree(p);
69
+ }
70
+
71
+ /* Protects the mark object from GC */
72
+ static void grpc_rb_server_credentials_mark(void *p) {
73
+ grpc_rb_server_credentials *wrapper = NULL;
74
+ if (p == NULL) {
75
+ return;
76
+ }
77
+ wrapper = (grpc_rb_server_credentials *)p;
78
+
79
+ /* If it's not already cleaned up, mark the mark object */
80
+ if (wrapper->mark != Qnil) {
81
+ rb_gc_mark(wrapper->mark);
82
+ }
83
+ }
84
+
85
+ /* Allocates ServerCredential instances.
86
+
87
+ Provides safe initial defaults for the instance fields. */
88
+ static VALUE grpc_rb_server_credentials_alloc(VALUE cls) {
89
+ grpc_rb_server_credentials *wrapper = ALLOC(grpc_rb_server_credentials);
90
+ wrapper->wrapped = NULL;
91
+ wrapper->mark = Qnil;
92
+ return Data_Wrap_Struct(cls, grpc_rb_server_credentials_mark,
93
+ grpc_rb_server_credentials_free, wrapper);
94
+ }
95
+
96
+ /* Clones ServerCredentials instances.
97
+
98
+ Gives ServerCredentials a consistent implementation of Ruby's object copy/dup
99
+ protocol. */
100
+ static VALUE grpc_rb_server_credentials_init_copy(VALUE copy, VALUE orig) {
101
+ grpc_rb_server_credentials *orig_ch = NULL;
102
+ grpc_rb_server_credentials *copy_ch = NULL;
103
+
104
+ if (copy == orig) {
105
+ return copy;
106
+ }
107
+
108
+ /* Raise an error if orig is not a server_credentials object or a subclass. */
109
+ if (TYPE(orig) != T_DATA ||
110
+ RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_server_credentials_free) {
111
+ rb_raise(rb_eTypeError, "not a %s",
112
+ rb_obj_classname(rb_cServerCredentials));
113
+ }
114
+
115
+ Data_Get_Struct(orig, grpc_rb_server_credentials, orig_ch);
116
+ Data_Get_Struct(copy, grpc_rb_server_credentials, copy_ch);
117
+
118
+ /* use ruby's MEMCPY to make a byte-for-byte copy of the server_credentials
119
+ wrapper object. */
120
+ MEMCPY(copy_ch, orig_ch, grpc_rb_server_credentials, 1);
121
+ return copy;
122
+ }
123
+
124
+ /* The attribute used on the mark object to hold the pem_root_certs. */
125
+ static ID id_pem_root_certs;
126
+
127
+ /* The attribute used on the mark object to hold the pem_private_key. */
128
+ static ID id_pem_private_key;
129
+
130
+ /* The attribute used on the mark object to hold the pem_private_key. */
131
+ static ID id_pem_cert_chain;
132
+
133
+ /*
134
+ call-seq:
135
+ creds = ServerCredentials.new(pem_root_certs, pem_private_key,
136
+ pem_cert_chain)
137
+ creds = ServerCredentials.new(nil, pem_private_key,
138
+ pem_cert_chain)
139
+
140
+ pem_root_certs: (required) PEM encoding of the server root certificate
141
+ pem_private_key: (optional) PEM encoding of the server's private key
142
+ pem_cert_chain: (optional) PEM encoding of the server's cert chain
143
+
144
+ Initializes ServerCredential instances. */
145
+ static VALUE grpc_rb_server_credentials_init(VALUE self, VALUE pem_root_certs,
146
+ VALUE pem_private_key,
147
+ VALUE pem_cert_chain) {
148
+ /* TODO support multiple key cert pairs in the ruby API. */
149
+ grpc_rb_server_credentials *wrapper = NULL;
150
+ grpc_server_credentials *creds = NULL;
151
+ grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
152
+ Data_Get_Struct(self, grpc_rb_server_credentials, wrapper);
153
+ if (pem_cert_chain == Qnil) {
154
+ rb_raise(rb_eRuntimeError,
155
+ "could not create a server credential: nil pem_cert_chain");
156
+ return Qnil;
157
+ } else if (pem_private_key == Qnil) {
158
+ rb_raise(rb_eRuntimeError,
159
+ "could not create a server credential: nil pem_private_key");
160
+ return Qnil;
161
+ }
162
+ key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
163
+ key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
164
+ if (pem_root_certs == Qnil) {
165
+ creds = grpc_ssl_server_credentials_create(NULL, &key_cert_pair, 1);
166
+ } else {
167
+ creds = grpc_ssl_server_credentials_create(RSTRING_PTR(pem_root_certs),
168
+ &key_cert_pair, 1);
169
+ }
170
+ if (creds == NULL) {
171
+ rb_raise(rb_eRuntimeError, "could not create a credentials, not sure why");
172
+ }
173
+ wrapper->wrapped = creds;
174
+
175
+ /* Add the input objects as hidden fields to preserve them. */
176
+ rb_ivar_set(self, id_pem_cert_chain, pem_cert_chain);
177
+ rb_ivar_set(self, id_pem_private_key, pem_private_key);
178
+ rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
179
+
180
+ return self;
181
+ }
182
+
183
+ /* rb_cServerCredentials is the ruby class that proxies
184
+ grpc_server_credentials. */
185
+ VALUE rb_cServerCredentials = Qnil;
186
+
187
+ void Init_grpc_server_credentials() {
188
+ rb_cServerCredentials =
189
+ rb_define_class_under(rb_mGrpcCore, "ServerCredentials", rb_cObject);
190
+
191
+ /* Allocates an object managed by the ruby runtime */
192
+ rb_define_alloc_func(rb_cServerCredentials, grpc_rb_server_credentials_alloc);
193
+
194
+ /* Provides a ruby constructor and support for dup/clone. */
195
+ rb_define_method(rb_cServerCredentials, "initialize",
196
+ grpc_rb_server_credentials_init, 3);
197
+ rb_define_method(rb_cServerCredentials, "initialize_copy",
198
+ grpc_rb_server_credentials_init_copy, 1);
199
+
200
+ id_pem_cert_chain = rb_intern("__pem_cert_chain");
201
+ id_pem_private_key = rb_intern("__pem_private_key");
202
+ id_pem_root_certs = rb_intern("__pem_root_certs");
203
+ }
204
+
205
+ /* Gets the wrapped grpc_server_credentials from the ruby wrapper */
206
+ grpc_server_credentials *grpc_rb_get_wrapped_server_credentials(VALUE v) {
207
+ grpc_rb_server_credentials *wrapper = NULL;
208
+ Data_Get_Struct(v, grpc_rb_server_credentials, wrapper);
209
+ return wrapper->wrapped;
210
+ }