| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/iomgr/port.h" |
| 22 |
|
| 23 |
#ifdef GRPC_CFSTREAM_ENDPOINT |
| 24 |
|
| 25 |
#import <CoreFoundation/CoreFoundation.h> |
| 26 |
|
| 27 |
#include <grpc/slice_buffer.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
|
| 31 |
#include "src/core/lib/address_utils/sockaddr_utils.h" |
| 32 |
#include "src/core/lib/gpr/string.h" |
| 33 |
#include "src/core/lib/iomgr/cfstream_handle.h" |
| 34 |
#include "src/core/lib/iomgr/closure.h" |
| 35 |
#include "src/core/lib/iomgr/endpoint.h" |
| 36 |
#import "src/core/lib/iomgr/endpoint_cfstream.h" |
| 37 |
#include "src/core/lib/iomgr/error_cfstream.h" |
| 38 |
#include "src/core/lib/iomgr/sockaddr.h" |
| 39 |
#include "src/core/lib/slice/slice.h" |
| 40 |
#include "src/core/lib/slice/slice_internal.h" |
| 41 |
#include "src/core/lib/slice/slice_string_helpers.h" |
| 42 |
|
| 43 |
extern grpc_core::TraceFlag grpc_tcp_trace; |
| 44 |
|
| 45 |
struct CFStreamEndpoint { |
| 46 |
grpc_endpoint base; |
| 47 |
gpr_refcount refcount; |
| 48 |
|
| 49 |
CFReadStreamRef read_stream; |
| 50 |
CFWriteStreamRef write_stream; |
| 51 |
CFStreamHandle* stream_sync; |
| 52 |
|
| 53 |
grpc_closure* read_cb; |
| 54 |
grpc_closure* write_cb; |
| 55 |
grpc_slice_buffer* read_slices; |
| 56 |
grpc_slice_buffer* write_slices; |
| 57 |
|
| 58 |
grpc_closure read_action; |
| 59 |
grpc_closure write_action; |
| 60 |
|
| 61 |
std::string peer_string; |
| 62 |
std::string local_address; |
| 63 |
}; |
| 64 |
static void CFStreamFree(CFStreamEndpoint* ep) { |
| 65 |
CFRelease(ep->read_stream); |
| 66 |
CFRelease(ep->write_stream); |
| 67 |
CFSTREAM_HANDLE_UNREF(ep->stream_sync, "free"); |
| 68 |
delete ep; |
| 69 |
} |
| 70 |
|
| 71 |
#ifndef NDEBUG |
| 72 |
#define EP_REF(ep, reason) CFStreamRef((ep), (reason), __FILE__, __LINE__) |
| 73 |
#define EP_UNREF(ep, reason) CFStreamUnref((ep), (reason), __FILE__, __LINE__) |
| 74 |
static void CFStreamUnref(CFStreamEndpoint* ep, const char* reason, |
| 75 |
const char* file, int line) { |
| 76 |
if (grpc_tcp_trace.enabled()) { |
| 77 |
gpr_atm val = gpr_atm_no_barrier_load(&ep->refcount.count); |
| 78 |
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 79 |
"CFStream endpoint unref %p : %s %" PRIdPTR " -> %" PRIdPTR, ep, |
| 80 |
reason, val, val - 1); |
| 81 |
} |
| 82 |
if (gpr_unref(&ep->refcount)) { |
| 83 |
CFStreamFree(ep); |
| 84 |
} |
| 85 |
} |
| 86 |
static void CFStreamRef(CFStreamEndpoint* ep, const char* reason, |
| 87 |
const char* file, int line) { |
| 88 |
if (grpc_tcp_trace.enabled()) { |
| 89 |
gpr_atm val = gpr_atm_no_barrier_load(&ep->refcount.count); |
| 90 |
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 91 |
"CFStream endpoint ref %p : %s %" PRIdPTR " -> %" PRIdPTR, ep, |
| 92 |
reason, val, val + 1); |
| 93 |
} |
| 94 |
gpr_ref(&ep->refcount); |
| 95 |
} |
| 96 |
#else |
| 97 |
#define EP_REF(ep, reason) CFStreamRef((ep)) |
| 98 |
#define EP_UNREF(ep, reason) CFStreamUnref((ep)) |
| 99 |
static void CFStreamUnref(CFStreamEndpoint* ep) { |
| 100 |
if (gpr_unref(&ep->refcount)) { |
| 101 |
CFStreamFree(ep); |
| 102 |
} |
| 103 |
} |
| 104 |
static void CFStreamRef(CFStreamEndpoint* ep) { gpr_ref(&ep->refcount); } |
| 105 |
#endif |
| 106 |
|
| 107 |
static grpc_error_handle CFStreamAnnotateError(grpc_error_handle src_error, |
| 108 |
CFStreamEndpoint* ep) { |
| 109 |
return grpc_error_set_str( |
| 110 |
grpc_error_set_int(src_error, grpc_core::StatusIntProperty::kRpcStatus, |
| 111 |
GRPC_STATUS_UNAVAILABLE), |
| 112 |
grpc_core::StatusStrProperty::kTargetAddress, ep->peer_string); |
| 113 |
} |
| 114 |
|
| 115 |
static void CallReadCb(CFStreamEndpoint* ep, grpc_error_handle error) { |
| 116 |
if (grpc_tcp_trace.enabled()) { |
| 117 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p call_read_cb %p %p:%p", ep, |
| 118 |
ep->read_cb, ep->read_cb->cb, ep->read_cb->cb_arg); |
| 119 |
size_t i; |
| 120 |
gpr_log(GPR_DEBUG, "read: error=%s", |
| 121 |
grpc_core::StatusToString(error).c_str()); |
| 122 |
|
| 123 |
for (i = 0; i < ep->read_slices->count; i++) { |
| 124 |
char* dump = grpc_dump_slice(ep->read_slices->slices[i], |
| 125 |
GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 126 |
gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", ep, ep->peer_string.c_str(), |
| 127 |
dump); |
| 128 |
gpr_free(dump); |
| 129 |
} |
| 130 |
} |
| 131 |
grpc_closure* cb = ep->read_cb; |
| 132 |
ep->read_cb = nullptr; |
| 133 |
ep->read_slices = nullptr; |
| 134 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error); |
| 135 |
} |
| 136 |
|
| 137 |
static void CallWriteCb(CFStreamEndpoint* ep, grpc_error_handle error) { |
| 138 |
if (grpc_tcp_trace.enabled()) { |
| 139 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p call_write_cb %p %p:%p", ep, |
| 140 |
ep->write_cb, ep->write_cb->cb, ep->write_cb->cb_arg); |
| 141 |
gpr_log(GPR_DEBUG, "write: error=%s", |
| 142 |
grpc_core::StatusToString(error).c_str()); |
| 143 |
} |
| 144 |
grpc_closure* cb = ep->write_cb; |
| 145 |
ep->write_cb = nullptr; |
| 146 |
ep->write_slices = nullptr; |
| 147 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, error); |
| 148 |
} |
| 149 |
|
| 150 |
static void ReadAction(void* arg, grpc_error_handle error) { |
| 151 |
CFStreamEndpoint* ep = static_cast<CFStreamEndpoint*>(arg); |
| 152 |
GPR_ASSERT(ep->read_cb != nullptr); |
| 153 |
if (!error.ok()) { |
| 154 |
grpc_slice_buffer_reset_and_unref(ep->read_slices); |
| 155 |
CallReadCb(ep, error); |
| 156 |
EP_UNREF(ep, "read"); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
GPR_ASSERT(ep->read_slices->count == 1); |
| 161 |
grpc_slice slice = ep->read_slices->slices[0]; |
| 162 |
size_t len = GRPC_SLICE_LENGTH(slice); |
| 163 |
CFIndex read_size = |
| 164 |
CFReadStreamRead(ep->read_stream, GRPC_SLICE_START_PTR(slice), len); |
| 165 |
if (read_size == -1) { |
| 166 |
grpc_slice_buffer_reset_and_unref(ep->read_slices); |
| 167 |
CFErrorRef stream_error = CFReadStreamCopyError(ep->read_stream); |
| 168 |
if (stream_error != nullptr) { |
| 169 |
error = CFStreamAnnotateError( |
| 170 |
GRPC_ERROR_CREATE_FROM_CFERROR(stream_error, "Read error"), ep); |
| 171 |
CFRelease(stream_error); |
| 172 |
} else { |
| 173 |
error = GRPC_ERROR_CREATE("Read error"); |
| 174 |
} |
| 175 |
CallReadCb(ep, error); |
| 176 |
EP_UNREF(ep, "read"); |
| 177 |
} else if (read_size == 0) { |
| 178 |
grpc_slice_buffer_reset_and_unref(ep->read_slices); |
| 179 |
CallReadCb(ep, |
| 180 |
CFStreamAnnotateError(GRPC_ERROR_CREATE("Socket closed"), ep)); |
| 181 |
EP_UNREF(ep, "read"); |
| 182 |
} else { |
| 183 |
if (read_size < static_cast<CFIndex>(len)) { |
| 184 |
grpc_slice_buffer_trim_end(ep->read_slices, len - read_size, nullptr); |
| 185 |
} |
| 186 |
CallReadCb(ep, absl::OkStatus()); |
| 187 |
EP_UNREF(ep, "read"); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
static void WriteAction(void* arg, grpc_error_handle error) { |
| 192 |
CFStreamEndpoint* ep = static_cast<CFStreamEndpoint*>(arg); |
| 193 |
GPR_ASSERT(ep->write_cb != nullptr); |
| 194 |
if (!error.ok()) { |
| 195 |
grpc_slice_buffer_reset_and_unref(ep->write_slices); |
| 196 |
CallWriteCb(ep, error); |
| 197 |
EP_UNREF(ep, "write"); |
| 198 |
return; |
| 199 |
} |
| 200 |
grpc_slice slice = grpc_slice_buffer_take_first(ep->write_slices); |
| 201 |
size_t slice_len = GRPC_SLICE_LENGTH(slice); |
| 202 |
CFIndex write_size = CFWriteStreamWrite( |
| 203 |
ep->write_stream, GRPC_SLICE_START_PTR(slice), slice_len); |
| 204 |
if (write_size == -1) { |
| 205 |
grpc_slice_buffer_reset_and_unref(ep->write_slices); |
| 206 |
CFErrorRef stream_error = CFWriteStreamCopyError(ep->write_stream); |
| 207 |
if (stream_error != nullptr) { |
| 208 |
error = CFStreamAnnotateError( |
| 209 |
GRPC_ERROR_CREATE_FROM_CFERROR(stream_error, "write failed."), ep); |
| 210 |
CFRelease(stream_error); |
| 211 |
} else { |
| 212 |
error = GRPC_ERROR_CREATE("write failed."); |
| 213 |
} |
| 214 |
CallWriteCb(ep, error); |
| 215 |
EP_UNREF(ep, "write"); |
| 216 |
} else { |
| 217 |
if (write_size < static_cast<CFIndex>(GRPC_SLICE_LENGTH(slice))) { |
| 218 |
grpc_slice_buffer_undo_take_first( |
| 219 |
ep->write_slices, grpc_slice_sub(slice, write_size, slice_len)); |
| 220 |
} |
| 221 |
if (ep->write_slices->length > 0) { |
| 222 |
ep->stream_sync->NotifyOnWrite(&ep->write_action); |
| 223 |
} else { |
| 224 |
CallWriteCb(ep, absl::OkStatus()); |
| 225 |
EP_UNREF(ep, "write"); |
| 226 |
} |
| 227 |
|
| 228 |
if (grpc_tcp_trace.enabled()) { |
| 229 |
grpc_slice trace_slice = grpc_slice_sub(slice, 0, write_size); |
| 230 |
char* dump = grpc_dump_slice(trace_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 231 |
gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", ep, ep->peer_string.c_str(), |
| 232 |
dump); |
| 233 |
gpr_free(dump); |
| 234 |
grpc_core::CSliceUnref(trace_slice); |
| 235 |
} |
| 236 |
} |
| 237 |
grpc_core::CSliceUnref(slice); |
| 238 |
} |
| 239 |
|
| 240 |
static void CFStreamRead(grpc_endpoint* ep, grpc_slice_buffer* slices, |
| 241 |
grpc_closure* cb, bool urgent, |
| 242 |
int /*min_progress_size*/) { |
| 243 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 244 |
if (grpc_tcp_trace.enabled()) { |
| 245 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p read (%p, %p) length:%zu", ep_impl, |
| 246 |
slices, cb, slices->length); |
| 247 |
} |
| 248 |
GPR_ASSERT(ep_impl->read_cb == nullptr); |
| 249 |
ep_impl->read_cb = cb; |
| 250 |
ep_impl->read_slices = slices; |
| 251 |
grpc_slice_buffer_reset_and_unref(slices); |
| 252 |
grpc_slice_buffer_add_indexed( |
| 253 |
slices, GRPC_SLICE_MALLOC(GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); |
| 254 |
EP_REF(ep_impl, "read"); |
| 255 |
ep_impl->stream_sync->NotifyOnRead(&ep_impl->read_action); |
| 256 |
} |
| 257 |
|
| 258 |
static void CFStreamWrite(grpc_endpoint* ep, grpc_slice_buffer* slices, |
| 259 |
grpc_closure* cb, void* arg, int /*max_frame_size*/) { |
| 260 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 261 |
if (grpc_tcp_trace.enabled()) { |
| 262 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p write (%p, %p) length:%zu", |
| 263 |
ep_impl, slices, cb, slices->length); |
| 264 |
} |
| 265 |
GPR_ASSERT(ep_impl->write_cb == nullptr); |
| 266 |
ep_impl->write_cb = cb; |
| 267 |
ep_impl->write_slices = slices; |
| 268 |
EP_REF(ep_impl, "write"); |
| 269 |
ep_impl->stream_sync->NotifyOnWrite(&ep_impl->write_action); |
| 270 |
} |
| 271 |
|
| 272 |
void CFStreamShutdown(grpc_endpoint* ep, grpc_error_handle why) { |
| 273 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 274 |
if (grpc_tcp_trace.enabled()) { |
| 275 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p shutdown (%s)", ep_impl, |
| 276 |
grpc_core::StatusToString(why).c_str()); |
| 277 |
} |
| 278 |
CFReadStreamClose(ep_impl->read_stream); |
| 279 |
CFWriteStreamClose(ep_impl->write_stream); |
| 280 |
ep_impl->stream_sync->Shutdown(why); |
| 281 |
if (grpc_tcp_trace.enabled()) { |
| 282 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p shutdown DONE (%s)", ep_impl, |
| 283 |
grpc_core::StatusToString(why).c_str()); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
void CFStreamDestroy(grpc_endpoint* ep) { |
| 288 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 289 |
if (grpc_tcp_trace.enabled()) { |
| 290 |
gpr_log(GPR_DEBUG, "CFStream endpoint:%p destroy", ep_impl); |
| 291 |
} |
| 292 |
EP_UNREF(ep_impl, "destroy"); |
| 293 |
} |
| 294 |
|
| 295 |
absl::string_view CFStreamGetPeer(grpc_endpoint* ep) { |
| 296 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 297 |
return ep_impl->peer_string; |
| 298 |
} |
| 299 |
|
| 300 |
absl::string_view CFStreamGetLocalAddress(grpc_endpoint* ep) { |
| 301 |
CFStreamEndpoint* ep_impl = reinterpret_cast<CFStreamEndpoint*>(ep); |
| 302 |
return ep_impl->local_address; |
| 303 |
} |
| 304 |
|
| 305 |
int CFStreamGetFD(grpc_endpoint* ep) { return 0; } |
| 306 |
|
| 307 |
bool CFStreamCanTrackErr(grpc_endpoint* ep) { return false; } |
| 308 |
|
| 309 |
void CFStreamAddToPollset(grpc_endpoint* ep, grpc_pollset* pollset) {} |
| 310 |
void CFStreamAddToPollsetSet(grpc_endpoint* ep, grpc_pollset_set* pollset) {} |
| 311 |
void CFStreamDeleteFromPollsetSet(grpc_endpoint* ep, |
| 312 |
grpc_pollset_set* pollset) {} |
| 313 |
|
| 314 |
static const grpc_endpoint_vtable vtable = {CFStreamRead, |
| 315 |
CFStreamWrite, |
| 316 |
CFStreamAddToPollset, |
| 317 |
CFStreamAddToPollsetSet, |
| 318 |
CFStreamDeleteFromPollsetSet, |
| 319 |
CFStreamShutdown, |
| 320 |
CFStreamDestroy, |
| 321 |
CFStreamGetPeer, |
| 322 |
CFStreamGetLocalAddress, |
| 323 |
CFStreamGetFD, |
| 324 |
CFStreamCanTrackErr}; |
| 325 |
|
| 326 |
grpc_endpoint* grpc_cfstream_endpoint_create(CFReadStreamRef read_stream, |
| 327 |
CFWriteStreamRef write_stream, |
| 328 |
const char* peer_string, |
| 329 |
CFStreamHandle* stream_sync) { |
| 330 |
CFStreamEndpoint* ep_impl = new CFStreamEndpoint; |
| 331 |
if (grpc_tcp_trace.enabled()) { |
| 332 |
gpr_log(GPR_DEBUG, |
| 333 |
"CFStream endpoint:%p create readStream:%p writeStream: %p", |
| 334 |
ep_impl, read_stream, write_stream); |
| 335 |
} |
| 336 |
ep_impl->base.vtable = &vtable; |
| 337 |
gpr_ref_init(&ep_impl->refcount, 1); |
| 338 |
ep_impl->read_stream = read_stream; |
| 339 |
ep_impl->write_stream = write_stream; |
| 340 |
CFRetain(read_stream); |
| 341 |
CFRetain(write_stream); |
| 342 |
ep_impl->stream_sync = stream_sync; |
| 343 |
CFSTREAM_HANDLE_REF(ep_impl->stream_sync, "endpoint create"); |
| 344 |
|
| 345 |
ep_impl->peer_string = peer_string; |
| 346 |
grpc_resolved_address resolved_local_addr; |
| 347 |
resolved_local_addr.len = sizeof(resolved_local_addr.addr); |
| 348 |
CFDataRef native_handle = static_cast<CFDataRef>(CFReadStreamCopyProperty( |
| 349 |
ep_impl->read_stream, kCFStreamPropertySocketNativeHandle)); |
| 350 |
CFSocketNativeHandle sockfd; |
| 351 |
CFDataGetBytes(native_handle, CFRangeMake(0, sizeof(CFSocketNativeHandle)), |
| 352 |
(UInt8*)&sockfd); |
| 353 |
if (native_handle) { |
| 354 |
CFRelease(native_handle); |
| 355 |
} |
| 356 |
absl::StatusOr<std::string> addr_uri; |
| 357 |
if (getsockname(sockfd, reinterpret_cast<sockaddr*>(resolved_local_addr.addr), |
| 358 |
&resolved_local_addr.len) < 0 || |
| 359 |
!(addr_uri = grpc_sockaddr_to_uri(&resolved_local_addr)).ok()) { |
| 360 |
ep_impl->local_address = ""; |
| 361 |
} else { |
| 362 |
ep_impl->local_address = addr_uri.value(); |
| 363 |
} |
| 364 |
ep_impl->read_cb = nil; |
| 365 |
ep_impl->write_cb = nil; |
| 366 |
ep_impl->read_slices = nil; |
| 367 |
ep_impl->write_slices = nil; |
| 368 |
GRPC_CLOSURE_INIT(&ep_impl->read_action, ReadAction, |
| 369 |
static_cast<void*>(ep_impl), grpc_schedule_on_exec_ctx); |
| 370 |
GRPC_CLOSURE_INIT(&ep_impl->write_action, WriteAction, |
| 371 |
static_cast<void*>(ep_impl), grpc_schedule_on_exec_ctx); |
| 372 |
|
| 373 |
return &ep_impl->base; |
| 374 |
} |
| 375 |
|
| 376 |
#endif /* GRPC_CFSTREAM_ENDPOINT */ |