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