Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright (c) 2012 University of Oslo. All Rights Reserved. |
| 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/un.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stddef.h> |
| 31 | |
| 32 | #include <network/UnixSocket.h> |
| 33 | #include <rfb/LogWriter.h> |
| 34 | |
| 35 | using namespace network; |
| 36 | using namespace rdr; |
| 37 | |
| 38 | static rfb::LogWriter vlog("UnixSocket"); |
| 39 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 40 | // -=- UnixSocket |
| 41 | |
Pierre Ossman | 559e8b8 | 2018-05-04 12:44:31 +0200 | [diff] [blame^] | 42 | UnixSocket::UnixSocket(int sock) : Socket(sock) |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | UnixSocket::UnixSocket(const char *path) |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 47 | { |
| 48 | int sock, err, result; |
| 49 | sockaddr_un addr; |
| 50 | socklen_t salen; |
| 51 | |
| 52 | if (strlen(path) >= sizeof(addr.sun_path)) |
| 53 | throw SocketException("socket path is too long", ENAMETOOLONG); |
| 54 | |
| 55 | // - Create a socket |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 56 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 57 | if (sock == -1) |
| 58 | throw SocketException("unable to create socket", errno); |
| 59 | |
| 60 | // - Attempt to connect |
| 61 | memset(&addr, 0, sizeof(addr)); |
| 62 | addr.sun_family = AF_UNIX; |
| 63 | strcpy(addr.sun_path, path); |
| 64 | salen = sizeof(addr); |
| 65 | while ((result = connect(sock, (sockaddr *)&addr, salen)) == -1) { |
| 66 | err = errno; |
| 67 | close(sock); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | if (result == -1) |
| 72 | throw SocketException("unable connect to socket", err); |
| 73 | |
Pierre Ossman | 559e8b8 | 2018-05-04 12:44:31 +0200 | [diff] [blame^] | 74 | setFd(sock); |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | char* UnixSocket::getPeerAddress() { |
| 78 | struct sockaddr_un addr; |
| 79 | socklen_t salen; |
| 80 | |
| 81 | // AF_UNIX only has a single address (the server side). |
| 82 | // Unfortunately we don't know which end we are, so we'll have to |
| 83 | // test a bit. |
| 84 | |
| 85 | salen = sizeof(addr); |
| 86 | if (getpeername(getFd(), (struct sockaddr *)&addr, &salen) != 0) { |
| 87 | vlog.error("unable to get peer name for socket"); |
| 88 | return rfb::strDup(""); |
| 89 | } |
| 90 | |
| 91 | if (salen > offsetof(struct sockaddr_un, sun_path)) |
| 92 | return rfb::strDup(addr.sun_path); |
| 93 | |
| 94 | salen = sizeof(addr); |
| 95 | if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) != 0) { |
| 96 | vlog.error("unable to get local name for socket"); |
| 97 | return rfb::strDup(""); |
| 98 | } |
| 99 | |
| 100 | if (salen > offsetof(struct sockaddr_un, sun_path)) |
| 101 | return rfb::strDup(addr.sun_path); |
| 102 | |
| 103 | // socketpair() will create unnamed sockets |
| 104 | |
| 105 | return rfb::strDup("(unnamed UNIX socket)"); |
| 106 | } |
| 107 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 108 | char* UnixSocket::getPeerEndpoint() { |
| 109 | return getPeerAddress(); |
| 110 | } |
| 111 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 112 | bool UnixSocket::cork(bool enable) |
| 113 | { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | UnixListener::UnixListener(const char *path, int mode) |
| 118 | { |
| 119 | struct sockaddr_un addr; |
| 120 | mode_t saved_umask; |
| 121 | int err, result; |
| 122 | |
| 123 | if (strlen(path) >= sizeof(addr.sun_path)) |
| 124 | throw SocketException("socket path is too long", ENAMETOOLONG); |
| 125 | |
| 126 | // - Create a socket |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 127 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 128 | throw SocketException("unable to create listening socket", errno); |
| 129 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 130 | // - Delete existing socket (ignore result) |
| 131 | unlink(path); |
| 132 | |
| 133 | // - Attempt to bind to the requested path |
| 134 | memset(&addr, 0, sizeof(addr)); |
| 135 | addr.sun_family = AF_UNIX; |
| 136 | strcpy(addr.sun_path, path); |
| 137 | saved_umask = umask(0777); |
| 138 | result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 139 | err = errno; |
| 140 | umask(saved_umask); |
| 141 | if (result < 0) { |
| 142 | close(fd); |
| 143 | throw SocketException("unable to bind listening socket", err); |
| 144 | } |
| 145 | |
| 146 | // - Set socket mode |
| 147 | if (chmod(path, mode) < 0) { |
| 148 | err = errno; |
| 149 | close(fd); |
| 150 | throw SocketException("unable to set socket mode", err); |
| 151 | } |
| 152 | |
Pierre Ossman | 559e8b8 | 2018-05-04 12:44:31 +0200 | [diff] [blame^] | 153 | listen(fd); |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 156 | UnixListener::~UnixListener() |
| 157 | { |
| 158 | struct sockaddr_un addr; |
| 159 | socklen_t salen = sizeof(addr); |
| 160 | |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 161 | if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) == 0) |
| 162 | unlink(addr.sun_path); |
| 163 | } |
| 164 | |
Pierre Ossman | 559e8b8 | 2018-05-04 12:44:31 +0200 | [diff] [blame^] | 165 | Socket* UnixListener::createSocket(int fd) { |
| 166 | return new UnixSocket(fd); |
Pierre Ossman | 5d05546 | 2018-05-03 14:04:38 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int UnixListener::getMyPort() { |
| 170 | return 0; |
| 171 | } |