Skip to content

Commit faaeab0

Browse files
committed
Merge pull request grpc#6124 from dgquintas/ip_parse_refactor
Factored out parse functions from sockaddr_resolver
2 parents d51d9ae + ff71c38 commit faaeab0

File tree

18 files changed

+237
-97
lines changed

18 files changed

+237
-97
lines changed

BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ cc_library(
275275
"src/core/ext/client_config/lb_policy.h",
276276
"src/core/ext/client_config/lb_policy_factory.h",
277277
"src/core/ext/client_config/lb_policy_registry.h",
278+
"src/core/ext/client_config/parse_address.h",
278279
"src/core/ext/client_config/resolver.h",
279280
"src/core/ext/client_config/resolver_factory.h",
280281
"src/core/ext/client_config/resolver_registry.h",
@@ -426,6 +427,7 @@ cc_library(
426427
"src/core/ext/client_config/lb_policy.c",
427428
"src/core/ext/client_config/lb_policy_factory.c",
428429
"src/core/ext/client_config/lb_policy_registry.c",
430+
"src/core/ext/client_config/parse_address.c",
429431
"src/core/ext/client_config/resolver.c",
430432
"src/core/ext/client_config/resolver_factory.c",
431433
"src/core/ext/client_config/resolver_registry.c",
@@ -603,6 +605,7 @@ cc_library(
603605
"src/core/ext/client_config/lb_policy.h",
604606
"src/core/ext/client_config/lb_policy_factory.h",
605607
"src/core/ext/client_config/lb_policy_registry.h",
608+
"src/core/ext/client_config/parse_address.h",
606609
"src/core/ext/client_config/resolver.h",
607610
"src/core/ext/client_config/resolver_factory.h",
608611
"src/core/ext/client_config/resolver_registry.h",
@@ -736,6 +739,7 @@ cc_library(
736739
"src/core/ext/client_config/lb_policy.c",
737740
"src/core/ext/client_config/lb_policy_factory.c",
738741
"src/core/ext/client_config/lb_policy_registry.c",
742+
"src/core/ext/client_config/parse_address.c",
739743
"src/core/ext/client_config/resolver.c",
740744
"src/core/ext/client_config/resolver_factory.c",
741745
"src/core/ext/client_config/resolver_registry.c",
@@ -1434,6 +1438,7 @@ objc_library(
14341438
"src/core/ext/client_config/lb_policy.c",
14351439
"src/core/ext/client_config/lb_policy_factory.c",
14361440
"src/core/ext/client_config/lb_policy_registry.c",
1441+
"src/core/ext/client_config/parse_address.c",
14371442
"src/core/ext/client_config/resolver.c",
14381443
"src/core/ext/client_config/resolver_factory.c",
14391444
"src/core/ext/client_config/resolver_registry.c",
@@ -1604,6 +1609,7 @@ objc_library(
16041609
"src/core/ext/client_config/lb_policy.h",
16051610
"src/core/ext/client_config/lb_policy_factory.h",
16061611
"src/core/ext/client_config/lb_policy_registry.h",
1612+
"src/core/ext/client_config/parse_address.h",
16071613
"src/core/ext/client_config/resolver.h",
16081614
"src/core/ext/client_config/resolver_factory.h",
16091615
"src/core/ext/client_config/resolver_registry.h",

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,7 @@ LIBGRPC_SRC = \
25832583
src/core/ext/client_config/lb_policy.c \
25842584
src/core/ext/client_config/lb_policy_factory.c \
25852585
src/core/ext/client_config/lb_policy_registry.c \
2586+
src/core/ext/client_config/parse_address.c \
25862587
src/core/ext/client_config/resolver.c \
25872588
src/core/ext/client_config/resolver_factory.c \
25882589
src/core/ext/client_config/resolver_registry.c \
@@ -2900,6 +2901,7 @@ LIBGRPC_UNSECURE_SRC = \
29002901
src/core/ext/client_config/lb_policy.c \
29012902
src/core/ext/client_config/lb_policy_factory.c \
29022903
src/core/ext/client_config/lb_policy_registry.c \
2904+
src/core/ext/client_config/parse_address.c \
29032905
src/core/ext/client_config/resolver.c \
29042906
src/core/ext/client_config/resolver_factory.c \
29052907
src/core/ext/client_config/resolver_registry.c \

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@
694694
'src/core/ext/client_config/lb_policy.c',
695695
'src/core/ext/client_config/lb_policy_factory.c',
696696
'src/core/ext/client_config/lb_policy_registry.c',
697+
'src/core/ext/client_config/parse_address.c',
697698
'src/core/ext/client_config/resolver.c',
698699
'src/core/ext/client_config/resolver_factory.c',
699700
'src/core/ext/client_config/resolver_registry.c',

build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ filegroups:
442442
- src/core/ext/client_config/lb_policy.h
443443
- src/core/ext/client_config/lb_policy_factory.h
444444
- src/core/ext/client_config/lb_policy_registry.h
445+
- src/core/ext/client_config/parse_address.h
445446
- src/core/ext/client_config/resolver.h
446447
- src/core/ext/client_config/resolver_factory.h
447448
- src/core/ext/client_config/resolver_registry.h
@@ -461,6 +462,7 @@ filegroups:
461462
- src/core/ext/client_config/lb_policy.c
462463
- src/core/ext/client_config/lb_policy_factory.c
463464
- src/core/ext/client_config/lb_policy_registry.c
465+
- src/core/ext/client_config/parse_address.c
464466
- src/core/ext/client_config/resolver.c
465467
- src/core/ext/client_config/resolver_factory.c
466468
- src/core/ext/client_config/resolver_registry.c

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ if test "$PHP_GRPC" != "no"; then
216216
src/core/ext/client_config/lb_policy.c \
217217
src/core/ext/client_config/lb_policy_factory.c \
218218
src/core/ext/client_config/lb_policy_registry.c \
219+
src/core/ext/client_config/parse_address.c \
219220
src/core/ext/client_config/resolver.c \
220221
src/core/ext/client_config/resolver_factory.c \
221222
src/core/ext/client_config/resolver_registry.c \

gRPC.podspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Pod::Spec.new do |s|
277277
'src/core/ext/client_config/lb_policy.h',
278278
'src/core/ext/client_config/lb_policy_factory.h',
279279
'src/core/ext/client_config/lb_policy_registry.h',
280+
'src/core/ext/client_config/parse_address.h',
280281
'src/core/ext/client_config/resolver.h',
281282
'src/core/ext/client_config/resolver_factory.h',
282283
'src/core/ext/client_config/resolver_registry.h',
@@ -459,6 +460,7 @@ Pod::Spec.new do |s|
459460
'src/core/ext/client_config/lb_policy.c',
460461
'src/core/ext/client_config/lb_policy_factory.c',
461462
'src/core/ext/client_config/lb_policy_registry.c',
463+
'src/core/ext/client_config/parse_address.c',
462464
'src/core/ext/client_config/resolver.c',
463465
'src/core/ext/client_config/resolver_factory.c',
464466
'src/core/ext/client_config/resolver_registry.c',
@@ -616,6 +618,7 @@ Pod::Spec.new do |s|
616618
'src/core/ext/client_config/lb_policy.h',
617619
'src/core/ext/client_config/lb_policy_factory.h',
618620
'src/core/ext/client_config/lb_policy_registry.h',
621+
'src/core/ext/client_config/parse_address.h',
619622
'src/core/ext/client_config/resolver.h',
620623
'src/core/ext/client_config/resolver_factory.h',
621624
'src/core/ext/client_config/resolver_registry.h',

grpc.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ Gem::Specification.new do |s|
288288
s.files += %w( src/core/ext/client_config/lb_policy.h )
289289
s.files += %w( src/core/ext/client_config/lb_policy_factory.h )
290290
s.files += %w( src/core/ext/client_config/lb_policy_registry.h )
291+
s.files += %w( src/core/ext/client_config/parse_address.h )
291292
s.files += %w( src/core/ext/client_config/resolver.h )
292293
s.files += %w( src/core/ext/client_config/resolver_factory.h )
293294
s.files += %w( src/core/ext/client_config/resolver_registry.h )
@@ -443,6 +444,7 @@ Gem::Specification.new do |s|
443444
s.files += %w( src/core/ext/client_config/lb_policy.c )
444445
s.files += %w( src/core/ext/client_config/lb_policy_factory.c )
445446
s.files += %w( src/core/ext/client_config/lb_policy_registry.c )
447+
s.files += %w( src/core/ext/client_config/parse_address.c )
446448
s.files += %w( src/core/ext/client_config/resolver.c )
447449
s.files += %w( src/core/ext/client_config/resolver_factory.c )
448450
s.files += %w( src/core/ext/client_config/resolver_registry.c )

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy.h" role="src" />
292292
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy_factory.h" role="src" />
293293
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy_registry.h" role="src" />
294+
<file baseinstalldir="/" name="src/core/ext/client_config/parse_address.h" role="src" />
294295
<file baseinstalldir="/" name="src/core/ext/client_config/resolver.h" role="src" />
295296
<file baseinstalldir="/" name="src/core/ext/client_config/resolver_factory.h" role="src" />
296297
<file baseinstalldir="/" name="src/core/ext/client_config/resolver_registry.h" role="src" />
@@ -446,6 +447,7 @@
446447
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy.c" role="src" />
447448
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy_factory.c" role="src" />
448449
<file baseinstalldir="/" name="src/core/ext/client_config/lb_policy_registry.c" role="src" />
450+
<file baseinstalldir="/" name="src/core/ext/client_config/parse_address.c" role="src" />
449451
<file baseinstalldir="/" name="src/core/ext/client_config/resolver.c" role="src" />
450452
<file baseinstalldir="/" name="src/core/ext/client_config/resolver_factory.c" role="src" />
451453
<file baseinstalldir="/" name="src/core/ext/client_config/resolver_registry.c" role="src" />
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
*
3+
* Copyright 2016, Google Inc.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
*/
33+
34+
#include "src/core/ext/client_config/parse_address.h"
35+
36+
#include <stdio.h>
37+
#include <string.h>
38+
#ifdef GPR_HAVE_UNIX_SOCKET
39+
#include <sys/un.h>
40+
#endif
41+
42+
#include <grpc/support/alloc.h>
43+
#include <grpc/support/host_port.h>
44+
#include <grpc/support/log.h>
45+
#include <grpc/support/string_util.h>
46+
47+
#ifdef GPR_HAVE_UNIX_SOCKET
48+
int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) {
49+
struct sockaddr_un *un = (struct sockaddr_un *)addr;
50+
51+
un->sun_family = AF_UNIX;
52+
strcpy(un->sun_path, uri->path);
53+
*len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
54+
55+
return 1;
56+
}
57+
#endif
58+
59+
int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) {
60+
const char *host_port = uri->path;
61+
char *host;
62+
char *port;
63+
int port_num;
64+
int result = 0;
65+
struct sockaddr_in *in = (struct sockaddr_in *)addr;
66+
67+
if (*host_port == '/') ++host_port;
68+
if (!gpr_split_host_port(host_port, &host, &port)) {
69+
return 0;
70+
}
71+
72+
memset(in, 0, sizeof(*in));
73+
*len = sizeof(*in);
74+
in->sin_family = AF_INET;
75+
if (inet_pton(AF_INET, host, &in->sin_addr) == 0) {
76+
gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host);
77+
goto done;
78+
}
79+
80+
if (port != NULL) {
81+
if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 ||
82+
port_num > 65535) {
83+
gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port);
84+
goto done;
85+
}
86+
in->sin_port = htons((uint16_t)port_num);
87+
} else {
88+
gpr_log(GPR_ERROR, "no port given for ipv4 scheme");
89+
goto done;
90+
}
91+
92+
result = 1;
93+
done:
94+
gpr_free(host);
95+
gpr_free(port);
96+
return result;
97+
}
98+
99+
int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) {
100+
const char *host_port = uri->path;
101+
char *host;
102+
char *port;
103+
int port_num;
104+
int result = 0;
105+
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
106+
107+
if (*host_port == '/') ++host_port;
108+
if (!gpr_split_host_port(host_port, &host, &port)) {
109+
return 0;
110+
}
111+
112+
memset(in6, 0, sizeof(*in6));
113+
*len = sizeof(*in6);
114+
in6->sin6_family = AF_INET6;
115+
if (inet_pton(AF_INET6, host, &in6->sin6_addr) == 0) {
116+
gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host);
117+
goto done;
118+
}
119+
120+
if (port != NULL) {
121+
if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 ||
122+
port_num > 65535) {
123+
gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port);
124+
goto done;
125+
}
126+
in6->sin6_port = htons((uint16_t)port_num);
127+
} else {
128+
gpr_log(GPR_ERROR, "no port given for ipv6 scheme");
129+
goto done;
130+
}
131+
132+
result = 1;
133+
done:
134+
gpr_free(host);
135+
gpr_free(port);
136+
return result;
137+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
*
3+
* Copyright 2015, Google Inc.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following disclaimer
14+
* in the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Google Inc. nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
*/
33+
34+
#ifndef GRPC_CORE_EXT_CLIENT_CONFIG_PARSE_ADDRESS_H
35+
#define GRPC_CORE_EXT_CLIENT_CONFIG_PARSE_ADDRESS_H
36+
37+
#include <stddef.h>
38+
39+
#include "src/core/ext/client_config/uri_parser.h"
40+
#include "src/core/lib/iomgr/sockaddr.h"
41+
42+
#ifdef GPR_HAVE_UNIX_SOCKET
43+
/** Populate \a addr and \a len from \a uri, whose path is expected to contain a
44+
* unix socket path. Returns true upon success. */
45+
int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len);
46+
#endif
47+
48+
/** Populate /a addr and \a len from \a uri, whose path is expected to contain a
49+
* host:port pair. Returns true upon success. */
50+
int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len);
51+
52+
/** Populate /a addr and \a len from \a uri, whose path is expected to contain a
53+
* host:port pair. Returns true upon success. */
54+
int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len);
55+
56+
#endif /* GRPC_CORE_EXT_CLIENT_CONFIG_PARSE_ADDRESS_H */

0 commit comments

Comments
 (0)