libfilezilla
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 thread_pool;
25 
26 enum class socket_event_flag
27 {
28  // This is a nonfatal condition. It
29  // means there are additional addresses to try.
30  connection_next,
31 
32  connection,
33  read,
34  write
35 };
36 
45 class FZ_PUBLIC_SYMBOL socket_event_source
46 {
47 public:
48  virtual ~socket_event_source() = default;
49 
55  return root_;
56  }
57 
58 protected:
59  socket_event_source() = default;
61  : root_(root)
62  {}
63 
64  socket_event_source* const root_{};
65 };
66 
68 struct socket_event_type;
69 
86 
88 struct hostaddress_event_type{};
89 
94 
101 void FZ_PUBLIC_SYMBOL remove_socket_events(event_handler * handler, socket_event_source const* const source);
102 
113 void FZ_PUBLIC_SYMBOL change_socket_event_handler(event_handler * old_handler, event_handler * new_handler, socket_event_source const* const source);
114 
116 class socket_thread;
117 
119 class FZ_PUBLIC_SYMBOL socket_base
120 {
121 public:
129  int set_buffer_sizes(int size_receive, int size_send);
130 
132  address_type address_family() const;
133 
139  std::string local_ip(bool strip_zone_index = false) const;
140 
146  int local_port(int& error) const;
147 
148  static std::string address_to_string(sockaddr const* addr, int addr_len, bool with_port = true, bool strip_zone_index = false);
149  static std::string address_to_string(char const* buf, int buf_len);
150 
156  bool bind(std::string const& address);
157 
158 #if FZ_WINDOWS
159  typedef intptr_t socket_t;
160 #else
161  typedef int socket_t;
162 #endif
163 
164 protected:
165  friend class socket_thread;
166 
167  socket_base(thread_pool& pool, event_handler* evt_handler, socket_event_source* ev_source);
168  virtual ~socket_base() = default;
169 
170  int close();
171 
172  void do_set_event_handler(event_handler* pEvtHandler);
173 
174  // Note: Unlocks the lock.
175  void detach_thread(scoped_lock & l);
176 
177  thread_pool & thread_pool_;
178  event_handler* evt_handler_;
179 
180  socket_t fd_{-1};
181 
182  socket_thread* socket_thread_{};
183 
184  unsigned int port_{};
185 
186  int family_;
187 
188  int buffer_sizes_[2];
189 
190  socket_event_source * const ev_source_{};
191 };
192 
193 class socket;
194 
196 {
198  none,
199 
201  listening,
202 };
203 
204 class FZ_PUBLIC_SYMBOL socket_descriptor final
205 {
206 public:
207  socket_descriptor() = default;
209  explicit socket_descriptor(socket_base::socket_t fd) noexcept : fd_(fd) {}
210 
211  socket_descriptor(socket_descriptor const&) = delete;
212  socket_descriptor& operator=(socket_descriptor const&) = delete;
213 
214  socket_descriptor(socket_descriptor && rhs) noexcept { std::swap(fd_, rhs.fd_); }
215  socket_descriptor& operator=(socket_descriptor && rhs) noexcept {
216  std::swap(fd_, rhs.fd_);
217  return *this;
218  }
219 
220  socket_base::socket_t detach() {
221  socket_base::socket_t ret = fd_;
222  fd_ = -1;
223  return ret;
224  }
225 
226  explicit operator bool() const { return fd_ != -1; }
227 
228 private:
229  socket_base::socket_t fd_{-1};
230 };
231 
239 class FZ_PUBLIC_SYMBOL listen_socket final : public socket_base, public socket_event_source
240 {
241  friend class socket_base;
242  friend class socket_thread;
243 public:
244  listen_socket(thread_pool& pool, event_handler* evt_handler);
245  virtual ~listen_socket();
246 
247  listen_socket(listen_socket const&) = delete;
248  listen_socket& operator=(listen_socket const&) = delete;
249 
258  int listen(address_type family, int port = 0);
259 
261  std::unique_ptr<socket> accept(int& error);
262 
269  socket_descriptor fast_accept(int& error);
270 
271  listen_socket_state get_state() const;
272 
273  void set_event_handler(event_handler* pEvtHandler) {
274  do_set_event_handler(pEvtHandler);
275  }
276 
277 private:
278  listen_socket_state state_{};
279 };
280 
281 
283 enum class socket_state
284 {
286  none,
287 
291  connecting,
292 
294  connected,
295 
299 
301  shut_down,
302 
304  closed,
305 
307  failed
308 };
309 
315 class FZ_PUBLIC_SYMBOL socket_interface : public socket_event_source
316 {
317 public:
318  socket_interface(socket_interface const&) = delete;
319  socket_interface& operator=(socket_interface const&) = delete;
320 
321  virtual int read(void* buffer, unsigned int size, int& error) = 0;
322  virtual int write(void const* buffer, unsigned int size, int& error) = 0;
323 
324  virtual void set_event_handler(event_handler* pEvtHandler) = 0;
325 
326  virtual native_string peer_host() const = 0;
327  virtual int peer_port(int& error) const = 0;
328 
329  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) = 0;
330 
331  virtual fz::socket_state get_state() const = 0;
332 
339  virtual int shutdown() = 0;
340 
341  virtual int shutdown_read() = 0;
342 
343 protected:
344  socket_interface() = default;
345 
346  explicit socket_interface(socket_event_source * root)
347  : socket_event_source(root)
348  {}
349 };
350 
359 class FZ_PUBLIC_SYMBOL socket final : public socket_base, public socket_interface
360 {
361  friend class socket_thread;
362 public:
363  socket(thread_pool& pool, event_handler* evt_handler);
364  virtual ~socket();
365 
366  socket(socket const&) = delete;
367  socket& operator=(socket const&) = delete;
368 
369  static std::unique_ptr<socket> from_descriptor(socket_descriptor && desc, thread_pool & pool, int & error);
370 
371  socket_state get_state() const override;
372  bool is_connected() const {
373  socket_state s = get_state();
375  };
376 
390  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override;
391 
407  virtual int read(void *buffer, unsigned int size, int& error) override;
408 
424  virtual int write(void const* buffer, unsigned int size, int& error) override;
425 
431  std::string peer_ip(bool strip_zone_index = false) const;
432 
434  virtual native_string peer_host() const override;
435 
441  virtual int peer_port(int& error) const override;
442 
449  int ideal_send_buffer_size();
450 
455  void retrigger(socket_event_flag event);
456 
457  virtual int shutdown() override;
458 
459  virtual void set_event_handler(event_handler* pEvtHandler) override;
460 
461  enum
462  {
464  flag_nodelay = 0x01,
465 
467  flag_keepalive = 0x02
468  };
469 
470  int flags() const { return flags_; }
471 
473  void set_flags(int flags, bool enable);
474 
476  void set_flags(int flags);
477 
483  void set_keepalive_interval(duration const& d);
484 
485  virtual int shutdown_read() override { return 0; }
486 
487 private:
488  friend class socket_base;
489  friend class listen_socket;
490  native_string host_;
491 
492  socket_state state_{};
493 
494  int flags_{};
495  duration keepalive_interval_;
496 };
497 
511 class FZ_PUBLIC_SYMBOL socket_layer : public socket_interface
512 {
513 public:
514  explicit socket_layer(event_handler* handler, socket_interface& next_layer, bool event_passthrough);
515  virtual ~socket_layer();
516 
517  socket_layer(socket_layer const&) = delete;
518  socket_layer& operator=(socket_layer const&) = delete;
519 
521  virtual void set_event_handler(event_handler* handler) override;
522 
528  virtual native_string peer_host() const override { return next_layer_.peer_host(); }
529 
535  virtual int peer_port(int& error) const override { return next_layer_.peer_port(error); }
536 
538  socket_interface& next() { return next_layer_; }
539 
561  virtual int shutdown_read() override;
562 
563 protected:
569  void forward_socket_event(socket_event_source* source, socket_event_flag t, int error);
570 
576  void forward_hostaddress_event(socket_event_source* source, std::string const& address);
577 
582  void set_event_passthrough();
583 
584  event_handler* event_handler_{};
585  socket_interface& next_layer_;
586  bool event_passthrough_{};
587 };
588 
597 std::string FZ_PUBLIC_SYMBOL socket_error_string(int error);
598 
602 native_string FZ_PUBLIC_SYMBOL socket_error_description(int error);
603 
604 
605 #ifdef FZ_WINDOWS
606 
607 #ifndef EISCONN
608 #define EISCONN WSAEISCONN
609 #endif
610 #ifndef EINPROGRESS
611 #define EINPROGRESS WSAEINPROGRESS
612 #endif
613 #ifndef EAFNOSUPPORT
614 #define EAFNOSUPPORT WSAEAFNOSUPPORT
615 #endif
616 #ifndef EADDRINUSE
617 #define EADDRINUSE WSAEADDRINUSE
618 #endif
619 #ifndef ENOBUFS
620 #define ENOBUFS WSAENOBUFS
621 #endif
622 #ifndef EPROTONOSUPPORT
623 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
624 #endif
625 #ifndef EALREADY
626 #define EALREADY WSAEALREADY
627 #endif
628 #ifndef ECONNREFUSED
629 #define ECONNREFUSED WSAECONNREFUSED
630 #endif
631 #ifndef ENOTSOCK
632 #define ENOTSOCK WSAENOTSOCK
633 #endif
634 #ifndef ETIMEDOUT
635 #define ETIMEDOUT WSAETIMEDOUT
636 #endif
637 #ifndef ENETUNREACH
638 #define ENETUNREACH WSAENETUNREACH
639 #endif
640 #ifndef EHOSTUNREACH
641 #define EHOSTUNREACH WSAEHOSTUNREACH
642 #endif
643 #ifndef ENOTCONN
644 #define ENOTCONN WSAENOTCONN
645 #endif
646 #ifndef ENETRESET
647 #define ENETRESET WSAENETRESET
648 #endif
649 #ifndef EOPNOTSUPP
650 #define EOPNOTSUPP WSAEOPNOTSUPP
651 #endif
652 #ifndef ESHUTDOWN
653 #define ESHUTDOWN WSAESHUTDOWN
654 #endif
655 #ifndef EMSGSIZE
656 #define EMSGSIZE WSAEMSGSIZE
657 #endif
658 #ifndef ECONNABORTED
659 #define ECONNABORTED WSAECONNABORTED
660 #endif
661 #ifndef ECONNRESET
662 #define ECONNRESET WSAECONNRESET
663 #endif
664 #ifndef EHOSTDOWN
665 #define EHOSTDOWN WSAEHOSTDOWN
666 #endif
667 
668 // For the future:
669 // Handle ERROR_NETNAME_DELETED=64
670 #endif //FZ_WINDOWS
671 
672 }
673 
674 #endif
A simple scoped lock.
Definition: mutex.hpp:61
Interface for sockets.
Definition: socket.hpp:315
Definition: socket.hpp:204
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:68
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:538
Declares the event_handler class.
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:119
Socket has been closed. Further events disabled.
Definition: socket.hpp:239
Various functions to deal with IP address strings.
socket_event_source * root() const
Gets the root source.
Definition: socket.hpp:54
Socket is in its normal working state. You can get send and receive events.
Socket has failed. Further events disabled.
This is the recommended event class.
Definition: event.hpp:63
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition: socket.hpp:93
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:359
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:511
std::wstring native_string
A string in the system'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:283
Only in listening state you can get a connection event.
The namespace used by libfilezilla.
Definition: apply.hpp:16
listen_socket_state
Definition: socket.hpp:195
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
All classes sending socket events should derive from this.
Definition: socket.hpp:45
Sets some global macros and further includes string.hpp.
How the socket is initially.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:23
Definition: thread_pool.hpp:59
Write side has finished shutting down. Receive still working normally.
virtual native_string peer_host() const override
Definition: socket.hpp:528
virtual int peer_port(int &error) const override
Definition: socket.hpp:535