• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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 <grpc/grpc.h>
22 
23 #include <string.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28 
29 #include "src/core/ext/transport/chttp2/server/chttp2_server.h"
30 
31 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/channel/handshaker.h"
34 #include "src/core/lib/security/context/security_context.h"
35 #include "src/core/lib/security/credentials/credentials.h"
36 #include "src/core/lib/surface/api_trace.h"
37 #include "src/core/lib/surface/server.h"
38 
grpc_server_add_secure_http2_port(grpc_server * server,const char * addr,grpc_server_credentials * creds)39 int grpc_server_add_secure_http2_port(grpc_server* server, const char* addr,
40                                       grpc_server_credentials* creds) {
41   grpc_core::ExecCtx exec_ctx;
42   grpc_error* err = GRPC_ERROR_NONE;
43   grpc_server_security_connector* sc = nullptr;
44   int port_num = 0;
45   grpc_security_status status;
46   grpc_channel_args* args = nullptr;
47   GRPC_API_TRACE(
48       "grpc_server_add_secure_http2_port("
49       "server=%p, addr=%s, creds=%p)",
50       3, (server, addr, creds));
51   // Create security context.
52   if (creds == nullptr) {
53     err = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
54         "No credentials specified for secure server port (creds==NULL)");
55     goto done;
56   }
57   status = grpc_server_credentials_create_security_connector(creds, &sc);
58   if (status != GRPC_SECURITY_OK) {
59     char* msg;
60     gpr_asprintf(&msg,
61                  "Unable to create secure server with credentials of type %s.",
62                  creds->type);
63     err = grpc_error_set_int(GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg),
64                              GRPC_ERROR_INT_SECURITY_STATUS, status);
65     gpr_free(msg);
66     goto done;
67   }
68   // Create channel args.
69   grpc_arg args_to_add[2];
70   args_to_add[0] = grpc_server_credentials_to_arg(creds);
71   args_to_add[1] = grpc_security_connector_to_arg(&sc->base);
72   args =
73       grpc_channel_args_copy_and_add(grpc_server_get_channel_args(server),
74                                      args_to_add, GPR_ARRAY_SIZE(args_to_add));
75   // Add server port.
76   err = grpc_chttp2_server_add_port(server, addr, args, &port_num);
77 done:
78   if (sc != nullptr) {
79     GRPC_SECURITY_CONNECTOR_UNREF(&sc->base, "server");
80   }
81 
82   if (err != GRPC_ERROR_NONE) {
83     const char* msg = grpc_error_string(err);
84     gpr_log(GPR_ERROR, "%s", msg);
85 
86     GRPC_ERROR_UNREF(err);
87   }
88   return port_num;
89 }
90