source: filezilla/trunk/fuentes/src/include/socket.h @ 3185

Last change on this file since 3185 was 3185, checked in by jrpelegrina, 3 years ago

Update new version: 3.15.02

File size: 5.6 KB
Line 
1#ifndef __SOCKET_H__
2#define __SOCKET_H__
3
4#include <libfilezilla/event_handler.hpp>
5
6#include <errno.h>
7
8// IPv6 capable, non-blocking socket class for use with wxWidgets.
9// Error codes are the same as used by the POSIX socket functions,
10// see 'man 2 socket', 'man 2 connect', ...
11
12enum class SocketEventType
13{
14        // This is a nonfatal condition. It
15        // means there are additional addresses to try.
16        connection_next,
17
18        connection,
19        read,
20        write,
21        close
22};
23
24class CSocketEventSource
25{
26public:
27        virtual ~CSocketEventSource() {}
28};
29
30struct socket_event_type;
31typedef fz::simple_event<socket_event_type, CSocketEventSource*, SocketEventType, int> CSocketEvent;
32
33struct hostaddress_event_type;
34typedef fz::simple_event<hostaddress_event_type, CSocketEventSource*, wxString> CHostAddressEvent;
35
36class CCallback
37{
38public:
39        virtual void cb() {}
40};
41
42void RemoveSocketEvents(fz::event_handler * handler, CSocketEventSource const* const source);
43void ChangeSocketEventHandler(fz::event_handler * oldHandler, fz::event_handler * newHandler, CSocketEventSource const* const source);
44
45class CSocketThread;
46class CSocket final : public CSocketEventSource
47{
48        friend class CSocketThread;
49public:
50        CSocket(fz::event_handler* pEvtHandler);
51        virtual ~CSocket();
52
53        CSocket(CSocket const&) = delete;
54        CSocket& operator=(CSocket const&) = delete;
55
56        enum SocketState
57        {
58                // How the socket is initially
59                none,
60
61                // Only in listening and connecting states you can get a connection event.
62                // After sending the event, socket is in connected state
63                listening,
64                connecting,
65
66                // Only in this state you can get send or receive events
67                connected,
68
69                // Graceful shutdown, you get close event once done
70                closing,
71                closed
72        };
73        SocketState GetState();
74
75        enum address_family
76        {
77                unspec, // AF_UNSPEC
78                ipv4,   // AF_INET
79                ipv6    // AF_INET6
80        };
81
82        // Connects to the given host, given as name, IPv4 or IPv6 address.
83        // Returns 0 on success, else an error code. Note: EINPROGRESS is
84        // not really an error. On success, you should still wait for the
85        // connection event.
86        // If host is a name that can be resolved, a hostaddress socket event gets sent.
87        // Once connections got established, a connection event gets sent. If
88        // connection could not be established, a close event gets sent.
89        int Connect(wxString const& host, unsigned int port, address_family family = unspec, wxString const& bind = wxString());
90
91        // After receiving a send or receive event, you can call these functions
92        // as long as their return value is positive.
93        int Read(void *buffer, unsigned int size, int& error);
94        int Peek(void *buffer, unsigned int size, int& error);
95        int Write(const void *buffer, unsigned int size, int& error);
96
97        int Close();
98
99        // Returns empty string on error
100        wxString GetLocalIP(bool strip_zone_index = false) const;
101        wxString GetPeerIP(bool strip_zone_index = false) const;
102
103        // Returns the hostname passed to Connect()
104        wxString GetPeerHost() const;
105
106        // -1 on error
107        int GetLocalPort(int& error);
108        int GetRemotePort(int& error);
109
110        // If connected, either ipv4 or ipv6, unspec otherwise
111        address_family GetAddressFamily() const;
112
113        static wxString GetErrorString(int error);
114        static wxString GetErrorDescription(int error);
115
116        // Can only be called if the state is none
117        void SetEventHandler(fz::event_handler* pEvtHandler);
118        fz::event_handler* GetEventHandler() { return m_pEvtHandler; }
119
120        static void Cleanup(bool force);
121
122        static wxString AddressToString(const struct sockaddr* addr, int addr_len, bool with_port = true, bool strip_zone_index = false);
123        static wxString AddressToString(char const* buf, int buf_len);
124
125        int Listen(address_family family, int port = 0);
126        CSocket* Accept(int& error);
127
128        enum Flags
129        {
130                flag_nodelay = 0x01,
131                flag_keepalive = 0x02
132        };
133
134        int GetFlags() const { return m_flags; }
135        void SetFlags(int flags);
136
137        // If called on listen socket, sizes will be inherited by
138        // accepted sockets
139        int SetBufferSizes(int size_read, int size_write);
140
141        void SetSynchronousReadCallback(CCallback* cb);
142
143protected:
144        static int DoSetFlags(int fd, int flags, int flags_mask);
145        static int DoSetBufferSizes(int fd, int size_read, int size_write);
146        static int SetNonblocking(int fd);
147
148        void DetachThread();
149
150        fz::event_handler* m_pEvtHandler;
151
152        int m_fd;
153
154        SocketState m_state;
155
156        CSocketThread* m_pSocketThread;
157
158        wxString m_host;
159        unsigned int m_port;
160        int m_family;
161
162        int m_flags;
163
164        int m_buffer_sizes[2];
165
166        CCallback* m_synchronous_read_cb{};
167};
168
169#ifdef FZ_WINDOWS
170
171#ifndef EISCONN
172#define EISCONN WSAEISCONN
173#endif
174#ifndef EINPROGRESS
175#define EINPROGRESS WSAEINPROGRESS
176#endif
177#ifndef EAFNOSUPPORT
178#define EAFNOSUPPORT WSAEAFNOSUPPORT
179#endif
180#ifndef EADDRINUSE
181#define EADDRINUSE WSAEADDRINUSE
182#endif
183#ifndef ENOBUFS
184#define ENOBUFS WSAENOBUFS
185#endif
186#ifndef EPROTONOSUPPORT
187#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
188#endif
189#ifndef EALREADY
190#define EALREADY WSAEALREADY
191#endif
192#ifndef ECONNREFUSED
193#define ECONNREFUSED WSAECONNREFUSED
194#endif
195#ifndef ENOTSOCK
196#define ENOTSOCK WSAENOTSOCK
197#endif
198#ifndef ETIMEDOUT
199#define ETIMEDOUT WSAETIMEDOUT
200#endif
201#ifndef ENETUNREACH
202#define ENETUNREACH WSAENETUNREACH
203#endif
204#ifndef EHOSTUNREACH
205#define EHOSTUNREACH WSAEHOSTUNREACH
206#endif
207#ifndef ENOTCONN
208#define ENOTCONN WSAENOTCONN
209#endif
210#ifndef ENETRESET
211#define ENETRESET WSAENETRESET
212#endif
213#ifndef EOPNOTSUPP
214#define EOPNOTSUPP WSAEOPNOTSUPP
215#endif
216#ifndef ESHUTDOWN
217#define ESHUTDOWN WSAESHUTDOWN
218#endif
219#ifndef EMSGSIZE
220#define EMSGSIZE WSAEMSGSIZE
221#endif
222#ifndef ECONNABORTED
223#define ECONNABORTED WSAECONNABORTED
224#endif
225#ifndef ECONNRESET
226#define ECONNRESET WSAECONNRESET
227#endif
228#ifndef EHOSTDOWN
229#define EHOSTDOWN WSAEHOSTDOWN
230#endif
231
232// For the future:
233// Handle ERROR_NETNAME_DELETED=64
234#endif //FZ_WINDOWS
235
236#endif //__SOCKET_H__
Note: See TracBrowser for help on using the repository browser.