socket.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_
  5. #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_
  6. #include <fcntl.h>
  7. #include <netinet/in.h>
  8. #include <stddef.h>
  9. #include <sys/socket.h>
  10. #include <sys/un.h>
  11. #include <string>
  12. #include <vector>
  13. #include "base/macros.h"
  14. namespace forwarder2 {
  15. // Wrapper class around unix socket api. Can be used to create, bind or
  16. // connect to both Unix domain sockets and TCP sockets.
  17. // TODO(pliard): Split this class into TCPSocket and UnixDomainSocket.
  18. class Socket {
  19. public:
  20. Socket();
  21. ~Socket();
  22. bool BindUnix(const std::string& path);
  23. bool BindTcp(const std::string& host, int port);
  24. bool ConnectUnix(const std::string& path);
  25. bool ConnectTcp(const std::string& host, int port);
  26. // Just a wrapper around unix socket shutdown(), see man 2 shutdown.
  27. void Shutdown();
  28. // Just a wrapper around unix socket close(), see man 2 close.
  29. void Close();
  30. bool IsClosed() const { return socket_ < 0; }
  31. int fd() const { return socket_; }
  32. bool Accept(Socket* new_socket);
  33. // Returns the port allocated to this socket or zero on error.
  34. int GetPort();
  35. // Just a wrapper around unix read() function.
  36. // Reads up to buffer_size, but may read less than buffer_size.
  37. // Returns the number of bytes read.
  38. int Read(void* buffer, size_t buffer_size);
  39. // Same as Read() but with a timeout.
  40. int ReadWithTimeout(void* buffer, size_t buffer_size, int timeout_secs);
  41. // Non-blocking version of Read() above. This must be called after a
  42. // successful call to select(). The socket must also be in non-blocking mode
  43. // before calling this method.
  44. int NonBlockingRead(void* buffer, size_t buffer_size);
  45. // Wrapper around send().
  46. int Write(const void* buffer, size_t count);
  47. // Same as NonBlockingRead() but for writing.
  48. int NonBlockingWrite(const void* buffer, size_t count);
  49. // Calls Read() multiple times until num_bytes is written to the provided
  50. // buffer. No bounds checking is performed.
  51. // Returns number of bytes read, which can be different from num_bytes in case
  52. // of errror.
  53. int ReadNumBytes(void* buffer, size_t num_bytes);
  54. // Same as ReadNumBytes() but with a timeout.
  55. int ReadNumBytesWithTimeout(void* buffer, size_t num_bytes, int timeout_secs);
  56. // Calls Write() multiple times until num_bytes is written. No bounds checking
  57. // is performed. Returns number of bytes written, which can be different from
  58. // num_bytes in case of errror.
  59. int WriteNumBytes(const void* buffer, size_t num_bytes);
  60. // Calls WriteNumBytes for the given std::string. Note that the null
  61. // terminator is not written to the socket.
  62. int WriteString(const std::string& buffer);
  63. bool has_error() const { return socket_error_; }
  64. // |event_fd| must be a valid pipe file descriptor created from the
  65. // PipeNotifier and must live (not be closed) at least as long as this socket
  66. // is alive.
  67. void AddEventFd(int event_fd);
  68. // Returns whether Accept() or Connect() was interrupted because the socket
  69. // received an external event fired through the provided fd.
  70. bool DidReceiveEventOnFd(int fd) const;
  71. bool DidReceiveEvent() const;
  72. static pid_t GetUnixDomainSocketProcessOwner(const std::string& path);
  73. private:
  74. enum EventType {
  75. READ,
  76. WRITE
  77. };
  78. union SockAddr {
  79. // IPv4 sockaddr
  80. sockaddr_in addr4;
  81. // IPv6 sockaddr
  82. sockaddr_in6 addr6;
  83. // Unix Domain sockaddr
  84. sockaddr_un addr_un;
  85. };
  86. struct Event {
  87. int fd;
  88. bool was_fired;
  89. };
  90. bool SetNonBlocking();
  91. // If |host| is empty, use localhost.
  92. bool InitTcpSocket(const std::string& host, int port);
  93. bool InitUnixSocket(const std::string& path);
  94. bool BindAndListen();
  95. bool Connect();
  96. bool Resolve(const std::string& host);
  97. bool InitSocketInternal();
  98. void SetSocketError();
  99. // Waits until a read or write i/o event has happened.
  100. bool WaitForEvent(EventType type, int timeout_secs);
  101. int socket_;
  102. int port_;
  103. bool socket_error_;
  104. // Family of the socket (AF_INET, AF_INET6 or PF_UNIX).
  105. int family_;
  106. SockAddr addr_;
  107. // Points to one of the members of the above union depending on the family.
  108. sockaddr* addr_ptr_;
  109. // Length of one of the members of the above union depending on the family.
  110. socklen_t addr_len_;
  111. // Used to listen for external events (e.g. process received a SIGTERM) while
  112. // blocking on I/O operations.
  113. std::vector<Event> events_;
  114. DISALLOW_COPY_AND_ASSIGN(Socket);
  115. };
  116. } // namespace forwarder
  117. #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_