blob: e6c669c0ab1974d293cf23884d58905e616d13a2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- RNBSocket.cpp -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Created by Greg Clayton on 12/12/07.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RNBSocket.h"
Greg Clayton95bf0fd2011-04-01 00:29:43 +000015#include <arpa/inet.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <errno.h>
17#include <fcntl.h>
Greg Clayton95bf0fd2011-04-01 00:29:43 +000018#include <netdb.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include <netinet/in.h>
20#include <netinet/tcp.h>
21#include <termios.h>
22#include "DNBLog.h"
23#include "DNBError.h"
24
Jason Molenda42999a42012-02-22 02:18:59 +000025#ifdef WITH_LOCKDOWN
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lockdown.h"
27#endif
28
29/* Once we have a RNBSocket object with a port # specified,
30 this function is called to wait for an incoming connection.
31 This function blocks while waiting for that connection. */
32
33rnb_err_t
Greg Clayton8b82f082011-04-12 05:54:46 +000034RNBSocket::Listen (in_port_t port, PortBoundCallback callback, const void *callback_baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035{
36 //DNBLogThreadedIf(LOG_RNB_COMM, "%8u RNBSocket::%s called", (uint32_t)m_timer.ElapsedMicroSeconds(true), __FUNCTION__);
37 // Disconnect without saving errno
38 Disconnect (false);
39
40 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +000041 int listen_fd = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
42 if (listen_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 err.SetError(errno, DNBError::POSIX);
44
45 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +000046 err.LogThreaded("::socket ( domain = AF_INET, type = SOCK_STREAM, protocol = IPPROTO_TCP ) => socket = %i", listen_fd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48 if (err.Fail())
49 return rnb_err;
50
51 // enable local address reuse
Greg Clayton8b82f082011-04-12 05:54:46 +000052 SetSocketOption (listen_fd, SOL_SOCKET, SO_REUSEADDR, 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54 struct sockaddr_in sa;
55 ::memset (&sa, 0, sizeof sa);
56 sa.sin_len = sizeof sa;
57 sa.sin_family = AF_INET;
Greg Clayton8b82f082011-04-12 05:54:46 +000058 sa.sin_port = htons (port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 sa.sin_addr.s_addr = htonl (INADDR_ANY);
60
Greg Clayton8b82f082011-04-12 05:54:46 +000061 int error = ::bind (listen_fd, (struct sockaddr *) &sa, sizeof(sa));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 if (error == -1)
63 err.SetError(errno, DNBError::POSIX);
64
65 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +000066 err.LogThreaded("::bind ( socket = %i, (struct sockaddr *) &sa, sizeof(sa)) )", listen_fd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
68 if (err.Fail())
69 {
Greg Clayton8b82f082011-04-12 05:54:46 +000070 ClosePort (listen_fd, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 return rnb_err;
72 }
73
Greg Clayton8b82f082011-04-12 05:54:46 +000074 if (callback && port == 0)
75 {
76 // We were asked to listen on port zero which means we
77 // must now read the actual port that was given to us
78 // as port zero is a special code for "find an open port
79 // for me".
80 socklen_t sa_len = sizeof (sa);
81 if (getsockname(listen_fd, (struct sockaddr *)&sa, &sa_len) == 0)
82 {
83 port = ntohs (sa.sin_port);
84 callback (callback_baton, port);
85 }
86 }
87
88 error = ::listen (listen_fd, 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 if (error == -1)
90 err.SetError(errno, DNBError::POSIX);
91
92 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +000093 err.LogThreaded("::listen ( socket = %i, backlog = 1 )", listen_fd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094
95 if (err.Fail())
96 {
Greg Clayton8b82f082011-04-12 05:54:46 +000097 ClosePort (listen_fd, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 return rnb_err;
99 }
100
Greg Clayton8b82f082011-04-12 05:54:46 +0000101 m_fd = ::accept (listen_fd, NULL, 0);
102 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 err.SetError(errno, DNBError::POSIX);
104
105 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +0000106 err.LogThreaded("::accept ( socket = %i, address = NULL, address_len = 0 )", listen_fd);
107
108 ClosePort (listen_fd, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
110 if (err.Fail())
111 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 return rnb_err;
113 }
114 else
115 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 // Keep our TCP packets coming without any delays.
Greg Clayton8b82f082011-04-12 05:54:46 +0000117 SetSocketOption (m_fd, IPPROTO_TCP, TCP_NODELAY, 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 }
119
120 return rnb_success;
121}
122
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000123rnb_err_t
124RNBSocket::Connect (const char *host, uint16_t port)
125{
126 Disconnect (false);
127
128 // Create the socket
Greg Clayton8b82f082011-04-12 05:54:46 +0000129 m_fd = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
130 if (m_fd == -1)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000131 return rnb_err;
132
133 // Enable local address reuse
Greg Clayton8b82f082011-04-12 05:54:46 +0000134 SetSocketOption (m_fd, SOL_SOCKET, SO_REUSEADDR, 1);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000135
136 struct sockaddr_in sa;
137 ::memset (&sa, 0, sizeof (sa));
138 sa.sin_family = AF_INET;
139 sa.sin_port = htons (port);
140
141 if (host == NULL)
142 host = "localhost";
143
144 int inet_pton_result = ::inet_pton (AF_INET, host, &sa.sin_addr);
145
146 if (inet_pton_result <= 0)
147 {
148 struct hostent *host_entry = gethostbyname (host);
149 if (host_entry)
150 {
151 std::string host_str (::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list));
152 inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
153 if (inet_pton_result <= 0)
154 {
155 Disconnect (false);
156 return rnb_err;
157 }
158 }
159 }
160
Greg Clayton8b82f082011-04-12 05:54:46 +0000161 if (-1 == ::connect (m_fd, (const struct sockaddr *)&sa, sizeof(sa)))
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000162 {
163 Disconnect (false);
164 return rnb_err;
165 }
166
167 // Keep our TCP packets coming without any delays.
Greg Clayton8b82f082011-04-12 05:54:46 +0000168 SetSocketOption (m_fd, IPPROTO_TCP, TCP_NODELAY, 1);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000169 return rnb_success;
170}
171
Jason Molenda42999a42012-02-22 02:18:59 +0000172rnb_err_t
173RNBSocket::useFD(int fd)
174{
175 if (fd < 0) {
176 DNBLogThreadedIf(LOG_RNB_COMM, "Bad file descriptor passed in.");
177 return rnb_err;
178 }
179
180 m_fd = fd;
181 return rnb_success;
182}
183
184#ifdef WITH_LOCKDOWN
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185rnb_err_t
186RNBSocket::ConnectToService()
187{
188 DNBLog("Connecting to com.apple.%s service...", DEBUGSERVER_PROGRAM_NAME);
189 // Disconnect from any previous connections
190 Disconnect(false);
Jason Molenda7eaf54d2013-01-18 01:20:12 +0000191 if (::secure_lockdown_checkin (&m_ld_conn, NULL, NULL) != kLDESuccess)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Jason Molenda7eaf54d2013-01-18 01:20:12 +0000193 DNBLogThreadedIf(LOG_RNB_COMM, "::secure_lockdown_checkin(&m_fd, NULL, NULL) failed");
Jason Molenda94379542012-05-16 00:36:21 +0000194 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 return rnb_not_connected;
196 }
Jason Molenda7eaf54d2013-01-18 01:20:12 +0000197 m_fd = ::lockdown_get_socket (m_ld_conn);
198 if (m_fd == -1)
199 {
200 DNBLogThreadedIf(LOG_RNB_COMM, "::lockdown_get_socket() failed");
201 return rnb_not_connected;
202 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000203 m_fd_from_lockdown = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 return rnb_success;
205}
206#endif
207
208rnb_err_t
209RNBSocket::OpenFile (const char *path)
210{
211 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000212 m_fd = open (path, O_RDWR);
213 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214 {
215 err.SetError(errno, DNBError::POSIX);
216 err.LogThreaded ("can't open file '%s'", path);
217 return rnb_not_connected;
218 }
219 else
220 {
221 struct termios stdin_termios;
222
Greg Clayton8b82f082011-04-12 05:54:46 +0000223 if (::tcgetattr (m_fd, &stdin_termios) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224 {
225 stdin_termios.c_lflag &= ~ECHO; // Turn off echoing
226 stdin_termios.c_lflag &= ~ICANON; // Get one char at a time
Greg Clayton8b82f082011-04-12 05:54:46 +0000227 ::tcsetattr (m_fd, TCSANOW, &stdin_termios);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 }
229 }
230 return rnb_success;
231}
232
233int
234RNBSocket::SetSocketOption(int fd, int level, int option_name, int option_value)
235{
236 return ::setsockopt(fd, level, option_name, &option_value, sizeof(option_value));
237}
238
239rnb_err_t
240RNBSocket::Disconnect (bool save_errno)
241{
Jason Molenda42999a42012-02-22 02:18:59 +0000242#ifdef WITH_LOCKDOWN
Greg Clayton8b82f082011-04-12 05:54:46 +0000243 if (m_fd_from_lockdown)
Jason Molenda7eaf54d2013-01-18 01:20:12 +0000244 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000245 m_fd_from_lockdown = false;
Jason Molenda7eaf54d2013-01-18 01:20:12 +0000246 m_fd = -1;
247 if (lockdown_deactivate (m_ld_conn) == 0)
248 return rnb_success;
249 else
250 return rnb_err;
251 }
Jason Molenda42999a42012-02-22 02:18:59 +0000252#endif
Greg Clayton8b82f082011-04-12 05:54:46 +0000253 return ClosePort (m_fd, save_errno);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
256
257rnb_err_t
258RNBSocket::Read (std::string &p)
259{
260 char buf[1024];
261 p.clear();
262
263 // Note that BUF is on the stack so we must be careful to keep any
264 // writes to BUF from overflowing or we'll have security issues.
265
Greg Clayton8b82f082011-04-12 05:54:46 +0000266 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 return rnb_err;
268
269 //DNBLogThreadedIf(LOG_RNB_COMM, "%8u RNBSocket::%s calling read()", (uint32_t)m_timer.ElapsedMicroSeconds(true), __FUNCTION__);
270 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000271 int bytesread = read (m_fd, buf, sizeof (buf));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 if (bytesread <= 0)
273 err.SetError(errno, DNBError::POSIX);
274 else
275 p.append(buf, bytesread);
276
277 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton43e0af02012-09-18 18:04:04 +0000278 err.LogThreaded("::read ( %i, %p, %llu ) => %i", m_fd, buf, sizeof (buf), (uint64_t)bytesread);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279
280 // Our port went away - we have to mark this so IsConnected will return the truth.
281 if (bytesread == 0)
282 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000283 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 return rnb_not_connected;
285 }
286 else if (bytesread == -1)
287 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000288 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 return rnb_err;
290 }
291 // Strip spaces from the end of the buffer
292 while (!p.empty() && isspace (p[p.size() - 1]))
293 p.erase (p.size () - 1);
294
295 // Most data in the debugserver packets valid printable characters...
296 DNBLogThreadedIf(LOG_RNB_COMM, "read: %s", p.c_str());
297 return rnb_success;
298}
299
300rnb_err_t
301RNBSocket::Write (const void *buffer, size_t length)
302{
Greg Clayton8b82f082011-04-12 05:54:46 +0000303 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 return rnb_err;
305
306 DNBError err;
Jason Molenda42999a42012-02-22 02:18:59 +0000307 int bytessent = write (m_fd, buffer, length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 if (bytessent < 0)
309 err.SetError(errno, DNBError::POSIX);
310
311 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton43e0af02012-09-18 18:04:04 +0000312 err.LogThreaded("::write ( socket = %i, buffer = %p, length = %llu) => %i", m_fd, buffer, length, (uint64_t)bytessent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313
314 if (bytessent < 0)
315 return rnb_err;
316
317 if (bytessent != length)
318 return rnb_err;
319
Greg Clayton490fbbe2011-10-28 22:59:14 +0000320 DNBLogThreadedIf(LOG_RNB_PACKETS, "putpkt: %*s", (int)length, (char *)buffer); // All data is string based in debugserver, so this is safe
321 DNBLogThreadedIf(LOG_RNB_COMM, "sent: %*s", (int)length, (char *)buffer);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322
323 return rnb_success;
324}
325
326
327rnb_err_t
328RNBSocket::ClosePort (int& fd, bool save_errno)
329{
330 int close_err = 0;
331 if (fd > 0)
332 {
333 errno = 0;
334 close_err = close (fd);
335 fd = -1;
336 }
337 return close_err != 0 ? rnb_err : rnb_success;
338}
339
340