blob: 5af9810d0e46ec841c314d446036f39b094c764c [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
25#if defined (__arm__)
26#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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172#if defined (__arm__)
173rnb_err_t
174RNBSocket::ConnectToService()
175{
176 DNBLog("Connecting to com.apple.%s service...", DEBUGSERVER_PROGRAM_NAME);
177 // Disconnect from any previous connections
178 Disconnect(false);
179
Greg Clayton8b82f082011-04-12 05:54:46 +0000180 m_fd = ::lockdown_checkin (NULL, NULL);
181 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
183 DNBLogThreadedIf(LOG_RNB_COMM, "::lockdown_checkin(NULL, NULL) failed");
184 return rnb_not_connected;
185 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000186 m_fd_from_lockdown = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 return rnb_success;
188}
189#endif
190
191rnb_err_t
192RNBSocket::OpenFile (const char *path)
193{
194 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000195 m_fd = open (path, O_RDWR);
196 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 {
198 err.SetError(errno, DNBError::POSIX);
199 err.LogThreaded ("can't open file '%s'", path);
200 return rnb_not_connected;
201 }
202 else
203 {
204 struct termios stdin_termios;
205
Greg Clayton8b82f082011-04-12 05:54:46 +0000206 if (::tcgetattr (m_fd, &stdin_termios) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 {
208 stdin_termios.c_lflag &= ~ECHO; // Turn off echoing
209 stdin_termios.c_lflag &= ~ICANON; // Get one char at a time
Greg Clayton8b82f082011-04-12 05:54:46 +0000210 ::tcsetattr (m_fd, TCSANOW, &stdin_termios);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 }
212 }
213 return rnb_success;
214}
215
216int
217RNBSocket::SetSocketOption(int fd, int level, int option_name, int option_value)
218{
219 return ::setsockopt(fd, level, option_name, &option_value, sizeof(option_value));
220}
221
222rnb_err_t
223RNBSocket::Disconnect (bool save_errno)
224{
Greg Clayton8b82f082011-04-12 05:54:46 +0000225 if (m_fd_from_lockdown)
226 m_fd_from_lockdown = false;
227 return ClosePort (m_fd, save_errno);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
230
231rnb_err_t
232RNBSocket::Read (std::string &p)
233{
234 char buf[1024];
235 p.clear();
236
237 // Note that BUF is on the stack so we must be careful to keep any
238 // writes to BUF from overflowing or we'll have security issues.
239
Greg Clayton8b82f082011-04-12 05:54:46 +0000240 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 return rnb_err;
242
243 //DNBLogThreadedIf(LOG_RNB_COMM, "%8u RNBSocket::%s calling read()", (uint32_t)m_timer.ElapsedMicroSeconds(true), __FUNCTION__);
244 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000245 int bytesread = read (m_fd, buf, sizeof (buf));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 if (bytesread <= 0)
247 err.SetError(errno, DNBError::POSIX);
248 else
249 p.append(buf, bytesread);
250
251 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +0000252 err.LogThreaded("::read ( %i, %p, %zu ) => %i", m_fd, buf, sizeof (buf), bytesread);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
254 // Our port went away - we have to mark this so IsConnected will return the truth.
255 if (bytesread == 0)
256 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000257 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 return rnb_not_connected;
259 }
260 else if (bytesread == -1)
261 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000262 m_fd = -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 return rnb_err;
264 }
265 // Strip spaces from the end of the buffer
266 while (!p.empty() && isspace (p[p.size() - 1]))
267 p.erase (p.size () - 1);
268
269 // Most data in the debugserver packets valid printable characters...
270 DNBLogThreadedIf(LOG_RNB_COMM, "read: %s", p.c_str());
271 return rnb_success;
272}
273
274rnb_err_t
275RNBSocket::Write (const void *buffer, size_t length)
276{
Greg Clayton8b82f082011-04-12 05:54:46 +0000277 if (m_fd == -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 return rnb_err;
279
280 DNBError err;
Greg Clayton8b82f082011-04-12 05:54:46 +0000281 int bytessent = send (m_fd, buffer, length, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 if (bytessent < 0)
283 err.SetError(errno, DNBError::POSIX);
284
285 if (err.Fail() || DNBLogCheckLogBit(LOG_RNB_COMM))
Greg Clayton8b82f082011-04-12 05:54:46 +0000286 err.LogThreaded("::send ( socket = %i, buffer = %p, length = %zu, flags = 0 ) => %i", m_fd, buffer, length, bytessent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287
288 if (bytessent < 0)
289 return rnb_err;
290
291 if (bytessent != length)
292 return rnb_err;
293
294 DNBLogThreadedIf(LOG_RNB_PACKETS, "putpkt: %*s", length, (char *)buffer); // All data is string based in debugserver, so this is safe
295 DNBLogThreadedIf(LOG_RNB_COMM, "sent: %*s", length, (char *)buffer);
296
297 return rnb_success;
298}
299
300
301rnb_err_t
302RNBSocket::ClosePort (int& fd, bool save_errno)
303{
304 int close_err = 0;
305 if (fd > 0)
306 {
307 errno = 0;
308 close_err = close (fd);
309 fd = -1;
310 }
311 return close_err != 0 ? rnb_err : rnb_success;
312}
313
314