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 | int port = 0; |
---|
39 | int ret; |
---|
40 | char * ed; |
---|
41 | |
---|
42 | if ( def == NULL ) |
---|
43 | return -1; |
---|
44 | |
---|
45 | switch (proto) { |
---|
46 | case ROAR_VIO_PROTO_P_HTTP: port = 80; break; |
---|
47 | case ROAR_VIO_PROTO_P_GOPHER: port = 70; break; |
---|
48 | default: |
---|
49 | return -1; |
---|
50 | } |
---|
51 | |
---|
52 | if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 ) |
---|
53 | return -1; |
---|
54 | |
---|
55 | if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 ) |
---|
56 | return -1; |
---|
57 | |
---|
58 | if ( !strncmp(dstr, "//", 2) ) |
---|
59 | dstr += 2; |
---|
60 | |
---|
61 | if ( (ed = strstr(dstr, "/")) != NULL ) |
---|
62 | *ed = 0; |
---|
63 | |
---|
64 | ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def); |
---|
65 | |
---|
66 | if ( ed != NULL ) |
---|
67 | *ed = '/'; |
---|
68 | |
---|
69 | return ret; |
---|
70 | } |
---|
71 | |
---|
72 | int roar_vio_open_proto (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
73 | char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
74 | char * host; |
---|
75 | char * tmp; |
---|
76 | |
---|
77 | ROAR_WARN("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef); |
---|
78 | |
---|
79 | if ( calls == NULL || dst == NULL || dstr == NULL ) |
---|
80 | return -1; |
---|
81 | |
---|
82 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
83 | |
---|
84 | if ( roar_vio_open_pass(calls, dst) == -1 ) |
---|
85 | return -1; |
---|
86 | |
---|
87 | dstr += 2; |
---|
88 | host = dstr; |
---|
89 | |
---|
90 | if ( (tmp = strstr(dstr, "/")) == NULL ) |
---|
91 | return -1; |
---|
92 | |
---|
93 | *tmp++ = 0; |
---|
94 | dstr = tmp; |
---|
95 | |
---|
96 | if ( (tmp = strstr(dstr, "#")) != NULL ) |
---|
97 | *tmp = 0; |
---|
98 | |
---|
99 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
100 | |
---|
101 | switch (proto) { |
---|
102 | case ROAR_VIO_PROTO_P_HTTP: |
---|
103 | return roar_vio_open_proto_http(calls, dst, host, dstr); |
---|
104 | break; |
---|
105 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
106 | return roar_vio_open_proto_gopher(calls, dst, host, dstr); |
---|
107 | break; |
---|
108 | } |
---|
109 | |
---|
110 | return -1; |
---|
111 | } |
---|
112 | |
---|
113 | int roar_vio_open_proto_http (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
114 | return -1; |
---|
115 | } |
---|
116 | |
---|
117 | int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { |
---|
118 | int len; |
---|
119 | |
---|
120 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
121 | return -1; |
---|
122 | |
---|
123 | ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
124 | |
---|
125 | len = strlen(file); |
---|
126 | |
---|
127 | if ( roar_vio_write(dst, file, len) != len ) |
---|
128 | return -1; |
---|
129 | |
---|
130 | if ( roar_vio_write(dst, "\n", 1) != 1 ) |
---|
131 | return -1; |
---|
132 | |
---|
133 | roar_vio_sync(dst); // for encryption/compression layers |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | //ll |
---|