[1347] | 1 | //vio_proto.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 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 | |
---|
[1349] | 37 | int roar_vio_proto_init_def (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
[1439] | 38 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[1349] | 39 | int port = 0; |
---|
| 40 | int ret; |
---|
| 41 | char * ed; |
---|
| 42 | |
---|
| 43 | if ( def == NULL ) |
---|
| 44 | return -1; |
---|
| 45 | |
---|
| 46 | switch (proto) { |
---|
[3078] | 47 | case ROAR_VIO_PROTO_P_HTTP: port = 80; break; |
---|
| 48 | case ROAR_VIO_PROTO_P_GOPHER: port = 70; break; |
---|
| 49 | case ROAR_VIO_PROTO_P_ICY: port = 8000; break; |
---|
[1349] | 50 | default: |
---|
| 51 | return -1; |
---|
| 52 | } |
---|
| 53 | |
---|
[1353] | 54 | if ( dstr == NULL ) |
---|
| 55 | dstr = "//"; |
---|
| 56 | |
---|
[1349] | 57 | if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 ) |
---|
| 58 | return -1; |
---|
| 59 | |
---|
| 60 | if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 ) |
---|
| 61 | return -1; |
---|
| 62 | |
---|
| 63 | if ( !strncmp(dstr, "//", 2) ) |
---|
| 64 | dstr += 2; |
---|
| 65 | |
---|
| 66 | if ( (ed = strstr(dstr, "/")) != NULL ) |
---|
| 67 | *ed = 0; |
---|
| 68 | |
---|
[1616] | 69 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 70 | |
---|
[1349] | 71 | ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def); |
---|
| 72 | |
---|
[1616] | 73 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 74 | |
---|
[1349] | 75 | if ( ed != NULL ) |
---|
| 76 | *ed = '/'; |
---|
| 77 | |
---|
[1650] | 78 | ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr); |
---|
| 79 | |
---|
[1349] | 80 | return ret; |
---|
[1439] | 81 | #else |
---|
| 82 | return -1; |
---|
| 83 | #endif |
---|
[1349] | 84 | } |
---|
| 85 | |
---|
| 86 | int roar_vio_open_proto (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
| 87 | char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
[1439] | 88 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[3066] | 89 | struct roar_vio_proto * self; |
---|
[1349] | 90 | char * host; |
---|
| 91 | char * tmp; |
---|
| 92 | |
---|
[1616] | 93 | ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef); |
---|
[1349] | 94 | |
---|
[1353] | 95 | if ( calls == NULL || dst == NULL || odef == NULL ) |
---|
[1349] | 96 | return -1; |
---|
| 97 | |
---|
[1616] | 98 | ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags); |
---|
[1349] | 99 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 100 | |
---|
[3066] | 101 | if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL ) |
---|
[1349] | 102 | return -1; |
---|
| 103 | |
---|
[3066] | 104 | memset(self, 0, sizeof(struct roar_vio_proto)); |
---|
| 105 | |
---|
| 106 | self->next = dst; |
---|
| 107 | |
---|
| 108 | calls->inst = self; |
---|
| 109 | |
---|
| 110 | calls->read = roar_vio_proto_read; |
---|
| 111 | calls->write = roar_vio_proto_write; |
---|
| 112 | // calls->lseek = roar_vio_proto_lseek; // TODO: this is currently not supported |
---|
| 113 | calls->nonblock = roar_vio_proto_nonblock; |
---|
| 114 | calls->sync = roar_vio_proto_sync; |
---|
| 115 | calls->ctl = roar_vio_proto_ctl; |
---|
| 116 | calls->close = roar_vio_proto_close; |
---|
| 117 | |
---|
[1353] | 118 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 119 | |
---|
| 120 | if ( dstr != NULL ) { |
---|
| 121 | dstr += 2; |
---|
| 122 | host = dstr; |
---|
[1349] | 123 | |
---|
[1353] | 124 | if ( (tmp = strstr(dstr, "/")) == NULL ) |
---|
| 125 | return -1; |
---|
| 126 | |
---|
| 127 | *tmp++ = 0; |
---|
| 128 | dstr = tmp; |
---|
[1349] | 129 | |
---|
[1353] | 130 | if ( (tmp = strstr(dstr, "#")) != NULL ) |
---|
| 131 | *tmp = 0; |
---|
| 132 | } else { |
---|
| 133 | ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type); |
---|
| 134 | if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) { |
---|
| 135 | dstr = odef->d.file; |
---|
| 136 | host = "localhost"; |
---|
[1349] | 137 | |
---|
[1353] | 138 | for (; *dstr == '/'; dstr++); |
---|
| 139 | |
---|
| 140 | } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
| 141 | dstr = ""; // index document |
---|
| 142 | host = odef->d.socket.host; |
---|
| 143 | } else { |
---|
| 144 | return -1; |
---|
| 145 | } |
---|
| 146 | } |
---|
[1349] | 147 | |
---|
| 148 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
[1616] | 149 | ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr); |
---|
[1349] | 150 | |
---|
| 151 | switch (proto) { |
---|
| 152 | case ROAR_VIO_PROTO_P_HTTP: |
---|
[3078] | 153 | case ROAR_VIO_PROTO_P_ICY: |
---|
[1349] | 154 | return roar_vio_open_proto_http(calls, dst, host, dstr); |
---|
| 155 | break; |
---|
| 156 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 157 | return roar_vio_open_proto_gopher(calls, dst, host, dstr); |
---|
| 158 | break; |
---|
| 159 | } |
---|
| 160 | |
---|
[1650] | 161 | ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol"); |
---|
[1349] | 162 | return -1; |
---|
[1439] | 163 | #else |
---|
| 164 | return -1; |
---|
| 165 | #endif |
---|
[1349] | 166 | } |
---|
| 167 | |
---|
[1439] | 168 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[3065] | 169 | ssize_t roar_vio_proto_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 170 | struct roar_vio_proto * self = vio->inst; |
---|
| 171 | ssize_t ret; |
---|
| 172 | ssize_t have = 0; |
---|
| 173 | size_t len; |
---|
| 174 | |
---|
| 175 | if ( self->reader.buffer != NULL ) { |
---|
| 176 | len = count; |
---|
| 177 | if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) { |
---|
| 178 | // This is very bad. |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if ( len ) { |
---|
| 183 | have = len; |
---|
| 184 | buf += len; |
---|
| 185 | count -= len; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ( count == 0 ) |
---|
| 190 | return have; |
---|
| 191 | |
---|
[3067] | 192 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 193 | |
---|
[3065] | 194 | if ( (ret = roar_vio_read(self->next, buf, count)) == -1 ) |
---|
| 195 | return ret; |
---|
| 196 | |
---|
| 197 | return have + ret; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | ssize_t roar_vio_proto_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 201 | struct roar_vio_proto * self = vio->inst; |
---|
| 202 | |
---|
| 203 | return roar_vio_write(self->next, buf, count); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | // TODO: this is currently not implemented as this is hard to implement with buffers: |
---|
| 207 | off_t roar_vio_proto_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
| 208 | |
---|
| 209 | int roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 210 | struct roar_vio_proto * self = vio->inst; |
---|
| 211 | |
---|
| 212 | /* we can simply use the next layer's nonblock as all we do in addtion * |
---|
| 213 | * to call there functions are our buffers which do not block normaly */ |
---|
| 214 | |
---|
| 215 | return roar_vio_nonblock(self->next, state); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | int roar_vio_proto_sync (struct roar_vio_calls * vio) { |
---|
| 219 | struct roar_vio_proto * self = vio->inst; |
---|
| 220 | |
---|
| 221 | return roar_vio_sync(self->next); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | int roar_vio_proto_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 225 | struct roar_vio_proto * self = vio->inst; |
---|
| 226 | |
---|
| 227 | if (vio == NULL || cmd == -1) |
---|
| 228 | return -1; |
---|
| 229 | |
---|
| 230 | ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 231 | |
---|
| 232 | switch (cmd) { |
---|
| 233 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 234 | *(struct roar_vio_calls **)data = self->next; |
---|
| 235 | return 0; |
---|
| 236 | break; |
---|
| 237 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 238 | self->next = *(struct roar_vio_calls **)data; |
---|
| 239 | return 0; |
---|
| 240 | break; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | int roar_vio_proto_close (struct roar_vio_calls * vio) { |
---|
| 247 | struct roar_vio_proto * self = vio->inst; |
---|
| 248 | |
---|
| 249 | if ( roar_vio_close(self->next) == -1 ) |
---|
| 250 | return -1; |
---|
| 251 | |
---|
| 252 | roar_mm_free(self); |
---|
| 253 | |
---|
| 254 | return 0; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | |
---|
[1349] | 258 | int roar_vio_open_proto_http (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
[3067] | 259 | struct roar_vio_proto * self; |
---|
| 260 | struct roar_buffer * bufbuf; |
---|
| 261 | char * buf; |
---|
| 262 | char * endofheader = NULL; |
---|
[1352] | 263 | char b0[80], b1[80]; |
---|
| 264 | int status; |
---|
| 265 | int len; |
---|
| 266 | |
---|
[1650] | 267 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 268 | |
---|
[1352] | 269 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 270 | return -1; |
---|
| 271 | |
---|
[3067] | 272 | self = calls->inst; |
---|
[3066] | 273 | calls->write = NULL; // Disable write as we do not support this |
---|
| 274 | |
---|
[3067] | 275 | if ( roar_buffer_new(&bufbuf, 1024) == -1 ) |
---|
| 276 | return -1; |
---|
| 277 | |
---|
| 278 | if ( roar_buffer_get_data(bufbuf, &buf) == -1 ) { |
---|
| 279 | roar_buffer_free(bufbuf); |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
| 282 | |
---|
[1650] | 283 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 284 | |
---|
[1352] | 285 | roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file); |
---|
| 286 | roar_vio_printf(dst, "Host: %s\r\n", host); |
---|
| 287 | roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n"); |
---|
| 288 | roar_vio_printf(dst, "Connection: close\r\n"); |
---|
| 289 | roar_vio_printf(dst, "\r\n"); |
---|
| 290 | |
---|
[1650] | 291 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 292 | |
---|
[1352] | 293 | roar_vio_sync(dst); |
---|
| 294 | |
---|
[1650] | 295 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 296 | |
---|
| 297 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) { |
---|
| 298 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 299 | roar_buffer_free(bufbuf); |
---|
[1352] | 300 | return -1; |
---|
[1650] | 301 | } |
---|
[1352] | 302 | |
---|
| 303 | buf[len] = 0; |
---|
| 304 | |
---|
[1650] | 305 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 306 | |
---|
[1352] | 307 | if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) { |
---|
[1650] | 308 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 309 | roar_buffer_free(bufbuf); |
---|
[1352] | 310 | return -1; |
---|
| 311 | } |
---|
| 312 | |
---|
[1650] | 313 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 314 | |
---|
| 315 | if ( status != 200 ) { |
---|
| 316 | ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status); |
---|
[3067] | 317 | roar_buffer_free(bufbuf); |
---|
[1352] | 318 | return -1; |
---|
[1650] | 319 | } |
---|
[1352] | 320 | |
---|
[1616] | 321 | ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status); |
---|
[1352] | 322 | // ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf); |
---|
| 323 | |
---|
[3067] | 324 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
| 325 | |
---|
| 326 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 327 | |
---|
| 328 | while ( endofheader == NULL ) { |
---|
| 329 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 330 | return -1; |
---|
| 331 | |
---|
| 332 | buf[len] = 0; |
---|
| 333 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
| 334 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 338 | ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf); |
---|
| 339 | |
---|
| 340 | if ( (endofheader - buf) == (len - 4) ) { |
---|
| 341 | roar_buffer_free(bufbuf); |
---|
| 342 | bufbuf = NULL; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if ( bufbuf != NULL ) { |
---|
| 346 | roar_buffer_set_offset(bufbuf, endofheader - buf + 4); |
---|
[3068] | 347 | roar_buffer_set_len(bufbuf, len - (endofheader - buf + 4) - 1); |
---|
[3067] | 348 | } |
---|
| 349 | self->reader.buffer = bufbuf; |
---|
| 350 | |
---|
| 351 | /* |
---|
[1352] | 352 | if ( !strcmp((buf+len)-4, "\r\n\r\n") ) |
---|
| 353 | return 0; |
---|
| 354 | |
---|
| 355 | while (*buf != '\r' && *buf != '\n') { |
---|
| 356 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 357 | return -1; |
---|
| 358 | } |
---|
[3067] | 359 | */ |
---|
[1352] | 360 | |
---|
| 361 | return 0; |
---|
[1349] | 362 | } |
---|
| 363 | |
---|
| 364 | int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
| 365 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 366 | return -1; |
---|
| 367 | |
---|
[3066] | 368 | calls->write = NULL; // Disable write as we do not support this |
---|
| 369 | |
---|
[1349] | 370 | ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 371 | |
---|
[1352] | 372 | if ( file[1] == '/' ) |
---|
| 373 | file += 2; |
---|
[1349] | 374 | |
---|
[1352] | 375 | roar_vio_printf(dst, "/%s\r\n", file); |
---|
[1349] | 376 | |
---|
| 377 | roar_vio_sync(dst); // for encryption/compression layers |
---|
| 378 | |
---|
| 379 | return 0; |
---|
| 380 | } |
---|
[1439] | 381 | #endif |
---|
[1349] | 382 | |
---|
[1347] | 383 | //ll |
---|