forked from uakfdotb/ent-ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.h
238 lines (198 loc) · 5.76 KB
/
socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
ent-ghost
Copyright [2011-2013] [Jack Lu]
This file is part of the ent-ghost source code.
ent-ghost is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ent-ghost source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ent-ghost source code. If not, see <http://www.gnu.org/licenses/>.
ent-ghost is modified from GHost++ (http://ghostplusplus.googlecode.com/)
GHost++ is Copyright [2008] [Trevor Hogan]
*/
#ifndef SOCKET_H
#define SOCKET_H
#ifdef WIN32
#include <winsock2.h>
#include <errno.h>
#define EADDRINUSE WSAEADDRINUSE
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EALREADY WSAEALREADY
#define ECONNABORTED WSAECONNABORTED
#define ECONNREFUSED WSAECONNREFUSED
#define ECONNRESET WSAECONNRESET
#define EDESTADDRREQ WSAEDESTADDRREQ
#define EDQUOT WSAEDQUOT
#define EHOSTDOWN WSAEHOSTDOWN
#define EHOSTUNREACH WSAEHOSTUNREACH
#define EINPROGRESS WSAEINPROGRESS
#define EISCONN WSAEISCONN
#define ELOOP WSAELOOP
#define EMSGSIZE WSAEMSGSIZE
// #define ENAMETOOLONG WSAENAMETOOLONG
#define ENETDOWN WSAENETDOWN
#define ENETRESET WSAENETRESET
#define ENETUNREACH WSAENETUNREACH
#define ENOBUFS WSAENOBUFS
#define ENOPROTOOPT WSAENOPROTOOPT
#define ENOTCONN WSAENOTCONN
// #define ENOTEMPTY WSAENOTEMPTY
#define ENOTSOCK WSAENOTSOCK
#define EOPNOTSUPP WSAEOPNOTSUPP
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define EPROTOTYPE WSAEPROTOTYPE
#define EREMOTE WSAEREMOTE
#define ESHUTDOWN WSAESHUTDOWN
#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
#define ESTALE WSAESTALE
#define ETIMEDOUT WSAETIMEDOUT
#define ETOOMANYREFS WSAETOOMANYREFS
#define EUSERS WSAEUSERS
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
typedef int SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket close
extern int GetLastError( );
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifdef WIN32
#define SHUT_RDWR 2
#endif
//
// CSocket
//
class CSocket
{
protected:
SOCKET m_Socket;
struct sockaddr_in m_SIN;
bool m_HasError;
int m_Error;
string m_CachedHostName;
public:
CSocket( );
CSocket( SOCKET nSocket, struct sockaddr_in nSIN );
~CSocket( );
virtual BYTEARRAY GetPort( );
virtual BYTEARRAY GetIP( );
virtual string GetIPString( );
virtual bool HasError( ) { return m_HasError; }
virtual int GetError( ) { return m_Error; }
virtual string GetErrorString( );
virtual void SetFD( fd_set *fd, fd_set *send_fd, int *nfds );
virtual void Allocate( int type );
virtual void Reset( );
virtual string GetHostName( );
};
//
// CTCPSocket
//
class CTCPSocket : public CSocket
{
protected:
bool m_Connected;
string m_LogFile;
private:
string m_RecvBuffer;
string m_SendBuffer;
uint32_t m_LastRecv;
uint32_t m_LastSend;
public:
CTCPSocket( );
CTCPSocket( SOCKET nSocket, struct sockaddr_in nSIN );
virtual ~CTCPSocket( );
virtual void Reset( );
virtual bool GetConnected( ) { return m_Connected; }
virtual string *GetBytes( ) { return &m_RecvBuffer; }
virtual void PutBytes( string bytes );
virtual void PutBytes( BYTEARRAY bytes );
virtual void ClearRecvBuffer( ) { m_RecvBuffer.clear( ); }
virtual void ClearSendBuffer( ) { m_SendBuffer.clear( ); }
virtual uint32_t GetLastRecv( ) { return m_LastRecv; }
virtual uint32_t GetLastSend( ) { return m_LastSend; }
virtual void DoRecv( fd_set *fd );
virtual void DoSend( fd_set *send_fd );
virtual void Disconnect( );
virtual void SetNoDelay( bool noDelay );
virtual void SetLogFile( string nLogFile ) { m_LogFile = nLogFile; }
};
//
// CTCPClient
//
class CTCPClient : public CTCPSocket
{
protected:
bool m_Connecting;
public:
CTCPClient( );
virtual ~CTCPClient( );
virtual void Reset( );
virtual void Disconnect( );
virtual bool GetConnecting( ) { return m_Connecting; }
virtual void Connect( string localaddress, string address, uint16_t port );
virtual bool CheckConnect( );
};
//
// CTCPServer
//
class CTCPServer : public CTCPSocket
{
public:
CTCPServer( );
virtual ~CTCPServer( );
virtual bool Listen( string address, uint16_t port );
virtual CTCPSocket *Accept( fd_set *fd );
};
//
// CUDPSocket
//
class CUDPSocket : public CSocket
{
protected:
struct in_addr m_BroadcastTarget;
public:
CUDPSocket( );
virtual ~CUDPSocket( );
virtual bool SendTo( struct sockaddr_in sin, BYTEARRAY message );
virtual bool SendTo( string address, uint16_t port, BYTEARRAY message );
virtual bool Broadcast( uint16_t port, BYTEARRAY message );
virtual void SetBroadcastTarget( string subnet );
virtual void SetDontRoute( bool dontRoute );
};
//
// CUDPServer
//
class CUDPServer : public CUDPSocket
{
public:
CUDPServer( );
virtual ~CUDPServer( );
virtual bool Bind( struct sockaddr_in sin );
virtual bool Bind( string address, uint16_t port );
virtual void RecvFrom( fd_set *fd, struct sockaddr_in *sin, string *message );
};
#endif