ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame^] | 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 | #include "src/core/iomgr/unix_sockets_posix.h" |
| 35 | |
| 36 | #ifdef GPR_HAVE_UNIX_SOCKET |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/un.h> |
| 40 | |
| 41 | #include <grpc/support/alloc.h> |
| 42 | |
| 43 | void grpc_create_socketpair_if_unix(int sv[2]) { |
| 44 | GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
| 45 | } |
| 46 | |
| 47 | grpc_resolved_addresses *grpc_resolve_unix_domain_address(const char* name) { |
| 48 | struct sockaddr_un *un; |
| 49 | |
| 50 | addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
| 51 | addrs->naddrs = 1; |
| 52 | addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address)); |
| 53 | un = (struct sockaddr_un *)addrs->addrs->addr; |
| 54 | un->sun_family = AF_UNIX; |
| 55 | strcpy(un->sun_path, name); |
| 56 | addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
| 57 | return addrs; |
| 58 | } |
| 59 | |
| 60 | int grpc_is_unix_socket(sa_family_t addr_family) { |
| 61 | return addr_family == AF_UNIX; |
| 62 | } |
| 63 | |
| 64 | static void unlink_if_unix_domain_socket(const struct sockaddr *addr) { |
| 65 | if (addr->sa_family != AF_UNIX) { |
| 66 | return; |
| 67 | } |
| 68 | struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 69 | struct stat st; |
| 70 | |
| 71 | if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
| 72 | unlink(un->sun_path); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, |
| 77 | size_t *len) { |
| 78 | struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 79 | |
| 80 | un->sun_family = AF_UNIX; |
| 81 | strcpy(un->sun_path, uri->path); |
| 82 | *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
| 83 | |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | static char *unix_get_default_authority(grpc_resolver_factory *factory, |
| 88 | grpc_uri *uri) { |
| 89 | return gpr_strdup("localhost"); |
| 90 | } |
| 91 | |
| 92 | char *grpc_sockaddr_to_uri_unix_if_possible(struct sockaddr *addr) { |
| 93 | if (addr->sa_family != AF_UNIX) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | char* result; |
| 98 | gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | #endif |