blob: 8b4abe24a6118f752ddc2cd0ff7c626ddb0e2e06 [file] [log] [blame]
David Garcia Quintas9885bff2016-04-07 17:31:29 -07001/*
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
48int 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
59int 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;
93done:
94 gpr_free(host);
95 gpr_free(port);
96 return result;
97}
98
99int 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;
133done:
134 gpr_free(host);
135 gpr_free(port);
136 return result;
137}