aboutsummaryrefslogtreecommitdiff
path: root/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?rev=1.11&content-type=text/x-cvsweb-markup&sortby=date/index.html
blob: be39518430534ca6691c1f26d4ac2830a068b326 (plain)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD><style>		A { color:black }</style>
<!-- hennerik CVSweb $Revision: 1.93 $ -->
<TITLE>code/nel/src/net/udp_sock.cpp - view - 1.11</TITLE></HEAD>
<BODY BGCOLOR="#eeeeee">
<table width="100%" border=0 cellspacing=0 cellpadding=1 bgcolor="#aaaaaa"><tr valign=bottom><td><a href="udp_sock.cpp?sortby=date"><IMG SRC="/inc/img/picto-up.gif" ALT="[BACK]" BORDER="0" WIDTH="14" HEIGHT="13"></a> <b>Return to <A HREF="udp_sock.cpp?sortby=date">udp_sock.cpp</A>
 CVS log</b> <IMG SRC="/inc/img/picto-news.gif" ALT="[TXT]" BORDER="0" WIDTH="13" HEIGHT="15"></td><td align=right><IMG SRC="/inc/img/picto-dir.gif" ALT="[DIR]" BORDER="0" WIDTH="15" HEIGHT="13"> <b>Up to  <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a></b></td></tr></table><HR noshade><table width="100%"><tr><td bgcolor="#ffffff">File:  <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/udp_sock.cpp?sortby=date">udp_sock.cpp</a>&nbsp;(<A HREF="/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/udp_sock.cpp?rev=1.11&amp;sortby=date" target="cvs_checkout" onClick="window.open('/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/udp_sock.cpp?rev=1.11','cvs_checkout','resizeable,scrollbars');"><b>download</b></A>)<BR>
Revision <B>1.11</B>, <i>Wed Apr 17 09:54:00 2002 UTC</i> (3 months, 1 week ago) by <i>cado</i>
<BR>Branch: <b>MAIN</b>
<BR>CVS Tags: <b>georges_v2, HEAD</b><BR>Changes since <b>1.10: +3 -4
 lines</b><PRE>
BUGFIX: wrong address
</PRE>
</td></tr></table><HR noshade><PRE>/** \file udp_sock.cpp
 * Network engine, layer 0, udp socket
 *
 * $Id: udp_sock.cpp,v 1.11 2002/04/17 09:54:00 cado Exp $
 */

/* Copyright, 2000 Nevrax Ltd.
 *
 * This file is part of NEVRAX NEL.
 * NEVRAX NEL 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 2, or (at your option)
 * any later version.

 * NEVRAX NEL 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 NEVRAX NEL; see the file COPYING. If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include &quot;stdnet.h&quot;

#include &quot;nel/net/udp_sock.h&quot;

#ifdef NL_OS_WINDOWS
#include &lt;winsock2.h&gt;
#define socklen_t int
#define ERROR_NUM WSAGetLastError()

#elif defined NL_OS_UNIX
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netinet/tcp.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#include &lt;errno.h&gt;
//#include &lt;fcntl.h&gt;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define ERROR_NUM errno
#define ERROR_MSG strerror(errno)
typedef int SOCKET;

#endif

using namespace NLMISC;

namespace NLNET {


/*
 * Constructor
 */
CUdpSock::CUdpSock( bool logging ) :
 &nbsp; &nbsp; &nbsp; &nbsp;CSock( logging ),
 &nbsp; &nbsp; &nbsp; &nbsp;_Bound( false )
{
 &nbsp; &nbsp; &nbsp; &nbsp;// Socket creation
 &nbsp; &nbsp; &nbsp; &nbsp;createSocket( SOCK_DGRAM, IPPROTO_UDP );
}


/** Binds the socket to the specified port. Call bind() for an unreliable socket if the host acts as a server and waits for
 * messages. If the host acts as a client, call sendTo(), there is no need to bind the socket.
 */
void CUdpSock::bind( uint16 port )
{
 &nbsp; &nbsp; &nbsp; &nbsp;CInetAddress addr; // any IP address
 &nbsp; &nbsp; &nbsp; &nbsp;addr.setPort( port );
 &nbsp; &nbsp; &nbsp; &nbsp;bind( addr );
 &nbsp; &nbsp; &nbsp; &nbsp;setLocalAddress(); // will not set the address if the host is multihomed, use bind(CInetAddress) instead
}


/*
 * Same as bind(uint16) but binds on a specified address/port (useful when the host has several addresses)
 */
void CUdpSock::bind( const CInetAddress&amp; addr )
{
#ifndef NL_OS_WINDOWS
 &nbsp; &nbsp; &nbsp; &nbsp;// Set Reuse Address On (does not work on Win98 and is useless on Win2000)
 &nbsp; &nbsp; &nbsp; &nbsp;int value = true;
 &nbsp; &nbsp; &nbsp; &nbsp;if ( setsockopt( _Sock, SOL_SOCKET, SO_REUSEADDR, &amp;value, sizeof(value) ) == SOCKET_ERROR )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;ReuseAddr failed&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;}
#endif

 &nbsp; &nbsp; &nbsp; &nbsp;_LocalAddr = addr;

 &nbsp; &nbsp; &nbsp; &nbsp;// Bind the socket
 &nbsp; &nbsp; &nbsp; &nbsp;if ( ::bind( _Sock, (sockaddr*)(_LocalAddr.sockAddr()), sizeof(sockaddr) ) == SOCKET_ERROR )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Bind failed&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;_Bound = true;
 &nbsp; &nbsp; &nbsp; &nbsp;if ( _Logging )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nldebug( &quot;LNETL0: Socket %d bound at %s&quot;, _Sock, _LocalAddr.asString().c_str() );
 &nbsp; &nbsp; &nbsp; &nbsp;}
}


/*
 * Sends a message
 */
void CUdpSock::sendTo( const uint8 *buffer, uint len, const CInetAddress&amp; addr )
{
 &nbsp; &nbsp; &nbsp; &nbsp;
 &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp;Send
 &nbsp; &nbsp; &nbsp; &nbsp;if ( ::sendto( _Sock, (const char*)buffer, len, 0, (sockaddr*)(addr.sockAddr()), sizeof(sockaddr) ) != (sint32)len )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Unable to send datagram&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;_BytesSent += len;

 &nbsp; &nbsp; &nbsp; &nbsp;if ( _Logging )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nldebug( &quot;LNETL0: Socket %d sent %d bytes to %s&quot;, _Sock, len, addr.asString().c_str() );
 &nbsp; &nbsp; &nbsp; &nbsp;}

 &nbsp; &nbsp; &nbsp; &nbsp;// If socket is unbound, retrieve local address
 &nbsp; &nbsp; &nbsp; &nbsp;if ( ! _Bound )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setLocalAddress();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_Bound = true;
 &nbsp; &nbsp; &nbsp; &nbsp;}

#ifdef NL_OS_WINDOWS
 &nbsp; &nbsp; &nbsp; &nbsp;// temporary by ace to know size of SO_MAX_MSG_SIZE
 &nbsp; &nbsp; &nbsp; &nbsp;static bool first = true;
 &nbsp; &nbsp; &nbsp; &nbsp;if (first)
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uint MMS, SB;
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int &nbsp;size = sizeof (MMS);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getsockopt (_Sock, SOL_SOCKET, SO_SNDBUF, (char *)&amp;SB, &amp;size);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getsockopt (_Sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&amp;MMS, &amp;size);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nlinfo (&quot;the udp SO_MAX_MSG_SIZE=%u, SO_SNDBUF=%u&quot;, MMS, SB);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;first = false;
 &nbsp; &nbsp; &nbsp; &nbsp;}
#endif
}


/*
 * Receives data from the peer. (blocking function)
 */
void CUdpSock::receive( uint8 *buffer, uint32&amp; len )
{
 &nbsp; &nbsp; &nbsp; &nbsp;nlassert( _Connected &amp;&amp; (buffer!=NULL) );

 &nbsp; &nbsp; &nbsp; &nbsp;// Receive incoming message
 &nbsp; &nbsp; &nbsp; &nbsp;len = ::recv( _Sock, (char*)buffer, len , 0 );

 &nbsp; &nbsp; &nbsp; &nbsp;// Check for errors (after setting the address)
 &nbsp; &nbsp; &nbsp; &nbsp;if ( len == SOCKET_ERROR )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Cannot receive data&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;}

 &nbsp; &nbsp; &nbsp; &nbsp;_BytesReceived += len;
 &nbsp; &nbsp; &nbsp; &nbsp;if ( _Logging )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nldebug( &quot;LNETL0: Socket %d received %d bytes from peer %s&quot;, _Sock, len, _RemoteAddr.asString().c_str() );
 &nbsp; &nbsp; &nbsp; &nbsp;}
}


/*
 * Receives data and say who the sender is. (blocking function)
 */
void CUdpSock::receivedFrom( uint8 *buffer, uint&amp; len, CInetAddress&amp; addr )
{
 &nbsp; &nbsp; &nbsp; &nbsp;// Receive incoming message
 &nbsp; &nbsp; &nbsp; &nbsp;sockaddr_in saddr;
 &nbsp; &nbsp; &nbsp; &nbsp;socklen_t saddrlen = sizeof(saddr);

 &nbsp; &nbsp; &nbsp; &nbsp;len = ::recvfrom( _Sock, (char*)buffer, len , 0, (sockaddr*)&amp;saddr, &amp;saddrlen );

 &nbsp; &nbsp; &nbsp; &nbsp;// If an error occurs, the saddr is not valid
 &nbsp; &nbsp; &nbsp; &nbsp;// When the remote socket is closed, get sender's address to know who is quitting
 &nbsp; &nbsp; &nbsp; &nbsp;addr.setSockAddr( &amp;saddr );

 &nbsp; &nbsp; &nbsp; &nbsp;// Check for errors (after setting the address)
 &nbsp; &nbsp; &nbsp; &nbsp;if ( len == SOCKET_ERROR )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Cannot receive data&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;}

 &nbsp; &nbsp; &nbsp; &nbsp;_BytesReceived += len;
 &nbsp; &nbsp; &nbsp; &nbsp;if ( _Logging )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nldebug( &quot;LNETL0: Socket %d received %d bytes from %s&quot;, _Sock, len, addr.asString().c_str() );
 &nbsp; &nbsp; &nbsp; &nbsp;}
}


} // NLNET
</PRE>