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 | |
---|
37 | int 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 | |
---|
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) { |
---|
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 |
---|
152 | int roar_vio_open_proto_http (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
153 | char buf[1024]; |
---|
154 | char b0[80], b1[80]; |
---|
155 | int status; |
---|
156 | int len; |
---|
157 | |
---|
158 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
159 | |
---|
160 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
161 | return -1; |
---|
162 | |
---|
163 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
164 | |
---|
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 | |
---|
171 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
172 | |
---|
173 | roar_vio_sync(dst); |
---|
174 | |
---|
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"); |
---|
179 | return -1; |
---|
180 | } |
---|
181 | |
---|
182 | buf[len] = 0; |
---|
183 | |
---|
184 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
185 | |
---|
186 | if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) { |
---|
187 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
188 | return -1; |
---|
189 | } |
---|
190 | |
---|
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); |
---|
195 | return -1; |
---|
196 | } |
---|
197 | |
---|
198 | ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status); |
---|
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; |
---|
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 | |
---|
218 | if ( file[1] == '/' ) |
---|
219 | file += 2; |
---|
220 | |
---|
221 | roar_vio_printf(dst, "/%s\r\n", file); |
---|
222 | |
---|
223 | roar_vio_sync(dst); // for encryption/compression layers |
---|
224 | |
---|
225 | return 0; |
---|
226 | } |
---|
227 | #endif |
---|
228 | |
---|
229 | //ll |
---|