source: roaraudio/libroar/basic.c @ 1099:8cc65a7ec79b

Last change on this file since 1099:8cc65a7ec79b was 1099:8cc65a7ec79b, checked in by phi, 15 years ago

added ROAR_NETWORK_READ() and ROAR_NETWORK_WRITE() to support reading and writeing on sockets on win32

File size: 9.2 KB
Line 
1//basic.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_connect_raw (char * server) {
38 char user_sock[80];
39 char * roar_server;
40 int i = 0;
41 int port = 0;
42 int fh = -1;
43 int is_decnet = 0;
44 char * obj = NULL;
45 struct passwd * pwd;
46#ifdef ROAR_HAVE_LIBDNET
47 struct stat decnet_stat;
48#endif
49
50 roar_errno = ROAR_ERROR_UNKNOWN;
51
52 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
53  server = roar_server;
54
55#ifndef ROAR_TARGET_WIN32
56 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
57   user_sock[i] = 0;
58   server = user_sock;
59 }
60#endif
61
62 if ( server == NULL || *server == 0 ) {
63  /* connect via defaults */
64
65
66  roar_server = getenv("HOME");
67
68  if ( roar_server == NULL ) {
69#ifndef ROAR_TARGET_WIN32
70   if ( (pwd = getpwuid(getuid())) == NULL ) {
71    roar_server = "/NX-HOME-DIR";
72   } else {
73    roar_server = pwd->pw_dir;
74   }
75#else
76   roar_server = "/WIN32-SUCKS";
77#endif
78  }
79
80  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
81
82  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
83   return fh;
84
85  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
86   return fh;
87
88  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
89   return fh;
90
91#ifdef ROAR_HAVE_LIBDNET
92  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
93   if ( roar_socket_get_local_nodename() ) {
94    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
95    return roar_socket_connect(user_sock, ROAR_DEFAULT_NUM);
96   }
97  }
98#endif
99
100 } else {
101  /* connect via (char*)server */
102  // find a port:
103  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
104   for (i = 0; server[i] != 0; i++) {
105    if ( server[i] == ':' ) {
106     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
107      is_decnet = 1;
108      obj = &server[i+2];
109      break;
110     }
111
112     port = atoi(server+i+1);
113     server[i] = 0;
114     break;
115    }
116   }
117  }
118
119  if ( is_decnet ) {
120    *user_sock = 0;
121   if ( *server == ':' ) {
122    if ( roar_socket_get_local_nodename() )
123     strncat(user_sock, roar_socket_get_local_nodename(), 6);
124   }
125
126   strncat(user_sock, server, 79);
127   server = user_sock;
128   if ( *obj == 0 ) {
129#ifdef DN_MAXOBJL
130    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
131#else
132    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
133#endif
134   }
135  }
136
137  if ( port || is_decnet ) {
138   fh = roar_socket_connect(server, port);
139   // restore the original string
140   server[i] = ':';
141  } else {
142   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
143  }
144 }
145
146 if ( fh == -1 )
147  roar_errno = ROAR_ERROR_CONNREFUSED;
148
149 ROAR_DBG("roar_connect_raw(*) = %i", fh);
150
151 return fh;
152}
153
154int roar_connect    (struct roar_connection * con, char * server) {
155 roar_errno = ROAR_ERROR_UNKNOWN;
156 con->fh = roar_connect_raw(server);
157
158 if ( con->fh == -1 )
159  return -1;
160
161 roar_errno = ROAR_ERROR_NONE;
162
163 return 0;
164}
165
166int roar_disconnect (struct roar_connection * con) {
167 struct roar_message m;
168
169 m.datalen = 0;
170 m.stream  = 0;
171 m.pos     = 0;
172 m.cmd     = ROAR_CMD_QUIT;
173
174 roar_req(con, &m, NULL);
175
176 close(con->fh);
177
178 con->fh = -1;
179
180 roar_errno = ROAR_ERROR_NONE;
181
182 return 0;
183}
184
185int roar_identify   (struct roar_connection * con, char * name) {
186 struct roar_message mes;
187 pid_t pid;
188 int max_len;
189
190 roar_errno = ROAR_ERROR_UNKNOWN;
191
192 ROAR_DBG("roar_identify(*): try to identify myself...");
193
194 mes.cmd    = ROAR_CMD_IDENTIFY;
195 mes.stream = 0;
196 mes.pos    = 0;
197
198 ROAR_DBG("roar_identify(*): name=%p", name);
199
200 if ( name == NULL )
201  name = "libroar client";
202
203 ROAR_DBG("roar_identify(*): name=%p", name);
204
205 max_len = strlen(name);
206 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
207
208 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
209  max_len = LIBROAR_BUFFER_MSGDATA - 5;
210
211 mes.datalen = 5 + max_len;
212 mes.data[0] = 1;
213
214 pid = getpid();
215 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
216 ROAR_DBG("roar_identify(*): pid = %i", pid);
217
218 strncpy(mes.data+5, name, max_len);
219
220 return roar_req(con, &mes, NULL);
221}
222
223#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
224int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
225 char buf[_ROAR_MESS_BUF_LEN];
226
227 roar_errno = ROAR_ERROR_UNKNOWN;
228
229 ROAR_DBG("roar_send_message(*): try to send an request...");
230
231 buf[0] = _ROAR_MESSAGE_VERSION;
232 buf[1] = (unsigned char) mes->cmd;
233 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
234 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
235 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
236
237 if ( ROAR_NETWORK_WRITE(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
238  roar_errno = ROAR_ERROR_PIPE;
239  return -1;
240 }
241
242 if ( mes->datalen != 0 ) {
243  if ( ROAR_NETWORK_WRITE(con->fh, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
244   roar_errno = ROAR_ERROR_PIPE;
245   return -1;
246  }
247 }
248
249 roar_errno = ROAR_ERROR_NONE;
250
251 ROAR_DBG("roar_send_message(*) = 0");
252 return 0;
253}
254
255int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
256 char buf[_ROAR_MESS_BUF_LEN];
257
258 roar_errno = ROAR_ERROR_UNKNOWN;
259
260 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
261
262 if ( data )
263  *data = NULL;
264
265 if ( ROAR_NETWORK_READ(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
266  roar_errno = ROAR_ERROR_PROTO;
267  return -1;
268 }
269
270 ROAR_DBG("roar_recv_message(*): Got a header");
271
272 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
273  roar_errno = ROAR_ERROR_PROTO;
274  return -1;
275 }
276
277 mes->cmd     = (unsigned char)buf[1];
278 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
279 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
280 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
281
282 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
283           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
284
285 if ( mes->datalen == 0 ) {
286  ROAR_DBG("roar_recv_message(*): no data in this pkg");
287  ROAR_DBG("roar_recv_message(*) = 0");
288  roar_errno = ROAR_ERROR_NONE;
289  return 0;
290 }
291
292 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
293  if ( ROAR_NETWORK_READ(con->fh, mes->data, mes->datalen) == mes->datalen ) {
294   ROAR_DBG("roar_recv_message(*): Got data!");
295   ROAR_DBG("roar_recv_message(*) = 0");
296   roar_errno = ROAR_ERROR_NONE;
297   return 0;
298  }
299
300  roar_errno = ROAR_ERROR_PIPE;
301  return -1;
302 } else {
303  if ( data == NULL ) {
304   roar_errno = ROAR_ERROR_MSGSIZE;
305   return -1;
306  }
307
308  if ( (*data = malloc(mes->datalen)) == NULL ) {
309   roar_errno = ROAR_ERROR_NOMEM;
310   return -1;
311  }
312
313  if ( mes->datalen == 0 ) {
314   roar_errno = ROAR_ERROR_NONE;
315   return 0;
316  }
317
318  if ( ROAR_NETWORK_READ(con->fh, *data, mes->datalen) == mes->datalen ) {
319   ROAR_DBG("roar_recv_message(*): Got data!");
320   ROAR_DBG("roar_recv_message(*) = 0");
321   roar_errno = ROAR_ERROR_NONE;
322   return 0;
323  }
324
325  roar_errno = ROAR_ERROR_PIPE;
326  return -1;
327 }
328
329 // what happened here?
330 return -1;
331}
332
333int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
334 if ( roar_send_message(con, mes, data ? *data : NULL) != 0 )
335  return -1;
336
337 if ( data )
338  free(*data);
339
340 return roar_recv_message(con, mes, data);
341}
342
343int roar_debug_message_print (struct roar_message * mes) {
344 if ( mes == NULL )
345  return -1;
346
347 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
348 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
349 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
350 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
351
352 ROAR_DBG("roar_debug_message_print(*) = 0");
353 return 0;
354}
355
356int roar_debug_audio_info_print (struct roar_audio_info * info) {
357 if ( info == NULL )
358  return -1;
359
360 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
361 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
362 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
363 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
364
365 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
366 return 0;
367}
368
369//ll
Note: See TracBrowser for help on using the repository browser.