wearefair-grpc 1.3.1.pre.c → 1.4.0.fair

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +418 -126
  3. data/include/grpc/grpc.h +15 -69
  4. data/include/grpc/grpc_security.h +1 -1
  5. data/include/grpc/impl/codegen/compression_types.h +3 -4
  6. data/include/grpc/impl/codegen/gpr_types.h +0 -1
  7. data/include/grpc/impl/codegen/grpc_types.h +69 -3
  8. data/include/grpc/impl/codegen/port_platform.h +6 -0
  9. data/include/grpc/impl/codegen/slice.h +2 -1
  10. data/include/grpc/load_reporting.h +6 -6
  11. data/include/grpc/slice.h +25 -3
  12. data/include/grpc/slice_buffer.h +4 -0
  13. data/src/core/ext/census/context.c +1 -1
  14. data/src/core/ext/census/resource.c +3 -1
  15. data/src/core/ext/filters/client_channel/channel_connectivity.c +1 -1
  16. data/src/core/ext/filters/client_channel/client_channel.c +158 -100
  17. data/src/core/ext/filters/client_channel/client_channel_plugin.c +3 -2
  18. data/src/core/ext/filters/client_channel/lb_policy.c +2 -1
  19. data/src/core/ext/filters/client_channel/lb_policy.h +5 -6
  20. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +153 -0
  21. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h +42 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +344 -88
  23. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +133 -0
  24. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +65 -0
  25. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +47 -5
  26. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +6 -0
  27. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +19 -8
  28. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +63 -34
  29. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +2 -1
  30. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +13 -12
  31. data/src/core/ext/filters/client_channel/lb_policy_factory.c +28 -5
  32. data/src/core/ext/filters/client_channel/lb_policy_factory.h +18 -4
  33. data/src/core/ext/filters/client_channel/parse_address.c +37 -7
  34. data/src/core/ext/filters/client_channel/parse_address.h +11 -8
  35. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.c +3 -3
  36. data/src/core/ext/filters/client_channel/subchannel.c +19 -16
  37. data/src/core/ext/filters/client_channel/subchannel.h +1 -0
  38. data/src/core/ext/filters/client_channel/uri_parser.c +36 -22
  39. data/src/core/ext/filters/client_channel/uri_parser.h +1 -1
  40. data/src/core/{lib/channel → ext/filters/deadline}/deadline_filter.c +42 -17
  41. data/src/core/{lib/channel → ext/filters/deadline}/deadline_filter.h +8 -9
  42. data/src/core/{lib/channel → ext/filters/http/client}/http_client_filter.c +19 -11
  43. data/src/core/{lib/channel → ext/filters/http/client}/http_client_filter.h +3 -6
  44. data/src/core/ext/filters/http/http_filters_plugin.c +104 -0
  45. data/src/core/{lib/channel/compress_filter.c → ext/filters/http/message_compress/message_compress_filter.c} +124 -23
  46. data/src/core/{lib/channel/compress_filter.h → ext/filters/http/message_compress/message_compress_filter.h} +5 -6
  47. data/src/core/{lib/channel → ext/filters/http/server}/http_server_filter.c +4 -6
  48. data/src/core/{lib/channel → ext/filters/http/server}/http_server_filter.h +3 -3
  49. data/src/core/ext/filters/load_reporting/load_reporting.c +2 -25
  50. data/src/core/ext/filters/load_reporting/load_reporting_filter.c +26 -1
  51. data/src/core/ext/filters/max_age/max_age_filter.c +14 -14
  52. data/src/core/{lib/channel → ext/filters/message_size}/message_size_filter.c +91 -47
  53. data/src/core/{lib/channel → ext/filters/message_size}/message_size_filter.h +3 -3
  54. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  55. data/src/core/ext/transport/chttp2/server/chttp2_server.c +2 -2
  56. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +2 -2
  57. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +3 -3
  58. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +296 -172
  59. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -2
  60. data/src/core/ext/transport/chttp2/transport/frame_data.c +203 -164
  61. data/src/core/ext/transport/chttp2/transport/frame_data.h +8 -14
  62. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +1 -1
  63. data/src/core/ext/transport/chttp2/transport/frame_ping.c +1 -1
  64. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  65. data/src/core/ext/transport/chttp2/transport/frame_settings.c +5 -5
  66. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +1 -1
  67. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +4 -4
  68. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +2 -4
  69. data/src/core/ext/transport/chttp2/transport/hpack_table.c +4 -3
  70. data/src/core/ext/transport/chttp2/transport/internal.h +50 -33
  71. data/src/core/ext/transport/chttp2/transport/parsing.c +10 -11
  72. data/src/core/ext/transport/chttp2/transport/writing.c +32 -13
  73. data/src/core/lib/channel/channel_args.c +28 -9
  74. data/src/core/lib/channel/channel_args.h +5 -1
  75. data/src/core/lib/channel/channel_stack.c +1 -1
  76. data/src/core/lib/channel/channel_stack.h +2 -2
  77. data/src/core/lib/channel/channel_stack_builder.c +13 -1
  78. data/src/core/lib/channel/channel_stack_builder.h +5 -1
  79. data/src/core/lib/channel/connected_channel.c +3 -1
  80. data/src/core/lib/channel/context.h +2 -2
  81. data/src/core/lib/compression/message_compress.c +2 -2
  82. data/src/core/lib/debug/trace.c +13 -6
  83. data/src/core/lib/debug/trace.h +27 -1
  84. data/src/core/lib/http/httpcli.c +1 -1
  85. data/src/core/lib/http/httpcli_security_connector.c +6 -10
  86. data/src/core/lib/http/parser.c +2 -2
  87. data/src/core/lib/http/parser.h +2 -1
  88. data/src/core/lib/iomgr/combiner.c +6 -6
  89. data/src/core/lib/iomgr/combiner.h +2 -1
  90. data/src/core/lib/iomgr/error.c +12 -5
  91. data/src/core/lib/iomgr/error.h +13 -13
  92. data/src/core/lib/iomgr/ev_epoll1_linux.c +984 -0
  93. data/src/core/lib/iomgr/ev_epoll1_linux.h +44 -0
  94. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +2146 -0
  95. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.h +43 -0
  96. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +1337 -0
  97. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.h +43 -0
  98. data/src/core/lib/iomgr/ev_epollex_linux.c +1511 -0
  99. data/src/core/lib/iomgr/ev_epollex_linux.h +43 -0
  100. data/src/core/lib/iomgr/{ev_epoll_linux.c → ev_epollsig_linux.c} +24 -31
  101. data/src/core/lib/iomgr/{ev_epoll_linux.h → ev_epollsig_linux.h} +4 -4
  102. data/src/core/lib/iomgr/ev_poll_posix.c +12 -27
  103. data/src/core/lib/iomgr/ev_poll_posix.h +2 -2
  104. data/src/core/lib/iomgr/ev_posix.c +22 -8
  105. data/src/core/lib/iomgr/ev_posix.h +4 -3
  106. data/src/core/lib/iomgr/exec_ctx.c +5 -0
  107. data/src/core/lib/iomgr/exec_ctx.h +2 -0
  108. data/src/core/lib/iomgr/iomgr.c +4 -0
  109. data/src/core/lib/iomgr/iomgr.h +3 -0
  110. data/src/core/lib/iomgr/is_epollexclusive_available.c +116 -0
  111. data/src/core/lib/iomgr/is_epollexclusive_available.h +41 -0
  112. data/src/core/lib/iomgr/lockfree_event.c +16 -0
  113. data/src/core/lib/iomgr/pollset.h +2 -5
  114. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  115. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  116. data/src/core/lib/iomgr/resource_quota.c +9 -8
  117. data/src/core/lib/iomgr/resource_quota.h +2 -1
  118. data/src/core/lib/iomgr/sockaddr_utils.h +1 -1
  119. data/src/core/lib/iomgr/socket_mutator.h +2 -0
  120. data/src/core/lib/iomgr/sys_epoll_wrapper.h +43 -0
  121. data/src/core/lib/iomgr/tcp_client_posix.c +6 -6
  122. data/src/core/lib/iomgr/tcp_client_uv.c +3 -3
  123. data/src/core/lib/iomgr/tcp_posix.c +7 -7
  124. data/src/core/lib/iomgr/tcp_posix.h +2 -1
  125. data/src/core/lib/iomgr/tcp_server_posix.c +1 -1
  126. data/src/core/lib/iomgr/tcp_uv.c +6 -6
  127. data/src/core/lib/iomgr/tcp_uv.h +2 -1
  128. data/src/core/lib/iomgr/tcp_windows.c +1 -1
  129. data/src/core/lib/iomgr/timer_generic.c +24 -25
  130. data/src/core/lib/iomgr/timer_manager.c +276 -0
  131. data/src/core/lib/iomgr/timer_manager.h +52 -0
  132. data/src/core/lib/iomgr/timer_uv.c +6 -0
  133. data/src/core/lib/iomgr/udp_server.c +42 -9
  134. data/src/core/lib/iomgr/udp_server.h +3 -1
  135. data/src/core/lib/security/credentials/credentials.c +0 -1
  136. data/src/core/lib/security/credentials/fake/fake_credentials.c +23 -0
  137. data/src/core/lib/security/credentials/fake/fake_credentials.h +12 -9
  138. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +1 -1
  139. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +1 -1
  140. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +1 -1
  141. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +24 -53
  142. data/src/core/lib/security/transport/client_auth_filter.c +9 -3
  143. data/src/core/lib/security/transport/secure_endpoint.c +7 -7
  144. data/src/core/lib/security/transport/secure_endpoint.h +1 -1
  145. data/src/core/lib/security/transport/security_connector.c +32 -51
  146. data/src/core/lib/security/transport/security_connector.h +10 -14
  147. data/src/core/lib/slice/b64.c +1 -1
  148. data/src/core/lib/slice/percent_encoding.c +3 -3
  149. data/src/core/lib/slice/slice.c +66 -33
  150. data/src/core/lib/slice/slice_buffer.c +25 -6
  151. data/src/core/lib/slice/slice_hash_table.c +33 -35
  152. data/src/core/lib/slice/slice_hash_table.h +7 -12
  153. data/src/core/lib/support/atomic.h +45 -0
  154. data/src/core/lib/support/atomic_with_atm.h +70 -0
  155. data/src/core/lib/support/atomic_with_std.h +48 -0
  156. data/src/core/lib/support/avl.c +14 -14
  157. data/src/core/lib/support/memory.h +74 -0
  158. data/src/core/lib/support/mpscq.c +12 -1
  159. data/src/core/lib/support/mpscq.h +4 -0
  160. data/src/core/lib/support/stack_lockfree.c +3 -36
  161. data/src/core/lib/support/time_posix.c +8 -0
  162. data/src/core/lib/support/tmpfile_posix.c +10 -10
  163. data/src/core/lib/surface/alarm.c +3 -1
  164. data/src/core/lib/surface/api_trace.c +2 -1
  165. data/src/core/lib/surface/api_trace.h +2 -2
  166. data/src/core/lib/surface/byte_buffer_reader.c +1 -1
  167. data/src/core/lib/surface/call.c +65 -22
  168. data/src/core/lib/surface/call.h +4 -2
  169. data/src/core/lib/surface/channel_init.c +2 -19
  170. data/src/core/lib/surface/channel_stack_type.c +18 -0
  171. data/src/core/lib/surface/channel_stack_type.h +2 -0
  172. data/src/core/lib/surface/completion_queue.c +249 -83
  173. data/src/core/lib/surface/completion_queue.h +18 -13
  174. data/src/core/lib/surface/completion_queue_factory.c +24 -9
  175. data/src/core/lib/surface/init.c +1 -52
  176. data/src/core/lib/surface/{lame_client.c → lame_client.cc} +37 -26
  177. data/src/core/lib/surface/server.c +50 -27
  178. data/src/core/lib/surface/server.h +2 -1
  179. data/src/core/lib/surface/version.c +2 -2
  180. data/src/core/lib/transport/bdp_estimator.c +20 -9
  181. data/src/core/lib/transport/bdp_estimator.h +5 -1
  182. data/src/core/lib/transport/byte_stream.c +23 -9
  183. data/src/core/lib/transport/byte_stream.h +15 -6
  184. data/src/core/lib/transport/connectivity_state.c +6 -6
  185. data/src/core/lib/transport/connectivity_state.h +2 -1
  186. data/src/core/lib/transport/service_config.c +6 -13
  187. data/src/core/lib/transport/service_config.h +2 -2
  188. data/src/core/lib/transport/static_metadata.c +403 -389
  189. data/src/core/lib/transport/static_metadata.h +127 -114
  190. data/src/core/plugin_registry/grpc_plugin_registry.c +12 -0
  191. data/src/core/tsi/fake_transport_security.c +5 -4
  192. data/src/core/tsi/ssl_transport_security.c +71 -82
  193. data/src/core/tsi/ssl_transport_security.h +39 -61
  194. data/src/core/tsi/transport_security.c +83 -2
  195. data/src/core/tsi/transport_security.h +27 -2
  196. data/src/core/tsi/transport_security_adapter.c +236 -0
  197. data/src/core/tsi/transport_security_adapter.h +62 -0
  198. data/src/core/tsi/transport_security_interface.h +179 -66
  199. data/src/ruby/ext/grpc/extconf.rb +2 -1
  200. data/src/ruby/ext/grpc/rb_byte_buffer.c +8 -6
  201. data/src/ruby/ext/grpc/rb_call.c +56 -48
  202. data/src/ruby/ext/grpc/rb_call.h +3 -4
  203. data/src/ruby/ext/grpc/rb_call_credentials.c +23 -22
  204. data/src/ruby/ext/grpc/rb_channel.c +45 -29
  205. data/src/ruby/ext/grpc/rb_channel_args.c +11 -9
  206. data/src/ruby/ext/grpc/rb_channel_credentials.c +16 -12
  207. data/src/ruby/ext/grpc/rb_completion_queue.c +7 -9
  208. data/src/ruby/ext/grpc/rb_compression_options.c +7 -6
  209. data/src/ruby/ext/grpc/rb_event_thread.c +10 -12
  210. data/src/ruby/ext/grpc/rb_event_thread.h +1 -2
  211. data/src/ruby/ext/grpc/rb_grpc.c +11 -15
  212. data/src/ruby/ext/grpc/rb_grpc.h +2 -2
  213. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +14 -6
  214. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +22 -10
  215. data/src/ruby/ext/grpc/rb_server.c +26 -28
  216. data/src/ruby/lib/grpc/version.rb +1 -1
  217. metadata +40 -18
  218. data/src/ruby/lib/grpc/grpc_c.bundle +0 -0
  219. data/src/ruby/lib/grpc/grpc_c.so +0 -0
@@ -41,7 +41,7 @@
41
41
 
42
42
  /* --- Tracing. --- */
43
43
 
44
- int tsi_tracing_enabled = 0;
44
+ grpc_tracer_flag tsi_tracing_enabled = GRPC_TRACER_INITIALIZER(false);
45
45
 
46
46
  /* --- tsi_result common implementation. --- */
47
47
 
@@ -73,6 +73,8 @@ const char *tsi_result_to_string(tsi_result result) {
73
73
  return "TSI_HANDSHAKE_IN_PROGRESS";
74
74
  case TSI_OUT_OF_RESOURCES:
75
75
  return "TSI_OUT_OF_RESOURCES";
76
+ case TSI_ASYNC:
77
+ return "TSI_ASYNC";
76
78
  default:
77
79
  return "UNKNOWN";
78
80
  }
@@ -92,6 +94,9 @@ tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
92
94
  protected_output_frames_size == NULL) {
93
95
  return TSI_INVALID_ARGUMENT;
94
96
  }
97
+ if (self->vtable == NULL || self->vtable->protect == NULL) {
98
+ return TSI_UNIMPLEMENTED;
99
+ }
95
100
  return self->vtable->protect(self, unprotected_bytes, unprotected_bytes_size,
96
101
  protected_output_frames,
97
102
  protected_output_frames_size);
@@ -104,6 +109,9 @@ tsi_result tsi_frame_protector_protect_flush(
104
109
  protected_output_frames_size == NULL || still_pending_size == NULL) {
105
110
  return TSI_INVALID_ARGUMENT;
106
111
  }
112
+ if (self->vtable == NULL || self->vtable->protect_flush == NULL) {
113
+ return TSI_UNIMPLEMENTED;
114
+ }
107
115
  return self->vtable->protect_flush(self, protected_output_frames,
108
116
  protected_output_frames_size,
109
117
  still_pending_size);
@@ -118,6 +126,9 @@ tsi_result tsi_frame_protector_unprotect(
118
126
  unprotected_bytes_size == NULL) {
119
127
  return TSI_INVALID_ARGUMENT;
120
128
  }
129
+ if (self->vtable == NULL || self->vtable->unprotect == NULL) {
130
+ return TSI_UNIMPLEMENTED;
131
+ }
121
132
  return self->vtable->unprotect(self, protected_frames_bytes,
122
133
  protected_frames_bytes_size, unprotected_bytes,
123
134
  unprotected_bytes_size);
@@ -139,6 +150,9 @@ tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
139
150
  return TSI_INVALID_ARGUMENT;
140
151
  }
141
152
  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
153
+ if (self->vtable == NULL || self->vtable->get_bytes_to_send_to_peer == NULL) {
154
+ return TSI_UNIMPLEMENTED;
155
+ }
142
156
  return self->vtable->get_bytes_to_send_to_peer(self, bytes, bytes_size);
143
157
  }
144
158
 
@@ -149,12 +163,18 @@ tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
149
163
  return TSI_INVALID_ARGUMENT;
150
164
  }
151
165
  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
166
+ if (self->vtable == NULL || self->vtable->process_bytes_from_peer == NULL) {
167
+ return TSI_UNIMPLEMENTED;
168
+ }
152
169
  return self->vtable->process_bytes_from_peer(self, bytes, bytes_size);
153
170
  }
154
171
 
155
172
  tsi_result tsi_handshaker_get_result(tsi_handshaker *self) {
156
173
  if (self == NULL) return TSI_INVALID_ARGUMENT;
157
174
  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
175
+ if (self->vtable == NULL || self->vtable->get_result == NULL) {
176
+ return TSI_UNIMPLEMENTED;
177
+ }
158
178
  return self->vtable->get_result(self);
159
179
  }
160
180
 
@@ -165,6 +185,9 @@ tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer) {
165
185
  if (tsi_handshaker_get_result(self) != TSI_OK) {
166
186
  return TSI_FAILED_PRECONDITION;
167
187
  }
188
+ if (self->vtable == NULL || self->vtable->extract_peer == NULL) {
189
+ return TSI_UNIMPLEMENTED;
190
+ }
168
191
  return self->vtable->extract_peer(self, peer);
169
192
  }
170
193
 
@@ -177,19 +200,77 @@ tsi_result tsi_handshaker_create_frame_protector(
177
200
  if (tsi_handshaker_get_result(self) != TSI_OK) {
178
201
  return TSI_FAILED_PRECONDITION;
179
202
  }
203
+ if (self->vtable == NULL || self->vtable->create_frame_protector == NULL) {
204
+ return TSI_UNIMPLEMENTED;
205
+ }
180
206
  result = self->vtable->create_frame_protector(self, max_protected_frame_size,
181
207
  protector);
182
208
  if (result == TSI_OK) {
183
- self->frame_protector_created = 1;
209
+ self->frame_protector_created = true;
184
210
  }
185
211
  return result;
186
212
  }
187
213
 
214
+ tsi_result tsi_handshaker_next(
215
+ tsi_handshaker *self, const unsigned char *received_bytes,
216
+ size_t received_bytes_size, unsigned char **bytes_to_send,
217
+ size_t *bytes_to_send_size, tsi_handshaker_result **handshaker_result,
218
+ tsi_handshaker_on_next_done_cb cb, void *user_data) {
219
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
220
+ if (self->handshaker_result_created) return TSI_FAILED_PRECONDITION;
221
+ if (self->vtable == NULL || self->vtable->next == NULL) {
222
+ return TSI_UNIMPLEMENTED;
223
+ }
224
+ return self->vtable->next(self, received_bytes, received_bytes_size,
225
+ bytes_to_send, bytes_to_send_size,
226
+ handshaker_result, cb, user_data);
227
+ }
228
+
188
229
  void tsi_handshaker_destroy(tsi_handshaker *self) {
189
230
  if (self == NULL) return;
190
231
  self->vtable->destroy(self);
191
232
  }
192
233
 
234
+ /* --- tsi_handshaker_result implementation. --- */
235
+
236
+ tsi_result tsi_handshaker_result_extract_peer(const tsi_handshaker_result *self,
237
+ tsi_peer *peer) {
238
+ if (self == NULL || peer == NULL) return TSI_INVALID_ARGUMENT;
239
+ memset(peer, 0, sizeof(tsi_peer));
240
+ if (self->vtable == NULL || self->vtable->extract_peer == NULL) {
241
+ return TSI_UNIMPLEMENTED;
242
+ }
243
+ return self->vtable->extract_peer(self, peer);
244
+ }
245
+
246
+ tsi_result tsi_handshaker_result_create_frame_protector(
247
+ const tsi_handshaker_result *self, size_t *max_protected_frame_size,
248
+ tsi_frame_protector **protector) {
249
+ if (self == NULL || protector == NULL) return TSI_INVALID_ARGUMENT;
250
+ if (self->vtable == NULL || self->vtable->create_frame_protector == NULL) {
251
+ return TSI_UNIMPLEMENTED;
252
+ }
253
+ return self->vtable->create_frame_protector(self, max_protected_frame_size,
254
+ protector);
255
+ }
256
+
257
+ tsi_result tsi_handshaker_result_get_unused_bytes(
258
+ const tsi_handshaker_result *self, unsigned char **bytes,
259
+ size_t *bytes_size) {
260
+ if (self == NULL || bytes == NULL || bytes_size == NULL) {
261
+ return TSI_INVALID_ARGUMENT;
262
+ }
263
+ if (self->vtable == NULL || self->vtable->get_unused_bytes == NULL) {
264
+ return TSI_UNIMPLEMENTED;
265
+ }
266
+ return self->vtable->get_unused_bytes(self, bytes, bytes_size);
267
+ }
268
+
269
+ void tsi_handshaker_result_destroy(tsi_handshaker_result *self) {
270
+ if (self == NULL) return;
271
+ self->vtable->destroy(self);
272
+ }
273
+
193
274
  /* --- tsi_peer implementation. --- */
194
275
 
195
276
  tsi_peer_property tsi_init_peer_property(void) {
@@ -34,13 +34,16 @@
34
34
  #ifndef GRPC_CORE_TSI_TRANSPORT_SECURITY_H
35
35
  #define GRPC_CORE_TSI_TRANSPORT_SECURITY_H
36
36
 
37
+ #include <stdbool.h>
38
+
39
+ #include "src/core/lib/debug/trace.h"
37
40
  #include "src/core/tsi/transport_security_interface.h"
38
41
 
39
42
  #ifdef __cplusplus
40
43
  extern "C" {
41
44
  #endif
42
45
 
43
- extern int tsi_tracing_enabled;
46
+ extern grpc_tracer_flag tsi_tracing_enabled;
44
47
 
45
48
  /* Base for tsi_frame_protector implementations.
46
49
  See transport_security_interface.h for documentation. */
@@ -81,11 +84,33 @@ typedef struct {
81
84
  size_t *max_protected_frame_size,
82
85
  tsi_frame_protector **protector);
83
86
  void (*destroy)(tsi_handshaker *self);
87
+ tsi_result (*next)(tsi_handshaker *self, const unsigned char *received_bytes,
88
+ size_t received_bytes_size, unsigned char **bytes_to_send,
89
+ size_t *bytes_to_send_size,
90
+ tsi_handshaker_result **handshaker_result,
91
+ tsi_handshaker_on_next_done_cb cb, void *user_data);
84
92
  } tsi_handshaker_vtable;
85
93
 
86
94
  struct tsi_handshaker {
87
95
  const tsi_handshaker_vtable *vtable;
88
- int frame_protector_created;
96
+ bool frame_protector_created;
97
+ bool handshaker_result_created;
98
+ };
99
+
100
+ /* Base for tsi_handshaker_result implementations.
101
+ See transport_security_interface.h for documentation. */
102
+ typedef struct {
103
+ tsi_result (*extract_peer)(const tsi_handshaker_result *self, tsi_peer *peer);
104
+ tsi_result (*create_frame_protector)(const tsi_handshaker_result *self,
105
+ size_t *max_output_protected_frame_size,
106
+ tsi_frame_protector **protector);
107
+ tsi_result (*get_unused_bytes)(const tsi_handshaker_result *self,
108
+ unsigned char **bytes, size_t *bytes_size);
109
+ void (*destroy)(tsi_handshaker_result *self);
110
+ } tsi_handshaker_result_vtable;
111
+
112
+ struct tsi_handshaker_result {
113
+ const tsi_handshaker_result_vtable *vtable;
89
114
  };
90
115
 
91
116
  /* Peer and property construction/destruction functions. */
@@ -0,0 +1,236 @@
1
+ /*
2
+ *
3
+ * Copyright 2017, 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 "src/core/tsi/transport_security_adapter.h"
35
+
36
+ #include <string.h>
37
+
38
+ #include <grpc/support/alloc.h>
39
+ #include <grpc/support/log.h>
40
+ #include "src/core/tsi/transport_security.h"
41
+
42
+ #define TSI_ADAPTER_INITIAL_BUFFER_SIZE 256
43
+
44
+ /* --- tsi_adapter_handshaker_result implementation ---*/
45
+
46
+ typedef struct {
47
+ tsi_handshaker_result base;
48
+ tsi_handshaker *wrapped;
49
+ unsigned char *unused_bytes;
50
+ size_t unused_bytes_size;
51
+ } tsi_adapter_handshaker_result;
52
+
53
+ static tsi_result adapter_result_extract_peer(const tsi_handshaker_result *self,
54
+ tsi_peer *peer) {
55
+ tsi_adapter_handshaker_result *impl = (tsi_adapter_handshaker_result *)self;
56
+ return tsi_handshaker_extract_peer(impl->wrapped, peer);
57
+ }
58
+
59
+ static tsi_result adapter_result_create_frame_protector(
60
+ const tsi_handshaker_result *self, size_t *max_output_protected_frame_size,
61
+ tsi_frame_protector **protector) {
62
+ tsi_adapter_handshaker_result *impl = (tsi_adapter_handshaker_result *)self;
63
+ return tsi_handshaker_create_frame_protector(
64
+ impl->wrapped, max_output_protected_frame_size, protector);
65
+ }
66
+
67
+ static tsi_result adapter_result_get_unused_bytes(
68
+ const tsi_handshaker_result *self, unsigned char **bytes,
69
+ size_t *byte_size) {
70
+ tsi_adapter_handshaker_result *impl = (tsi_adapter_handshaker_result *)self;
71
+ *bytes = impl->unused_bytes;
72
+ *byte_size = impl->unused_bytes_size;
73
+ return TSI_OK;
74
+ }
75
+
76
+ static void adapter_result_destroy(tsi_handshaker_result *self) {
77
+ tsi_adapter_handshaker_result *impl = (tsi_adapter_handshaker_result *)self;
78
+ tsi_handshaker_destroy(impl->wrapped);
79
+ gpr_free(impl->unused_bytes);
80
+ gpr_free(self);
81
+ }
82
+
83
+ static const tsi_handshaker_result_vtable result_vtable = {
84
+ adapter_result_extract_peer, adapter_result_create_frame_protector,
85
+ adapter_result_get_unused_bytes, adapter_result_destroy,
86
+ };
87
+
88
+ /* Ownership of wrapped tsi_handshaker is transferred to the result object. */
89
+ static tsi_result tsi_adapter_create_handshaker_result(
90
+ tsi_handshaker *wrapped, const unsigned char *unused_bytes,
91
+ size_t unused_bytes_size, tsi_handshaker_result **handshaker_result) {
92
+ if (wrapped == NULL || (unused_bytes_size > 0 && unused_bytes == NULL)) {
93
+ return TSI_INVALID_ARGUMENT;
94
+ }
95
+ tsi_adapter_handshaker_result *impl = gpr_zalloc(sizeof(*impl));
96
+ impl->base.vtable = &result_vtable;
97
+ impl->wrapped = wrapped;
98
+ impl->unused_bytes_size = unused_bytes_size;
99
+ if (unused_bytes_size > 0) {
100
+ impl->unused_bytes = gpr_malloc(unused_bytes_size);
101
+ memcpy(impl->unused_bytes, unused_bytes, unused_bytes_size);
102
+ } else {
103
+ impl->unused_bytes = NULL;
104
+ }
105
+ *handshaker_result = &impl->base;
106
+ return TSI_OK;
107
+ }
108
+
109
+ /* --- tsi_adapter_handshaker implementation ---*/
110
+
111
+ typedef struct {
112
+ tsi_handshaker base;
113
+ tsi_handshaker *wrapped;
114
+ unsigned char *adapter_buffer;
115
+ size_t adapter_buffer_size;
116
+ } tsi_adapter_handshaker;
117
+
118
+ static tsi_result adapter_get_bytes_to_send_to_peer(tsi_handshaker *self,
119
+ unsigned char *bytes,
120
+ size_t *bytes_size) {
121
+ return tsi_handshaker_get_bytes_to_send_to_peer(
122
+ tsi_adapter_handshaker_get_wrapped(self), bytes, bytes_size);
123
+ }
124
+
125
+ static tsi_result adapter_process_bytes_from_peer(tsi_handshaker *self,
126
+ const unsigned char *bytes,
127
+ size_t *bytes_size) {
128
+ return tsi_handshaker_process_bytes_from_peer(
129
+ tsi_adapter_handshaker_get_wrapped(self), bytes, bytes_size);
130
+ }
131
+
132
+ static tsi_result adapter_get_result(tsi_handshaker *self) {
133
+ return tsi_handshaker_get_result(tsi_adapter_handshaker_get_wrapped(self));
134
+ }
135
+
136
+ static tsi_result adapter_extract_peer(tsi_handshaker *self, tsi_peer *peer) {
137
+ return tsi_handshaker_extract_peer(tsi_adapter_handshaker_get_wrapped(self),
138
+ peer);
139
+ }
140
+
141
+ static tsi_result adapter_create_frame_protector(
142
+ tsi_handshaker *self, size_t *max_protected_frame_size,
143
+ tsi_frame_protector **protector) {
144
+ return tsi_handshaker_create_frame_protector(
145
+ tsi_adapter_handshaker_get_wrapped(self), max_protected_frame_size,
146
+ protector);
147
+ }
148
+
149
+ static void adapter_destroy(tsi_handshaker *self) {
150
+ tsi_adapter_handshaker *impl = (tsi_adapter_handshaker *)self;
151
+ tsi_handshaker_destroy(impl->wrapped);
152
+ gpr_free(impl->adapter_buffer);
153
+ gpr_free(self);
154
+ }
155
+
156
+ static tsi_result adapter_next(
157
+ tsi_handshaker *self, const unsigned char *received_bytes,
158
+ size_t received_bytes_size, unsigned char **bytes_to_send,
159
+ size_t *bytes_to_send_size, tsi_handshaker_result **handshaker_result,
160
+ tsi_handshaker_on_next_done_cb cb, void *user_data) {
161
+ /* Input sanity check. */
162
+ if ((received_bytes_size > 0 && received_bytes == NULL) ||
163
+ bytes_to_send == NULL || bytes_to_send_size == NULL ||
164
+ handshaker_result == NULL) {
165
+ return TSI_INVALID_ARGUMENT;
166
+ }
167
+
168
+ /* If there are received bytes, process them first. */
169
+ tsi_adapter_handshaker *impl = (tsi_adapter_handshaker *)self;
170
+ tsi_result status = TSI_OK;
171
+ size_t bytes_consumed = received_bytes_size;
172
+ if (received_bytes_size > 0) {
173
+ status = tsi_handshaker_process_bytes_from_peer(
174
+ impl->wrapped, received_bytes, &bytes_consumed);
175
+ if (status != TSI_OK) return status;
176
+ }
177
+
178
+ /* Get bytes to send to the peer, if available. */
179
+ size_t offset = 0;
180
+ do {
181
+ size_t to_send_size = impl->adapter_buffer_size - offset;
182
+ status = tsi_handshaker_get_bytes_to_send_to_peer(
183
+ impl->wrapped, impl->adapter_buffer + offset, &to_send_size);
184
+ offset += to_send_size;
185
+ if (status == TSI_INCOMPLETE_DATA) {
186
+ impl->adapter_buffer_size *= 2;
187
+ impl->adapter_buffer =
188
+ gpr_realloc(impl->adapter_buffer, impl->adapter_buffer_size);
189
+ }
190
+ } while (status == TSI_INCOMPLETE_DATA);
191
+ if (status != TSI_OK) return status;
192
+ *bytes_to_send = impl->adapter_buffer;
193
+ *bytes_to_send_size = offset;
194
+
195
+ /* If handshake completes, create tsi_handshaker_result. */
196
+ if (tsi_handshaker_is_in_progress(impl->wrapped)) {
197
+ *handshaker_result = NULL;
198
+ } else {
199
+ size_t unused_bytes_size = received_bytes_size - bytes_consumed;
200
+ const unsigned char *unused_bytes =
201
+ unused_bytes_size == 0 ? NULL : received_bytes + bytes_consumed;
202
+ status = tsi_adapter_create_handshaker_result(
203
+ impl->wrapped, unused_bytes, unused_bytes_size, handshaker_result);
204
+ if (status == TSI_OK) {
205
+ impl->base.handshaker_result_created = true;
206
+ impl->wrapped = NULL;
207
+ }
208
+ }
209
+ return status;
210
+ }
211
+
212
+ static const tsi_handshaker_vtable handshaker_vtable = {
213
+ adapter_get_bytes_to_send_to_peer,
214
+ adapter_process_bytes_from_peer,
215
+ adapter_get_result,
216
+ adapter_extract_peer,
217
+ adapter_create_frame_protector,
218
+ adapter_destroy,
219
+ adapter_next,
220
+ };
221
+
222
+ tsi_handshaker *tsi_create_adapter_handshaker(tsi_handshaker *wrapped) {
223
+ GPR_ASSERT(wrapped != NULL);
224
+ tsi_adapter_handshaker *impl = gpr_zalloc(sizeof(*impl));
225
+ impl->base.vtable = &handshaker_vtable;
226
+ impl->wrapped = wrapped;
227
+ impl->adapter_buffer_size = TSI_ADAPTER_INITIAL_BUFFER_SIZE;
228
+ impl->adapter_buffer = gpr_malloc(impl->adapter_buffer_size);
229
+ return &impl->base;
230
+ }
231
+
232
+ tsi_handshaker *tsi_adapter_handshaker_get_wrapped(tsi_handshaker *adapter) {
233
+ if (adapter == NULL) return NULL;
234
+ tsi_adapter_handshaker *impl = (tsi_adapter_handshaker *)adapter;
235
+ return impl->wrapped;
236
+ }