static void on_connected(void *arg, grpc_endpoint *tcp) { internal_request *req = arg; if (!tcp) { next_address(req); return; } req->ep = tcp; if (req->use_ssl) { grpc_channel_security_connector *sc = NULL; const unsigned char *pem_root_certs = NULL; size_t pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs); if (pem_root_certs == NULL || pem_root_certs_size == 0) { gpr_log(GPR_ERROR, "Could not get default pem root certs."); finish(req, 0); return; } GPR_ASSERT(grpc_httpcli_ssl_channel_security_connector_create( pem_root_certs, pem_root_certs_size, req->host, &sc) == GRPC_SECURITY_OK); grpc_setup_secure_transport(&sc->base, tcp, on_secure_transport_setup_done, req); grpc_security_connector_unref(&sc->base); } else { start_write(req); } }
static void state_unref(grpc_server_secure_state *state) { if (gpr_unref(&state->refcount)) { /* ensure all threads have unlocked */ gpr_mu_lock(&state->mu); gpr_mu_unlock(&state->mu); /* clean up */ grpc_security_connector_unref(state->sc); gpr_free(state); } }
static void secure_transport_setup_done(grpc_secure_transport_setup *s, int is_success) { if (is_success) { s->cb(s->user_data, GRPC_SECURITY_OK, s->endpoint); } else { if (s->endpoint != NULL) { grpc_endpoint_shutdown(s->endpoint); grpc_endpoint_destroy(s->endpoint); } s->cb(s->user_data, GRPC_SECURITY_ERROR, NULL); } if (s->handshaker != NULL) tsi_handshaker_destroy(s->handshaker); if (s->handshake_buffer != NULL) gpr_free(s->handshake_buffer); gpr_slice_buffer_destroy(&s->left_overs); grpc_security_connector_unref(s->connector); gpr_free(s); }
static void connector_pointer_arg_destroy(void *p) { grpc_security_connector_unref(p); }
int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, grpc_server_credentials *creds) { grpc_resolved_addresses *resolved = NULL; grpc_tcp_server *tcp = NULL; grpc_server_secure_state *state = NULL; size_t i; unsigned count = 0; int port_num = -1; int port_temp; grpc_security_status status = GRPC_SECURITY_ERROR; grpc_security_connector *sc = NULL; /* create security context */ if (creds == NULL) goto error; status = grpc_server_credentials_create_security_connector(creds, &sc); if (status != GRPC_SECURITY_OK) { gpr_log(GPR_ERROR, "Unable to create secure server with credentials of type %s.", creds->type); goto error; } /* resolve address */ resolved = grpc_blocking_resolve_address(addr, "https"); if (!resolved) { goto error; } tcp = grpc_tcp_server_create(); if (!tcp) { goto error; } for (i = 0; i < resolved->naddrs; i++) { port_temp = grpc_tcp_server_add_port( tcp, (struct sockaddr *)&resolved->addrs[i].addr, resolved->addrs[i].len); if (port_temp >= 0) { if (port_num == -1) { port_num = port_temp; } else { GPR_ASSERT(port_num == port_temp); } count++; } } if (count == 0) { gpr_log(GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs); goto error; } if (count != resolved->naddrs) { gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved", count, resolved->naddrs); /* if it's an error, don't we want to goto error; here ? */ } grpc_resolved_addresses_destroy(resolved); state = gpr_malloc(sizeof(*state)); state->server = server; state->tcp = tcp; state->sc = sc; state->is_shutdown = 0; gpr_mu_init(&state->mu); gpr_ref_init(&state->refcount, 1); /* Register with the server only upon success */ grpc_server_add_listener(server, state, start, destroy); return port_num; /* Error path: cleanup and return */ error: if (sc) { grpc_security_connector_unref(sc); } if (resolved) { grpc_resolved_addresses_destroy(resolved); } if (tcp) { grpc_tcp_server_destroy(tcp, NULL, NULL); } if (state) { gpr_free(state); } return 0; }