[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) { |
---|
| 47 | case ROAR_VIO_PROTO_P_HTTP: port = 80; break; |
---|
| 48 | case ROAR_VIO_PROTO_P_GOPHER: port = 70; break; |
---|
| 49 | default: |
---|
| 50 | return -1; |
---|
| 51 | } |
---|
| 52 | |
---|
[1353] | 53 | if ( dstr == NULL ) |
---|
| 54 | dstr = "//"; |
---|
| 55 | |
---|
[1349] | 56 | if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 ) |
---|
| 57 | return -1; |
---|
| 58 | |
---|
| 59 | if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 ) |
---|
| 60 | return -1; |
---|
| 61 | |
---|
| 62 | if ( !strncmp(dstr, "//", 2) ) |
---|
| 63 | dstr += 2; |
---|
| 64 | |
---|
| 65 | if ( (ed = strstr(dstr, "/")) != NULL ) |
---|
| 66 | *ed = 0; |
---|
| 67 | |
---|
[1616] | 68 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 69 | |
---|
[1349] | 70 | ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def); |
---|
| 71 | |
---|
[1616] | 72 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 73 | |
---|
[1349] | 74 | if ( ed != NULL ) |
---|
| 75 | *ed = '/'; |
---|
| 76 | |
---|
[1650] | 77 | ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr); |
---|
| 78 | |
---|
[1349] | 79 | return ret; |
---|
[1439] | 80 | #else |
---|
| 81 | return -1; |
---|
| 82 | #endif |
---|
[1349] | 83 | } |
---|
| 84 | |
---|
| 85 | int roar_vio_open_proto (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
| 86 | char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
[1439] | 87 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[1349] | 88 | char * host; |
---|
| 89 | char * tmp; |
---|
| 90 | |
---|
[1616] | 91 | ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef); |
---|
[1349] | 92 | |
---|
[1353] | 93 | if ( calls == NULL || dst == NULL || odef == NULL ) |
---|
[1349] | 94 | return -1; |
---|
| 95 | |
---|
[1616] | 96 | ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags); |
---|
[1349] | 97 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 98 | |
---|
| 99 | if ( roar_vio_open_pass(calls, dst) == -1 ) |
---|
| 100 | return -1; |
---|
| 101 | |
---|
[1353] | 102 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 103 | |
---|
| 104 | if ( dstr != NULL ) { |
---|
| 105 | dstr += 2; |
---|
| 106 | host = dstr; |
---|
[1349] | 107 | |
---|
[1353] | 108 | if ( (tmp = strstr(dstr, "/")) == NULL ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
| 111 | *tmp++ = 0; |
---|
| 112 | dstr = tmp; |
---|
[1349] | 113 | |
---|
[1353] | 114 | if ( (tmp = strstr(dstr, "#")) != NULL ) |
---|
| 115 | *tmp = 0; |
---|
| 116 | } else { |
---|
| 117 | ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type); |
---|
| 118 | if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) { |
---|
| 119 | dstr = odef->d.file; |
---|
| 120 | host = "localhost"; |
---|
[1349] | 121 | |
---|
[1353] | 122 | for (; *dstr == '/'; dstr++); |
---|
| 123 | |
---|
| 124 | } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
| 125 | dstr = ""; // index document |
---|
| 126 | host = odef->d.socket.host; |
---|
| 127 | } else { |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | } |
---|
[1349] | 131 | |
---|
| 132 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
[1616] | 133 | ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr); |
---|
[1349] | 134 | |
---|
| 135 | switch (proto) { |
---|
| 136 | case ROAR_VIO_PROTO_P_HTTP: |
---|
| 137 | return roar_vio_open_proto_http(calls, dst, host, dstr); |
---|
| 138 | break; |
---|
| 139 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 140 | return roar_vio_open_proto_gopher(calls, dst, host, dstr); |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | |
---|
[1650] | 144 | ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol"); |
---|
[1349] | 145 | return -1; |
---|
[1439] | 146 | #else |
---|
| 147 | return -1; |
---|
| 148 | #endif |
---|
[1349] | 149 | } |
---|
| 150 | |
---|
[1439] | 151 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[1349] | 152 | int roar_vio_open_proto_http (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
[1352] | 153 | char buf[1024]; |
---|
| 154 | char b0[80], b1[80]; |
---|
| 155 | int status; |
---|
| 156 | int len; |
---|
| 157 | |
---|
[1650] | 158 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 159 | |
---|
[1352] | 160 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
[1650] | 163 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 164 | |
---|
[1352] | 165 | roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file); |
---|
| 166 | roar_vio_printf(dst, "Host: %s\r\n", host); |
---|
| 167 | roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n"); |
---|
| 168 | roar_vio_printf(dst, "Connection: close\r\n"); |
---|
| 169 | roar_vio_printf(dst, "\r\n"); |
---|
| 170 | |
---|
[1650] | 171 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 172 | |
---|
[1352] | 173 | roar_vio_sync(dst); |
---|
| 174 | |
---|
[1650] | 175 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 176 | |
---|
| 177 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) { |
---|
| 178 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[1352] | 179 | return -1; |
---|
[1650] | 180 | } |
---|
[1352] | 181 | |
---|
| 182 | buf[len] = 0; |
---|
| 183 | |
---|
[1650] | 184 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 185 | |
---|
[1352] | 186 | if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) { |
---|
[1650] | 187 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[1352] | 188 | return -1; |
---|
| 189 | } |
---|
| 190 | |
---|
[1650] | 191 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 192 | |
---|
| 193 | if ( status != 200 ) { |
---|
| 194 | ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status); |
---|
[1352] | 195 | return -1; |
---|
[1650] | 196 | } |
---|
[1352] | 197 | |
---|
[1616] | 198 | ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status); |
---|
[1352] | 199 | // ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf); |
---|
| 200 | |
---|
| 201 | if ( !strcmp((buf+len)-4, "\r\n\r\n") ) |
---|
| 202 | return 0; |
---|
| 203 | |
---|
| 204 | while (*buf != '\r' && *buf != '\n') { |
---|
| 205 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | return 0; |
---|
[1349] | 210 | } |
---|
| 211 | |
---|
| 212 | int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
| 213 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 214 | return -1; |
---|
| 215 | |
---|
| 216 | ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 217 | |
---|
[1352] | 218 | if ( file[1] == '/' ) |
---|
| 219 | file += 2; |
---|
[1349] | 220 | |
---|
[1352] | 221 | roar_vio_printf(dst, "/%s\r\n", file); |
---|
[1349] | 222 | |
---|
| 223 | roar_vio_sync(dst); // for encryption/compression layers |
---|
| 224 | |
---|
| 225 | return 0; |
---|
| 226 | } |
---|
[1439] | 227 | #endif |
---|
[1349] | 228 | |
---|
[1347] | 229 | //ll |
---|