source: roaraudio/libroar/basic.c @ 1394:b8703c5336a6

Last change on this file since 1394:b8703c5336a6 was 1393:c551ccbda1b5, checked in by phi, 15 years ago

do not declare unused var on win32

File size: 10.4 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;
[1393]45#ifndef ROAR_TARGET_WIN32
[1026]46 struct passwd * pwd;
[1393]47#endif
[828]48#ifdef ROAR_HAVE_LIBDNET
49 struct stat decnet_stat;
50#endif
[0]51
[807]52 roar_errno = ROAR_ERROR_UNKNOWN;
53
[61]54 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]55  server = roar_server;
56
[1093]57#ifndef ROAR_TARGET_WIN32
[448]58 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
59   user_sock[i] = 0;
60   server = user_sock;
61 }
[1093]62#endif
[448]63
[61]64 if ( server == NULL || *server == 0 ) {
[0]65  /* connect via defaults */
66
67
[1026]68  roar_server = getenv("HOME");
69
70  if ( roar_server == NULL ) {
[1078]71#ifndef ROAR_TARGET_WIN32
[1026]72   if ( (pwd = getpwuid(getuid())) == NULL ) {
73    roar_server = "/NX-HOME-DIR";
74   } else {
75    roar_server = pwd->pw_dir;
76   }
[1078]77#else
78   roar_server = "/WIN32-SUCKS";
79#endif
[1026]80  }
81
82  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
[0]83
84  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
85   return fh;
86
[237]87  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
88   return fh;
89
[0]90  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
91   return fh;
92
[828]93#ifdef ROAR_HAVE_LIBDNET
94  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
95   if ( roar_socket_get_local_nodename() ) {
96    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
97    return roar_socket_connect(user_sock, ROAR_DEFAULT_NUM);
98   }
[522]99  }
[828]100#endif
[522]101
[0]102 } else {
103  /* connect via (char*)server */
[501]104  // find a port:
105  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
106   for (i = 0; server[i] != 0; i++) {
107    if ( server[i] == ':' ) {
108     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
109      is_decnet = 1;
[516]110      obj = &server[i+2];
[501]111      break;
112     }
113
114     port = atoi(server+i+1);
115     server[i] = 0;
116     break;
117    }
[0]118   }
119  }
120
[521]121  if ( is_decnet ) {
122    *user_sock = 0;
123   if ( *server == ':' ) {
124    if ( roar_socket_get_local_nodename() )
[1066]125     strncat(user_sock, roar_socket_get_local_nodename(), 6);
[521]126   }
127
[1066]128   strncat(user_sock, server, 79);
[516]129   server = user_sock;
[521]130   if ( *obj == 0 ) {
[1066]131#ifdef DN_MAXOBJL
132    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
133#else
134    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
135#endif
[521]136   }
[516]137  }
138
[501]139  if ( port || is_decnet ) {
[0]140   fh = roar_socket_connect(server, port);
141   // restore the original string
142   server[i] = ':';
143  } else {
144   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
145  }
146 }
147
[807]148 if ( fh == -1 )
149  roar_errno = ROAR_ERROR_CONNREFUSED;
150
[0]151 ROAR_DBG("roar_connect_raw(*) = %i", fh);
152
153 return fh;
154}
155
156int roar_connect    (struct roar_connection * con, char * server) {
[1315]157 int fh;
158
159 if ( con == NULL ) {
160  roar_errno = ROAR_ERROR_INVAL;
161  return -1;
162 }
163
[807]164 roar_errno = ROAR_ERROR_UNKNOWN;
[1315]165 fh = roar_connect_raw(server);
[0]166
[1315]167 if ( fh == -1 )
[0]168  return -1;
169
[1315]170 return roar_connect_fh(con, fh);
171}
172
173int roar_connect_fh (struct roar_connection * con, int fh) {
174
175 if ( con == NULL || fh == -1 ) {
176  roar_errno = ROAR_ERROR_INVAL;
177  return -1;
178 }
179
180 // specal hack to set an ilegal value used internaly in libroar:
181 if ( fh == -2 )
182  fh = -1;
183
184 memset(con, 0, sizeof(struct roar_connection));
185
186 con->fh = fh;
187
[807]188 roar_errno = ROAR_ERROR_NONE;
[0]189 return 0;
190}
191
192int roar_disconnect (struct roar_connection * con) {
193 struct roar_message m;
194
195 m.datalen = 0;
196 m.stream  = 0;
[133]197 m.pos     = 0;
[0]198 m.cmd     = ROAR_CMD_QUIT;
199
200 roar_req(con, &m, NULL);
201
202 close(con->fh);
203
204 con->fh = -1;
205
[807]206 roar_errno = ROAR_ERROR_NONE;
207
[0]208 return 0;
209}
210
211int roar_identify   (struct roar_connection * con, char * name) {
212 struct roar_message mes;
213 pid_t pid;
214 int max_len;
215
[807]216 roar_errno = ROAR_ERROR_UNKNOWN;
217
[0]218 ROAR_DBG("roar_identify(*): try to identify myself...");
219
220 mes.cmd    = ROAR_CMD_IDENTIFY;
221 mes.stream = 0;
222 mes.pos    = 0;
223
224 ROAR_DBG("roar_identify(*): name=%p", name);
225
226 if ( name == NULL )
227  name = "libroar client";
228
229 ROAR_DBG("roar_identify(*): name=%p", name);
230
231 max_len = strlen(name);
232 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
233
234 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
235  max_len = LIBROAR_BUFFER_MSGDATA - 5;
236
237 mes.datalen = 5 + max_len;
238 mes.data[0] = 1;
239
240 pid = getpid();
241 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
242 ROAR_DBG("roar_identify(*): pid = %i", pid);
243
244 strncpy(mes.data+5, name, max_len);
245
246 return roar_req(con, &mes, NULL);
247}
248
[8]249#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
[0]250int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
[1319]251 struct roar_vio_calls vio;
252
253 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
254  return -1;
255
256 return roar_vsend_message(&vio, mes, data);
257}
258
259int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
[0]260 char buf[_ROAR_MESS_BUF_LEN];
261
[807]262 roar_errno = ROAR_ERROR_UNKNOWN;
263
[0]264 ROAR_DBG("roar_send_message(*): try to send an request...");
265
[8]266 buf[0] = _ROAR_MESSAGE_VERSION;
267 buf[1] = (unsigned char) mes->cmd;
268 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
269 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
270 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
[0]271
[1319]272 if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]273  roar_errno = ROAR_ERROR_PIPE;
[0]274  return -1;
[807]275 }
[0]276
[807]277 if ( mes->datalen != 0 ) {
[1319]278  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
[807]279   roar_errno = ROAR_ERROR_PIPE;
[0]280   return -1;
[807]281  }
282 }
283
[1334]284 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
285
[807]286 roar_errno = ROAR_ERROR_NONE;
[0]287
288 ROAR_DBG("roar_send_message(*) = 0");
289 return 0;
290}
291
292int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]293 struct roar_vio_calls vio;
294
295 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
296  return -1;
297
298 return roar_vrecv_message(&vio, mes, data);
299}
300
301int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
[0]302 char buf[_ROAR_MESS_BUF_LEN];
303
[807]304 roar_errno = ROAR_ERROR_UNKNOWN;
305
[0]306 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
307
308 if ( data )
309  *data = NULL;
310
[1319]311 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]312  roar_errno = ROAR_ERROR_PROTO;
[0]313  return -1;
[807]314 }
[0]315
316 ROAR_DBG("roar_recv_message(*): Got a header");
317
[807]318 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
319  roar_errno = ROAR_ERROR_PROTO;
[8]320  return -1;
[807]321 }
[8]322
323 mes->cmd     = (unsigned char)buf[1];
324 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
325 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
326 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
[0]327
328 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
329           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
330
[104]331 if ( mes->datalen == 0 ) {
332  ROAR_DBG("roar_recv_message(*): no data in this pkg");
333  ROAR_DBG("roar_recv_message(*) = 0");
[807]334  roar_errno = ROAR_ERROR_NONE;
[104]335  return 0;
336 }
337
[0]338 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
[1319]339  if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) {
[0]340   ROAR_DBG("roar_recv_message(*): Got data!");
341   ROAR_DBG("roar_recv_message(*) = 0");
[807]342   roar_errno = ROAR_ERROR_NONE;
[0]343   return 0;
344  }
[807]345
346  roar_errno = ROAR_ERROR_PIPE;
[0]347  return -1;
348 } else {
[807]349  if ( data == NULL ) {
350   roar_errno = ROAR_ERROR_MSGSIZE;
[0]351   return -1;
[807]352  }
[0]353
[807]354  if ( (*data = malloc(mes->datalen)) == NULL ) {
355   roar_errno = ROAR_ERROR_NOMEM;
[0]356   return -1;
[807]357  }
[0]358
[807]359  if ( mes->datalen == 0 ) {
360   roar_errno = ROAR_ERROR_NONE;
[0]361   return 0;
[807]362  }
[0]363
[1319]364  if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) {
[0]365   ROAR_DBG("roar_recv_message(*): Got data!");
366   ROAR_DBG("roar_recv_message(*) = 0");
[807]367   roar_errno = ROAR_ERROR_NONE;
[0]368   return 0;
369  }
[807]370
371  roar_errno = ROAR_ERROR_PIPE;
[0]372  return -1;
373 }
374
[807]375 // what happened here?
[0]376 return -1;
377}
378
379int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]380 struct roar_vio_calls vio;
381
382 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
383  return -1;
384
385 return roar_vreq(&vio, mes, data);
386}
387
388int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
389 if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 )
[0]390  return -1;
391
392 if ( data )
393  free(*data);
394
[1319]395 return roar_vrecv_message(vio, mes, data);
[0]396}
397
398int roar_debug_message_print (struct roar_message * mes) {
399 if ( mes == NULL )
400  return -1;
401
402 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
403 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
404 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
405 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
406
407 ROAR_DBG("roar_debug_message_print(*) = 0");
408 return 0;
409}
410
411int roar_debug_audio_info_print (struct roar_audio_info * info) {
412 if ( info == NULL )
413  return -1;
414
415 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
416 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
417 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
418 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
419
420 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
421 return 0;
422}
423
424//ll
Note: See TracBrowser for help on using the repository browser.