~ubuntu-branches/ubuntu/quantal/jackd2/quantal

« back to all changes in this revision

Viewing changes to .pc/0000_sync_upstream_VCS.patch/windows/JackNetWinSocket.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Adrian Knoth, Reinhard Tartler, Jonas Smedegaard
  • Date: 2010-06-19 18:54:29 UTC
  • Revision ID: james.westby@ubuntu.com-20100619185429-zhbhh0mqvukgzx0l
Tags: 1.9.5~dfsg-15
[ Adrian Knoth ]
* Also provide the shlibs file for libjack-jackd2-0
* Fix FTBFS on sparc64 (Closes: #586257)

[ Reinhard Tartler ]
* jackd must not be a virtual package, use 'jack-daemon' for that
* add breaks/replaces on old libjack0
* change shlibsfile to prefer jackd2's libjack
* use conflicts instead of breaks. libjack-jackd2-0 has file conflicts
  with libjack0 and will keep it

[ Jonas Smedegaard ]
* Update control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2008 Romain Moret at Grame
 
3
 
 
4
This program is free software; you can redistribute it and/or modify
 
5
it under the terms of the GNU General Public License as published by
 
6
the Free Software Foundation; either version 2 of the License, or
 
7
(at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU General Public License
 
15
along with this program; if not, write to the Free Software
 
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 
 
18
*/
 
19
 
 
20
#include "JackNetWinSocket.h"
 
21
 
 
22
namespace Jack
 
23
{
 
24
    //utility *********************************************************************************************************
 
25
    SERVER_EXPORT int GetHostName ( char * name, int size )
 
26
    {
 
27
        if ( gethostname ( name, size ) == SOCKET_ERROR )
 
28
        {
 
29
            jack_error ( "Can't get 'hostname' : %s", strerror ( NET_ERROR_CODE ) );
 
30
            strcpy ( name, "default" );
 
31
            return -1;
 
32
        }
 
33
        return 0;
 
34
    }
 
35
 
 
36
    win_net_error_t NetErrorList[] =
 
37
    {
 
38
        E ( 0,                  "No error" ),
 
39
        E ( WSAEINTR,           "Interrupted system call" ),
 
40
        E ( WSAEBADF,           "Bad file number" ),
 
41
        E ( WSAEACCES,          "Permission denied" ),
 
42
        E ( WSAEFAULT,          "Bad address" ),
 
43
        E ( WSAEINVAL,          "Invalid argument" ),
 
44
        E ( WSAEMFILE,          "Too many open sockets" ),
 
45
        E ( WSAEWOULDBLOCK,     "Operation would block" ),
 
46
        E ( WSAEINPROGRESS,     "Operation now in progress" ),
 
47
        E ( WSAEALREADY,        "Operation already in progress" ),
 
48
        E ( WSAENOTSOCK,        "Socket operation on non-socket" ),
 
49
        E ( WSAEDESTADDRREQ,    "Destination address required" ),
 
50
        E ( WSAEMSGSIZE,        "Message too long" ),
 
51
        E ( WSAEPROTOTYPE,      "Protocol wrong type for socket" ),
 
52
        E ( WSAENOPROTOOPT,     "Bad protocol option" ),
 
53
        E ( WSAEPROTONOSUPPORT, "Protocol not supported" ),
 
54
        E ( WSAESOCKTNOSUPPORT, "Socket type not supported" ),
 
55
        E ( WSAEOPNOTSUPP,      "Operation not supported on socket" ),
 
56
        E ( WSAEPFNOSUPPORT,    "Protocol family not supported" ),
 
57
        E ( WSAEAFNOSUPPORT,    "Address family not supported" ),
 
58
        E ( WSAEADDRINUSE,      "Address already in use" ),
 
59
        E ( WSAEADDRNOTAVAIL,   "Can't assign requested address" ),
 
60
        E ( WSAENETDOWN,        "Network is down" ),
 
61
        E ( WSAENETUNREACH,     "Network is unreachable" ),
 
62
        E ( WSAENETRESET,       "Net connection reset" ),
 
63
        E ( WSAECONNABORTED,    "Software caused connection abort" ),
 
64
        E ( WSAECONNRESET,      "Connection reset by peer" ),
 
65
        E ( WSAENOBUFS,         "No buffer space available" ),
 
66
        E ( WSAEISCONN,         "Socket is already connected" ),
 
67
        E ( WSAENOTCONN,        "Socket is not connected" ),
 
68
        E ( WSAESHUTDOWN,       "Can't send after socket shutdown" ),
 
69
        E ( WSAETOOMANYREFS,    "Too many references, can't splice" ),
 
70
        E ( WSAETIMEDOUT,       "Connection timed out" ),
 
71
        E ( WSAECONNREFUSED,    "Connection refused" ),
 
72
        E ( WSAELOOP,           "Too many levels of symbolic links" ),
 
73
        E ( WSAENAMETOOLONG,    "File name too long" ),
 
74
        E ( WSAEHOSTDOWN,       "Host is down" ),
 
75
        E ( WSAEHOSTUNREACH,    "No route to host" ),
 
76
        E ( WSAENOTEMPTY,       "Directory not empty" ),
 
77
        E ( WSAEPROCLIM,        "Too many processes" ),
 
78
        E ( WSAEUSERS,          "Too many users" ),
 
79
        E ( WSAEDQUOT,          "Disc quota exceeded" ),
 
80
        E ( WSAESTALE,          "Stale NFS file handle" ),
 
81
        E ( WSAEREMOTE,         "Too many levels of remote in path" ),
 
82
        E ( WSASYSNOTREADY,     "Network system is unavailable" ),
 
83
        E ( WSAVERNOTSUPPORTED, "Winsock version out of range" ),
 
84
        E ( WSANOTINITIALISED,  "WSAStartup not yet called" ),
 
85
        E ( WSAEDISCON,         "Graceful shutdown in progress" ),
 
86
        E ( WSAHOST_NOT_FOUND,  "Host not found" ),
 
87
        E ( WSANO_DATA,         "No host data of that type was found" ),
 
88
        { -1, NULL },
 
89
    };
 
90
 
 
91
    SERVER_EXPORT const char* PrintError ( int error )
 
92
    {
 
93
        int i;
 
94
        for ( i = 0; NetErrorList[i].code >= 0; ++i )
 
95
        {
 
96
            if ( error == NetErrorList[i].code )
 
97
                return NetErrorList[i].msg;
 
98
        }
 
99
        return strerror ( error );
 
100
    }
 
101
 
 
102
    //construct/destruct***********************************************************************************************
 
103
    JackNetWinSocket::JackNetWinSocket()
 
104
    {
 
105
        fSockfd = 0;
 
106
        fSendAddr.sin_family = AF_INET;
 
107
        fSendAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
 
108
        memset ( &fSendAddr.sin_zero, 0, 8 );
 
109
        fRecvAddr.sin_family = AF_INET;
 
110
        fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
 
111
        memset ( &fRecvAddr.sin_zero, 0, 8 );
 
112
    }
 
113
 
 
114
    JackNetWinSocket::JackNetWinSocket ( const char* ip, int port )
 
115
    {
 
116
        fSockfd = 0;
 
117
        fPort = port;
 
118
        fSendAddr.sin_family = AF_INET;
 
119
        fSendAddr.sin_port = htons ( port );
 
120
        fSendAddr.sin_addr.s_addr = inet_addr ( ip );
 
121
        memset ( &fSendAddr.sin_zero, 0, 8 );
 
122
        fRecvAddr.sin_family = AF_INET;
 
123
        fRecvAddr.sin_port = htons ( port );
 
124
        fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
 
125
        memset ( &fRecvAddr.sin_zero, 0, 8 );
 
126
    }
 
127
 
 
128
    JackNetWinSocket::JackNetWinSocket ( const JackNetWinSocket& socket )
 
129
    {
 
130
        fSockfd = 0;
 
131
        fPort = socket.fPort;
 
132
        fSendAddr = socket.fSendAddr;
 
133
        fRecvAddr = socket.fRecvAddr;
 
134
    }
 
135
 
 
136
    JackNetWinSocket::~JackNetWinSocket()
 
137
    {
 
138
        Close();
 
139
    }
 
140
 
 
141
    JackNetWinSocket& JackNetWinSocket::operator= ( const JackNetWinSocket& socket )
 
142
    {
 
143
        if ( this != &socket )
 
144
        {
 
145
            fSockfd = 0;
 
146
            fPort = socket.fPort;
 
147
            fSendAddr = socket.fSendAddr;
 
148
            fRecvAddr = socket.fRecvAddr;
 
149
        }
 
150
        return *this;
 
151
    }
 
152
 
 
153
    //socket***********************************************************************************************************
 
154
    int JackNetWinSocket::NewSocket()
 
155
    {
 
156
        if ( fSockfd )
 
157
        {
 
158
            Close();
 
159
            Reset();
 
160
        }
 
161
        fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 );
 
162
        return fSockfd;
 
163
    }
 
164
 
 
165
    int JackNetWinSocket::Bind()
 
166
    {
 
167
        return bind ( fSockfd, reinterpret_cast<SOCKADDR*> ( &fRecvAddr ), sizeof ( SOCKADDR ) );
 
168
    }
 
169
 
 
170
    int JackNetWinSocket::BindWith ( const char* ip )
 
171
    {
 
172
        fRecvAddr.sin_addr.s_addr = inet_addr ( ip );
 
173
        return Bind();
 
174
    }
 
175
 
 
176
    int JackNetWinSocket::BindWith ( int port )
 
177
    {
 
178
        fRecvAddr.sin_port = htons ( port );
 
179
        return Bind();
 
180
    }
 
181
 
 
182
    int JackNetWinSocket::Connect()
 
183
    {
 
184
        return connect ( fSockfd, reinterpret_cast<SOCKADDR*> ( &fSendAddr ), sizeof ( SOCKADDR ) );
 
185
    }
 
186
 
 
187
    int JackNetWinSocket::ConnectTo ( const char* ip )
 
188
    {
 
189
        fSendAddr.sin_addr.s_addr = inet_addr ( ip );
 
190
        return Connect();
 
191
    }
 
192
 
 
193
    void JackNetWinSocket::Close()
 
194
    {
 
195
        if ( fSockfd )
 
196
            closesocket ( fSockfd );
 
197
        fSockfd = 0;
 
198
    }
 
199
 
 
200
    void JackNetWinSocket::Reset()
 
201
    {
 
202
        fSendAddr.sin_family = AF_INET;
 
203
        fSendAddr.sin_port = htons ( fPort );
 
204
        fSendAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
 
205
        memset ( &fSendAddr.sin_zero, 0, 8 );
 
206
        fRecvAddr.sin_family = AF_INET;
 
207
        fRecvAddr.sin_port = htons ( fPort );
 
208
        fRecvAddr.sin_addr.s_addr = htonl ( INADDR_ANY );
 
209
        memset ( &fRecvAddr.sin_zero, 0, 8 );
 
210
    }
 
211
 
 
212
    bool JackNetWinSocket::IsSocket()
 
213
    {
 
214
        return ( fSockfd ) ? true : false;
 
215
    }
 
216
 
 
217
    //IP/PORT***********************************************************************************************************
 
218
    void JackNetWinSocket::SetPort ( int port )
 
219
    {
 
220
        fPort = port;
 
221
        fSendAddr.sin_port = htons ( port );
 
222
        fRecvAddr.sin_port = htons ( port );
 
223
    }
 
224
 
 
225
    int JackNetWinSocket::GetPort()
 
226
    {
 
227
        return fPort;
 
228
    }
 
229
 
 
230
    //address***********************************************************************************************************
 
231
    int JackNetWinSocket::SetAddress ( const char* ip, int port )
 
232
    {
 
233
        fSendAddr.sin_addr.s_addr = inet_addr ( ip );
 
234
        fSendAddr.sin_port = htons ( port );
 
235
        return 0;
 
236
    }
 
237
 
 
238
    char* JackNetWinSocket::GetSendIP()
 
239
    {
 
240
        return inet_ntoa ( fSendAddr.sin_addr );
 
241
    }
 
242
 
 
243
    char* JackNetWinSocket::GetRecvIP()
 
244
    {
 
245
        return inet_ntoa ( fRecvAddr.sin_addr );
 
246
    }
 
247
 
 
248
    //utility************************************************************************************************************
 
249
    int JackNetWinSocket::GetName ( char* name )
 
250
    {
 
251
        return gethostname ( name, 255 );
 
252
    }
 
253
 
 
254
    int JackNetWinSocket::JoinMCastGroup ( const char* ip )
 
255
    {
 
256
        struct ip_mreq multicast_req;
 
257
        multicast_req.imr_multiaddr.s_addr = inet_addr ( ip );
 
258
        multicast_req.imr_interface.s_addr = htonl ( INADDR_ANY );
 
259
        //12 is IP_ADD_MEMBERSHIP in winsock2 (differs from winsock1...)
 
260
        return SetOption ( IPPROTO_IP, 12, &multicast_req, sizeof ( multicast_req ) );
 
261
    }
 
262
 
 
263
    //options************************************************************************************************************
 
264
    int JackNetWinSocket::SetOption ( int level, int optname, const void* optval, SOCKLEN optlen )
 
265
    {
 
266
        return setsockopt ( fSockfd, level, optname, static_cast<const char*> ( optval ), optlen );
 
267
    }
 
268
 
 
269
    int JackNetWinSocket::GetOption ( int level, int optname, void* optval, SOCKLEN* optlen )
 
270
    {
 
271
        return getsockopt ( fSockfd, level, optname, static_cast<char*> ( optval ), optlen );
 
272
    }
 
273
 
 
274
    //tiemout************************************************************************************************************
 
275
    int JackNetWinSocket::SetTimeOut ( int usec )
 
276
    {
 
277
        jack_log ( "JackNetWinSocket::SetTimeout %d usec", usec );
 
278
 
 
279
        //negative timeout, or exceeding 10s, return
 
280
        if ( ( usec < 0 ) || ( usec > 10000000 ) )
 
281
            return SOCKET_ERROR;
 
282
        int time = usec / 1000;
 
283
        return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &time, sizeof ( time ) );
 
284
    }
 
285
 
 
286
    //local loop*********************************************************************************************************
 
287
    int JackNetWinSocket::SetLocalLoop()
 
288
    {
 
289
        //char disable = 0;
 
290
        /*
 
291
        see http://msdn.microsoft.com/en-us/library/aa916098.aspx
 
292
        Default value is TRUE. When TRUE, data that is sent from the local interface to the multicast group to
 
293
        which the socket is joined, including data sent from the same socket, will be echoed to its receive buffer.
 
294
        */
 
295
        char disable = 1;
 
296
        return SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &disable, sizeof ( disable ) );
 
297
    }
 
298
 
 
299
    //network operations*************************************************************************************************
 
300
    int JackNetWinSocket::SendTo ( const void* buffer, size_t nbytes, int flags )
 
301
    {
 
302
        return sendto ( fSockfd, reinterpret_cast<const char*> ( buffer ), nbytes, flags, reinterpret_cast<SOCKADDR*> ( &fSendAddr ), sizeof ( SOCKADDR ) );
 
303
    }
 
304
 
 
305
    int JackNetWinSocket::SendTo ( const void* buffer, size_t nbytes, int flags, const char* ip )
 
306
    {
 
307
        fSendAddr.sin_addr.s_addr = inet_addr ( ip );
 
308
        return SendTo ( buffer, nbytes, flags );
 
309
    }
 
310
 
 
311
    int JackNetWinSocket::Send ( const void* buffer, size_t nbytes, int flags )
 
312
    {
 
313
        return send ( fSockfd, reinterpret_cast<const char*> ( buffer ), nbytes, flags );
 
314
    }
 
315
 
 
316
    int JackNetWinSocket::RecvFrom ( void* buffer, size_t nbytes, int flags )
 
317
    {
 
318
        SOCKLEN addr_len = sizeof ( SOCKADDR );
 
319
        return recvfrom ( fSockfd, reinterpret_cast<char*> ( buffer ), nbytes, flags, reinterpret_cast<SOCKADDR*> ( &fRecvAddr ), &addr_len );
 
320
    }
 
321
 
 
322
    int JackNetWinSocket::Recv ( void* buffer, size_t nbytes, int flags )
 
323
    {
 
324
        return recv ( fSockfd, reinterpret_cast<char*> ( buffer ), nbytes, flags );
 
325
    }
 
326
 
 
327
    int JackNetWinSocket::CatchHost ( void* buffer, size_t nbytes, int flags )
 
328
    {
 
329
        SOCKLEN addr_len = sizeof ( SOCKADDR );
 
330
        return recvfrom ( fSockfd, reinterpret_cast<char*> ( buffer ), nbytes, flags, reinterpret_cast<SOCKADDR*> ( &fSendAddr ), &addr_len );
 
331
    }
 
332
 
 
333
    net_error_t JackNetWinSocket::GetError()
 
334
    {
 
335
        switch ( NET_ERROR_CODE )
 
336
        {
 
337
            case WSABASEERR:
 
338
                return NET_NO_ERROR;
 
339
            case WSAETIMEDOUT:
 
340
                return NET_NO_DATA;
 
341
            case WSAEWOULDBLOCK:
 
342
                return NET_NO_DATA;
 
343
            case WSAECONNREFUSED:
 
344
                return NET_CONN_ERROR;
 
345
            case WSAECONNRESET:
 
346
                return NET_CONN_ERROR;
 
347
            case WSAEACCES:
 
348
                return NET_CONN_ERROR;
 
349
            case WSAECONNABORTED:
 
350
                return NET_CONN_ERROR;
 
351
            case WSAEHOSTDOWN:
 
352
                return NET_CONN_ERROR;
 
353
            case WSAEHOSTUNREACH:
 
354
                return NET_CONN_ERROR;
 
355
            default:
 
356
                return NET_OP_ERROR;
 
357
        }
 
358
    }
 
359
}
 
360