JackNetUnixSocket.cpp 8.5 KB
Newer Older
sletz's avatar
sletz committed
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright (C) 2008 Romain Moret at Grame

This program 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 of the License, or
(at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "JackNetUnixSocket.h"

namespace Jack
{
	//utility *********************************************************************************************************
	int GetHostName ( char * name, int size )
	{
		if ( gethostname ( name, size) == SOCKET_ERROR )
		{
			jack_error ( "Can't get 'hostname' : %s", strerror ( NET_ERROR_CODE ) );
			strcpy ( name, "default" );
			return -1;
		}
		return 0;
	}

	//construct/destruct***********************************************************************************************
	JackNetUnixSocket::JackNetUnixSocket()
	{
		fSockfd = 0;
		fSendAddr.sin_family = AF_INET;
		fSendAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
		memset ( &fSendAddr.sin_zero, 0, 8 );
		fRecvAddr.sin_family = AF_INET;
		fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
		memset ( &fRecvAddr.sin_zero, 0, 8 );
	}

	JackNetUnixSocket::JackNetUnixSocket ( const char* ip, int port )
	{
		fSockfd = 0;
		fPort = port;
		fSendAddr.sin_family = AF_INET;
		fSendAddr.sin_port = htons ( port );
		inet_aton ( ip, &fSendAddr.sin_addr );
		memset ( &fSendAddr.sin_zero, 0, 8 );
		fRecvAddr.sin_family = AF_INET;
		fRecvAddr.sin_port = htons ( port );
		fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
		memset ( &fRecvAddr.sin_zero, 0, 8 );
	}

	JackNetUnixSocket::~JackNetUnixSocket()
	{
		Close();
	}

	//socket***********************************************************************************************************
	int JackNetUnixSocket::NewSocket()
	{
		if ( fSockfd )
		{
			Close();
			Reset();
		}
		fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 );
		return fSockfd;
	}

	int JackNetUnixSocket::Bind()
	{
		return bind ( fSockfd, reinterpret_cast<socket_address_t*> ( &fRecvAddr ), sizeof ( socket_address_t ) );
	}

	int JackNetUnixSocket::BindWith ( const char* ip )
	{
		int addr_conv = inet_aton ( ip, &fRecvAddr.sin_addr );
		if ( addr_conv < 0 )
			return addr_conv;
		return Bind();
	}

	int JackNetUnixSocket::BindWith ( int port )
	{
		fRecvAddr.sin_port = htons ( port );
		return Bind();
	}

	int JackNetUnixSocket::Connect()
	{
		return connect ( fSockfd, reinterpret_cast<socket_address_t*> ( &fSendAddr ), sizeof ( socket_address_t ) );
	}

	int JackNetUnixSocket::ConnectTo ( const char* ip )
	{
		int addr_conv = inet_aton ( ip, &fSendAddr.sin_addr );
		if ( addr_conv < 0 )
			return addr_conv;
		return Connect();
	}

	void JackNetUnixSocket::Close()
	{
		if ( fSockfd )
			close ( fSockfd );
		fSockfd = 0;
	}

	void JackNetUnixSocket::Reset()
	{
		fSendAddr.sin_family = AF_INET;
		fSendAddr.sin_port = htons ( fPort );
		fSendAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
		memset ( &fSendAddr.sin_zero, 0, 8 );
		fRecvAddr.sin_family = AF_INET;
		fRecvAddr.sin_port = htons ( fPort );
		fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
		memset ( &fRecvAddr.sin_zero, 0, 8 );
	}

	bool JackNetUnixSocket::IsSocket()
	{
		return ( fSockfd ) ? true : false;
	}

	//IP/PORT***********************************************************************************************************
	void JackNetUnixSocket::SetPort ( int port )
	{
		fPort = port;
		fSendAddr.sin_port = htons ( port );
		fRecvAddr.sin_port = htons ( port );
	}

	int JackNetUnixSocket::GetPort()
	{
		return fPort;
	}

	//address***********************************************************************************************************
	int JackNetUnixSocket::SetAddress ( const char* ip, int port )
	{
		int addr_conv = inet_aton ( ip, &fSendAddr.sin_addr );
		if ( addr_conv < 0 )
			return addr_conv;
		fSendAddr.sin_port = htons ( port );
		return 0;
	}

	char* JackNetUnixSocket::GetSendIP()
	{
		return inet_ntoa ( fSendAddr.sin_addr );
	}

	char* JackNetUnixSocket::GetRecvIP()
	{
		return inet_ntoa ( fRecvAddr.sin_addr );
	}

	//utility************************************************************************************************************
	int JackNetUnixSocket::GetName ( char* name )
	{
		return gethostname ( name, 255 );
	}

	int JackNetUnixSocket::JoinMCastGroup ( const char* ip )
	{
		struct ip_mreq multicast_req;
		inet_aton ( ip, &multicast_req.imr_multiaddr );
		multicast_req.imr_interface.s_addr = htonl ( INADDR_ANY );
		return SetOption ( IPPROTO_IP, IP_ADD_MEMBERSHIP, &multicast_req, sizeof ( multicast_req ) );
	}

	void JackNetUnixSocket::CopyParams ( JackNetUnixSocket* socket )
	{
		fPort = socket->fPort;
		fSendAddr = socket->fSendAddr;
		fRecvAddr = socket->fRecvAddr;
	}

	//options************************************************************************************************************
	int JackNetUnixSocket::SetOption ( int level, int optname, const void* optval, socklen_t optlen )
	{
		return setsockopt ( fSockfd, level, optname, optval, optlen );
	}

	int JackNetUnixSocket::GetOption ( int level, int optname, void* optval, socklen_t* optlen )
	{
		return getsockopt ( fSockfd, level, optname, optval, optlen );
	}

    //timeout*************************************************************************************************************
    int JackNetUnixSocket::SetTimeOut ( int& msec )
    {
        //negative timeout, or exceeding 10s, return
        if ( ( msec < 0 ) || ( msec > 10000 ) )
            return -1;
        struct timeval timeout;
        //less than 1sec
        if ( msec < 1000 )
        {
            timeout.tv_sec = 0;
            timeout.tv_usec = msec * 1000;
            return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof ( timeout ) );
        }
        //more than 1sec
        if ( msec >= 1000 )
        {
            float sec = static_cast<float>( msec ) / 1000.0f;
            timeout.tv_sec = ( int ) sec;
            float usec = ( sec - timeout.tv_sec ) * 1000000;
            timeout.tv_usec = ( int ) usec;
            return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof ( timeout ) );
        }
        return -1;
    }

    //local loop**********************************************************************************************************
    int JackNetUnixSocket::SetLocalLoop()
    {
    	char disable = 0;
		return SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &disable, sizeof ( disable ) );
    }

	//network operations**************************************************************************************************
	int JackNetUnixSocket::SendTo ( const void* buffer, size_t nbytes, int flags )
	{
		return sendto ( fSockfd, buffer, nbytes, flags, reinterpret_cast<socket_address_t*> ( &fSendAddr ), sizeof ( socket_address_t ) );
	}

	int JackNetUnixSocket::SendTo ( const void* buffer, size_t nbytes, int flags, const char* ip )
	{
		int addr_conv = inet_aton ( ip, &fSendAddr.sin_addr );
		if ( addr_conv < 1 )
			return addr_conv;
		return SendTo ( buffer, nbytes, flags );
	}

	int JackNetUnixSocket::Send ( const void* buffer, size_t nbytes, int flags )
	{
		return send ( fSockfd, buffer, nbytes, flags );
	}

	int JackNetUnixSocket::RecvFrom ( void* buffer, size_t nbytes, int flags )
	{
		socklen_t addr_len = sizeof ( socket_address_t );
		return recvfrom ( fSockfd, buffer, nbytes, flags, reinterpret_cast<socket_address_t*> ( &fRecvAddr ), &addr_len );
	}

	int JackNetUnixSocket::Recv ( void* buffer, size_t nbytes, int flags )
	{
		return recv ( fSockfd, buffer, nbytes, flags );
	}

	int JackNetUnixSocket::CatchHost ( void* buffer, size_t nbytes, int flags )
	{
		socklen_t addr_len = sizeof ( socket_address_t );
		return recvfrom ( fSockfd, buffer, nbytes, flags, reinterpret_cast<socket_address_t*> ( &fSendAddr ), &addr_len );
	}

	net_error_t JackNetUnixSocket::GetError()
	{
		switch ( errno )
		{
			case EAGAIN:
				return NET_NO_DATA;
			case ECONNABORTED:
				return NET_CONN_ERROR;
			case ECONNREFUSED:
				return NET_CONN_ERROR;
			case ECONNRESET:
				return NET_CONN_ERROR;
            case EHOSTDOWN:
                return NET_CONN_ERROR;
            case EHOSTUNREACH:
                return NET_CONN_ERROR;
			default:
				return NET_OP_ERROR;
		}
	}
}