JackNetUnixSocket.cpp 12.3 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2008-2011 Romain Moret at Grame
sletz's avatar
sletz committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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
    //socket***********************************************************************************************************
    int JackNetUnixSocket::NewSocket()
    {
        if ( fSockfd )
        {
            Close();
            Reset();
        }
        fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 );
sletz's avatar
sletz committed
102

sletz's avatar
sletz committed
103
104
105
106
107
        /* Enable address reuse */
        int res, on = 1;
        if ((res = setsockopt( fSockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0) {
            StrError(NET_ERROR_CODE);
        }
sletz's avatar
sletz committed
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
        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
226
    //timeout************************************************************************************************************
sletz's avatar
sletz committed
227
228
229
230
231
232

#if defined(__sun__) || defined(sun)
    int JackNetUnixSocket::SetTimeOut ( int us )
    {
        int	flags;
        fTimeOut = us;
sletz's avatar
sletz committed
233

sletz's avatar
sletz committed
234
235
236
237
        if ((flags = fcntl(fSockfd, F_GETFL, 0)) < 0) {
		    jack_error("JackNetUnixSocket::SetTimeOut error in fcntl F_GETFL");
		    return -1;
	    }
sletz's avatar
sletz committed
238

sletz's avatar
sletz committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	    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;
sletz's avatar
sletz committed
255

sletz's avatar
sletz committed
256
257
            tv.tv_sec = fTimeOut / 1000000;
	        tv.tv_usec = fTimeOut % 1000000;
sletz's avatar
sletz committed
258

sletz's avatar
sletz committed
259
260
	        FD_ZERO(&fdset);
	        FD_SET(fSockfd, &fdset);
sletz's avatar
sletz committed
261

sletz's avatar
sletz committed
262
263
264
	        do {
		        res = select(fSockfd + 1, &fdset, NULL, NULL, &tv);
	        } while (res < 0 && errno == EINTR);
sletz's avatar
sletz committed
265

sletz's avatar
sletz committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
	        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;
sletz's avatar
sletz committed
284

sletz's avatar
sletz committed
285
            tv.tv_sec = fTimeOut / 1000000;
sletz's avatar
sletz committed
286
287
            tv.tv_usec = fTimeOut % 1000000;

sletz's avatar
sletz committed
288
289
	        FD_ZERO(&fdset);
	        FD_SET(fSockfd, &fdset);
sletz's avatar
sletz committed
290

sletz's avatar
sletz committed
291
292
293
	        do {
		        res = select(fSockfd + 1, NULL, &fdset, NULL, &tv);
	        } while (res < 0 && errno == EINTR);
sletz's avatar
sletz committed
294

sletz's avatar
sletz committed
295
296
297
298
299
300
301
302
303
304
305
306
	        if (res < 0) {
		        return res;
            } else if (res == 0) {
                errno = ETIMEDOUT;
		        return -1;
	        }
        }

        return 0;
    }

#else
307
    int JackNetUnixSocket::SetTimeOut ( int us )
308
    {
moret's avatar
moret committed
309
310
        jack_log ( "JackNetUnixSocket::SetTimeout %d usecs", us );

moret's avatar
moret committed
311
312
313
        //negative timeout, or exceding 10s, return
        if ( ( us < 0 ) || ( us > 10000000 ) )
            return SOCKET_ERROR;
314
        struct timeval timeout;
moret's avatar
moret committed
315

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

    //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
344
345
346
347
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
348
349
350
351
352
353
354
355
        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
356
357
358
359
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
360
361
362
363
364
        return SendTo ( buffer, nbytes, flags );
    }

    int JackNetUnixSocket::Send ( const void* buffer, size_t nbytes, int flags )
    {
sletz's avatar
sletz committed
365
366
367
368
    #if defined(__sun__) || defined(sun)
        if (WaitWrite() < 0)
            return -1;
    #endif
369
370
371
372
373
374
        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
375
376
377
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
sletz's avatar
sletz committed
378
    #endif
379
380
381
382
383
        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
384
385
386
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
sletz's avatar
sletz committed
387
    #endif
388
389
390
391
392
393
        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
394
395
396
    #if defined(__sun__) || defined(sun)
        if (WaitRead() < 0)
            return -1;
sletz's avatar
sletz committed
397
    #endif
398
399
400
401
402
403
404
        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
405
            case EAGAIN:
sletz's avatar
sletz committed
406
            case ETIMEDOUT:
moret's avatar
moret committed
407
                return NET_NO_DATA;
sletz's avatar
sletz committed
408

moret's avatar
moret committed
409
410
411
            case ECONNABORTED:
            case ECONNREFUSED:
            case ECONNRESET:
sletz's avatar
sletz committed
412
            case EINVAL:
moret's avatar
moret committed
413
414
            case EHOSTDOWN:
            case EHOSTUNREACH:
sletz's avatar
sletz committed
415
416
            case ENETDOWN:
            case ENETUNREACH:
moret's avatar
moret committed
417
                return NET_CONN_ERROR;
sletz's avatar
sletz committed
418

moret's avatar
moret committed
419
            default:
sletz's avatar
sletz committed
420
421
                //return NET_OP_ERROR;
                return NET_CONN_ERROR;
422
423
        }
    }
sletz's avatar
sletz committed
424
}