blob: f464ac85271eaac2bed3feef97d5de95636f39cf [file] [log] [blame]
Pierre Ossman5d055462018-05-03 14:04:38 +02001/* 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
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/socket.h>
27#include <sys/un.h>
28#include <unistd.h>
29#include <errno.h>
30#include <string.h>
31#include <signal.h>
32#include <fcntl.h>
33#include <stdlib.h>
34#include <stddef.h>
35
36#include <network/UnixSocket.h>
37#include <rfb/LogWriter.h>
38
39using namespace network;
40using namespace rdr;
41
42static rfb::LogWriter vlog("UnixSocket");
43
44// -=- Socket initialisation
45static bool socketsInitialised = false;
46static void initSockets() {
47 if (socketsInitialised)
48 return;
49 signal(SIGPIPE, SIG_IGN);
50 socketsInitialised = true;
51}
52
53
54// -=- UnixSocket
55
56UnixSocket::UnixSocket(int sock, bool close)
57 : Socket(new FdInStream(sock), new FdOutStream(sock), true), closeFd(close)
58{
59}
60
61UnixSocket::UnixSocket(const char *path)
62 : closeFd(true)
63{
64 int sock, err, result;
65 sockaddr_un addr;
66 socklen_t salen;
67
68 if (strlen(path) >= sizeof(addr.sun_path))
69 throw SocketException("socket path is too long", ENAMETOOLONG);
70
71 // - Create a socket
72 initSockets();
73 sock = socket(AF_UNIX, SOCK_STREAM, 0);
74 if (sock == -1)
75 throw SocketException("unable to create socket", errno);
76
77 // - Attempt to connect
78 memset(&addr, 0, sizeof(addr));
79 addr.sun_family = AF_UNIX;
80 strcpy(addr.sun_path, path);
81 salen = sizeof(addr);
82 while ((result = connect(sock, (sockaddr *)&addr, salen)) == -1) {
83 err = errno;
84 close(sock);
85 break;
86 }
87
88 if (result == -1)
89 throw SocketException("unable connect to socket", err);
90
91 // - By default, close the socket on exec()
92 fcntl(sock, F_SETFD, FD_CLOEXEC);
93
94 // Create the input and output streams
95 instream = new FdInStream(sock);
96 outstream = new FdOutStream(sock);
97 ownStreams = true;
98}
99
100UnixSocket::~UnixSocket() {
101 if (closeFd)
102 close(getFd());
103}
104
105int UnixSocket::getMyPort() {
106 return 0;
107}
108
109char* UnixSocket::getPeerAddress() {
110 struct sockaddr_un addr;
111 socklen_t salen;
112
113 // AF_UNIX only has a single address (the server side).
114 // Unfortunately we don't know which end we are, so we'll have to
115 // test a bit.
116
117 salen = sizeof(addr);
118 if (getpeername(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
119 vlog.error("unable to get peer name for socket");
120 return rfb::strDup("");
121 }
122
123 if (salen > offsetof(struct sockaddr_un, sun_path))
124 return rfb::strDup(addr.sun_path);
125
126 salen = sizeof(addr);
127 if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
128 vlog.error("unable to get local name for socket");
129 return rfb::strDup("");
130 }
131
132 if (salen > offsetof(struct sockaddr_un, sun_path))
133 return rfb::strDup(addr.sun_path);
134
135 // socketpair() will create unnamed sockets
136
137 return rfb::strDup("(unnamed UNIX socket)");
138}
139
140int UnixSocket::getPeerPort() {
141 return 0;
142}
143
144char* UnixSocket::getPeerEndpoint() {
145 return getPeerAddress();
146}
147
148bool UnixSocket::sameMachine() {
149 return true;
150}
151
152void UnixSocket::shutdown()
153{
154 Socket::shutdown();
155 ::shutdown(getFd(), 2);
156}
157
158bool UnixSocket::cork(bool enable)
159{
160 return true;
161}
162
163UnixListener::UnixListener(const char *path, int mode)
164{
165 struct sockaddr_un addr;
166 mode_t saved_umask;
167 int err, result;
168
169 if (strlen(path) >= sizeof(addr.sun_path))
170 throw SocketException("socket path is too long", ENAMETOOLONG);
171
172 // - Create a socket
173 initSockets();
174 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
175 throw SocketException("unable to create listening socket", errno);
176
177 // - By default, close the socket on exec()
178 fcntl(fd, F_SETFD, FD_CLOEXEC);
179
180 // - Delete existing socket (ignore result)
181 unlink(path);
182
183 // - Attempt to bind to the requested path
184 memset(&addr, 0, sizeof(addr));
185 addr.sun_family = AF_UNIX;
186 strcpy(addr.sun_path, path);
187 saved_umask = umask(0777);
188 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
189 err = errno;
190 umask(saved_umask);
191 if (result < 0) {
192 close(fd);
193 throw SocketException("unable to bind listening socket", err);
194 }
195
196 // - Set socket mode
197 if (chmod(path, mode) < 0) {
198 err = errno;
199 close(fd);
200 throw SocketException("unable to set socket mode", err);
201 }
202
203 // - Set it to be a listening socket
204 if (listen(fd, 5) < 0) {
205 err = errno;
206 close(fd);
207 throw SocketException("unable to set socket to listening mode", err);
208 }
209}
210
211UnixListener::UnixListener(int sock)
212{
213 fd = sock;
214}
215
216UnixListener::~UnixListener()
217{
218 struct sockaddr_un addr;
219 socklen_t salen = sizeof(addr);
220
221 close(fd);
222
223 if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) == 0)
224 unlink(addr.sun_path);
225}
226
227void UnixListener::shutdown()
228{
229 ::shutdown(getFd(), 2);
230}
231
232
233Socket*
234UnixListener::accept() {
235 int new_sock = -1;
236
237 // Accept an incoming connection
238 if ((new_sock = ::accept(fd, 0, 0)) < 0)
239 throw SocketException("unable to accept new connection", errno);
240
241 // - By default, close the socket on exec()
242 fcntl(new_sock, F_SETFD, FD_CLOEXEC);
243
244 // - Create the socket object
245 return new UnixSocket(new_sock);
246}
247
248int UnixListener::getMyPort() {
249 return 0;
250}