libfilezilla
socket.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_SOCKET_HEADER
2 #define LIBFILEZILLA_SOCKET_HEADER
3 
11 #include "libfilezilla.hpp"
12 
13 #include "event_handler.hpp"
14 #include "iputils.hpp"
15 
16 #include <memory>
17 
18 #include <errno.h>
19 
21 struct sockaddr;
22 
23 namespace fz {
24 class buffer;
25 class thread_pool;
26 
35 {
41  connection_next = 0x1,
42 
47  connection = 0x2,
48 
53  read = 0x4,
54 
59  write = 0x8,
60 };
61 
62 inline bool operator&(socket_event_flag lhs, socket_event_flag rhs) {
63  return (static_cast<std::underlying_type_t<socket_event_flag>>(lhs) & static_cast<std::underlying_type_t<socket_event_flag>>(rhs)) != 0;
64 }
65 inline socket_event_flag operator|(socket_event_flag lhs, socket_event_flag rhs)
66 {
67  return static_cast<socket_event_flag>(static_cast<std::underlying_type_t<socket_event_flag>>(lhs) | static_cast<std::underlying_type_t<socket_event_flag>>(rhs));
68 }
69 inline socket_event_flag& operator|=(socket_event_flag& lhs, socket_event_flag rhs)
70 {
71  lhs = lhs | rhs;
72  return lhs;
73 }
74 
75 
84 class FZ_PUBLIC_SYMBOL socket_event_source
85 {
86 public:
87  virtual ~socket_event_source() = default;
88 
94  return root_;
95  }
96 
97 protected:
98  socket_event_source() = delete;
100  : root_(root)
101  {}
102 
103  socket_event_source* const root_{};
104 };
105 
107 struct socket_event_type;
108 
132 
134 struct hostaddress_event_type{};
135 
140 
147 void FZ_PUBLIC_SYMBOL remove_socket_events(event_handler * handler, socket_event_source const* const source);
148 
160 fz::socket_event_flag FZ_PUBLIC_SYMBOL change_socket_event_handler(event_handler * old_handler, event_handler * new_handler, socket_event_source const* const source, fz::socket_event_flag remove);
161 
163 class socket_thread;
164 
166 class FZ_PUBLIC_SYMBOL socket_base
167 {
168 public:
176  int set_buffer_sizes(int size_receive, int size_send);
177 
179  address_type address_family() const;
180 
186  std::string local_ip(bool strip_zone_index = false) const;
187 
193  int local_port(int& error) const;
194 
195  static std::string address_to_string(sockaddr const* addr, int addr_len, bool with_port = true, bool strip_zone_index = false);
196  static std::string address_to_string(char const* buf, int buf_len);
197 
203  bool bind(std::string const& address);
204 
205 #if FZ_WINDOWS
206  typedef intptr_t socket_t;
207 #else
208  typedef int socket_t;
209 #endif
210 
211 protected:
212  friend class socket_thread;
213 
214  socket_base(thread_pool& pool, event_handler* evt_handler, socket_event_source* ev_source);
215  virtual ~socket_base() = default;
216 
217  int close();
218 
219  // Note: Unlocks the lock.
220  void detach_thread(scoped_lock & l);
221 
222  thread_pool & thread_pool_;
223  event_handler* evt_handler_;
224 
225  socket_thread* socket_thread_{};
226 
227  socket_event_source * const ev_source_{};
228 
229  socket_t fd_{-1};
230 
231  unsigned int port_{};
232 
233  int family_;
234 
235  int buffer_sizes_[2];
236 };
237 
238 class socket;
239 
241 {
243  none,
244 
246  listening,
247 };
248 
250 class FZ_PUBLIC_SYMBOL socket_descriptor final
251 {
252 public:
253  socket_descriptor() = default;
255  explicit socket_descriptor(socket_base::socket_t fd) noexcept : fd_(fd) {}
256 
257  socket_descriptor(socket_descriptor const&) = delete;
258  socket_descriptor& operator=(socket_descriptor const&) = delete;
259 
260  socket_descriptor(socket_descriptor && rhs) noexcept { std::swap(fd_, rhs.fd_); }
261  socket_descriptor& operator=(socket_descriptor && rhs) noexcept {
262  std::swap(fd_, rhs.fd_);
263  return *this;
264  }
265 
266  socket_base::socket_t detach() {
267  socket_base::socket_t ret = fd_;
268  fd_ = -1;
269  return ret;
270  }
271 
272  explicit operator bool() const { return fd_ != -1; }
273 
279  std::string peer_ip(bool strip_zone_index = false) const;
280 
286  int peer_port(int& error) const;
287 
288 private:
289  socket_base::socket_t fd_{-1};
290 };
291 
299 class FZ_PUBLIC_SYMBOL listen_socket final : public socket_base, public socket_event_source
300 {
301  friend class socket_base;
302  friend class socket_thread;
303 public:
304  listen_socket(thread_pool& pool, event_handler* evt_handler);
305  virtual ~listen_socket();
306 
307  listen_socket(listen_socket const&) = delete;
308  listen_socket& operator=(listen_socket const&) = delete;
309 
317  int listen(address_type family, int port = 0);
318 
320  std::unique_ptr<socket> accept(int& error, fz::event_handler * handler = nullptr);
321 
328  socket_descriptor fast_accept(int& error);
329 
330  listen_socket_state get_state() const;
331 
332  void set_event_handler(event_handler* pEvtHandler);
333 
334 private:
335  listen_socket_state state_{};
336 };
337 
338 
340 enum class socket_state : unsigned char
341 {
343  none,
344 
348  connecting,
349 
351  connected,
352 
356 
358  shut_down,
359 
361  closed,
362 
364  failed
365 };
366 
372 class FZ_PUBLIC_SYMBOL socket_interface : public socket_event_source
373 {
374 public:
375  socket_interface(socket_interface const&) = delete;
376  socket_interface& operator=(socket_interface const&) = delete;
377 
378  virtual int read(void* buffer, unsigned int size, int& error) = 0;
379  virtual int write(void const* buffer, unsigned int size, int& error) = 0;
380 
381  template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
382  int read(void* buffer, T size, int& error)
383  {
384  if (size < 0) {
385  error = EINVAL;
386  return -1;
387  }
388 
389  return read(buffer, static_cast<unsigned int>(size), error);
390  }
391  template<typename T, std::enable_if_t<std::is_unsigned_v<T> && (sizeof(T) > sizeof(unsigned int)), int> = 0>
392  int read(void* buffer, T size, int& error)
393  {
394  if (size > std::numeric_limits<unsigned int>::max()) {
395  size = std::numeric_limits<unsigned int>::max();
396  }
397  return read(buffer, static_cast<unsigned int>(size), error);
398  }
399 
400  template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
401  int write(void const* buffer, T size, int& error)
402  {
403  if (size < 0) {
404  error = EINVAL;
405  return -1;
406  }
407 
408  return write(buffer, static_cast<std::make_unsigned_t<T>>(size), error);
409  }
410  template<typename T, std::enable_if_t<std::is_unsigned_v<T> && (sizeof(T) > sizeof(unsigned int)), int> = 0>
411  int write(void const* buffer, T size, int& error)
412  {
413  if (size > std::numeric_limits<unsigned int>::max()) {
414  size = std::numeric_limits<unsigned int>::max();
415  }
416  return write(buffer, static_cast<unsigned int>(size), error);
417  }
418 
419  virtual void set_event_handler(event_handler* pEvtHandler, fz::socket_event_flag retrigger_block = fz::socket_event_flag{}) = 0;
420 
421  virtual native_string peer_host() const = 0;
422  virtual int peer_port(int& error) const = 0;
423 
424  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) = 0;
425 
426  virtual fz::socket_state get_state() const = 0;
427 
438  virtual int shutdown() = 0;
439 
441  virtual int shutdown_read() = 0;
442 
443 protected:
444  socket_interface() = delete;
445 
446  explicit socket_interface(socket_event_source * root)
447  : socket_event_source(root)
448  {}
449 };
450 
459 class FZ_PUBLIC_SYMBOL socket final : public socket_base, public socket_interface
460 {
461  friend class socket_thread;
462 public:
463  socket(thread_pool& pool, event_handler* evt_handler);
464  virtual ~socket();
465 
466  socket(socket const&) = delete;
467  socket& operator=(socket const&) = delete;
468 
469  static std::unique_ptr<socket> from_descriptor(socket_descriptor && desc, thread_pool & pool, int & error, fz::event_handler * handler = nullptr);
470 
471  socket_state get_state() const override;
472  bool is_connected() const {
473  socket_state s = get_state();
475  };
476 
490  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override;
491 
507  virtual int read(void *buffer, unsigned int size, int& error) override;
508 
524  virtual int write(void const* buffer, unsigned int size, int& error) override;
525 
531  std::string peer_ip(bool strip_zone_index = false) const;
532 
534  virtual native_string peer_host() const override;
535 
541  virtual int peer_port(int& error) const override;
542 
549  int ideal_send_buffer_size();
550 
551  virtual int shutdown() override;
552 
565  virtual void set_event_handler(event_handler* pEvtHandler, fz::socket_event_flag retrigger_block = fz::socket_event_flag{}) override;
566 
567  enum
568  {
570  flag_nodelay = 0x01,
571 
573  flag_keepalive = 0x02
574  };
575 
576  int flags() const { return flags_; }
577 
579  void set_flags(int flags, bool enable);
580 
582  void set_flags(int flags);
583 
589  void set_keepalive_interval(duration const& d);
590 
591  virtual int shutdown_read() override { return 0; }
592 
593  socket_t get_descriptor();
594 
595 #ifndef FZ_WINDOWS
596 
611  int send_fd(fz::buffer & buf, int fd, int & error);
612 
622  int read_fd(fz::buffer & buf, int &fd, int & error);
623 #endif
624 
625 private:
626  friend class socket_base;
627  friend class listen_socket;
628  native_string host_;
629 
630  duration keepalive_interval_;
631 
632  int flags_{};
633  socket_state state_{};
634 };
635 
651 class FZ_PUBLIC_SYMBOL socket_layer : public socket_interface
652 {
653 public:
654  explicit socket_layer(event_handler* handler, socket_interface& next_layer, bool event_passthrough);
655  virtual ~socket_layer();
656 
657  socket_layer(socket_layer const&) = delete;
658  socket_layer& operator=(socket_layer const&) = delete;
659 
661  virtual void set_event_handler(event_handler* handler, fz::socket_event_flag retrigger_block = fz::socket_event_flag{}) override;
662 
668  virtual native_string peer_host() const override { return next_layer_.peer_host(); }
669 
675  virtual int peer_port(int& error) const override { return next_layer_.peer_port(error); }
676 
678  socket_interface& next() { return next_layer_; }
679 
701  virtual int shutdown_read() override;
702 
703  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override {
704  return next_layer_.connect(host, port, family);
705  }
706 
707  virtual int shutdown() override {
708  return next_layer_.shutdown();
709  }
710 
711  virtual socket_state get_state() const override {
712  return next_layer_.get_state();
713  }
714 
715 protected:
721  void forward_socket_event(socket_event_source* source, socket_event_flag t, int error);
722 
728  void forward_hostaddress_event(socket_event_source* source, std::string const& address);
729 
734  void set_event_passthrough(socket_event_flag retrigger_block = socket_event_flag{});
735 
736  event_handler* event_handler_{};
737  socket_interface& next_layer_;
738  bool event_passthrough_{};
739 };
740 
749 std::string FZ_PUBLIC_SYMBOL socket_error_string(int error);
750 
754 native_string FZ_PUBLIC_SYMBOL socket_error_description(int error);
755 
756 
757 #ifdef FZ_WINDOWS
758 
760 class FZ_PRIVATE_SYMBOL winsock_initializer final
761 {
762 public:
763  winsock_initializer();
764  ~winsock_initializer();
765 
766 private:
767  bool initialized_{};
768 };
769 
770 #ifndef EISCONN
771 #define EISCONN WSAEISCONN
772 #endif
773 #ifndef EINPROGRESS
774 #define EINPROGRESS WSAEINPROGRESS
775 #endif
776 #ifndef EAFNOSUPPORT
777 #define EAFNOSUPPORT WSAEAFNOSUPPORT
778 #endif
779 #ifndef EADDRINUSE
780 #define EADDRINUSE WSAEADDRINUSE
781 #endif
782 #ifndef ENOBUFS
783 #define ENOBUFS WSAENOBUFS
784 #endif
785 #ifndef EPROTONOSUPPORT
786 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
787 #endif
788 #ifndef EALREADY
789 #define EALREADY WSAEALREADY
790 #endif
791 #ifndef ECONNREFUSED
792 #define ECONNREFUSED WSAECONNREFUSED
793 #endif
794 #ifndef ENOTSOCK
795 #define ENOTSOCK WSAENOTSOCK
796 #endif
797 #ifndef ETIMEDOUT
798 #define ETIMEDOUT WSAETIMEDOUT
799 #endif
800 #ifndef ENETUNREACH
801 #define ENETUNREACH WSAENETUNREACH
802 #endif
803 #ifndef EHOSTUNREACH
804 #define EHOSTUNREACH WSAEHOSTUNREACH
805 #endif
806 #ifndef ENOTCONN
807 #define ENOTCONN WSAENOTCONN
808 #endif
809 #ifndef ENETRESET
810 #define ENETRESET WSAENETRESET
811 #endif
812 #ifndef EOPNOTSUPP
813 #define EOPNOTSUPP WSAEOPNOTSUPP
814 #endif
815 #ifndef ESHUTDOWN
816 #define ESHUTDOWN WSAESHUTDOWN
817 #endif
818 #ifndef EMSGSIZE
819 #define EMSGSIZE WSAEMSGSIZE
820 #endif
821 #ifndef ECONNABORTED
822 #define ECONNABORTED WSAECONNABORTED
823 #endif
824 #ifndef ECONNRESET
825 #define ECONNRESET WSAECONNRESET
826 #endif
827 #ifndef EHOSTDOWN
828 #define EHOSTDOWN WSAEHOSTDOWN
829 #endif
830 
831 // For the future:
832 // Handle ERROR_NETNAME_DELETED=64
833 #endif //FZ_WINDOWS
834 
835 }
836 
837 #endif
fz::socket_event_flag change_socket_event_handler(event_handler *old_handler, event_handler *new_handler, socket_event_source const *const source, fz::socket_event_flag remove)
Changes all pending socket events from source.
Data has become available.
std::string socket_error_string(int error)
Gets a symbolic name for socket errors.
A simple scoped lock.
Definition: mutex.hpp:92
Interface for sockets.
Definition: socket.hpp:372
Lightweight holder for socket descriptors.
Definition: socket.hpp:250
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:107
virtual int shutdown_read() override
Definition: socket.hpp:591
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:54
socket_interface & next()
The next layer further down. Usually another layer or the actual socket.
Definition: socket.hpp:678
Declares the event_handler class.
Socket has failed. Further events disabled.
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:166
Simple Listen socket.
Definition: socket.hpp:299
Various functions to deal with IP address strings.
Socket has been closed. Further events disabled.
This is the recommended event class.
Definition: event.hpp:67
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition: socket.hpp:139
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:459
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition: socket.hpp:707
socket_event_source * root() const
Gets the root source.
Definition: socket.hpp:93
void remove_socket_events(event_handler *handler, socket_event_source const *const source)
Remove all pending socket events from source sent to handler.
A base class for socket layers.
Definition: socket.hpp:651
std::wstring native_string
A string in the system&#39;s native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:33
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:340
Only in listening state you can get a connection event.
The namespace used by libfilezilla.
Definition: apply.hpp:17
listen_socket_state
Definition: socket.hpp:240
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
Socket is in its normal working state. You can get send and receive events.
How the socket is initially.
All classes sending socket events should derive from this.
Definition: socket.hpp:84
The duration class represents a time interval in milliseconds.
Definition: time.hpp:290
Sets some global macros and further includes string.hpp.
How the socket is initially.
data can be written.
Write side has finished shutting down. Receive still working normally.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:63
virtual native_string peer_host() const override
Definition: socket.hpp:668
virtual int peer_port(int &error) const override
Definition: socket.hpp:675
socket_event_flag
The type of a socket event.
Definition: socket.hpp:34