source: roaraudio/libroar/serverinfo.c @ 4433:df25c6da63e6

Last change on this file since 4433:df25c6da63e6 was 4433:df25c6da63e6, checked in by phi, 14 years ago

added a lot debug lions, fixed some small bugs in size calc

File size: 7.0 KB
Line 
1//serverinfo.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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
38struct ie {
39 int type;
40 size_t len;
41 char * buf;
42};
43
44struct roar_server_info * roar_server_info(struct roar_connection * con) {
45 struct roar_message mes;
46 uint16_t * d16;
47
48 memset(&mes, 0, sizeof(mes));
49
50 mes.cmd = ROAR_CMD_SERVER_INFO;
51 mes.datalen = 2*2;
52 d16 = (uint16_t*)mes.data;
53
54 d16[0] = ROAR_HOST2NET16(0); // version
55 d16[1] = ROAR_HOST2NET16(ROAR_IT_SERVER);
56
57 if ( roar_req(con, &mes, NULL) != 0 )
58  return NULL;
59
60 if ( mes.cmd != ROAR_CMD_OK )
61  return NULL;
62
63 return roar_server_info_from_mes(&mes);
64}
65
66int roar_server_info_free(struct roar_server_info * info) {
67 if ( info == NULL )
68  return -1;
69
70 roar_mm_free(info);
71
72 return 0;
73}
74
75#define _add(t, m) do { if ( info->m != NULL && (sl = strlen(info->m)) != 0 ) { iebuf[idx].type = (t); iebuf[idx].len = sl; iebuf[idx].buf = (info->m); idx++; needlen += 4 + sl; } } while (0)
76
77int roar_server_info_to_mes(struct roar_message * mes, struct roar_server_info * info) {
78 size_t needlen = 4;
79 size_t sl;
80 int idx = 0;
81 struct ie iebuf[sizeof(struct roar_server_info)/sizeof(char*)];
82 uint16_t * d16, * dptr;
83 char * textpart;
84 int i;
85
86 if ( mes == NULL || info == NULL )
87  return -1;
88
89 _add(ROAR_ITST_VERSION, version);
90 _add(ROAR_ITST_LOCATION, location);
91 _add(ROAR_ITST_DESCRIPTION, description);
92 _add(ROAR_ITST_CONTACT, contact);
93 _add(ROAR_ITST_SERIAL, serial);
94 _add(ROAR_ITST_ADDRESS, address);
95 _add(ROAR_ITST_UIURL, uiurl);
96 _add(ROAR_ITST_UN_SYSNAME, un.sysname);
97 _add(ROAR_ITST_UN_RELEASE, un.release);
98 _add(ROAR_ITST_UN_NODENAME, un.nodename);
99 _add(ROAR_ITST_UN_MACHINE, un.machine);
100
101 if ( needlen > LIBROAR_BUFFER_MSGDATA ) {
102  return -1;
103 }
104
105 memset(mes, 0, sizeof(struct roar_message));
106
107 mes->datalen = needlen;
108
109 d16 = (uint16_t*)mes->data;
110
111 mes->data[0] = 0; // version
112 mes->data[1] = 0; // reserved
113
114 d16[1] = ROAR_HOST2NET16(idx);
115
116 dptr = &(d16[2]);
117
118 for (i = 0; i < idx; i++) {
119  dptr[0] = ROAR_HOST2NET16(iebuf[i].type & 0xFF);
120  dptr[1] = ROAR_HOST2NET16(iebuf[i].len);
121  dptr += 2;
122 }
123
124 textpart = mes->data + (4 + 4*idx);
125
126 for (i = 0; i < idx; i++) {
127  memcpy(textpart, iebuf[i].buf, iebuf[i].len);
128  textpart += iebuf[i].len;
129 }
130
131 return 0;
132}
133
134struct roar_server_info * roar_server_info_from_mes(struct roar_message * mes) {
135 struct ie iebuf[sizeof(struct roar_server_info)/sizeof(char*)];
136 struct ie * ieptr;
137 struct roar_server_info * ret = NULL;
138 uint16_t * d16, * dptr;
139 int idx;
140 int i;
141 size_t needlen = 4;
142 char * textpart;
143 char * textbuf;
144 char ** tptr;
145
146 ROAR_DBG("roar_server_info(mes=%p{.datalen=%llu) = ?", mes, (long long unsigned int)mes->datalen);
147
148 if ( mes == NULL )
149  return NULL;
150
151 memset(iebuf, 0, sizeof(iebuf));
152
153 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
154
155 // some basic texts like version:
156 if ( mes->datalen < needlen )
157  return NULL;
158
159 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
160
161 if ( mes->data[0] != 0 ) /* version */
162  return NULL;
163
164 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
165
166 if ( mes->data[1] != 0 ) /* reserved */
167  return NULL;
168
169 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
170
171 d16 = (uint16_t*)mes->data;
172
173 idx = ROAR_NET2HOST16(d16[1]);
174
175 ROAR_DBG("roar_server_info(mes=%p): idx=%i", mes, idx);
176
177 // return error if our index buffer is too short.
178 if ( idx > (sizeof(iebuf)/sizeof(*iebuf)) )
179  return NULL;
180
181 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
182
183 ROAR_DBG("roar_server_info(mes=%p): needlen=%llu", mes, (long long unsigned int)needlen);
184
185 needlen += 4*idx;
186
187 // recheck if we have a complet index.
188 if ( mes->datalen < needlen )
189  return NULL;
190
191 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
192
193 d16 = (uint16_t*)mes->data;
194 dptr = &(d16[2]);
195
196 textpart = mes->data + (4 + 4*idx);
197
198 ROAR_DBG("roar_server_info(mes=%p): needlen=%llu", mes, (long long unsigned int)needlen);
199
200 for (i = 0; i < idx; i++) {
201  iebuf[i].type = ROAR_NET2HOST16(dptr[0]) & 0xFF;
202  iebuf[i].len  = ROAR_NET2HOST16(dptr[1]);
203  iebuf[i].buf  = textpart;
204  needlen  += iebuf[i].len;
205  textpart += iebuf[i].len;
206  dptr += 2;
207  ROAR_DBG("roar_server_info(mes=%p): iebuf[i]={.len=%llu,...}", mes, (long long unsigned int)iebuf[i].len);
208 }
209
210 ROAR_DBG("roar_server_info(mes=%p): needlen=%llu", mes, (long long unsigned int)needlen);
211
212 // recheck if we have all the data...
213 if ( mes->datalen < needlen )
214  return NULL;
215
216 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
217
218 // alloc the needed space. this can be reduced in future to the actual needed value.
219 ret = roar_mm_malloc(2*sizeof(struct roar_server_info) + mes->datalen);
220
221 if ( ret == NULL )
222  return NULL;
223
224 ROAR_DBG("roar_server_info(mes=%p) = ?", mes);
225
226 // for the size see the alloc call above.
227 memset(ret, 0, 2*sizeof(struct roar_server_info) + mes->datalen);
228
229 textbuf = (char*)ret + sizeof(struct roar_server_info);
230
231 for (i = 0; i < idx; i++) {
232  ieptr = &(iebuf[i]);
233
234   // ignore empty fields
235  if ( ieptr->len == 0 )
236   continue;
237  if ( ieptr->len == 1 && ieptr->buf[0] == 0 )
238   continue;
239
240#define _ck(TYPE,member) case TYPE: tptr = &(ret->member); break;
241  switch (ieptr->type) {
242   _ck(ROAR_ITST_VERSION, version);
243   _ck(ROAR_ITST_LOCATION, location);
244   _ck(ROAR_ITST_DESCRIPTION, description);
245   _ck(ROAR_ITST_CONTACT, contact);
246   _ck(ROAR_ITST_SERIAL, serial);
247   _ck(ROAR_ITST_ADDRESS, address);
248   _ck(ROAR_ITST_UIURL, uiurl);
249   _ck(ROAR_ITST_UN_SYSNAME, un.sysname);
250   _ck(ROAR_ITST_UN_RELEASE, un.release);
251   _ck(ROAR_ITST_UN_NODENAME, un.nodename);
252   _ck(ROAR_ITST_UN_MACHINE, un.machine);
253   default:
254     continue;
255    break;
256  }
257#undef _ck
258
259  *tptr = textbuf;
260   memcpy(textbuf, ieptr->buf, ieptr->len);
261   textbuf += ieptr->len;
262   *textbuf = 0; // set \0
263   textbuf++;
264 }
265
266 ROAR_DBG("roar_server_info(mes=%p) = %p", mes, ret);
267
268 return ret;
269}
270
271
272//ll
Note: See TracBrowser for help on using the repository browser.