Jack2  1.9.11-RC1
JackSocket.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 #include "JackSocket.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <string.h>
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <fcntl.h>
28 
29 namespace Jack
30 {
31 
32 static void BuildName(const char* client_name, char* res, const char* dir, int which, int size)
33 {
34  char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
35  JackTools::RewriteName(client_name, ext_client_name);
36  if (getenv("JACK_PROMISCUOUS_SERVER")) {
37  snprintf(res, size, "%s/jack_%s_%d", dir, ext_client_name, which);
38  } else {
39  snprintf(res, size, "%s/jack_%s_%d_%d", dir, ext_client_name, JackTools::GetUID(), which);
40  }
41 }
42 
43 JackClientSocket::JackClientSocket(int socket): JackClientRequestInterface(), fSocket(socket),fTimeOut(0)
44 {}
45 
46 #if defined(__sun__) || defined(sun)
47 
48 void JackClientSocket::SetReadTimeOut(long sec)
49 {
50  int flags;
51  fTimeOut = sec;
52 
53  if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
54  jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_GETFL");
55  return;
56  }
57 
58  flags |= O_NONBLOCK;
59  if (fcntl(fSocket, F_SETFL, flags) < 0) {
60  jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_SETFL");
61  return;
62  }
63 }
64 
65 void JackClientSocket::SetWriteTimeOut(long sec)
66 {
67  int flags;
68  fTimeOut = sec;
69 
70  if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
71  jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_GETFL");
72  return;
73  }
74 
75  flags |= O_NONBLOCK;
76  if (fcntl(fSocket, F_SETFL, flags) < 0) {
77  jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_SETFL");
78  return;
79  }
80 }
81 
82 #else
83 
84 void JackClientSocket::SetReadTimeOut(long sec)
85 {
86  struct timeval timout;
87  timout.tv_sec = sec;
88  timout.tv_usec = 0;
89  if (setsockopt(fSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
90  jack_error("SetReadTimeOut fd = %ld err = %s", fSocket, strerror(errno));
91  }
92 }
93 
94 void JackClientSocket::SetWriteTimeOut(long sec)
95 {
96  struct timeval timout;
97  timout.tv_sec = sec ;
98  timout.tv_usec = 0;
99  if (setsockopt(fSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
100  jack_error("SetWriteTimeOut fd = %ld err = %s", fSocket, strerror(errno));
101  }
102 }
103 
104 #endif
105 
106 void JackClientSocket::SetNonBlocking(bool onoff)
107 {
108  if (onoff) {
109  long flags = 0;
110  if (fcntl(fSocket, F_SETFL, flags | O_NONBLOCK) < 0) {
111  jack_error("SetNonBlocking fd = %ld err = %s", fSocket, strerror(errno));
112  }
113  }
114 }
115 
116 int JackClientSocket::Connect(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
117 {
118  struct sockaddr_un addr;
119 
120  if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
121  jack_error("Cannot create socket err = %s", strerror(errno));
122  return -1;
123  }
124 
125  addr.sun_family = AF_UNIX;
126  BuildName(name, addr.sun_path, dir, which, sizeof(addr.sun_path));
127  jack_log("JackClientSocket::Connect : addr.sun_path %s", addr.sun_path);
128 
129  if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
130  jack_error("Cannot connect to server socket err = %s", strerror(errno));
131  close(fSocket);
132  return -1;
133  }
134 
135 #ifdef __APPLE__
136  int on = 1;
137  if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
138  jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
139  }
140 #endif
141 
142  return 0;
143 }
144 
145 int JackClientSocket::Close()
146 {
147  jack_log("JackClientSocket::Close");
148  if (fSocket > 0) {
149  shutdown(fSocket, SHUT_RDWR);
150  close(fSocket);
151  fSocket = -1;
152  return 0;
153  } else {
154  return -1;
155  }
156 }
157 
158 int JackClientSocket::Read(void* data, int len)
159 {
160  int res;
161 
162 #if defined(__sun__) || defined(sun)
163  if (fTimeOut > 0) {
164 
165  struct timeval tv;
166  fd_set fdset;
167  ssize_t res;
168 
169  tv.tv_sec = fTimeOut;
170  tv.tv_usec = 0;
171 
172  FD_ZERO(&fdset);
173  FD_SET(fSocket, &fdset);
174 
175  do {
176  res = select(fSocket + 1, &fdset, NULL, NULL, &tv);
177  } while (res < 0 && errno == EINTR);
178 
179  if (res < 0) {
180  return res;
181  } else if (res == 0) {
182  return -1;
183  }
184  }
185 #endif
186 
187  if ((res = read(fSocket, data, len)) != len) {
188  if (errno == EWOULDBLOCK || errno == EAGAIN) {
189  jack_error("JackClientSocket::Read time out");
190  return 0; // For a non blocking socket, a read failure is not considered as an error
191  } else if (res != 0) {
192  jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
193  //return 0;
194  return -1;
195  } else {
196  jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
197  return -1;
198  }
199  } else {
200  return 0;
201  }
202 }
203 
204 int JackClientSocket::Write(void* data, int len)
205 {
206  int res;
207 
208 #if defined(__sun__) || defined(sun)
209  if (fTimeOut > 0) {
210 
211  struct timeval tv;
212  fd_set fdset;
213  ssize_t res;
214 
215  tv.tv_sec = fTimeOut;
216  tv.tv_usec = 0;
217 
218  FD_ZERO(&fdset);
219  FD_SET(fSocket, &fdset);
220 
221  do {
222  res = select(fSocket + 1, NULL, &fdset, NULL, &tv);
223  } while (res < 0 && errno == EINTR);
224 
225  if (res < 0) {
226  return res;
227  } else if (res == 0) {
228  return -1;
229  }
230  }
231 #endif
232 
233  if ((res = write(fSocket, data, len)) != len) {
234  if (errno == EWOULDBLOCK || errno == EAGAIN) {
235  jack_log("JackClientSocket::Write time out");
236  return 0; // For a non blocking socket, a write failure is not considered as an error
237  } else if (res != 0) {
238  jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
239  //return 0;
240  return -1;
241  } else {
242  jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
243  return -1;
244  }
245  } else {
246  return 0;
247  }
248 }
249 
250 int JackServerSocket::Bind(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
251 {
252  struct sockaddr_un addr;
253 
254  if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
255  jack_error("Cannot create server socket err = %s", strerror(errno));
256  return -1;
257  }
258 
259  addr.sun_family = AF_UNIX;
260  // Socket name has to be kept in fName to be "unlinked".
261  BuildName(name, fName, dir, which, sizeof(addr.sun_path));
262  strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
263 
264  jack_log("JackServerSocket::Bind : addr.sun_path %s", addr.sun_path);
265  unlink(fName); // Security...
266 
267  if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
268  jack_error("Cannot bind server to socket err = %s", strerror(errno));
269  goto error;
270  }
271 
272  if (listen(fSocket, 100) < 0) {
273  jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
274  goto error;
275  }
276 
277  return 0;
278 
279 error:
280  unlink(fName);
281  close(fSocket);
282  return -1;
283 }
284 
285 JackClientSocket* JackServerSocket::Accept()
286 {
287  struct sockaddr_un client_addr;
288  socklen_t client_addrlen;
289 
290  memset(&client_addr, 0, sizeof(client_addr));
291  client_addrlen = sizeof(client_addr);
292 
293  int fd = accept(fSocket, (struct sockaddr*)&client_addr, &client_addrlen);
294  if (fd < 0) {
295  jack_error("Cannot accept new connection err = %s", strerror(errno));
296  return 0;
297  } else {
298  return new JackClientSocket(fd);
299  }
300 }
301 
302 int JackServerSocket::Close()
303 {
304  if (fSocket > 0) {
305  jack_log("JackServerSocket::Close %s", fName);
306  shutdown(fSocket, SHUT_RDWR);
307  close(fSocket);
308  unlink(fName);
309  fSocket = -1;
310  return 0;
311  } else {
312  return -1;
313  }
314 }
315 
316 } // end of namespace
317 
318 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108