JackNetUnixSocket.cpp 12.1 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
/*
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"
sletz's avatar
sletz committed
21
22
#include <unistd.h>
#include <fcntl.h>
sletz's avatar
sletz committed
23
24

namespace Jack
25
26
27
28
{
    //utility *********************************************************************************************************
    int GetHostName ( char * name, int size )
    {
moret's avatar
moret committed
29
        if ( gethostname ( name, size ) == SOCKET_ERROR )
30
31
32
        {
            jack_error ( "Can't get 'hostname' : %s", strerror ( NET_ERROR_CODE ) );
            strcpy ( name, "default" );
moret's avatar
moret committed
33
            return SOCKET_ERROR;
34
35
36
37
38
39
40
41
        }
        return 0;
    }

    //construct/destruct***********************************************************************************************
    JackNetUnixSocket::JackNetUnixSocket()
    {
        fSockfd = 0;
moret's avatar
moret committed
42
        fPort = 0;
sletz's avatar
sletz committed
43
        fTimeOut = 0;
44
45
46
47
48
49
50
51
52
53
54
55
        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;
sletz's avatar
sletz committed
56
        fTimeOut = 0;
57
58
59
60
61
62
63
64
65
66
        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 );
    }

67
68
    JackNetUnixSocket::JackNetUnixSocket ( const JackNetUnixSocket& socket )
    {
69
        fSockfd = 0;
sletz's avatar
sletz committed
70
        fTimeOut = 0;
71
72
73
74
75
        fPort = socket.fPort;
        fSendAddr = socket.fSendAddr;
        fRecvAddr = socket.fRecvAddr;
    }

76
77
78
79
80
    JackNetUnixSocket::~JackNetUnixSocket()
    {
        Close();
    }

81
82
83
84
85
86
87
88
89
    JackNetUnixSocket& JackNetUnixSocket::operator= ( const JackNetUnixSocket& socket )
    {
        if ( this != &socket )
        {
            fSockfd = 0;
            fPort = socket.fPort;
            fSendAddr = socket.fSendAddr;
            fRecvAddr = socket.fRecvAddr;
        }
90
        return *this;
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
    //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 ) );
    }

    //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 );
    }

moret's avatar
moret committed
219
    //timeout************************************************************************************************************
sletz's avatar
sletz committed
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
290
291
292
293
294
295
296
297
298
299

#if defined(__sun__) || defined(sun)
    int JackNetUnixSocket::SetTimeOut ( int us )
    {
        int	flags;
        fTimeOut = us;
    
        if ((flags = fcntl(fSockfd, F_GETFL, 0)) < 0) {
		    jack_error("JackNetUnixSocket::SetTimeOut error in fcntl F_GETFL");
		    return -1;
	    }
    
	    flags |= O_NONBLOCK;
	    if (fcntl(fSockfd, F_SETFL, flags) < 0) {
		    jack_error("JackNetUnixSocket::SetTimeOut error in fcntl F_SETFL");
		    return 1;
	    }

        return 0;
    }

    int JackNetUnixSocket::WaitRead()
    {
        if (fTimeOut > 0) {

            struct timeval tv;
	        fd_set fdset;
            ssize_t	res;
    
            tv.tv_sec = fTimeOut / 1000000;
	        tv.tv_usec = fTimeOut % 1000000;
    
	        FD_ZERO(&fdset);
	        FD_SET(fSockfd, &fdset);
    
	        do {
		        res = select(fSockfd + 1, &fdset, NULL, NULL, &tv);
	        } while (res < 0 && errno == EINTR);
    
	        if (res < 0) {
 		        return res;
            } else if (res == 0) {
                errno = ETIMEDOUT;
		        return -1;
	        }
        }

        return 0;
    }

    int JackNetUnixSocket::WaitWrite()
    {
        if (fTimeOut > 0) {

            struct timeval tv;
	        fd_set fdset;
            ssize_t	res;
    
            tv.tv_sec = fTimeOut / 1000000;
	        tv.tv_usec = fTimeOut % 1000000;
    
	        FD_ZERO(&fdset);
	        FD_SET(fSockfd, &fdset);
    
	        do {
		        res = select(fSockfd + 1, NULL, &fdset, NULL, &tv);
	        } while (res < 0 && errno == EINTR);
    
	        if (res < 0) {
		        return res;
            } else if (res == 0) {
                errno = ETIMEDOUT;
		        return -1;
	        }
        }

        return 0;
    }

#else
300
    int JackNetUnixSocket::SetTimeOut ( int us )
301
    {
moret's avatar
moret committed
302
303
        jack_log ( "JackNetUnixSocket::SetTimeout %d usecs", us );

moret's avatar
moret committed
304
305
306
        //negative timeout, or exceding 10s, return
        if ( ( us < 0 ) || ( us > 10000000 ) )
            return SOCKET_ERROR;
307
        struct timeval timeout;
moret's avatar
moret committed
308

309
        //less than 1sec
moret's avatar
moret committed
310
        if ( us < 1000000 )
311
312
        {
            timeout.tv_sec = 0;
moret's avatar
moret committed
313
            timeout.tv_usec = us;
314
315
        }
        //more than 1sec
moret's avatar
moret committed
316
        else
317
        {
moret's avatar
moret committed
318
            float sec = static_cast<float> ( us ) / 1000000.f;
319
            timeout.tv_sec = ( int ) sec;
moret's avatar
moret committed
320
            float usec = ( sec - static_cast<float> ( timeout.tv_sec ) ) * 1000000;
321
322
            timeout.tv_usec = ( int ) usec;
        }
moret's avatar
moret committed
323
        return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof ( timeout ) );
324
    }
sletz's avatar
sletz committed
325
#endif
326
327
328
329
330
331
332
333
334
335
336

    //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 )
    {
sletz's avatar
sletz committed
337
338
339
340
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
341
342
343
344
345
346
347
348
        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;
sletz's avatar
sletz committed
349
350
351
352
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
353
354
355
356
357
        return SendTo ( buffer, nbytes, flags );
    }

    int JackNetUnixSocket::Send ( const void* buffer, size_t nbytes, int flags )
    {
sletz's avatar
sletz committed
358
359
360
361
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
362
363
364
365
366
367
        return send ( fSockfd, buffer, nbytes, flags );
    }

    int JackNetUnixSocket::RecvFrom ( void* buffer, size_t nbytes, int flags )
    {
        socklen_t addr_len = sizeof ( socket_address_t );
sletz's avatar
sletz committed
368
369
370
371
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
    #endif  
372
373
374
375
376
        return recvfrom ( fSockfd, buffer, nbytes, flags, reinterpret_cast<socket_address_t*> ( &fRecvAddr ), &addr_len );
    }

    int JackNetUnixSocket::Recv ( void* buffer, size_t nbytes, int flags )
    {
sletz's avatar
sletz committed
377
378
379
380
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
    #endif    
381
382
383
384
385
386
        return recv ( fSockfd, buffer, nbytes, flags );
    }

    int JackNetUnixSocket::CatchHost ( void* buffer, size_t nbytes, int flags )
    {
        socklen_t addr_len = sizeof ( socket_address_t );
sletz's avatar
sletz committed
387
388
389
390
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
    #endif    
391
392
393
394
395
396
397
        return recvfrom ( fSockfd, buffer, nbytes, flags, reinterpret_cast<socket_address_t*> ( &fSendAddr ), &addr_len );
    }

    net_error_t JackNetUnixSocket::GetError()
    {
        switch ( errno )
        {
moret's avatar
moret committed
398
            case EAGAIN:
sletz's avatar
sletz committed
399
            case ETIMEDOUT:
moret's avatar
moret committed
400
                return NET_NO_DATA;
sletz's avatar
sletz committed
401
                
moret's avatar
moret committed
402
403
404
            case ECONNABORTED:
            case ECONNREFUSED:
            case ECONNRESET:
sletz's avatar
sletz committed
405
            case EINVAL:
moret's avatar
moret committed
406
407
            case EHOSTDOWN:
            case EHOSTUNREACH:
sletz's avatar
sletz committed
408
409
            case ENETDOWN:
            case ENETUNREACH:
moret's avatar
moret committed
410
                return NET_CONN_ERROR;
sletz's avatar
sletz committed
411
                
moret's avatar
moret committed
412
413
            default:
                return NET_OP_ERROR;
414
415
        }
    }
sletz's avatar
sletz committed
416
}