source: roaraudio/libroar/basic.c @ 1078:fa6ec67f156f

Last change on this file since 1078:fa6ec67f156f was 1078:fa6ec67f156f, checked in by phi, 15 years ago

do not use incomplete struct on win32 (no pwd.h *grrr*)

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