blob: 92eff20959ca82b40f077f083a5d34b57ae27de0 [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);
191
Jason Molenda94379542012-05-16 00:36:21 +0000192 if (::lockdown_secure_checkin (&m_fd, NULL, NULL, NULL) != kLDESuccess)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 {
Jason Molenda94379542012-05-16 00:36:21 +0000194 DNBLogThreadedIf(LOG_RNB_COMM, "::lockdown_secure_checkin(&m_fd, NULL, NULL, NULL) failed");
195 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 return rnb_not_connected;
197 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000198 m_fd_from_lockdown = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 return rnb_success;
200}
201#endif
202
203rnb_err_t
204RNBSocket::OpenFile (const char *path)
205{
206 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000207 m_fd = open (path, O_RDWR);
208 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 {
210 err.SetError(errno, DNBError::POSIX);
211 err.LogThreaded ("can't open file '%s'", path);
212 return rnb_not_connected;
213 }
214 else
215 {
216 struct termios stdin_termios;
217
Greg Clayton8b82f082011-04-12 05:54:46 +0000218 if (::tcgetattr (m_fd, &stdin_termios) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219 {
220 stdin_termios.c_lflag &= ~ECHO; // Turn off echoing
221 stdin_termios.c_lflag &= ~ICANON; // Get one char at a time
Greg Clayton8b82f082011-04-12 05:54:46 +0000222 ::tcsetattr (m_fd, TCSANOW, &stdin_termios);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 }
224 }
225 return rnb_success;
226}
227
228int
229RNBSocket::SetSocketOption(int fd, int level, int option_name, int option_value)
230{
231 return ::setsockopt(fd, level, option_name, &option_value, sizeof(option_value));
232}
233
234rnb_err_t
235RNBSocket::Disconnect (bool save_errno)
236{
Jason Molenda42999a42012-02-22 02:18:59 +0000237#ifdef WITH_LOCKDOWN
Greg Clayton8b82f082011-04-12 05:54:46 +0000238 if (m_fd_from_lockdown)
239 m_fd_from_lockdown = false;
Jason Molenda42999a42012-02-22 02:18:59 +0000240#endif
Greg Clayton8b82f082011-04-12 05:54:46 +0000241 return ClosePort (m_fd, save_errno);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
244
245rnb_err_t
246RNBSocket::Read (std::string &p)
247{
248 char buf[1024];
249 p.clear();
250
251 // Note that BUF is on the stack so we must be careful to keep any
252 // writes to BUF from overflowing or we'll have security issues.
253
Greg Clayton8b82f082011-04-12 05:54:46 +0000254 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 return rnb_err;
256
257 //DNBLogThreadedIf(LOG_RNB_COMM, "%8u RNBSocket::%s calling read()", (uint32_t)m_timer.ElapsedMicroSeconds(true), __FUNCTION__);
258 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000259 int bytesread = read (m_fd, buf, sizeof (buf));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260 if (bytesread <= 0)
261 err.SetError(errno, DNBError::POSIX);
262 else
263 p.append(buf, bytesread);
264
265 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +0000266 err.LogThreaded("::read ( %i, %p, %zu ) => %i", m_fd, buf, sizeof (buf), bytesread);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
268 // Our port went away - we have to mark this so IsConnected will return the truth.
269 if (bytesread == 0)
270 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000271 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 return rnb_not_connected;
273 }
274 else if (bytesread == -1)
275 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000276 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 return rnb_err;
278 }
279 // Strip spaces from the end of the buffer
280 while (!p.empty() && isspace (p[p.size() - 1]))
281 p.erase (p.size () - 1);
282
283 // Most data in the debugserver packets valid printable characters...
284 DNBLogThreadedIf(LOG_RNB_COMM, "read: %s", p.c_str());
285 return rnb_success;
286}
287
288rnb_err_t
289RNBSocket::Write (const void *buffer, size_t length)
290{
Greg Clayton8b82f082011-04-12 05:54:46 +0000291 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 return rnb_err;
293
294 DNBError err;
Jason Molenda42999a42012-02-22 02:18:59 +0000295 int bytessent = write (m_fd, buffer, length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 if (bytessent < 0)
297 err.SetError(errno, DNBError::POSIX);
298
299 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Jason Molenda42999a42012-02-22 02:18:59 +0000300 err.LogThreaded("::write ( socket = %i, buffer = %p, length = %zu) => %i", m_fd, buffer, length, bytessent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301
302 if (bytessent < 0)
303 return rnb_err;
304
305 if (bytessent != length)
306 return rnb_err;
307
Greg Clayton490fbbe2011-10-28 22:59:14 +0000308 DNBLogThreadedIf(LOG_RNB_PACKETS, "putpkt: %*s", (int)length, (char *)buffer); // All data is string based in debugserver, so this is safe
309 DNBLogThreadedIf(LOG_RNB_COMM, "sent: %*s", (int)length, (char *)buffer);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
311 return rnb_success;
312}
313
314
315rnb_err_t
316RNBSocket::ClosePort (int& fd, bool save_errno)
317{
318 int close_err = 0;
319 if (fd > 0)
320 {
321 errno = 0;
322 close_err = close (fd);
323 fd = -1;
324 }
325 return close_err != 0 ? rnb_err : rnb_success;
326}
327
328