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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* Socket classes
* Copyright (c) 2008 d4rk
* Copyright (C) 2008-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include <map>
#include <memory>
#include <string.h>
#include <vector>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "PlatformDefs.h"
#ifdef TARGET_POSIX
typedef int SOCKET;
#endif
namespace SOCKETS
{
// types of sockets
enum SocketType
{
ST_TCP,
ST_UDP,
ST_UNIX
};
/**********************************************************************/
/* IP address abstraction class */
/**********************************************************************/
class CAddress
{
public:
union
{
sockaddr_in saddr4;
sockaddr_in6 saddr6;
sockaddr saddr_generic;
} saddr;
socklen_t size;
public:
CAddress()
{
memset(&saddr, 0, sizeof(saddr));
saddr.saddr4.sin_family = AF_INET;
saddr.saddr4.sin_addr.s_addr = htonl(INADDR_ANY);
size = sizeof(saddr.saddr4);
}
explicit CAddress(const char *address)
{
SetAddress(address);
}
void SetAddress(const char *address)
{
in6_addr addr6;
memset(&saddr, 0, sizeof(saddr));
if (inet_pton(AF_INET6, address, &addr6) == 1)
{
saddr.saddr6.sin6_family = AF_INET6;
saddr.saddr6.sin6_addr = addr6;
size = sizeof(saddr.saddr6);
}
else
{
saddr.saddr4.sin_family = AF_INET;
saddr.saddr4.sin_addr.s_addr = inet_addr(address);
size = sizeof(saddr.saddr4);
}
}
// returns statically alloced buffer, do not free
const char *Address()
{
if (saddr.saddr_generic.sa_family == AF_INET6)
{
static char buf[INET6_ADDRSTRLEN];
return inet_ntop(AF_INET6, &saddr.saddr6.sin6_addr, buf, size);
}
else
return inet_ntoa(saddr.saddr4.sin_addr);
}
unsigned long ULong()
{
if (saddr.saddr_generic.sa_family == AF_INET6)
{
// IPv4 coercion (see http://home.samfundet.no/~sesse/ipv6-porting.pdf).
// We hash the entire IPv6 address because XBMC might conceivably need to
// distinguish between different hosts in the same subnet.
// This hash function (djbhash) is not too strong, but good enough.
uint32_t hash = 5381;
for (int i = 0; i < 16; ++i)
{
hash = hash * 33 + saddr.saddr6.sin6_addr.s6_addr[i];
}
// Move into 224.0.0.0/3. As a special safeguard, make sure we don't
// end up with the the special broadcast address 255.255.255.255.
hash |= 0xe0000000u;
if (hash == 0xffffffffu)
hash = 0xfffffffeu;
return (unsigned long)htonl(hash);
}
else
return (unsigned long)saddr.saddr4.sin_addr.s_addr;
}
};
/**********************************************************************/
/* Base class for all sockets */
/**********************************************************************/
class CBaseSocket
{
public:
CBaseSocket()
{
m_Type = ST_TCP;
m_bReady = false;
m_bBound = false;
m_iPort = 0;
}
virtual ~CBaseSocket() { Close(); }
// socket functions
virtual bool Bind(bool localOnly, int port, int range=0) = 0;
virtual bool Connect() = 0;
virtual void Close() {}
// state functions
bool Ready() { return m_bReady; }
bool Bound() { return m_bBound; }
SocketType Type() { return m_Type; }
int Port() { return m_iPort; }
virtual SOCKET Socket() = 0;
protected:
virtual void SetBound(bool set=true) { m_bBound = set; }
virtual void SetReady(bool set=true) { m_bReady = set; }
protected:
SocketType m_Type;
bool m_bReady;
bool m_bBound;
int m_iPort;
};
/**********************************************************************/
/* Base class for UDP socket implementations */
/**********************************************************************/
class CUDPSocket : public CBaseSocket
{
public:
CUDPSocket()
{
m_Type = ST_UDP;
}
// I/O functions
virtual int SendTo(const CAddress& addr, const int bufferlength,
const void* buffer) = 0;
// read datagrams, return no. of bytes read or -1 or error
virtual int Read(CAddress& addr, const int buffersize, void *buffer) = 0;
virtual bool Broadcast(const CAddress& addr, const int datasize,
const void* data) = 0;
};
// Implementation specific classes
/**********************************************************************/
/* POSIX based UDP socket implementation */
/**********************************************************************/
class CPosixUDPSocket : public CUDPSocket
{
public:
CPosixUDPSocket()
{
m_iSock = INVALID_SOCKET;
m_ipv6Socket = false;
}
bool Bind(bool localOnly, int port, int range=0) override;
bool Connect() override { return false; }
bool Listen(int timeout);
int SendTo(const CAddress& addr, const int datasize, const void* data) override;
int Read(CAddress& addr, const int buffersize, void *buffer) override;
bool Broadcast(const CAddress& addr, const int datasize, const void* data) override
{
//! @todo implement
return false;
}
SOCKET Socket() override { return m_iSock; }
void Close() override;
protected:
SOCKET m_iSock;
CAddress m_addr;
private:
bool CheckIPv6(int port, int range);
bool m_ipv6Socket;
};
/**********************************************************************/
/* Create and return platform dependent sockets */
/**********************************************************************/
class CSocketFactory
{
public:
static std::unique_ptr<CUDPSocket> CreateUDPSocket();
};
/**********************************************************************/
/* Listens on multiple sockets for reads */
/**********************************************************************/
#define LISTENERROR 1
#define LISTENEMPTY 2
class CSocketListener
{
public:
CSocketListener();
void AddSocket(CBaseSocket *);
bool Listen(int timeoutMs); // in ms, -1=>never timeout, 0=>poll
void Clear();
CBaseSocket* GetFirstReadySocket();
CBaseSocket* GetNextReadySocket();
protected:
std::vector<CBaseSocket*> m_sockets;
int m_iReadyCount;
int m_iMaxSockets;
int m_iCurrentSocket;
fd_set m_fdset;
};
}
|