blob: 91c2ff369b189b2dcb4f56e9f57ee2b931e2cab0 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ConnectionFileDescriptor.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#include "lldb/Core/ConnectionFileDescriptor.h"
11
12// C Includes
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <errno.h>
14#include <fcntl.h>
Greg Claytonf29a08f2011-02-09 17:41:27 +000015#include <arpa/inet.h>
Chris Lattner24943d22010-06-08 16:52:24 +000016#include <netdb.h>
17#include <netinet/in.h>
18#include <netinet/tcp.h>
19#include <sys/socket.h>
Greg Clayton36f63a92010-10-19 03:25:40 +000020#include <sys/un.h>
Greg Claytonf29a08f2011-02-09 17:41:27 +000021#include <sys/types.h>
Eli Friedman057ef852010-06-09 18:23:21 +000022#include <string.h>
23#include <stdlib.h>
Jim Ingham3ce4df52012-07-12 01:17:55 +000024#include <unistd.h>
Chris Lattner24943d22010-06-08 16:52:24 +000025
26// C++ Includes
27// Other libraries and framework includes
28// Project includes
29#include "lldb/lldb-private-log.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000030#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Core/Communication.h"
32#include "lldb/Core/Log.h"
33#include "lldb/Core/RegularExpression.h"
34#include "lldb/Core/Timer.h"
35
36using namespace lldb;
37using namespace lldb_private;
38
Greg Clayton8d2ea282011-07-17 20:36:25 +000039static bool
40DecodeHostAndPort (const char *host_and_port,
41 std::string &host_str,
42 std::string &port_str,
43 int32_t& port,
44 Error *error_ptr)
45{
46 RegularExpression regex ("([^:]+):([0-9]+)");
47 if (regex.Execute (host_and_port, 2))
48 {
49 if (regex.GetMatchAtIndex (host_and_port, 1, host_str) &&
50 regex.GetMatchAtIndex (host_and_port, 2, port_str))
51 {
52 port = Args::StringToSInt32 (port_str.c_str(), INT32_MIN);
53 if (port != INT32_MIN)
54 {
55 if (error_ptr)
56 error_ptr->Clear();
57 return true;
58 }
59 }
60 }
61 host_str.clear();
62 port_str.clear();
63 port = INT32_MIN;
64 if (error_ptr)
65 error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port);
66 return false;
67}
68
Chris Lattner24943d22010-06-08 16:52:24 +000069ConnectionFileDescriptor::ConnectionFileDescriptor () :
70 Connection(),
Greg Clayton8d2ea282011-07-17 20:36:25 +000071 m_fd_send (-1),
72 m_fd_recv (-1),
73 m_fd_send_type (eFDTypeFile),
74 m_fd_recv_type (eFDTypeFile),
Greg Claytonac304e42011-07-19 16:44:54 +000075 m_udp_send_sockaddr (),
Greg Clayton63afdb02011-06-17 01:22:15 +000076 m_should_close_fd (false),
Greg Claytondb9d6f42012-01-31 04:56:17 +000077 m_socket_timeout_usec(0),
Greg Claytonf4669562012-08-08 22:27:52 +000078 m_pipe_read(-1),
79 m_pipe_write(-1),
Jim Inghamb4386992012-07-17 01:47:11 +000080 m_mutex (Mutex::eMutexTypeRecursive),
81 m_shutting_down (false)
Chris Lattner24943d22010-06-08 16:52:24 +000082{
Greg Clayton8d2ea282011-07-17 20:36:25 +000083 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
84 if (log)
85 log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", this);
Chris Lattner24943d22010-06-08 16:52:24 +000086}
87
88ConnectionFileDescriptor::ConnectionFileDescriptor (int fd, bool owns_fd) :
89 Connection(),
Greg Clayton8d2ea282011-07-17 20:36:25 +000090 m_fd_send (fd),
91 m_fd_recv (fd),
92 m_fd_send_type (eFDTypeFile),
93 m_fd_recv_type (eFDTypeFile),
Greg Claytonac304e42011-07-19 16:44:54 +000094 m_udp_send_sockaddr (),
Greg Clayton63afdb02011-06-17 01:22:15 +000095 m_should_close_fd (owns_fd),
Greg Claytondb9d6f42012-01-31 04:56:17 +000096 m_socket_timeout_usec(0),
Greg Claytonf4669562012-08-08 22:27:52 +000097 m_pipe_read(-1),
98 m_pipe_write(-1),
Jim Inghamb4386992012-07-17 01:47:11 +000099 m_mutex (Mutex::eMutexTypeRecursive),
100 m_shutting_down (false)
Chris Lattner24943d22010-06-08 16:52:24 +0000101{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000102 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
103 if (log)
104 log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = %i, owns_fd = %i)", this, fd, owns_fd);
Greg Claytonf4669562012-08-08 22:27:52 +0000105 OpenCommandPipe ();
Chris Lattner24943d22010-06-08 16:52:24 +0000106}
107
108
109ConnectionFileDescriptor::~ConnectionFileDescriptor ()
110{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000111 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
112 if (log)
113 log->Printf ("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", this);
Chris Lattner24943d22010-06-08 16:52:24 +0000114 Disconnect (NULL);
Greg Claytonf4669562012-08-08 22:27:52 +0000115 CloseCommandPipe ();
Jim Ingham3ce4df52012-07-12 01:17:55 +0000116}
117
118void
Greg Claytonf4669562012-08-08 22:27:52 +0000119ConnectionFileDescriptor::OpenCommandPipe ()
Jim Ingham3ce4df52012-07-12 01:17:55 +0000120{
Greg Claytonf4669562012-08-08 22:27:52 +0000121 CloseCommandPipe();
Jim Ingham3ce4df52012-07-12 01:17:55 +0000122
123 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
124 // Make the command file descriptor here:
125 int filedes[2];
126 int result = pipe (filedes);
127 if (result != 0)
128 {
129 if (log)
130 log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor () - could not make pipe: %s",
131 this,
132 strerror(errno));
133 }
134 else
135 {
Greg Claytonf4669562012-08-08 22:27:52 +0000136 m_pipe_read = filedes[0];
137 m_pipe_write = filedes[1];
Jim Ingham3ce4df52012-07-12 01:17:55 +0000138 }
139}
140
141void
Greg Claytonf4669562012-08-08 22:27:52 +0000142ConnectionFileDescriptor::CloseCommandPipe ()
Jim Ingham3ce4df52012-07-12 01:17:55 +0000143{
Greg Claytonf4669562012-08-08 22:27:52 +0000144 if (m_pipe_read != -1)
Jim Ingham3ce4df52012-07-12 01:17:55 +0000145 {
Greg Claytonf4669562012-08-08 22:27:52 +0000146 close (m_pipe_read);
147 m_pipe_read = -1;
Jim Ingham3ce4df52012-07-12 01:17:55 +0000148 }
149
Greg Claytonf4669562012-08-08 22:27:52 +0000150 if (m_pipe_write != -1)
Jim Ingham3ce4df52012-07-12 01:17:55 +0000151 {
Greg Claytonf4669562012-08-08 22:27:52 +0000152 close (m_pipe_write);
153 m_pipe_write = -1;
Jim Ingham3ce4df52012-07-12 01:17:55 +0000154 }
Chris Lattner24943d22010-06-08 16:52:24 +0000155}
156
157bool
158ConnectionFileDescriptor::IsConnected () const
159{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000160 return m_fd_send >= 0 || m_fd_recv >= 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000161}
162
163ConnectionStatus
164ConnectionFileDescriptor::Connect (const char *s, Error *error_ptr)
165{
Greg Claytondb9d6f42012-01-31 04:56:17 +0000166 Mutex::Locker locker (m_mutex);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000167 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
168 if (log)
169 log->Printf ("%p ConnectionFileDescriptor::Connect (url = '%s')", this, s);
Chris Lattner24943d22010-06-08 16:52:24 +0000170
Greg Claytonf4669562012-08-08 22:27:52 +0000171 OpenCommandPipe();
Jim Ingham3ce4df52012-07-12 01:17:55 +0000172
Chris Lattner24943d22010-06-08 16:52:24 +0000173 if (s && s[0])
174 {
175 char *end = NULL;
176 if (strstr(s, "listen://"))
177 {
178 // listen://HOST:PORT
179 unsigned long listen_port = ::strtoul(s + strlen("listen://"), &end, 0);
180 return SocketListen (listen_port, error_ptr);
181 }
Greg Clayton36f63a92010-10-19 03:25:40 +0000182 else if (strstr(s, "unix-accept://"))
183 {
184 // unix://SOCKNAME
185 return NamedSocketAccept (s + strlen("unix-accept://"), error_ptr);
186 }
Chris Lattner24943d22010-06-08 16:52:24 +0000187 else if (strstr(s, "connect://"))
188 {
Greg Clayton1e5b0212011-07-15 16:31:38 +0000189 return ConnectTCP (s + strlen("connect://"), error_ptr);
190 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000191 else if (strstr(s, "tcp-connect://"))
Greg Clayton1e5b0212011-07-15 16:31:38 +0000192 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000193 return ConnectTCP (s + strlen("tcp-connect://"), error_ptr);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000194 }
195 else if (strstr(s, "udp://"))
196 {
197 return ConnectUDP (s + strlen("udp://"), error_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000198 }
Greg Clayton180546b2011-04-30 01:09:13 +0000199 else if (strstr(s, "fd://"))
200 {
201 // Just passing a native file descriptor within this current process
202 // that is already opened (possibly from a service or other source).
203 s += strlen ("fd://");
204 bool success = false;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000205 m_fd_send = m_fd_recv = Args::StringToSInt32 (s, -1, 0, &success);
206
Greg Clayton180546b2011-04-30 01:09:13 +0000207 if (success)
208 {
209 // We have what looks to be a valid file descriptor, but we
Jim Ingham3ce4df52012-07-12 01:17:55 +0000210 // should make sure it is. We currently are doing this by trying to
Greg Clayton180546b2011-04-30 01:09:13 +0000211 // get the flags from the file descriptor and making sure it
Greg Claytond0691fe2011-07-02 23:21:06 +0000212 // isn't a bad fd.
Greg Clayton180546b2011-04-30 01:09:13 +0000213 errno = 0;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000214 int flags = ::fcntl (m_fd_send, F_GETFL, 0);
Greg Clayton180546b2011-04-30 01:09:13 +0000215 if (flags == -1 || errno == EBADF)
216 {
217 if (error_ptr)
218 error_ptr->SetErrorStringWithFormat ("stale file descriptor: %s", s);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000219 m_fd_send = m_fd_recv = -1;
Greg Clayton180546b2011-04-30 01:09:13 +0000220 return eConnectionStatusError;
221 }
222 else
223 {
Greg Claytond0691fe2011-07-02 23:21:06 +0000224 // Try and get a socket option from this file descriptor to
225 // see if this is a socket and set m_is_socket accordingly.
226 int resuse;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000227 bool is_socket = GetSocketOption (m_fd_send, SOL_SOCKET, SO_REUSEADDR, resuse) == 0;
Greg Claytond52d00f2011-07-16 03:19:08 +0000228 if (is_socket)
Greg Clayton8d2ea282011-07-17 20:36:25 +0000229 m_fd_send_type = m_fd_recv_type = eFDTypeSocket;
Greg Claytonbba48362012-01-14 20:47:38 +0000230 // Don't take ownership of a file descriptor that gets passed
231 // to us since someone else opened the file descriptor and
232 // handed it to us.
233 // TODO: Since are using a URL to open connection we should
234 // eventually parse options using the web standard where we
235 // have "fd://123?opt1=value;opt2=value" and we can have an
236 // option be "owns=1" or "owns=0" or something like this to
237 // allow us to specify this. For now, we assume we must
238 // assume we don't own it.
239 m_should_close_fd = false;
Greg Clayton180546b2011-04-30 01:09:13 +0000240 return eConnectionStatusSuccess;
241 }
242 }
243
244 if (error_ptr)
245 error_ptr->SetErrorStringWithFormat ("invalid file descriptor: \"fd://%s\"", s);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000246 m_fd_send = m_fd_recv = -1;
Greg Clayton180546b2011-04-30 01:09:13 +0000247 return eConnectionStatusError;
248 }
Chris Lattner24943d22010-06-08 16:52:24 +0000249 else if (strstr(s, "file://"))
250 {
251 // file:///PATH
252 const char *path = s + strlen("file://");
Greg Clayton2f28ece2012-01-04 22:56:43 +0000253 do
254 {
255 m_fd_send = m_fd_recv = ::open (path, O_RDWR);
256 } while (m_fd_send == -1 && errno == EINTR);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000257 if (m_fd_send == -1)
Chris Lattner24943d22010-06-08 16:52:24 +0000258 {
259 if (error_ptr)
260 error_ptr->SetErrorToErrno();
261 return eConnectionStatusError;
262 }
263
Greg Clayton8d2ea282011-07-17 20:36:25 +0000264 int flags = ::fcntl (m_fd_send, F_GETFL, 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000265 if (flags >= 0)
266 {
267 if ((flags & O_NONBLOCK) == 0)
268 {
269 flags |= O_NONBLOCK;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000270 ::fcntl (m_fd_send, F_SETFL, flags);
Chris Lattner24943d22010-06-08 16:52:24 +0000271 }
272 }
273 m_should_close_fd = true;
274 return eConnectionStatusSuccess;
275 }
276 if (error_ptr)
Greg Clayton58e26e02011-03-24 04:28:38 +0000277 error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
Chris Lattner24943d22010-06-08 16:52:24 +0000278 return eConnectionStatusError;
279 }
280 if (error_ptr)
Greg Clayton58e26e02011-03-24 04:28:38 +0000281 error_ptr->SetErrorString("invalid connect arguments");
Chris Lattner24943d22010-06-08 16:52:24 +0000282 return eConnectionStatusError;
283}
284
285ConnectionStatus
286ConnectionFileDescriptor::Disconnect (Error *error_ptr)
287{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000288 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
289 if (log)
290 log->Printf ("%p ConnectionFileDescriptor::Disconnect ()", this);
Jim Ingham3ce4df52012-07-12 01:17:55 +0000291
Greg Clayton8d2ea282011-07-17 20:36:25 +0000292 ConnectionStatus status = eConnectionStatusSuccess;
Jim Ingham3ce4df52012-07-12 01:17:55 +0000293
Jim Inghamb4386992012-07-17 01:47:11 +0000294 if (m_fd_send < 0 && m_fd_recv < 0)
Greg Clayton8d2ea282011-07-17 20:36:25 +0000295 {
Jim Inghamb4386992012-07-17 01:47:11 +0000296 if (log)
297 log->Printf ("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect", this);
298 return eConnectionStatusSuccess;
299 }
300
301 // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is quite likely
302 // because somebody is doing a blocking read on our file descriptor. If that's the case,
303 // then send the "q" char to the command file channel so the read will wake up and the connection
304 // will then know to shut down.
305
306 m_shutting_down = true;
307
308 Mutex::Locker locker;
309 bool got_lock= locker.TryLock (m_mutex);
310
311 if (!got_lock)
312 {
Greg Claytonf4669562012-08-08 22:27:52 +0000313 if (m_pipe_write != -1 )
Jim Ingham3ce4df52012-07-12 01:17:55 +0000314 {
Greg Claytonf4669562012-08-08 22:27:52 +0000315 write (m_pipe_write, "q", 1);
316 close (m_pipe_write);
317 m_pipe_write = -1;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000318 }
Jim Inghamb4386992012-07-17 01:47:11 +0000319 locker.Lock (m_mutex);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000320 }
Jim Inghamb4386992012-07-17 01:47:11 +0000321
322 if (m_should_close_fd == true)
323 {
324 if (m_fd_send == m_fd_recv)
325 {
326 status = Close (m_fd_send, error_ptr);
327 }
328 else
329 {
330 // File descriptors are the different, close both if needed
331 if (m_fd_send >= 0)
332 status = Close (m_fd_send, error_ptr);
333 if (m_fd_recv >= 0)
334 {
335 ConnectionStatus recv_status = Close (m_fd_recv, error_ptr);
336 if (status == eConnectionStatusSuccess)
337 status = recv_status;
338 }
339 }
340 }
341
342 // Now set all our descriptors to invalid values.
343
344 m_fd_send = m_fd_recv = -1;
345
346 if (status != eConnectionStatusSuccess)
347 {
348
349 return status;
350 }
351
352 m_shutting_down = false;
353 return eConnectionStatusSuccess;
Chris Lattner24943d22010-06-08 16:52:24 +0000354}
355
356size_t
Greg Clayton63afdb02011-06-17 01:22:15 +0000357ConnectionFileDescriptor::Read (void *dst,
358 size_t dst_len,
359 uint32_t timeout_usec,
360 ConnectionStatus &status,
361 Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000362{
Greg Claytone005f2c2010-11-06 01:53:30 +0000363 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
Chris Lattner24943d22010-06-08 16:52:24 +0000364 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000365 log->Printf ("%p ConnectionFileDescriptor::Read () ::read (fd = %i, dst = %p, dst_len = %" PRIu64 ")...",
Greg Clayton851e30e2012-09-18 18:04:04 +0000366 this, m_fd_recv, dst, (uint64_t)dst_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000367
Jim Inghamb4386992012-07-17 01:47:11 +0000368 Mutex::Locker locker;
369 bool got_lock = locker.TryLock (m_mutex);
370 if (!got_lock)
371 {
372 if (log)
373 log->Printf ("%p ConnectionFileDescriptor::Read () failed to get the connection lock.",
374 this);
375 if (error_ptr)
376 error_ptr->SetErrorString ("failed to get the connection lock for read.");
377
378 status = eConnectionStatusTimedOut;
379 return 0;
380 }
381 else if (m_shutting_down)
382 return eConnectionStatusError;
383
Greg Claytond52d00f2011-07-16 03:19:08 +0000384 ssize_t bytes_read = 0;
Greg Claytond52d00f2011-07-16 03:19:08 +0000385
Jim Ingham3ce4df52012-07-12 01:17:55 +0000386 status = BytesAvailable (timeout_usec, error_ptr);
387 if (status == eConnectionStatusSuccess)
Greg Clayton63afdb02011-06-17 01:22:15 +0000388 {
Jim Ingham3ce4df52012-07-12 01:17:55 +0000389 do
Greg Clayton2f28ece2012-01-04 22:56:43 +0000390 {
Jim Ingham3ce4df52012-07-12 01:17:55 +0000391 bytes_read = ::read (m_fd_recv, dst, dst_len);
392 } while (bytes_read < 0 && errno == EINTR);
Greg Clayton63afdb02011-06-17 01:22:15 +0000393 }
Greg Claytond52d00f2011-07-16 03:19:08 +0000394
Greg Clayton63afdb02011-06-17 01:22:15 +0000395 if (status != eConnectionStatusSuccess)
396 return 0;
397
Chris Lattner24943d22010-06-08 16:52:24 +0000398 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000399 if (bytes_read == 0)
400 {
Caroline Tice242db722011-02-03 20:02:43 +0000401 error.Clear(); // End-of-file. Do not automatically close; pass along for the end-of-file handlers.
402 status = eConnectionStatusEndOfFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000403 }
404 else if (bytes_read < 0)
405 {
406 error.SetErrorToErrno();
407 }
408 else
409 {
410 error.Clear();
411 }
412
413 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000414 log->Printf ("%p ConnectionFileDescriptor::Read () ::read (fd = %i, dst = %p, dst_len = %" PRIu64 ") => %" PRIi64 ", error = %s",
Chris Lattner24943d22010-06-08 16:52:24 +0000415 this,
Greg Clayton8d2ea282011-07-17 20:36:25 +0000416 m_fd_recv,
Chris Lattner24943d22010-06-08 16:52:24 +0000417 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000418 (uint64_t)dst_len,
419 (int64_t)bytes_read,
Chris Lattner24943d22010-06-08 16:52:24 +0000420 error.AsCString());
421
422 if (error_ptr)
423 *error_ptr = error;
424
425 if (error.Fail())
426 {
427 uint32_t error_value = error.GetError();
428 switch (error_value)
429 {
430 case EAGAIN: // The file was marked for non-blocking I/O, and no data were ready to be read.
Greg Clayton8d2ea282011-07-17 20:36:25 +0000431 if (m_fd_recv_type == eFDTypeSocket || m_fd_recv_type == eFDTypeSocketUDP)
432 status = eConnectionStatusTimedOut;
433 else
434 status = eConnectionStatusSuccess;
Chris Lattner24943d22010-06-08 16:52:24 +0000435 return 0;
436
Chris Lattner24943d22010-06-08 16:52:24 +0000437 case EFAULT: // Buf points outside the allocated address space.
438 case EINTR: // A read from a slow device was interrupted before any data arrived by the delivery of a signal.
439 case EINVAL: // The pointer associated with fildes was negative.
440 case EIO: // An I/O error occurred while reading from the file system.
441 // The process group is orphaned.
442 // The file is a regular file, nbyte is greater than 0,
443 // the starting position is before the end-of-file, and
444 // the starting position is greater than or equal to the
445 // offset maximum established for the open file
446 // descriptor associated with fildes.
447 case EISDIR: // An attempt is made to read a directory.
448 case ENOBUFS: // An attempt to allocate a memory buffer fails.
449 case ENOMEM: // Insufficient memory is available.
450 status = eConnectionStatusError;
451 break; // Break to close....
452
Greg Claytond976b0e2011-05-29 00:45:15 +0000453 case ENOENT: // no such file or directory
Greg Clayton5fba9ee2011-05-19 00:17:26 +0000454 case EBADF: // fildes is not a valid file or socket descriptor open for reading.
Chris Lattner24943d22010-06-08 16:52:24 +0000455 case ENXIO: // An action is requested of a device that does not exist..
456 // A requested action cannot be performed by the device.
457 case ECONNRESET:// The connection is closed by the peer during a read attempt on a socket.
458 case ENOTCONN: // A read is attempted on an unconnected socket.
459 status = eConnectionStatusLostConnection;
460 break; // Break to close....
461
462 case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a socket.
463 status = eConnectionStatusTimedOut;
464 return 0;
465 }
466
Greg Clayton8a797082012-01-27 18:57:04 +0000467 //Disconnect (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000468 return 0;
469 }
470 return bytes_read;
471}
472
473size_t
474ConnectionFileDescriptor::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
475{
Greg Claytone005f2c2010-11-06 01:53:30 +0000476 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
Chris Lattner24943d22010-06-08 16:52:24 +0000477 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000478 log->Printf ("%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 ")", this, src, (uint64_t)src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000479
480 if (!IsConnected ())
481 {
482 if (error_ptr)
Greg Clayton58e26e02011-03-24 04:28:38 +0000483 error_ptr->SetErrorString("not connected");
Chris Lattner24943d22010-06-08 16:52:24 +0000484 status = eConnectionStatusNoConnection;
485 return 0;
486 }
487
488
489 Error error;
490
491 ssize_t bytes_sent = 0;
492
Greg Clayton8d2ea282011-07-17 20:36:25 +0000493 switch (m_fd_send_type)
Greg Claytond52d00f2011-07-16 03:19:08 +0000494 {
495 case eFDTypeFile: // Other FD requireing read/write
Greg Clayton2f28ece2012-01-04 22:56:43 +0000496 do
497 {
498 bytes_sent = ::write (m_fd_send, src, src_len);
499 } while (bytes_sent < 0 && errno == EINTR);
Greg Claytond52d00f2011-07-16 03:19:08 +0000500 break;
501
502 case eFDTypeSocket: // Socket requiring send/recv
Greg Clayton2f28ece2012-01-04 22:56:43 +0000503 do
504 {
505 bytes_sent = ::send (m_fd_send, src, src_len, 0);
506 } while (bytes_sent < 0 && errno == EINTR);
Greg Claytond52d00f2011-07-16 03:19:08 +0000507 break;
508
509 case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom
Greg Claytonac304e42011-07-19 16:44:54 +0000510 assert (m_udp_send_sockaddr.GetFamily() != 0);
Greg Clayton2f28ece2012-01-04 22:56:43 +0000511 do
512 {
513 bytes_sent = ::sendto (m_fd_send,
514 src,
515 src_len,
516 0,
517 m_udp_send_sockaddr,
518 m_udp_send_sockaddr.GetLength());
519 } while (bytes_sent < 0 && errno == EINTR);
Greg Claytond52d00f2011-07-16 03:19:08 +0000520 break;
521 }
Chris Lattner24943d22010-06-08 16:52:24 +0000522
523 if (bytes_sent < 0)
524 error.SetErrorToErrno ();
525 else
526 error.Clear ();
527
528 if (log)
529 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000530 switch (m_fd_send_type)
Greg Claytond52d00f2011-07-16 03:19:08 +0000531 {
532 case eFDTypeFile: // Other FD requireing read/write
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000533 log->Printf ("%p ConnectionFileDescriptor::Write() ::write (fd = %i, src = %p, src_len = %" PRIu64 ") => %" PRIi64 " (error = %s)",
Greg Claytond52d00f2011-07-16 03:19:08 +0000534 this,
Greg Clayton8d2ea282011-07-17 20:36:25 +0000535 m_fd_send,
Greg Claytond52d00f2011-07-16 03:19:08 +0000536 src,
Greg Clayton851e30e2012-09-18 18:04:04 +0000537 (uint64_t)src_len,
538 (int64_t)bytes_sent,
Greg Claytond52d00f2011-07-16 03:19:08 +0000539 error.AsCString());
540 break;
541
542 case eFDTypeSocket: // Socket requiring send/recv
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000543 log->Printf ("%p ConnectionFileDescriptor::Write() ::send (socket = %i, src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
Greg Claytond52d00f2011-07-16 03:19:08 +0000544 this,
Greg Clayton8d2ea282011-07-17 20:36:25 +0000545 m_fd_send,
Greg Claytond52d00f2011-07-16 03:19:08 +0000546 src,
Greg Clayton851e30e2012-09-18 18:04:04 +0000547 (uint64_t)src_len,
548 (int64_t)bytes_sent,
Greg Claytond52d00f2011-07-16 03:19:08 +0000549 error.AsCString());
550 break;
551
552 case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000553 log->Printf ("%p ConnectionFileDescriptor::Write() ::sendto (socket = %i, src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
Greg Claytond52d00f2011-07-16 03:19:08 +0000554 this,
Greg Clayton8d2ea282011-07-17 20:36:25 +0000555 m_fd_send,
Greg Claytond52d00f2011-07-16 03:19:08 +0000556 src,
Greg Clayton851e30e2012-09-18 18:04:04 +0000557 (uint64_t)src_len,
558 (int64_t)bytes_sent,
Greg Claytond52d00f2011-07-16 03:19:08 +0000559 error.AsCString());
560 break;
561 }
Chris Lattner24943d22010-06-08 16:52:24 +0000562 }
563
564 if (error_ptr)
565 *error_ptr = error;
566
567 if (error.Fail())
568 {
569 switch (error.GetError())
570 {
571 case EAGAIN:
572 case EINTR:
573 status = eConnectionStatusSuccess;
574 return 0;
575
576 case ECONNRESET:// The connection is closed by the peer during a read attempt on a socket.
577 case ENOTCONN: // A read is attempted on an unconnected socket.
578 status = eConnectionStatusLostConnection;
579 break; // Break to close....
580
581 default:
582 status = eConnectionStatusError;
583 break; // Break to close....
584 }
585
Chris Lattner24943d22010-06-08 16:52:24 +0000586 return 0;
587 }
588
589 status = eConnectionStatusSuccess;
590 return bytes_sent;
591}
592
593ConnectionStatus
594ConnectionFileDescriptor::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
595{
Jim Inghamb4386992012-07-17 01:47:11 +0000596 // Don't need to take the mutex here separately since we are only called from Read. If we
597 // ever get used more generally we will need to lock here as well.
598
Greg Claytone005f2c2010-11-06 01:53:30 +0000599 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
Chris Lattner24943d22010-06-08 16:52:24 +0000600 if (log)
601 log->Printf("%p ConnectionFileDescriptor::BytesAvailable (timeout_usec = %u)", this, timeout_usec);
602 struct timeval *tv_ptr;
603 struct timeval tv;
604 if (timeout_usec == UINT32_MAX)
605 {
606 // Infinite wait...
607 tv_ptr = NULL;
608 }
609 else
610 {
611 TimeValue time_value;
612 time_value.OffsetWithMicroSeconds (timeout_usec);
613 tv = time_value.GetAsTimeVal();
614 tv_ptr = &tv;
615 }
616
Greg Claytonf4669562012-08-08 22:27:52 +0000617 while (m_fd_recv >= 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000618 {
619 fd_set read_fds;
620 FD_ZERO (&read_fds);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000621 FD_SET (m_fd_recv, &read_fds);
Greg Claytonf4669562012-08-08 22:27:52 +0000622 if (m_pipe_read != -1)
623 FD_SET (m_pipe_read, &read_fds);
624 int nfds = std::max<int>(m_fd_recv, m_pipe_read) + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000625
626 Error error;
627
628
629 if (log)
Greg Clayton63afdb02011-06-17 01:22:15 +0000630 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds = %i, fd = %i, NULL, NULL, timeout = %p)...",
Greg Clayton8d2ea282011-07-17 20:36:25 +0000631 this, nfds, m_fd_recv, tv_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000632
633 const int num_set_fds = ::select (nfds, &read_fds, NULL, NULL, tv_ptr);
634 if (num_set_fds < 0)
635 error.SetErrorToErrno();
636 else
637 error.Clear();
638
639 if (log)
Greg Clayton63afdb02011-06-17 01:22:15 +0000640 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds = %i, fd = %i, NULL, NULL, timeout = %p) => %d, error = %s",
Greg Clayton8d2ea282011-07-17 20:36:25 +0000641 this, nfds, m_fd_recv, tv_ptr, num_set_fds, error.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000642
643 if (error_ptr)
644 *error_ptr = error;
645
646 if (error.Fail())
647 {
648 switch (error.GetError())
649 {
650 case EBADF: // One of the descriptor sets specified an invalid descriptor.
Greg Clayton5fba9ee2011-05-19 00:17:26 +0000651 return eConnectionStatusLostConnection;
652
Chris Lattner24943d22010-06-08 16:52:24 +0000653 case EINVAL: // The specified time limit is invalid. One of its components is negative or too large.
654 default: // Other unknown error
655 return eConnectionStatusError;
656
657 case EAGAIN: // The kernel was (perhaps temporarily) unable to
658 // allocate the requested number of file descriptors,
659 // or we have non-blocking IO
660 case EINTR: // A signal was delivered before the time limit
661 // expired and before any of the selected events
662 // occurred.
663 break; // Lets keep reading to until we timeout
664 }
665 }
666 else if (num_set_fds == 0)
667 {
668 return eConnectionStatusTimedOut;
669 }
670 else if (num_set_fds > 0)
671 {
Greg Claytonf4669562012-08-08 22:27:52 +0000672 if (m_pipe_read != -1 && FD_ISSET(m_pipe_read, &read_fds))
Jim Ingham3ce4df52012-07-12 01:17:55 +0000673 {
674 // We got a command to exit. Read the data from that pipe:
675 char buffer[16];
676 ssize_t bytes_read;
677
678 do
679 {
Greg Claytonf4669562012-08-08 22:27:52 +0000680 bytes_read = ::read (m_pipe_read, buffer, sizeof(buffer));
Jim Ingham3ce4df52012-07-12 01:17:55 +0000681 } while (bytes_read < 0 && errno == EINTR);
682 assert (bytes_read == 1 && buffer[0] == 'q');
683
684 if (log)
685 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() got data: %*s from the command channel.",
686 this, (int) bytes_read, buffer);
Jim Inghamb4386992012-07-17 01:47:11 +0000687
Jim Ingham3ce4df52012-07-12 01:17:55 +0000688 return eConnectionStatusEndOfFile;
689 }
690 else
691 return eConnectionStatusSuccess;
Chris Lattner24943d22010-06-08 16:52:24 +0000692 }
693 }
694
695 if (error_ptr)
Greg Clayton58e26e02011-03-24 04:28:38 +0000696 error_ptr->SetErrorString("not connected");
Chris Lattner24943d22010-06-08 16:52:24 +0000697 return eConnectionStatusLostConnection;
698}
699
700ConnectionStatus
701ConnectionFileDescriptor::Close (int& fd, Error *error_ptr)
702{
703 if (error_ptr)
704 error_ptr->Clear();
705 bool success = true;
Greg Claytondb9d6f42012-01-31 04:56:17 +0000706 // Avoid taking a lock if we can
Chris Lattner24943d22010-06-08 16:52:24 +0000707 if (fd >= 0)
708 {
Greg Claytondb9d6f42012-01-31 04:56:17 +0000709 Mutex::Locker locker (m_mutex);
710 // Check the FD after the lock is taken to ensure only one thread
711 // can get into the close scope below
712 if (fd >= 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000713 {
Greg Claytondb9d6f42012-01-31 04:56:17 +0000714 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
715 if (log)
716 log->Printf ("%p ConnectionFileDescriptor::Close (fd = %i)", this,fd);
717
718 success = ::close (fd) == 0;
719 // A reference to a FD was passed in, set it to an invalid value
720 fd = -1;
721 if (!success && error_ptr)
722 {
723 // Only set the error if we have been asked to since something else
724 // might have caused us to try and shut down the connection and may
725 // have already set the error.
726 error_ptr->SetErrorToErrno();
727 }
Chris Lattner24943d22010-06-08 16:52:24 +0000728 }
Chris Lattner24943d22010-06-08 16:52:24 +0000729 }
Chris Lattner24943d22010-06-08 16:52:24 +0000730 if (success)
731 return eConnectionStatusSuccess;
732 else
733 return eConnectionStatusError;
734}
735
736ConnectionStatus
Greg Clayton36f63a92010-10-19 03:25:40 +0000737ConnectionFileDescriptor::NamedSocketAccept (const char *socket_name, Error *error_ptr)
738{
739 ConnectionStatus result = eConnectionStatusError;
740 struct sockaddr_un saddr_un;
741
Greg Clayton8d2ea282011-07-17 20:36:25 +0000742 m_fd_send_type = m_fd_recv_type = eFDTypeSocket;
Greg Clayton36f63a92010-10-19 03:25:40 +0000743
744 int listen_socket = ::socket (AF_UNIX, SOCK_STREAM, 0);
745 if (listen_socket == -1)
746 {
747 if (error_ptr)
748 error_ptr->SetErrorToErrno();
749 return eConnectionStatusError;
750 }
751
752 saddr_un.sun_family = AF_UNIX;
753 ::strncpy(saddr_un.sun_path, socket_name, sizeof(saddr_un.sun_path) - 1);
754 saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
Johnny Chen0d6e36a2010-12-20 21:07:54 +0000755#if defined(__APPLE__) || defined(__FreeBSD__)
Greg Clayton36f63a92010-10-19 03:25:40 +0000756 saddr_un.sun_len = SUN_LEN (&saddr_un);
Johnny Chen0d6e36a2010-12-20 21:07:54 +0000757#endif
Greg Clayton36f63a92010-10-19 03:25:40 +0000758
759 if (::bind (listen_socket, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0)
760 {
761 if (::listen (listen_socket, 5) == 0)
762 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000763 m_fd_send = m_fd_recv = ::accept (listen_socket, NULL, 0);
764 if (m_fd_send > 0)
Greg Clayton36f63a92010-10-19 03:25:40 +0000765 {
766 m_should_close_fd = true;
767
768 if (error_ptr)
769 error_ptr->Clear();
770 result = eConnectionStatusSuccess;
771 }
772 }
773 }
774
775 if (result != eConnectionStatusSuccess)
776 {
777 if (error_ptr)
778 error_ptr->SetErrorToErrno();
779 }
780 // We are done with the listen port
781 Close (listen_socket, NULL);
782 return result;
783}
784
785ConnectionStatus
Greg Claytonb72d0f02011-04-12 05:54:46 +0000786ConnectionFileDescriptor::NamedSocketConnect (const char *socket_name, Error *error_ptr)
787{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000788 Disconnect (NULL);
789 m_fd_send_type = m_fd_recv_type = eFDTypeSocket;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000790
791 // Open the socket that was passed in as an option
792 struct sockaddr_un saddr_un;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000793 m_fd_send = m_fd_recv = ::socket (AF_UNIX, SOCK_STREAM, 0);
794 if (m_fd_send == -1)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000795 {
796 if (error_ptr)
797 error_ptr->SetErrorToErrno();
798 return eConnectionStatusError;
799 }
800
801 saddr_un.sun_family = AF_UNIX;
802 ::strncpy(saddr_un.sun_path, socket_name, sizeof(saddr_un.sun_path) - 1);
803 saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
804#if defined(__APPLE__) || defined(__FreeBSD__)
805 saddr_un.sun_len = SUN_LEN (&saddr_un);
806#endif
807
Greg Clayton8d2ea282011-07-17 20:36:25 +0000808 if (::connect (m_fd_send, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000809 {
810 if (error_ptr)
811 error_ptr->SetErrorToErrno();
Greg Clayton8d2ea282011-07-17 20:36:25 +0000812 Disconnect (NULL);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000813 return eConnectionStatusError;
814 }
815 if (error_ptr)
816 error_ptr->Clear();
817 return eConnectionStatusSuccess;
818}
819
820ConnectionStatus
Chris Lattner24943d22010-06-08 16:52:24 +0000821ConnectionFileDescriptor::SocketListen (uint16_t listen_port_num, Error *error_ptr)
822{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000823 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
824 if (log)
825 log->Printf ("%p ConnectionFileDescriptor::SocketListen (port = %i)", this, listen_port_num);
Chris Lattner24943d22010-06-08 16:52:24 +0000826
Greg Clayton8d2ea282011-07-17 20:36:25 +0000827 Disconnect (NULL);
828 m_fd_send_type = m_fd_recv_type = eFDTypeSocket;
Chris Lattner24943d22010-06-08 16:52:24 +0000829 int listen_port = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
830 if (listen_port == -1)
831 {
832 if (error_ptr)
833 error_ptr->SetErrorToErrno();
834 return eConnectionStatusError;
835 }
836
837 // enable local address reuse
838 SetSocketOption (listen_port, SOL_SOCKET, SO_REUSEADDR, 1);
839
Greg Claytond5b3e3c2011-07-19 20:03:42 +0000840 SocketAddress localhost;
841 if (localhost.SetToLocalhost (AF_INET, listen_port_num))
Chris Lattner24943d22010-06-08 16:52:24 +0000842 {
Greg Claytond5b3e3c2011-07-19 20:03:42 +0000843 int err = ::bind (listen_port, localhost, localhost.GetLength());
844 if (err == -1)
845 {
846 if (error_ptr)
847 error_ptr->SetErrorToErrno();
848 Close (listen_port, NULL);
849 return eConnectionStatusError;
850 }
Chris Lattner24943d22010-06-08 16:52:24 +0000851
Greg Claytond5b3e3c2011-07-19 20:03:42 +0000852 err = ::listen (listen_port, 1);
853 if (err == -1)
854 {
855 if (error_ptr)
856 error_ptr->SetErrorToErrno();
857 Close (listen_port, NULL);
858 return eConnectionStatusError;
859 }
Chris Lattner24943d22010-06-08 16:52:24 +0000860
Greg Claytond5b3e3c2011-07-19 20:03:42 +0000861 m_fd_send = m_fd_recv = ::accept (listen_port, NULL, 0);
862 if (m_fd_send == -1)
863 {
864 if (error_ptr)
865 error_ptr->SetErrorToErrno();
866 Close (listen_port, NULL);
867 return eConnectionStatusError;
868 }
Chris Lattner24943d22010-06-08 16:52:24 +0000869 }
870
871 // We are done with the listen port
872 Close (listen_port, NULL);
873
874 m_should_close_fd = true;
875
876 // Keep our TCP packets coming without any delays.
Greg Clayton8d2ea282011-07-17 20:36:25 +0000877 SetSocketOption (m_fd_send, IPPROTO_TCP, TCP_NODELAY, 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000878 if (error_ptr)
879 error_ptr->Clear();
880 return eConnectionStatusSuccess;
881}
882
883ConnectionStatus
Greg Clayton1e5b0212011-07-15 16:31:38 +0000884ConnectionFileDescriptor::ConnectTCP (const char *host_and_port, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000885{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000886 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
887 if (log)
888 log->Printf ("%p ConnectionFileDescriptor::ConnectTCP (host/port = %s)", this, host_and_port);
889 Disconnect (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000890
Greg Clayton8d2ea282011-07-17 20:36:25 +0000891 m_fd_send_type = m_fd_recv_type = eFDTypeSocket;
Chris Lattner24943d22010-06-08 16:52:24 +0000892 std::string host_str;
893 std::string port_str;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000894 int32_t port = INT32_MIN;
895 if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, error_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +0000896 return eConnectionStatusError;
Chris Lattner24943d22010-06-08 16:52:24 +0000897
Chris Lattner24943d22010-06-08 16:52:24 +0000898 // Create the socket
Greg Clayton8d2ea282011-07-17 20:36:25 +0000899 m_fd_send = m_fd_recv = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
900 if (m_fd_send == -1)
Chris Lattner24943d22010-06-08 16:52:24 +0000901 {
902 if (error_ptr)
903 error_ptr->SetErrorToErrno();
904 return eConnectionStatusError;
905 }
906
907 m_should_close_fd = true;
908
909 // Enable local address reuse
Greg Clayton8d2ea282011-07-17 20:36:25 +0000910 SetSocketOption (m_fd_send, SOL_SOCKET, SO_REUSEADDR, 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000911
912 struct sockaddr_in sa;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000913 ::memset (&sa, 0, sizeof (sa));
Chris Lattner24943d22010-06-08 16:52:24 +0000914 sa.sin_family = AF_INET;
915 sa.sin_port = htons (port);
916
917 int inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
918
919 if (inet_pton_result <= 0)
920 {
921 struct hostent *host_entry = gethostbyname (host_str.c_str());
922 if (host_entry)
923 host_str = ::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list);
924 inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
925 if (inet_pton_result <= 0)
926 {
927
928 if (error_ptr)
929 {
930 if (inet_pton_result == -1)
931 error_ptr->SetErrorToErrno();
932 else
Greg Clayton58e26e02011-03-24 04:28:38 +0000933 error_ptr->SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000934 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000935 Disconnect (NULL);
936
Chris Lattner24943d22010-06-08 16:52:24 +0000937 return eConnectionStatusError;
938 }
939 }
940
Greg Clayton8d2ea282011-07-17 20:36:25 +0000941 if (-1 == ::connect (m_fd_send, (const struct sockaddr *)&sa, sizeof(sa)))
Chris Lattner24943d22010-06-08 16:52:24 +0000942 {
943 if (error_ptr)
944 error_ptr->SetErrorToErrno();
Greg Clayton8d2ea282011-07-17 20:36:25 +0000945 Disconnect (NULL);
946
Chris Lattner24943d22010-06-08 16:52:24 +0000947 return eConnectionStatusError;
948 }
949
950 // Keep our TCP packets coming without any delays.
Greg Clayton8d2ea282011-07-17 20:36:25 +0000951 SetSocketOption (m_fd_send, IPPROTO_TCP, TCP_NODELAY, 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000952 if (error_ptr)
953 error_ptr->Clear();
954 return eConnectionStatusSuccess;
955}
956
Greg Clayton1e5b0212011-07-15 16:31:38 +0000957ConnectionStatus
958ConnectionFileDescriptor::ConnectUDP (const char *host_and_port, Error *error_ptr)
959{
Greg Clayton8d2ea282011-07-17 20:36:25 +0000960 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
961 if (log)
962 log->Printf ("%p ConnectionFileDescriptor::ConnectUDP (host/port = %s)", this, host_and_port);
963 Disconnect (NULL);
964
965 m_fd_send_type = m_fd_recv_type = eFDTypeSocketUDP;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000966
Greg Clayton1e5b0212011-07-15 16:31:38 +0000967 std::string host_str;
968 std::string port_str;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000969 int32_t port = INT32_MIN;
970 if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, error_ptr))
Greg Clayton1e5b0212011-07-15 16:31:38 +0000971 return eConnectionStatusError;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000972
973 // Setup the receiving end of the UDP connection on this localhost
974 // on port zero. After we bind to port zero we can read the port.
975 m_fd_recv = ::socket (AF_INET, SOCK_DGRAM, 0);
976 if (m_fd_recv == -1)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000977 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000978 // Socket creation failed...
Greg Clayton1e5b0212011-07-15 16:31:38 +0000979 if (error_ptr)
980 error_ptr->SetErrorToErrno();
Greg Clayton1e5b0212011-07-15 16:31:38 +0000981 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000982 else
Greg Clayton1e5b0212011-07-15 16:31:38 +0000983 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000984 // Socket was created, now lets bind to the requested port
Peter Collingbourneea6d7832011-07-22 19:12:42 +0000985 SocketAddress addr;
986 addr.SetToLocalhost (AF_INET, 0);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000987
Peter Collingbourneea6d7832011-07-22 19:12:42 +0000988 if (::bind (m_fd_recv, addr, addr.GetLength()) == -1)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000989 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000990 // Bind failed...
Greg Clayton1e5b0212011-07-15 16:31:38 +0000991 if (error_ptr)
Greg Clayton8d2ea282011-07-17 20:36:25 +0000992 error_ptr->SetErrorToErrno();
993 Disconnect (NULL);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000994 }
995 }
Greg Clayton8d2ea282011-07-17 20:36:25 +0000996
997 if (m_fd_recv == -1)
998 return eConnectionStatusError;
999
1000 // At this point we have setup the recieve port, now we need to
1001 // setup the UDP send socket
1002
1003 struct addrinfo hints;
1004 struct addrinfo *service_info_list = NULL;
Greg Clayton1e5b0212011-07-15 16:31:38 +00001005
Greg Clayton8d2ea282011-07-17 20:36:25 +00001006 ::memset (&hints, 0, sizeof(hints));
1007 hints.ai_family = AF_INET;
1008 hints.ai_socktype = SOCK_DGRAM;
1009 int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list);
1010 if (err != 0)
Greg Clayton1e5b0212011-07-15 16:31:38 +00001011 {
1012 if (error_ptr)
Greg Clayton8d2ea282011-07-17 20:36:25 +00001013 error_ptr->SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
1014 host_str.c_str(),
1015 port_str.c_str(),
1016 err,
1017 gai_strerror(err));
1018 Disconnect (NULL);
1019 return eConnectionStatusError;
Greg Clayton1e5b0212011-07-15 16:31:38 +00001020 }
1021
Greg Clayton8d2ea282011-07-17 20:36:25 +00001022 for (struct addrinfo *service_info_ptr = service_info_list;
1023 service_info_ptr != NULL;
1024 service_info_ptr = service_info_ptr->ai_next)
1025 {
1026 m_fd_send = ::socket (service_info_ptr->ai_family,
1027 service_info_ptr->ai_socktype,
1028 service_info_ptr->ai_protocol);
1029
1030 if (m_fd_send != -1)
1031 {
Greg Claytonac304e42011-07-19 16:44:54 +00001032 m_udp_send_sockaddr = service_info_ptr;
Greg Clayton8d2ea282011-07-17 20:36:25 +00001033 break;
1034 }
1035 else
1036 continue;
1037 }
1038
1039 :: freeaddrinfo (service_info_list);
1040
1041 if (m_fd_send == -1)
1042 {
1043 Disconnect (NULL);
1044 return eConnectionStatusError;
1045 }
1046
Greg Clayton1e5b0212011-07-15 16:31:38 +00001047 if (error_ptr)
1048 error_ptr->Clear();
Greg Clayton8d2ea282011-07-17 20:36:25 +00001049
1050 m_should_close_fd = true;
Greg Clayton1e5b0212011-07-15 16:31:38 +00001051 return eConnectionStatusSuccess;
1052}
1053
Greg Claytond0691fe2011-07-02 23:21:06 +00001054#if defined(__MINGW32__) || defined(__MINGW64__)
1055typedef const char * set_socket_option_arg_type;
1056typedef char * get_socket_option_arg_type;
1057#else // #if defined(__MINGW32__) || defined(__MINGW64__)
1058typedef const void * set_socket_option_arg_type;
1059typedef void * get_socket_option_arg_type;
1060#endif // #if defined(__MINGW32__) || defined(__MINGW64__)
1061
1062int
1063ConnectionFileDescriptor::GetSocketOption(int fd, int level, int option_name, int &option_value)
1064{
1065 get_socket_option_arg_type option_value_p = static_cast<get_socket_option_arg_type>(&option_value);
1066 socklen_t option_value_size = sizeof(int);
1067 return ::getsockopt(fd, level, option_name, option_value_p, &option_value_size);
1068}
1069
Chris Lattner24943d22010-06-08 16:52:24 +00001070int
1071ConnectionFileDescriptor::SetSocketOption(int fd, int level, int option_name, int option_value)
1072{
Greg Claytond0691fe2011-07-02 23:21:06 +00001073 set_socket_option_arg_type option_value_p = static_cast<get_socket_option_arg_type>(&option_value);
Greg Claytonf29a08f2011-02-09 17:41:27 +00001074 return ::setsockopt(fd, level, option_name, option_value_p, sizeof(option_value));
Chris Lattner24943d22010-06-08 16:52:24 +00001075}
1076
Greg Clayton63afdb02011-06-17 01:22:15 +00001077bool
Greg Claytond0691fe2011-07-02 23:21:06 +00001078ConnectionFileDescriptor::SetSocketReceiveTimeout (uint32_t timeout_usec)
Greg Clayton63afdb02011-06-17 01:22:15 +00001079{
Greg Clayton8d2ea282011-07-17 20:36:25 +00001080 switch (m_fd_recv_type)
Greg Clayton63afdb02011-06-17 01:22:15 +00001081 {
Greg Claytond52d00f2011-07-16 03:19:08 +00001082 case eFDTypeFile: // Other FD requireing read/write
1083 break;
1084
1085 case eFDTypeSocket: // Socket requiring send/recv
1086 case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom
Greg Clayton63afdb02011-06-17 01:22:15 +00001087 {
Greg Claytond52d00f2011-07-16 03:19:08 +00001088 // Check in case timeout for m_fd has already been set to this value
1089 if (timeout_usec == m_socket_timeout_usec)
1090 return true;
1091 //printf ("ConnectionFileDescriptor::SetSocketReceiveTimeout (timeout_usec = %u)\n", timeout_usec);
1092
1093 struct timeval timeout;
Jim Ingham64f8cab2012-01-21 02:03:41 +00001094 if (timeout_usec == UINT32_MAX)
1095 {
1096 timeout.tv_sec = 0;
1097 timeout.tv_usec = 0;
1098 }
Greg Clayton838c27d2012-01-21 02:28:13 +00001099 else if (timeout_usec == 0)
1100 {
1101 // Sending in zero does an infinite timeout, so set this as low
1102 // as we can go to get an effective zero timeout...
1103 timeout.tv_sec = 0;
1104 timeout.tv_usec = 1;
1105 }
Jim Ingham64f8cab2012-01-21 02:03:41 +00001106 else
1107 {
1108 timeout.tv_sec = timeout_usec / TimeValue::MicroSecPerSec;
1109 timeout.tv_usec = timeout_usec % TimeValue::MicroSecPerSec;
1110 }
Greg Clayton8d2ea282011-07-17 20:36:25 +00001111 if (::setsockopt (m_fd_recv, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == 0)
Greg Claytond52d00f2011-07-16 03:19:08 +00001112 {
1113 m_socket_timeout_usec = timeout_usec;
1114 return true;
1115 }
Greg Clayton63afdb02011-06-17 01:22:15 +00001116 }
1117 }
1118 return false;
1119}
1120
Greg Clayton8d2ea282011-07-17 20:36:25 +00001121in_port_t
1122ConnectionFileDescriptor::GetSocketPort (int fd)
1123{
1124 // We bound to port zero, so we need to figure out which port we actually bound to
Greg Claytonac304e42011-07-19 16:44:54 +00001125 SocketAddress sock_addr;
1126 socklen_t sock_addr_len = sock_addr.GetMaxLength ();
1127 if (::getsockname (fd, sock_addr, &sock_addr_len) == 0)
1128 return sock_addr.GetPort ();
Greg Clayton8d2ea282011-07-17 20:36:25 +00001129
Greg Claytonac304e42011-07-19 16:44:54 +00001130 return 0;
Greg Clayton8d2ea282011-07-17 20:36:25 +00001131}
1132
1133// If the read file descriptor is a socket, then return
1134// the port number that is being used by the socket.
1135in_port_t
1136ConnectionFileDescriptor::GetReadPort () const
1137{
1138 return ConnectionFileDescriptor::GetSocketPort (m_fd_recv);
1139}
1140
1141// If the write file descriptor is a socket, then return
1142// the port number that is being used by the socket.
1143in_port_t
1144ConnectionFileDescriptor::GetWritePort () const
1145{
1146 return ConnectionFileDescriptor::GetSocketPort (m_fd_send);
1147}
1148
Chris Lattner24943d22010-06-08 16:52:24 +00001149