blob: 8ff474c6d4b74bea061abbaf5436a2c8508b4900 [file] [log] [blame]
ahedberg80d6b122016-03-17 17:37:35 -04001/*
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#include "src/core/iomgr/unix_sockets_posix.h"
35
36#ifdef GPR_HAVE_UNIX_SOCKET
37
ahedberg799fd4b2016-03-22 13:47:46 -040038#include <string.h>
ahedberg80d6b122016-03-17 17:37:35 -040039#include <sys/types.h>
ahedberg43df2952016-03-18 10:46:38 -040040#include <sys/stat.h>
ahedberg80d6b122016-03-17 17:37:35 -040041#include <sys/un.h>
42
43#include <grpc/support/alloc.h>
44
45void grpc_create_socketpair_if_unix(int sv[2]) {
46 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
47}
48
49grpc_resolved_addresses *grpc_resolve_unix_domain_address(const char* name) {
50 struct sockaddr_un *un;
51
ahedberg43df2952016-03-18 10:46:38 -040052 grpc_resolved_addresses *addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
ahedberg80d6b122016-03-17 17:37:35 -040053 addrs->naddrs = 1;
54 addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address));
55 un = (struct sockaddr_un *)addrs->addrs->addr;
56 un->sun_family = AF_UNIX;
57 strcpy(un->sun_path, name);
58 addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
59 return addrs;
60}
61
ahedberg43df2952016-03-18 10:46:38 -040062int grpc_is_unix_socket(const struct sockaddr *addr) {
63 return addr->sa_family == AF_UNIX;
ahedberg80d6b122016-03-17 17:37:35 -040064}
65
ahedbergfcbcbff2016-03-22 13:23:47 -040066void grpc_unlink_if_unix_domain_socket(const struct sockaddr *addr) {
ahedberg80d6b122016-03-17 17:37:35 -040067 if (addr->sa_family != AF_UNIX) {
68 return;
69 }
70 struct sockaddr_un *un = (struct sockaddr_un *)addr;
71 struct stat st;
72
73 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
74 unlink(un->sun_path);
75 }
76}
77
ahedbergfcbcbff2016-03-22 13:23:47 -040078int grpc_parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, size_t *len) {
ahedberg80d6b122016-03-17 17:37:35 -040079 struct sockaddr_un *un = (struct sockaddr_un *)addr;
80
81 un->sun_family = AF_UNIX;
82 strcpy(un->sun_path, uri->path);
83 *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
84
85 return 1;
86}
87
ahedbergfcbcbff2016-03-22 13:23:47 -040088char *grpc_unix_get_default_authority(grpc_resolver_factory *factory,
89 grpc_uri *uri) {
ahedberg80d6b122016-03-17 17:37:35 -040090 return gpr_strdup("localhost");
91}
92
ahedberg43df2952016-03-18 10:46:38 -040093char *grpc_sockaddr_to_uri_unix_if_possible(const struct sockaddr *addr) {
ahedberg80d6b122016-03-17 17:37:35 -040094 if (addr->sa_family != AF_UNIX) {
95 return NULL;
96 }
97
98 char* result;
99 gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path);
100 return result;
101}
102
103#endif