source: roaraudio/libroar/proto.c @ 5407:edd703e264d0

Last change on this file since 5407:edd703e264d0 was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

File size: 10.2 KB
Line 
1//proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#define _ROAR_MESS_BUF_LEN_V0  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
39#define _ROAR_MESS_BUF_LEN_V1  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */ + \
40                                1 /* flags */)
41#define _ROAR_MESS_BUF_LEN_V2  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 8 /* pos */ + 2 /* datalen */ + \
42                                4 /* flags */   + 2 /* seq */)
43#define _ROAR_MESS_BUF_LEN     _ROAR_MESS_BUF_LEN_V0
44#define _ROAR_MESS_BUF_LEN_MAX ROAR_MAX3(_ROAR_MESS_BUF_LEN_V0, _ROAR_MESS_BUF_LEN_V1, _ROAR_MESS_BUF_LEN_V2)
45
46#define _ROAR_MESS_CRC_LEN_V0  0 /* No CRC */
47#define _ROAR_MESS_CRC_LEN_V1  1
48#define _ROAR_MESS_CRC_LEN_V2  4
49#define _ROAR_MESS_CRC_LEN_MAX ROAR_MAX3(_ROAR_MESS_CRC_LEN_V0, _ROAR_MESS_CRC_LEN_V1, _ROAR_MESS_CRC_LEN_V2)
50
51int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
52 struct roar_vio_calls * vio;
53
54 if ( (vio = roar_get_connection_vio2(con)) == NULL )
55  return -1;
56
57 return roar_vsend_message(vio, mes, data);
58}
59
60int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
61 char buf[_ROAR_MESS_BUF_LEN_MAX];
62 char crc[_ROAR_MESS_CRC_LEN_MAX];
63 size_t headerlen = 0;
64 size_t crclen    = 0;
65 char * bufptr;
66
67 roar_err_set(ROAR_ERROR_UNKNOWN);
68
69 ROAR_DBG("roar_send_message(*): try to send an request...");
70
71 headerlen = _ROAR_MESS_BUF_LEN;
72
73 mes->version = _ROAR_MESSAGE_VERSION;
74
75 buf[0] = mes->version; // first byte is always the version.
76
77 switch (mes->version) {
78  case 0:
79    buf[1]              = (unsigned char) mes->cmd;
80    *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
81    *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
82    *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
83    headerlen           = _ROAR_MESS_BUF_LEN_V0;
84   break;
85  case 1:
86    buf[1]              = (unsigned char) (mes->flags & 0xFF);
87    buf[2]              = (unsigned char)  mes->cmd;
88    // ...
89    roar_err_set(ROAR_ERROR_NSVERSION);
90    return -1;
91   break;
92  case 2:
93    headerlen           = 16;
94    buf[1]              = (unsigned char) mes->cmd;
95    *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
96    *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->flags);
97    if ( mes->flags & ROAR_MF_LSPOS ) {
98     *(uint64_t*)(buf+8) = ROAR_HOST2NET16(mes->pos64);
99     bufptr = buf+16;
100     headerlen          += 4;
101    } else {
102     *(uint32_t*)(buf+8) = ROAR_HOST2NET16(mes->pos);
103     bufptr = buf+12;
104    }
105    *(uint16_t*)(bufptr+0) = ROAR_HOST2NET16(mes->datalen);
106    *(uint16_t*)(bufptr+2) = ROAR_HOST2NET16(mes->seq);
107   break;
108  default:
109    roar_err_set(ROAR_ERROR_BADVERSION);
110    return -1;
111 }
112
113 if ( roar_vio_write(vio, buf, headerlen) != (ssize_t)headerlen ) {
114  return -1;
115 }
116
117 if ( mes->datalen != 0 ) {
118  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != (ssize_t)mes->datalen ) {
119   return -1;
120  }
121 }
122
123 if ( crclen != 0 ) {
124  if ( roar_vio_write(vio, crc, crclen) != (ssize_t)crclen ) {
125   return -1;
126  }
127 }
128
129 roar_err_clear();
130
131 ROAR_DBG("roar_send_message(*) = 0");
132 return 0;
133}
134
135int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
136 return roar_recv_message2(con, mes, data, NULL);
137}
138int roar_recv_message2 (struct roar_connection * con, struct roar_message * mes, char ** data,
139                        struct roar_error_frame * errorframe) {
140 struct roar_vio_calls * vio;
141
142 if ( (vio = roar_get_connection_vio2(con)) == NULL )
143  return -1;
144
145 return roar_vrecv_message2(vio, mes, data, errorframe);
146}
147
148int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
149 return roar_vrecv_message2(vio, mes, data, NULL);
150}
151
152int roar_vrecv_message2(struct roar_vio_calls  * vio, struct roar_message * mes, char ** data,
153                        struct roar_error_frame * errorframe) {
154 // TODO: add CRC support.
155 char buf[_ROAR_MESS_BUF_LEN_MAX];
156// char crc[_ROAR_MESS_CRC_LEN_MAX];
157 size_t headerlen = 0;
158// size_t crclen    = 0;
159 size_t needlen;
160 char * bufptr;
161
162 roar_err_set(ROAR_ERROR_UNKNOWN);
163
164 ROAR_DBG("roar_vrecv_message2(vio=%p, mes=%p, data=%p, errorframe=%p) = ?", vio, mes, data, errorframe);
165
166 ROAR_DBG("roar_vrecv_message2(*): try to get a response form the server...");
167
168 if ( vio == NULL || mes == NULL ) {
169  roar_err_set(ROAR_ERROR_FAULT);
170  return -1;
171 }
172
173 if ( data != NULL )
174  *data = NULL;
175
176 memset(mes, 0, sizeof(struct roar_message));
177
178 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN_V0) != _ROAR_MESS_BUF_LEN_V0 ) {
179  roar_err_set(ROAR_ERROR_PROTO);
180  return -1;
181 }
182
183 ROAR_DBG("roar_vrecv_message2(*): Got a header");
184
185 mes->version = buf[0];
186 headerlen    = _ROAR_MESS_BUF_LEN_V0;
187
188 switch (mes->version) {
189  case 0:
190    // we already have all data, so create the struct
191    mes->cmd     = (unsigned char)buf[1];
192    mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
193    mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
194    mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
195   break;
196  case 2:
197    mes->cmd     = (unsigned char)buf[1];
198    mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
199    mes->flags   = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
200    if ( mes->flags & ROAR_MF_LSPOS ) {
201     headerlen = 20;
202    } else {
203     headerlen = 16;
204    }
205   break;
206  default:
207    roar_err_set(ROAR_ERROR_NSVERSION);
208    return -1;
209   break;
210 }
211
212 // check specal case where headerlen < _ROAR_MESS_BUF_LEN_V0, this means that we need to put
213 // some data we read as header into the data part of the message.
214 if ( headerlen > _ROAR_MESS_BUF_LEN_V0 ) {
215  needlen = headerlen - _ROAR_MESS_BUF_LEN_V0;
216  if ( roar_vio_read(vio, buf+_ROAR_MESS_BUF_LEN_V0, needlen) != (ssize_t)needlen ) {
217   roar_err_set(ROAR_ERROR_PROTO);
218   return -1;
219  }
220 }
221
222 switch (mes->version) {
223  case 2:
224    if ( mes->flags & ROAR_MF_LSPOS ) {
225     mes->pos64   = ROAR_NET2HOST32(*(uint64_t*)(buf+8));
226     bufptr       = buf+16;
227    } else {
228     mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+8));
229     bufptr       = buf+12;
230    }
231    mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(bufptr+0));
232    mes->seq     = ROAR_NET2HOST16(*(uint16_t*)(bufptr+1));
233   break;
234 }
235
236 if ( (int16_t)mes->stream == -1 )
237  mes->stream = -1;
238
239 ROAR_DBG("roar_vrecv_message2(*): command=%i(%s)", mes->cmd,
240           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
241
242// Below we only have data handling, handling of header is finished:
243
244 if ( mes->datalen == 0 ) {
245  ROAR_DBG("roar_vrecv_message2(*): no data in this pkg");
246  if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
247   roar_err_init(errorframe);
248  }
249  roar_err_clear();
250  ROAR_DBG("roar_vrecv_message2(*) = 0");
251  return 0;
252 }
253
254 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
255  if ( roar_vio_read(vio, mes->data, mes->datalen) == (ssize_t)mes->datalen ) {
256   ROAR_DBG("roar_vrecv_message2(*): Got data!");
257   roar_err_clear();
258   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
259    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
260     roar_err_init(errorframe);
261    }
262   }
263   ROAR_DBG("roar_vrecv_message2(*) = 0");
264   return 0;
265  }
266
267  return -1;
268 } else {
269  if ( data == NULL ) {
270   roar_err_set(ROAR_ERROR_MSGSIZE);
271   return -1;
272  }
273
274  if ( (*data = roar_mm_malloc(mes->datalen)) == NULL ) {
275   roar_err_set(ROAR_ERROR_NOMEM);
276   return -1;
277  }
278
279  if ( mes->datalen == 0 ) {
280   roar_err_clear();
281   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
282    roar_err_init(errorframe);
283   }
284   return 0;
285  }
286
287  if ( roar_vio_read(vio, *data, mes->datalen) == (ssize_t)mes->datalen ) {
288   ROAR_DBG("roar_vrecv_message2(*): Got data!");
289
290   roar_err_clear();
291   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
292    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
293     roar_err_init(errorframe);
294    }
295   }
296   ROAR_DBG("roar_vrecv_message2(*) = 0");
297   return 0;
298  }
299
300  // error is still set (by roar_vio_read()).
301  return -1;
302 }
303
304 // what happened here?
305 return -1;
306}
307
308int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
309 return roar_req2(con, mes, data, NULL);
310}
311
312int roar_req2          (struct roar_connection * con, struct roar_message * mes, char ** data,
313                        struct roar_error_frame * errorframe) {
314 struct roar_vio_calls * vio;
315
316 if ( (vio = roar_get_connection_vio2(con)) == NULL )
317  return -1;
318
319 return roar_vreq2(vio, mes, data, errorframe);
320}
321
322int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
323 return roar_vreq2(vio, mes, data, NULL);
324}
325
326int roar_vreq2         (struct roar_vio_calls  * vio, struct roar_message * mes, char ** data,
327                        struct roar_error_frame * errorframe) {
328 if ( roar_vsend_message(vio, mes, data != NULL ? *data : NULL) != 0 )
329  return -1;
330
331 if ( data != NULL )
332  roar_mm_free(*data);
333
334 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
335
336 return roar_vrecv_message2(vio, mes, data, errorframe);
337}
338
339//ll
Note: See TracBrowser for help on using the repository browser.