source: roaraudio/libroar/basic.c @ 828:8b4d4f52ca50

Last change on this file since 828:8b4d4f52ca50 was 828:8b4d4f52ca50, checked in by phi, 16 years ago

do not try DECnet on non DECnet kernels...

File size: 8.7 KB
RevLine 
[0]1//basic.c:
2
[690]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
[0]35#include "libroar.h"
36
37int roar_connect_raw (char * server) {
38 char user_sock[80];
39 char * roar_server;
[701]40 int i = 0;
[0]41 int port = 0;
42 int fh = -1;
[501]43 int is_decnet = 0;
[516]44 char * obj = NULL;
[828]45#ifdef ROAR_HAVE_LIBDNET
46 struct stat decnet_stat;
47#endif
[0]48
[807]49 roar_errno = ROAR_ERROR_UNKNOWN;
50
[61]51 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]52  server = roar_server;
53
[448]54 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
55   user_sock[i] = 0;
56   server = user_sock;
57 }
58
[61]59 if ( server == NULL || *server == 0 ) {
[0]60  /* connect via defaults */
61
62
63  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
64
65  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
66   return fh;
67
[237]68  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
69   return fh;
70
[0]71  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
72   return fh;
73
[828]74#ifdef ROAR_HAVE_LIBDNET
75  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
76   if ( roar_socket_get_local_nodename() ) {
77    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
78    return roar_socket_connect(user_sock, ROAR_DEFAULT_NUM);
79   }
[522]80  }
[828]81#endif
[522]82
[0]83 } else {
84  /* connect via (char*)server */
[501]85  // find a port:
86  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
87   for (i = 0; server[i] != 0; i++) {
88    if ( server[i] == ':' ) {
89     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
90      is_decnet = 1;
[516]91      obj = &server[i+2];
[501]92      break;
93     }
94
95     port = atoi(server+i+1);
96     server[i] = 0;
97     break;
98    }
[0]99   }
100  }
101
[521]102  if ( is_decnet ) {
103    *user_sock = 0;
104   if ( *server == ':' ) {
105    if ( roar_socket_get_local_nodename() )
106     strcat(user_sock, roar_socket_get_local_nodename());
107   }
108
109   strcat(user_sock, server);
[516]110   server = user_sock;
[521]111   if ( *obj == 0 ) {
112    strcat(user_sock, ROAR_DEFAULT_OBJECT);
113   }
[516]114  }
115
[501]116  if ( port || is_decnet ) {
[0]117   fh = roar_socket_connect(server, port);
118   // restore the original string
119   server[i] = ':';
120  } else {
121   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
122  }
123 }
124
[807]125 if ( fh == -1 )
126  roar_errno = ROAR_ERROR_CONNREFUSED;
127
[0]128 ROAR_DBG("roar_connect_raw(*) = %i", fh);
129
130 return fh;
131}
132
133int roar_connect    (struct roar_connection * con, char * server) {
[807]134 roar_errno = ROAR_ERROR_UNKNOWN;
[0]135 con->fh = roar_connect_raw(server);
136
137 if ( con->fh == -1 )
138  return -1;
139
[807]140 roar_errno = ROAR_ERROR_NONE;
141
[0]142 return 0;
143}
144
145int roar_disconnect (struct roar_connection * con) {
146 struct roar_message m;
147
148 m.datalen = 0;
149 m.stream  = 0;
[133]150 m.pos     = 0;
[0]151 m.cmd     = ROAR_CMD_QUIT;
152
153 roar_req(con, &m, NULL);
154
155 close(con->fh);
156
157 con->fh = -1;
158
[807]159 roar_errno = ROAR_ERROR_NONE;
160
[0]161 return 0;
162}
163
164int roar_identify   (struct roar_connection * con, char * name) {
165 struct roar_message mes;
166 pid_t pid;
167 int max_len;
168
[807]169 roar_errno = ROAR_ERROR_UNKNOWN;
170
[0]171 ROAR_DBG("roar_identify(*): try to identify myself...");
172
173 mes.cmd    = ROAR_CMD_IDENTIFY;
174 mes.stream = 0;
175 mes.pos    = 0;
176
177 ROAR_DBG("roar_identify(*): name=%p", name);
178
179 if ( name == NULL )
180  name = "libroar client";
181
182 ROAR_DBG("roar_identify(*): name=%p", name);
183
184 max_len = strlen(name);
185 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
186
187 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
188  max_len = LIBROAR_BUFFER_MSGDATA - 5;
189
190 mes.datalen = 5 + max_len;
191 mes.data[0] = 1;
192
193 pid = getpid();
194 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
195 ROAR_DBG("roar_identify(*): pid = %i", pid);
196
197 strncpy(mes.data+5, name, max_len);
198
199 return roar_req(con, &mes, NULL);
200}
201
[8]202#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
[0]203int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
204 char buf[_ROAR_MESS_BUF_LEN];
205
[807]206 roar_errno = ROAR_ERROR_UNKNOWN;
207
[0]208 ROAR_DBG("roar_send_message(*): try to send an request...");
209
[8]210 buf[0] = _ROAR_MESSAGE_VERSION;
211 buf[1] = (unsigned char) mes->cmd;
212 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
213 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
214 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
[0]215
[807]216 if ( write(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
217  roar_errno = ROAR_ERROR_PIPE;
[0]218  return -1;
[807]219 }
[0]220
[807]221 if ( mes->datalen != 0 ) {
222  if ( write(con->fh, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
223   roar_errno = ROAR_ERROR_PIPE;
[0]224   return -1;
[807]225  }
226 }
227
228 roar_errno = ROAR_ERROR_NONE;
[0]229
230 ROAR_DBG("roar_send_message(*) = 0");
231 return 0;
232}
233
234int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
235 char buf[_ROAR_MESS_BUF_LEN];
236
[807]237 roar_errno = ROAR_ERROR_UNKNOWN;
238
[0]239 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
240
241 if ( data )
242  *data = NULL;
243
[807]244 if ( read(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
245  roar_errno = ROAR_ERROR_PROTO;
[0]246  return -1;
[807]247 }
[0]248
249 ROAR_DBG("roar_recv_message(*): Got a header");
250
[807]251 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
252  roar_errno = ROAR_ERROR_PROTO;
[8]253  return -1;
[807]254 }
[8]255
256 mes->cmd     = (unsigned char)buf[1];
257 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
258 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
259 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
[0]260
261 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
262           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
263
[104]264 if ( mes->datalen == 0 ) {
265  ROAR_DBG("roar_recv_message(*): no data in this pkg");
266  ROAR_DBG("roar_recv_message(*) = 0");
[807]267  roar_errno = ROAR_ERROR_NONE;
[104]268  return 0;
269 }
270
[0]271 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
272  if ( read(con->fh, mes->data, mes->datalen) == mes->datalen ) {
273   ROAR_DBG("roar_recv_message(*): Got data!");
274   ROAR_DBG("roar_recv_message(*) = 0");
[807]275   roar_errno = ROAR_ERROR_NONE;
[0]276   return 0;
277  }
[807]278
279  roar_errno = ROAR_ERROR_PIPE;
[0]280  return -1;
281 } else {
[807]282  if ( data == NULL ) {
283   roar_errno = ROAR_ERROR_MSGSIZE;
[0]284   return -1;
[807]285  }
[0]286
[807]287  if ( (*data = malloc(mes->datalen)) == NULL ) {
288   roar_errno = ROAR_ERROR_NOMEM;
[0]289   return -1;
[807]290  }
[0]291
[807]292  if ( mes->datalen == 0 ) {
293   roar_errno = ROAR_ERROR_NONE;
[0]294   return 0;
[807]295  }
[0]296
297  if ( read(con->fh, *data, mes->datalen) == mes->datalen ) {
298   ROAR_DBG("roar_recv_message(*): Got data!");
299   ROAR_DBG("roar_recv_message(*) = 0");
[807]300   roar_errno = ROAR_ERROR_NONE;
[0]301   return 0;
302  }
[807]303
304  roar_errno = ROAR_ERROR_PIPE;
[0]305  return -1;
306 }
307
[807]308 // what happened here?
[0]309 return -1;
310}
311
312int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
313 if ( roar_send_message(con, mes, data ? *data : NULL) != 0 )
314  return -1;
315
316 if ( data )
317  free(*data);
318
319 return roar_recv_message(con, mes, data);
320}
321
322int roar_debug_message_print (struct roar_message * mes) {
323 if ( mes == NULL )
324  return -1;
325
326 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
327 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
328 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
329 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
330
331 ROAR_DBG("roar_debug_message_print(*) = 0");
332 return 0;
333}
334
335int roar_debug_audio_info_print (struct roar_audio_info * info) {
336 if ( info == NULL )
337  return -1;
338
339 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
340 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
341 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
342 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
343
344 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
345 return 0;
346}
347
348//ll
Note: See TracBrowser for help on using the repository browser.