• 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 "src/core/lib/gpr/host_port.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/lib/gpr/string.h"
30 
gpr_join_host_port(char ** out,const char * host,int port)31 int gpr_join_host_port(char** out, const char* host, int port) {
32   if (host[0] != '[' && strchr(host, ':') != nullptr) {
33     /* IPv6 literals must be enclosed in brackets. */
34     return gpr_asprintf(out, "[%s]:%d", host, port);
35   } else {
36     /* Ordinary non-bracketed host:port. */
37     return gpr_asprintf(out, "%s:%d", host, port);
38   }
39 }
40 
gpr_split_host_port(const char * name,char ** host,char ** port)41 int gpr_split_host_port(const char* name, char** host, char** port) {
42   const char* host_start;
43   size_t host_len;
44   const char* port_start;
45 
46   *host = nullptr;
47   *port = nullptr;
48 
49   if (name[0] == '[') {
50     /* Parse a bracketed host, typically an IPv6 literal. */
51     const char* rbracket = strchr(name, ']');
52     if (rbracket == nullptr) {
53       /* Unmatched [ */
54       return 0;
55     }
56     if (rbracket[1] == '\0') {
57       /* ]<end> */
58       port_start = nullptr;
59     } else if (rbracket[1] == ':') {
60       /* ]:<port?> */
61       port_start = rbracket + 2;
62     } else {
63       /* ]<invalid> */
64       return 0;
65     }
66     host_start = name + 1;
67     host_len = static_cast<size_t>(rbracket - host_start);
68     if (memchr(host_start, ':', host_len) == nullptr) {
69       /* Require all bracketed hosts to contain a colon, because a hostname or
70          IPv4 address should never use brackets. */
71       return 0;
72     }
73   } else {
74     const char* colon = strchr(name, ':');
75     if (colon != nullptr && strchr(colon + 1, ':') == nullptr) {
76       /* Exactly 1 colon.  Split into host:port. */
77       host_start = name;
78       host_len = static_cast<size_t>(colon - name);
79       port_start = colon + 1;
80     } else {
81       /* 0 or 2+ colons.  Bare hostname or IPv6 litearal. */
82       host_start = name;
83       host_len = strlen(name);
84       port_start = nullptr;
85     }
86   }
87 
88   /* Allocate return values. */
89   *host = static_cast<char*>(gpr_malloc(host_len + 1));
90   memcpy(*host, host_start, host_len);
91   (*host)[host_len] = '\0';
92 
93   if (port_start != nullptr) {
94     *port = gpr_strdup(port_start);
95   }
96 
97   return 1;
98 }
99