summaryrefslogtreecommitdiff
path: root/posix/JackSocket.cpp
blob: be43d0116c84938ee0dbbb0e96ebffbb54b1f57c (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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright (C) 2004-2008 Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "JackSocket.h"
#include "JackConstants.h"
#include "JackTools.h"
#include "JackError.h"
#include "promiscuous.h"
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>

namespace Jack
{

static void BuildName(const char* client_name, char* res, const char* dir, int which, int size, bool promiscuous)
{
    char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
    JackTools::RewriteName(client_name, ext_client_name);
    if (promiscuous) {
	    snprintf(res, size, "%s/jack_%s_%d", dir, ext_client_name, which);
    } else {
	    snprintf(res, size, "%s/jack_%s_%d_%d", dir, ext_client_name, JackTools::GetUID(), which);
    }
}

JackClientSocket::JackClientSocket(): JackClientRequestInterface(), fSocket(-1), fTimeOut(0)
{
    const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
    fPromiscuous = (promiscuous != NULL);
    fPromiscuousGid = jack_group2gid(promiscuous);
}

JackClientSocket::JackClientSocket(int socket): JackClientRequestInterface(), fSocket(socket),fTimeOut(0), fPromiscuous(false), fPromiscuousGid(-1)
{}

#if defined(__sun__) || defined(sun)

void JackClientSocket::SetReadTimeOut(long sec)
{
    int	flags;
    fTimeOut = sec;

    if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
		jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_GETFL");
		return;
	}

	flags |= O_NONBLOCK;
	if (fcntl(fSocket, F_SETFL, flags) < 0) {
		jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_SETFL");
		return;
	}
}

void JackClientSocket::SetWriteTimeOut(long sec)
{
    int	flags;
    fTimeOut = sec;

    if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
		jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_GETFL");
		return;
	}

	flags |= O_NONBLOCK;
	if (fcntl(fSocket, F_SETFL, flags) < 0) {
		jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_SETFL");
		return;
	}
}

#else

void JackClientSocket::SetReadTimeOut(long sec)
{
    struct timeval timout;
    timout.tv_sec = sec;
    timout.tv_usec = 0;
    if (setsockopt(fSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
        jack_error("SetReadTimeOut fd = %ld err = %s", fSocket, strerror(errno));
    }
}

void JackClientSocket::SetWriteTimeOut(long sec)
{
    struct timeval timout;
    timout.tv_sec = sec ;
    timout.tv_usec = 0;
    if (setsockopt(fSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
        jack_error("SetWriteTimeOut fd = %ld err = %s", fSocket, strerror(errno));
    }
}

#endif

void JackClientSocket::SetNonBlocking(bool onoff)
{
    if (onoff) {
        long flags = 0;
        if (fcntl(fSocket, F_SETFL, flags | O_NONBLOCK) < 0) {
            jack_error("SetNonBlocking fd = %ld err = %s", fSocket, strerror(errno));
        }
    }
}

int JackClientSocket::Connect(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
{
    struct sockaddr_un addr;

    if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        jack_error("Cannot create socket err = %s", strerror(errno));
        return -1;
    }

    addr.sun_family = AF_UNIX;
    BuildName(name, addr.sun_path, dir, which, sizeof(addr.sun_path), fPromiscuous);
    jack_log("JackClientSocket::Connect : addr.sun_path %s", addr.sun_path);

    if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        jack_error("Cannot connect to server socket err = %s", strerror(errno));
        close(fSocket);
        fSocket = -1;
        return -1;
    }

#ifdef __APPLE__
    int on = 1;
    if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
        jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
    }
#endif

    return 0;
}

int JackClientSocket::Close()
{
    jack_log("JackClientSocket::Close");
    if (fSocket > 0) {
        shutdown(fSocket, SHUT_RDWR);
        close(fSocket);
        fSocket = -1;
        return 0;
    } else {
        return -1;
    }
}

int JackClientSocket::Read(void* data, int len)
{
    int res;

#if defined(__sun__) || defined(sun)
    if (fTimeOut > 0) {

        struct timeval tv;
	    fd_set fdset;
        ssize_t	res;

        tv.tv_sec = fTimeOut;
	    tv.tv_usec = 0;

	    FD_ZERO(&fdset);
	    FD_SET(fSocket, &fdset);

	    do {
		    res = select(fSocket + 1, &fdset, NULL, NULL, &tv);
	    } while (res < 0 && errno == EINTR);

	    if (res < 0) {
		    return res;
        } else if (res == 0) {
		    return -1;
	    }
    }
#endif

    if ((res = read(fSocket, data, len)) != len) {
        if (errno == EWOULDBLOCK || errno == EAGAIN) {
            jack_error("JackClientSocket::Read time out");
            return 0;  // For a non blocking socket, a read failure is not considered as an error
        } else if (res != 0) {
            jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
            //return 0;
            return -1;
        } else {
            jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
            return -1;
        }
    } else {
        return 0;
    }
}

int JackClientSocket::Write(void* data, int len)
{
    int res;

#if defined(__sun__) || defined(sun)
    if (fTimeOut > 0) {

        struct timeval tv;
	    fd_set fdset;
        ssize_t res;

        tv.tv_sec = fTimeOut;
	    tv.tv_usec = 0;

	    FD_ZERO(&fdset);
	    FD_SET(fSocket, &fdset);

	    do {
		    res = select(fSocket + 1, NULL, &fdset, NULL, &tv);
	    } while (res < 0 && errno == EINTR);

	    if (res < 0) {
		    return res;
        } else if (res == 0) {
		   return -1;
	    }
   }
#endif

    if ((res = write(fSocket, data, len)) != len) {
        if (errno == EWOULDBLOCK || errno == EAGAIN) {
            jack_log("JackClientSocket::Write time out");
            return 0;  // For a non blocking socket, a write failure is not considered as an error
        } else if (res != 0) {
            jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
            //return 0;
            return -1;
        } else {
            jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
            return -1;
        }
    } else {
        return 0;
    }
}

JackServerSocket::JackServerSocket(): fSocket( -1)
{
    const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
    fPromiscuous = (promiscuous != NULL);
    fPromiscuousGid = jack_group2gid(promiscuous);
}

int JackServerSocket::Bind(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
{
    struct sockaddr_un addr;

    if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        jack_error("Cannot create server socket err = %s", strerror(errno));
        return -1;
    }

    addr.sun_family = AF_UNIX;
    // Socket name has to be kept in fName to be "unlinked".
    BuildName(name, fName, dir, which, sizeof(addr.sun_path), fPromiscuous);
    strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
   
    jack_log("JackServerSocket::Bind : addr.sun_path %s", addr.sun_path);
    unlink(fName); // Security...

    if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        jack_error("Cannot bind server to socket err = %s", strerror(errno));
        goto error;
    }

    if (listen(fSocket, 100) < 0) {
        jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
        goto error;
    }

    if (fPromiscuous && (jack_promiscuous_perms(-1, fName, fPromiscuousGid) < 0))
        goto error;

    return 0;

error:
    unlink(fName);
    close(fSocket);
    fSocket = -1;
    return -1;
}

JackClientSocket* JackServerSocket::Accept()
{
    struct sockaddr_un client_addr;
    socklen_t client_addrlen;

    memset(&client_addr, 0, sizeof(client_addr));
    client_addrlen = sizeof(client_addr);

    int fd = accept(fSocket, (struct sockaddr*)&client_addr, &client_addrlen);
    if (fd < 0) {
        jack_error("Cannot accept new connection err = %s", strerror(errno));
        return 0;
    } else {
        return new JackClientSocket(fd);
    }
}

int JackServerSocket::Close()
{
    if (fSocket > 0) {
        jack_log("JackServerSocket::Close %s", fName);
        shutdown(fSocket, SHUT_RDWR);
        close(fSocket);
        unlink(fName);
        fSocket = -1;
        return 0;
    } else {
        return -1;
    }
}

} // end of namespace