source: roaraudio/libroar/vio_proto.c @ 3065:fffa0ef8d698

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

wrote most of the new proto inst parts

File size: 8.2 KB
Line 
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
37int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
38#ifndef ROAR_WITHOUT_VIO_PROTO
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
53 if ( dstr == NULL )
54  dstr = "//";
55
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
68 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
69
70 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
71
72 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
73
74 if ( ed != NULL )
75  *ed = '/';
76
77 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
78
79 return ret;
80#else
81 return -1;
82#endif
83}
84
85int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
86                              char * dstr, int proto, struct roar_vio_defaults * odef) {
87#ifndef ROAR_WITHOUT_VIO_PROTO
88 char * host;
89 char * tmp;
90
91 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
92
93 if ( calls == NULL || dst == NULL || odef == NULL )
94  return -1;
95
96 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
97 ROAR_DBG("roar_vio_open_proto(*) = ?");
98
99 if ( roar_vio_open_pass(calls, dst) == -1 )
100  return -1;
101
102 ROAR_DBG("roar_vio_open_proto(*) = ?");
103
104 if ( dstr != NULL ) {
105  dstr += 2;
106  host  = dstr;
107
108  if ( (tmp = strstr(dstr, "/")) == NULL )
109   return -1;
110
111  *tmp++ = 0;
112  dstr   = tmp;
113
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";
121
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 }
131
132 ROAR_DBG("roar_vio_open_proto(*) = ?");
133 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
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
144 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
145 return -1;
146#else
147 return -1;
148#endif
149}
150
151#ifndef ROAR_WITHOUT_VIO_PROTO
152ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
153 struct roar_vio_proto * self = vio->inst;
154 ssize_t ret;
155 ssize_t have = 0;
156 size_t  len;
157
158 if ( self->reader.buffer != NULL ) {
159  len = count;
160  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
161   // This is very bad.
162   return -1;
163  }
164
165  if ( len ) {
166   have   = len;
167   buf   += len;
168   count -= len;
169  }
170 }
171
172 if ( count == 0 )
173  return have;
174
175 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
176  return ret;
177
178 return have + ret;
179}
180
181ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
182 struct roar_vio_proto * self = vio->inst;
183
184 return roar_vio_write(self->next, buf, count);
185}
186
187// TODO: this is currently not implemented as this is hard to implement with buffers:
188off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
189
190int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
191 struct roar_vio_proto * self = vio->inst;
192
193 /* we can simply use the next layer's nonblock as all we do in addtion *
194  * to call there functions are our buffers which do not block normaly  */
195
196 return roar_vio_nonblock(self->next, state);
197}
198
199int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
200 struct roar_vio_proto * self = vio->inst;
201
202 return roar_vio_sync(self->next);
203}
204
205int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
206 struct roar_vio_proto * self = vio->inst;
207
208 if (vio == NULL || cmd == -1)
209  return -1;
210
211 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
212
213 switch (cmd) {
214  case ROAR_VIO_CTL_GET_NEXT:
215    *(struct roar_vio_calls **)data = self->next;
216    return 0;
217   break;
218  case ROAR_VIO_CTL_SET_NEXT:
219    self->next = *(struct roar_vio_calls **)data;
220    return 0;
221   break;
222 }
223
224 return roar_vio_ctl(self->next, cmd, data);
225}
226
227int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
228 struct roar_vio_proto * self = vio->inst;
229
230 if ( roar_vio_close(self->next) == -1 )
231  return -1;
232
233 roar_mm_free(self);
234
235 return 0;
236}
237
238
239int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
240 char buf[1024];
241 char b0[80], b1[80];
242 int  status;
243 int  len;
244
245 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
246
247 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
248  return -1;
249
250 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
251
252 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
253 roar_vio_printf(dst, "Host: %s\r\n", host);
254 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
255 roar_vio_printf(dst, "Connection: close\r\n");
256 roar_vio_printf(dst, "\r\n");
257
258 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
259
260 roar_vio_sync(dst);
261
262 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
263
264 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
265  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
266  return -1;
267 }
268
269 buf[len] = 0;
270
271 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
272
273 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
274  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
275  return -1;
276 }
277
278 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
279
280 if ( status != 200 ) {
281  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
282  return -1;
283 }
284
285 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
286// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
287
288 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
289  return 0;
290
291 while (*buf != '\r' && *buf != '\n') {
292  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
293   return -1;
294 }
295
296 return 0;
297}
298
299int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
300 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
301  return -1;
302
303 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
304
305 if ( file[1] == '/' )
306  file += 2;
307
308 roar_vio_printf(dst, "/%s\r\n", file);
309
310 roar_vio_sync(dst); // for encryption/compression layers
311
312 return 0;
313}
314#endif
315
316//ll
Note: See TracBrowser for help on using the repository browser.