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