aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/ext/filters/client_channel/proxy_mapper_registry.cc
blob: a02a5f5e2cdadc1ba452397769cf1369c0e1247b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *
 * Copyright 2017 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <grpc/support/port_platform.h>

#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"

#include <string.h>

#include <grpc/support/alloc.h>

//
// grpc_proxy_mapper_list
//

typedef struct {
  grpc_proxy_mapper** list;
  size_t num_mappers;
} grpc_proxy_mapper_list;

static void grpc_proxy_mapper_list_register(grpc_proxy_mapper_list* list,
                                            bool at_start,
                                            grpc_proxy_mapper* mapper) {
  list->list = static_cast<grpc_proxy_mapper**>(gpr_realloc(
      list->list, (list->num_mappers + 1) * sizeof(grpc_proxy_mapper*)));
  if (at_start) {
    memmove(list->list + 1, list->list,
            sizeof(grpc_proxy_mapper*) * list->num_mappers);
    list->list[0] = mapper;
  } else {
    list->list[list->num_mappers] = mapper;
  }
  ++list->num_mappers;
}

static bool grpc_proxy_mapper_list_map_name(grpc_proxy_mapper_list* list,
                                            const char* server_uri,
                                            const grpc_channel_args* args,
                                            char** name_to_resolve,
                                            grpc_channel_args** new_args) {
  for (size_t i = 0; i < list->num_mappers; ++i) {
    if (grpc_proxy_mapper_map_name(list->list[i], server_uri, args,
                                   name_to_resolve, new_args)) {
      return true;
    }
  }
  return false;
}

static bool grpc_proxy_mapper_list_map_address(
    grpc_proxy_mapper_list* list, const grpc_resolved_address* address,
    const grpc_channel_args* args, grpc_resolved_address** new_address,
    grpc_channel_args** new_args) {
  for (size_t i = 0; i < list->num_mappers; ++i) {
    if (grpc_proxy_mapper_map_address(list->list[i], address, args, new_address,
                                      new_args)) {
      return true;
    }
  }
  return false;
}

static void grpc_proxy_mapper_list_destroy(grpc_proxy_mapper_list* list) {
  for (size_t i = 0; i < list->num_mappers; ++i) {
    grpc_proxy_mapper_destroy(list->list[i]);
  }
  gpr_free(list->list);
  // Clean up in case we re-initialze later.
  // TODO(ctiller): This should ideally live in
  // grpc_proxy_mapper_registry_init().  However, if we did this there,
  // then we would do it AFTER we start registering proxy mappers from
  // third-party plugins, so they'd never show up (and would leak memory).
  // We probably need some sort of dependency system for plugins to fix
  // this.
  memset(list, 0, sizeof(*list));
}

//
// plugin
//

static grpc_proxy_mapper_list g_proxy_mapper_list;

void grpc_proxy_mapper_registry_init() {}

void grpc_proxy_mapper_registry_shutdown() {
  grpc_proxy_mapper_list_destroy(&g_proxy_mapper_list);
}

void grpc_proxy_mapper_register(bool at_start, grpc_proxy_mapper* mapper) {
  grpc_proxy_mapper_list_register(&g_proxy_mapper_list, at_start, mapper);
}

bool grpc_proxy_mappers_map_name(const char* server_uri,
                                 const grpc_channel_args* args,
                                 char** name_to_resolve,
                                 grpc_channel_args** new_args) {
  return grpc_proxy_mapper_list_map_name(&g_proxy_mapper_list, server_uri, args,
                                         name_to_resolve, new_args);
}
bool grpc_proxy_mappers_map_address(const grpc_resolved_address* address,
                                    const grpc_channel_args* args,
                                    grpc_resolved_address** new_address,
                                    grpc_channel_args** new_args) {
  return grpc_proxy_mapper_list_map_address(&g_proxy_mapper_list, address, args,
                                            new_address, new_args);
}