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