1 | //vio_proxy.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_vio_proxy_init_def(struct roar_vio_defaults * def, |
---|
39 | char * dstr, enum roar_vio_proxy_type type, |
---|
40 | struct roar_vio_defaults * odef) { |
---|
41 | int hint = -1; |
---|
42 | |
---|
43 | (void)type; // TODO: generate hints from type. |
---|
44 | |
---|
45 | ROAR_DBG("roar_vio_proxy_init_def(def=%p, dstr='%s', type=%i, odef=%p) = ?", def, dstr, (int)type, odef); |
---|
46 | |
---|
47 | if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 ) |
---|
48 | return -1; |
---|
49 | |
---|
50 | if ( roar_vio_socket_init_dstr_def(def, dstr, hint, odef == NULL ? SOCK_STREAM : odef->d.socket.type, odef) == -1 ) { |
---|
51 | ROAR_DBG("roar_vio_proxy_init_def(def=%p, dstr='%s', type=%i, odef=%p) = -1", def, dstr, (int)type, odef); |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | ROAR_DBG("roar_vio_proxy_init_def(def=%p, dstr='%s', type=%i, odef=%p) = 0", def, dstr, (int)type, odef); |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | static int roar_vio_proxy_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
60 | if ( cmd == ROAR_VIO_CTL_GET_NAME ) { |
---|
61 | *(char **)data = "proxy"; |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | return roar_vio_pass_ctl(vio, cmd, data); |
---|
66 | } |
---|
67 | |
---|
68 | static int init_socks4(struct roar_vio_calls * dst, enum roar_vio_proxy_type type, struct roar_vio_defaults * odef) { |
---|
69 | char buf[9] = { |
---|
70 | 0x04, // [0] Version |
---|
71 | 0x01, // [1] mode=connect (listen is 0x02) |
---|
72 | 0, 0, 0, 0, // [2..5] host IP |
---|
73 | 0, // [6] Port MSB |
---|
74 | 0, // [7] Port LSB |
---|
75 | 0x00 // [8] EOS (\0) of username |
---|
76 | }; |
---|
77 | enum roar_vio_proxy_type needed_type = ROAR_VIO_PROXY_INVALID; |
---|
78 | |
---|
79 | ROAR_DBG("init_socks4(dst=%p, type=%i, odef=%p{.d.socket={.domain=%i, .type=%i, .host='%s'}}) = ?", dst, (int)type, odef, odef->d.socket.domain, odef->d.socket.type, odef->d.socket.host); |
---|
80 | |
---|
81 | switch (odef->d.socket.domain) { |
---|
82 | #ifdef ROAR_HAVE_IPV4 |
---|
83 | case AF_INET: needed_type = ROAR_VIO_PROXY_SOCKS4; break; |
---|
84 | #endif |
---|
85 | #ifdef ROAR_HAVE_LIBDNET |
---|
86 | case AF_DECnet: |
---|
87 | if ( odef->d.socket.sa.dn.sdn_objnum == 0 ) { |
---|
88 | needed_type = ROAR_VIO_PROXY_SOCKS4d; |
---|
89 | } else { |
---|
90 | needed_type = ROAR_VIO_PROXY_SOCKS4; |
---|
91 | } |
---|
92 | break; |
---|
93 | #endif |
---|
94 | #ifdef ROAR_HAVE_IPV6 |
---|
95 | case AF_INET6: |
---|
96 | #endif |
---|
97 | #ifdef ROAR_HAVE_IPX |
---|
98 | case AF_IPX: |
---|
99 | #endif |
---|
100 | #ifdef ROAR_HAVE_UNIX |
---|
101 | case AF_UNIX: |
---|
102 | #endif |
---|
103 | default: |
---|
104 | roar_err_set(ROAR_ERROR_AFNOTSUP); |
---|
105 | return -1; |
---|
106 | break; |
---|
107 | } |
---|
108 | |
---|
109 | if ( needed_type == ROAR_VIO_PROXY_SOCKS4 && type != needed_type ) |
---|
110 | needed_type = type; |
---|
111 | |
---|
112 | if ( needed_type != type ) { |
---|
113 | roar_err_set(ROAR_ERROR_INVAL); |
---|
114 | return -1; |
---|
115 | } |
---|
116 | |
---|
117 | switch (odef->d.socket.domain) { |
---|
118 | #ifdef ROAR_HAVE_IPV4 |
---|
119 | case AF_INET: |
---|
120 | if ( roar_vio_socket_init_inet4host_def(odef) == -1 ) |
---|
121 | return -1; |
---|
122 | buf[2] = ((char*)&(odef->d.socket.sa.in.sin_port))[0]; |
---|
123 | buf[3] = ((char*)&(odef->d.socket.sa.in.sin_port))[1]; |
---|
124 | buf[4] = ((char*)&(odef->d.socket.sa.in.sin_addr.s_addr))[0]; |
---|
125 | buf[5] = ((char*)&(odef->d.socket.sa.in.sin_addr.s_addr))[1]; |
---|
126 | buf[6] = ((char*)&(odef->d.socket.sa.in.sin_addr.s_addr))[2]; |
---|
127 | buf[7] = ((char*)&(odef->d.socket.sa.in.sin_addr.s_addr))[3]; |
---|
128 | ROAR_DBG("init_socks4(dst=%p, type=%i, odef=%p{.d.socket={.domain=%i, .type=%i, .host='%s'}}): buf[]={%i, %i, %i, %i, %i, %i, %i, %i, %i}", |
---|
129 | dst, (int)type, odef, odef->d.socket.domain, odef->d.socket.type, odef->d.socket.host, |
---|
130 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8]); |
---|
131 | break; |
---|
132 | #endif |
---|
133 | #ifdef ROAR_HAVE_LIBDNET |
---|
134 | case AF_DECnet: |
---|
135 | if ( type == ROAR_VIO_PROXY_SOCKS4 ) { |
---|
136 | buf[2] = 0; |
---|
137 | buf[3] = odef->d.socket.sa.dn.sdn_objnum; |
---|
138 | buf[4] = 0; |
---|
139 | buf[5] = 2; |
---|
140 | buf[6] = odef->d.socket.sa.dn.sdn_nodeaddr[0]; |
---|
141 | buf[7] = odef->d.socket.sa.dn.sdn_nodeaddr[1]; |
---|
142 | } else { |
---|
143 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
144 | return -1; |
---|
145 | } |
---|
146 | break; |
---|
147 | #endif |
---|
148 | } |
---|
149 | |
---|
150 | if ( roar_vio_write(dst, buf, sizeof(buf)) != (ssize_t)sizeof(buf) ) |
---|
151 | return -1; |
---|
152 | |
---|
153 | if ( roar_vio_read(dst, buf, 8) != (ssize_t)8 ) |
---|
154 | return -1; |
---|
155 | |
---|
156 | if ( buf[1] != 0x5a ) { |
---|
157 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | int roar_vio_open_proxy (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
165 | enum roar_vio_proxy_type type, struct roar_vio_defaults * odef) { |
---|
166 | int init_only = -1; |
---|
167 | int (*init)(struct roar_vio_calls * dst, enum roar_vio_proxy_type type, struct roar_vio_defaults * odef) = NULL; |
---|
168 | |
---|
169 | ROAR_DBG("roar_vio_open_proxy(calls=%p, dst=%p, type=%i, odef=%p{.type=%i}) = ?", calls, dst, (int)type, odef, (int)odef->type); |
---|
170 | |
---|
171 | if ( odef->type != ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
172 | roar_err_set(ROAR_ERROR_NOTSOCK); |
---|
173 | return -1; |
---|
174 | } |
---|
175 | |
---|
176 | switch (type) { |
---|
177 | case ROAR_VIO_PROXY_NONE: init_only = 1; init = NULL; break; |
---|
178 | case ROAR_VIO_PROXY_SOCKS: |
---|
179 | case ROAR_VIO_PROXY_SOCKS4: |
---|
180 | case ROAR_VIO_PROXY_SOCKS4a: |
---|
181 | case ROAR_VIO_PROXY_SOCKS4d: |
---|
182 | init_only = 1; |
---|
183 | init = init_socks4; |
---|
184 | break; |
---|
185 | #ifndef DEBUG |
---|
186 | default: |
---|
187 | break; |
---|
188 | #endif |
---|
189 | } |
---|
190 | |
---|
191 | if ( init_only == 1 ) { |
---|
192 | if ( init != NULL ) |
---|
193 | if ( init(dst, type, odef) == -1 ) |
---|
194 | return -1; |
---|
195 | if ( roar_vio_open_pass(calls, dst) == -1 ) |
---|
196 | return -1; |
---|
197 | |
---|
198 | calls->ctl = roar_vio_proxy_ctl; |
---|
199 | |
---|
200 | return 0; |
---|
201 | } else { |
---|
202 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
203 | return -1; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | // DSTR interface: |
---|
208 | static enum roar_vio_proxy_type _dstrtype2proxytype(struct roar_vio_dstr_chain * chainelement) { |
---|
209 | switch (chainelement->type) { |
---|
210 | case ROAR_VIO_DSTR_OBJT_SOCKS: return ROAR_VIO_PROXY_SOCKS; break; |
---|
211 | case ROAR_VIO_DSTR_OBJT_SOCKS4: return ROAR_VIO_PROXY_SOCKS4; break; |
---|
212 | case ROAR_VIO_DSTR_OBJT_SOCKS4A: return ROAR_VIO_PROXY_SOCKS4a; break; |
---|
213 | case ROAR_VIO_DSTR_OBJT_SOCKS4D: return ROAR_VIO_PROXY_SOCKS4d; break; |
---|
214 | case ROAR_VIO_DSTR_OBJT_SOCKS5: return ROAR_VIO_PROXY_SOCKS5; break; |
---|
215 | default: |
---|
216 | return ROAR_VIO_PROXY_INVALID; |
---|
217 | break; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | int roar_vio_proxy_setdef(struct roar_vio_dstr_chain * cur, struct roar_vio_dstr_chain * next) { |
---|
222 | next->def = &(next->store_def); |
---|
223 | return roar_vio_proxy_init_def(next->def, cur->dst, _dstrtype2proxytype(cur), cur->def); |
---|
224 | } |
---|
225 | |
---|
226 | int roar_vio_proxy_openvio(struct roar_vio_calls * calls, struct roar_vio_calls * dst, struct roar_vio_dstr_chain * cur, struct roar_vio_dstr_chain * next) { |
---|
227 | return roar_vio_open_proxy(calls, dst, _dstrtype2proxytype(cur), cur->def); |
---|
228 | } |
---|
229 | |
---|
230 | //ll |
---|