Debian Codegraph @

plugins/dm.gameconnection/clsocket/PassiveSocket.cpp

raw
1 /*---------------------------------------------------------------------------*/
2 /* */
3 /* PassiveSocket.cpp - Passive Socket Implementation */
4 /* */
5 /* Author : Mark Carrier ([email protected]) */
6 /* */
7 /*---------------------------------------------------------------------------*/
8 /* Copyright (c) 2007-2009 CarrierLabs, LLC. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * 4. The name "CarrierLabs" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * [email protected].
29 *
30 * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
31 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
34 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 *----------------------------------------------------------------------------*/
43 #include "PassiveSocket.h"
44
45
46
47 CPassiveSocket::CPassiveSocket(CSocketType nType) : CSimpleSocket(nType)
48 {
49 }
50
51 bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort)
52 {
53 bool bRetVal = false;
54 #ifdef WIN32
55 ULONG inAddr;
56 #else
57 in_addr_t inAddr;
58 #endif
59
60 //--------------------------------------------------------------------------
61 // Set the following socket option SO_REUSEADDR. This will allow the file
62 // descriptor to be reused immediately after the socket is closed instead
63 // of setting in a TIMED_WAIT state.
64 //--------------------------------------------------------------------------
65 memset(&m_stMulticastGroup,0,sizeof(m_stMulticastGroup));
66 m_stMulticastGroup.sin_family = AF_INET;
67 m_stMulticastGroup.sin_port = htons(nPort);
68
69 //--------------------------------------------------------------------------
70 // If no IP Address (interface ethn) is supplied, or the loop back is
71 // specified then bind to any interface, else bind to specified interface.
72 //--------------------------------------------------------------------------
73 if ((pInterface == NULL) || (!strlen(pInterface)))
74 {
75 m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
76 }
77 else
78 {
79 if ((inAddr = inet_addr(pInterface)) != INADDR_NONE)
80 {
81 m_stMulticastGroup.sin_addr.s_addr = inAddr;
82 }
83 }
84
85 //--------------------------------------------------------------------------
86 // Bind to the specified port
87 //--------------------------------------------------------------------------
88 if (bind(m_socket, (struct sockaddr *)&m_stMulticastGroup, sizeof(m_stMulticastGroup)) == 0)
89 {
90 //----------------------------------------------------------------------
91 // Join the multicast group
92 //----------------------------------------------------------------------
93 m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr(pGroup);
94 m_stMulticastRequest.imr_interface.s_addr = m_stMulticastGroup.sin_addr.s_addr;
95
96 if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
97 (void *)&m_stMulticastRequest,
98 sizeof(m_stMulticastRequest)) == CSimpleSocket::SocketSuccess)
99 {
100 bRetVal = true;
101 }
102
103 m_timer.SetEndTime();
104 }
105
106 m_timer.Initialize();
107 m_timer.SetStartTime();
108
109
110 //--------------------------------------------------------------------------
111 // If there was a socket error then close the socket to clean out the
112 // connection in the backlog.
113 //--------------------------------------------------------------------------
114 TranslateSocketError();
115
116 if (bRetVal == false)
117 {
118 Close();
119 }
120
121 return bRetVal;
122 }
123
124
125 //------------------------------------------------------------------------------
126 //
127 // Listen() -
128 //
129 //------------------------------------------------------------------------------
130 bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog)
131 {
132 bool bRetVal = false;
133 #ifdef WIN32
134 ULONG inAddr;
135 #else
136 in_addr_t inAddr;
137
138 int32 nReuse;
139 nReuse = IPTOS_LOWDELAY;
140
141 //--------------------------------------------------------------------------
142 // Set the following socket option SO_REUSEADDR. This will allow the file
143 // descriptor to be reused immediately after the socket is closed instead
144 // of setting in a TIMED_WAIT state.
145 //--------------------------------------------------------------------------
146 SETSOCKOPT(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&nReuse, sizeof(int32));
147 SETSOCKOPT(m_socket, IPPROTO_TCP, IP_TOS, &nReuse, sizeof(int32));
148 #endif
149
150 memset(&m_stServerSockaddr,0,sizeof(m_stServerSockaddr));
151 m_stServerSockaddr.sin_family = AF_INET;
152 m_stServerSockaddr.sin_port = htons(nPort);
153
154 //--------------------------------------------------------------------------
155 // If no IP Address (interface ethn) is supplied, or the loop back is
156 // specified then bind to any interface, else bind to specified interface.
157 //--------------------------------------------------------------------------
158 if ((pAddr == NULL) || (!strlen(pAddr)))
159 {
160 m_stServerSockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
161 }
162 else
163 {
164 if ((inAddr = inet_addr(pAddr)) != INADDR_NONE)
165 {
166 m_stServerSockaddr.sin_addr.s_addr = inAddr;
167 }
168 }
169
170 m_timer.Initialize();
171 m_timer.SetStartTime();
172
173 //--------------------------------------------------------------------------
174 // Bind to the specified port
175 //--------------------------------------------------------------------------
176 if (bind(m_socket, (struct sockaddr *)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) != CSimpleSocket::SocketError)
177 {
178 if (m_nSocketType == CSimpleSocket::SocketTypeTcp)
179 {
180 if (listen(m_socket, nConnectionBacklog) != CSimpleSocket::SocketError)
181 {
182 bRetVal = true;
183 }
184 }
185 else
186 {
187 bRetVal = true;
188 }
189 }
190
191 m_timer.SetEndTime();
192
193 //--------------------------------------------------------------------------
194 // If there was a socket error then close the socket to clean out the
195 // connection in the backlog.
196 //--------------------------------------------------------------------------
197 TranslateSocketError();
198
199 if (bRetVal == false)
200 {
201 CSocketError err = GetSocketError();
202 Close();
203 SetSocketError(err);
204 }
205
206 return bRetVal;
207 }
208
209
210 //------------------------------------------------------------------------------
211 //
212 // Accept() -
213 //
214 //------------------------------------------------------------------------------
215 CActiveSocket *CPassiveSocket::Accept()
216 {
217 uint32 nSockLen;
218 CActiveSocket *pClientSocket = NULL;
219 SOCKET socket = static_cast<SOCKET>(CSimpleSocket::SocketError);
220
221 if (m_nSocketType != CSimpleSocket::SocketTypeTcp)
222 {
223 SetSocketError(CSimpleSocket::SocketProtocolError);
224 return pClientSocket;
225 }
226
227 pClientSocket = new CActiveSocket();
228
229 //--------------------------------------------------------------------------
230 // Wait for incoming connection.
231 //--------------------------------------------------------------------------
232 if (pClientSocket != NULL)
233 {
234 CSocketError socketErrno = SocketSuccess;
235
236 m_timer.Initialize();
237 m_timer.SetStartTime();
238
239 nSockLen = sizeof(m_stClientSockaddr);
240
241 do
242 {
243 errno = 0;
244 socket = accept(m_socket, (struct sockaddr *)&m_stClientSockaddr, (socklen_t *)&nSockLen);
245
246 if (socket != -1)
247 {
248 pClientSocket->SetSocketHandle(socket);
249 pClientSocket->TranslateSocketError();
250 socketErrno = pClientSocket->GetSocketError();
251 socklen_t socketLength = sizeof(struct sockaddr);
252
253 //-------------------------------------------------------------
254 // Store client and server IP and port information for this
255 // connection.
256 //-------------------------------------------------------------
257 getpeername(m_socket, (struct sockaddr *)&pClientSocket->m_stClientSockaddr, &socketLength);
258 memcpy((void *)&pClientSocket->m_stClientSockaddr, (void *)&m_stClientSockaddr, socketLength);
259
260 memset(&pClientSocket->m_stServerSockaddr, 0, socketLength);
261 getsockname(m_socket, (struct sockaddr *)&pClientSocket->m_stServerSockaddr, &socketLength);
262 }
263 else
264 {
265 TranslateSocketError();
266 socketErrno = GetSocketError();
267 }
268
269 } while (socketErrno == CSimpleSocket::SocketInterrupted);
270
271 m_timer.SetEndTime();
272
273 if (socketErrno != CSimpleSocket::SocketSuccess)
274 {
275 delete pClientSocket;
276 pClientSocket = NULL;
277 }
278 }
279
280 return pClientSocket;
281 }
282
283
284 //------------------------------------------------------------------------------
285 //
286 // Send() - Send data on a valid socket
287 //
288 //------------------------------------------------------------------------------
289 int32 CPassiveSocket::Send(const uint8 *pBuf, size_t bytesToSend)
290 {
291 SetSocketError(SocketSuccess);
292 m_nBytesSent = 0;
293
294 switch(m_nSocketType)
295 {
296 case CSimpleSocket::SocketTypeUdp:
297 {
298 if (IsSocketValid())
299 {
300 if ((bytesToSend > 0) && (pBuf != NULL))
301 {
302 m_timer.Initialize();
303 m_timer.SetStartTime();
304
305 m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0,
306 (const sockaddr *)&m_stClientSockaddr,
307 sizeof(m_stClientSockaddr));
308
309 m_timer.SetEndTime();
310
311 if (m_nBytesSent == CSimpleSocket::SocketError)
312 {
313 TranslateSocketError();
314 }
315 }
316 }
317 break;
318 }
319 case CSimpleSocket::SocketTypeTcp:
320 CSimpleSocket::Send(pBuf, bytesToSend);
321 break;
322 default:
323 SetSocketError(SocketProtocolError);
324 break;
325 }
326
327 return m_nBytesSent;
328 }