1 | //roarclientpass.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of roarclients 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 | * RoarAudio 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 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * ckport: ignore-symbol: roar_socket_open of target libroar0 -- Used to get a clientfh. |
---|
28 | */ |
---|
29 | |
---|
30 | #include <roaraudio.h> |
---|
31 | |
---|
32 | void usage (void) { |
---|
33 | printf("roarclientpass [OPTIONS]...\n"); |
---|
34 | |
---|
35 | printf("\nOptions:\n\n"); |
---|
36 | |
---|
37 | printf(" --server SERVER - Set server hostname\n" |
---|
38 | " --stdin - Client is on stdin\n" |
---|
39 | " --stdout - Client is on stdout\n" |
---|
40 | " --stdio - Same as --stdin --stdout\n" |
---|
41 | " --stderr - Client is on stderr\n" |
---|
42 | " --client-fh FH - Client is on FH\n" |
---|
43 | " --proto PROTO - Client uses protocol PROTO (default: RoarAudio)\n" |
---|
44 | " --byteorder BO - Client uses byteorder BO (default: network)\n" |
---|
45 | " --listen - This is a listen mode connection\n" |
---|
46 | " --mode MODE - Set mode of operation: none, listen, connect (default: none)\n" |
---|
47 | " --bind BIND - Set host/node/path for mode listen and connect\n" |
---|
48 | " --port PORT - Set port for mode listen and connect\n" |
---|
49 | // " --type TYPE - Set type for mode listen and connect (default: unknown)\n" |
---|
50 | " --help - Show this help\n" |
---|
51 | ); |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | #define _BV(x) (1<<(x)) |
---|
56 | #define F_STDIN _BV(ROAR_STDIN) |
---|
57 | #define F_STDOUT _BV(ROAR_STDOUT) |
---|
58 | #define F_STDERR _BV(ROAR_STDERR) |
---|
59 | |
---|
60 | int run (int client, int in, int out) { |
---|
61 | struct roar_vio_calls socks[3]; |
---|
62 | struct roar_vio_select vios[3]; |
---|
63 | int alive = 1; |
---|
64 | int ret; |
---|
65 | ssize_t len; |
---|
66 | char buf[1024]; |
---|
67 | |
---|
68 | roar_vio_open_fh_socket(&(socks[0]), client); |
---|
69 | roar_vio_open_fh_socket(&(socks[1]), in); |
---|
70 | roar_vio_open_fh_socket(&(socks[2]), out); |
---|
71 | |
---|
72 | ROAR_VIO_SELECT_SETVIO(&(vios[0]), &(socks[0]), ROAR_VIO_SELECT_READ|ROAR_VIO_SELECT_WRITE); |
---|
73 | ROAR_VIO_SELECT_SETVIO(&(vios[1]), &(socks[1]), ROAR_VIO_SELECT_READ); |
---|
74 | ROAR_VIO_SELECT_SETVIO(&(vios[2]), &(socks[2]), ROAR_VIO_SELECT_WRITE); |
---|
75 | |
---|
76 | while (alive) { |
---|
77 | ret = roar_vio_select(vios, 3, NULL, NULL); |
---|
78 | if ( ret < 0 ) |
---|
79 | break; |
---|
80 | |
---|
81 | if ( ret == 0 ) |
---|
82 | continue; |
---|
83 | |
---|
84 | if ( (vios[0].eventsa & ROAR_VIO_SELECT_READ) && (vios[2].eventsa & ROAR_VIO_SELECT_WRITE) ) { |
---|
85 | len = roar_vio_read(&(socks[0]), buf, sizeof(buf)); |
---|
86 | |
---|
87 | if ( len < 1 ) { |
---|
88 | alive = 0; |
---|
89 | } else { |
---|
90 | if ( roar_vio_write(&(socks[2]), buf, len) != len ) |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if ( (vios[0].eventsa & ROAR_VIO_SELECT_WRITE) && (vios[1].eventsa & ROAR_VIO_SELECT_READ) ) { |
---|
96 | len = roar_vio_read(&(socks[1]), buf, sizeof(buf)); |
---|
97 | |
---|
98 | if ( len < 1 ) { |
---|
99 | alive = 0; |
---|
100 | } else { |
---|
101 | if ( roar_vio_write(&(socks[0]), buf, len) != len ) |
---|
102 | break; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | roar_vio_close(&(socks[0])); |
---|
108 | roar_vio_close(&(socks[1])); |
---|
109 | roar_vio_close(&(socks[2])); |
---|
110 | |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | int main (int argc, char * argv[]) { |
---|
115 | struct roar_connection con; |
---|
116 | struct roar_client client; |
---|
117 | char * server = NULL; |
---|
118 | char * k; |
---|
119 | int i; |
---|
120 | int clientfh = -1; |
---|
121 | int cflags = 0; |
---|
122 | int flags = 0; |
---|
123 | int proto = ROAR_PROTO_ROARAUDIO; |
---|
124 | int byteorder = ROAR_BYTEORDER_NETWORK; |
---|
125 | int mode = ROAR_SOCKET_MODE_NONE; |
---|
126 | int type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
127 | char * host = NULL; |
---|
128 | int port = -1; |
---|
129 | enum {PASSFH, EXEC} command = PASSFH; |
---|
130 | |
---|
131 | for (i = 1; i < argc; i++) { |
---|
132 | k = argv[i]; |
---|
133 | |
---|
134 | if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) { |
---|
135 | server = argv[++i]; |
---|
136 | } else if ( !strcmp(k, "--stdin") ) { |
---|
137 | cflags |= F_STDIN; |
---|
138 | } else if ( !strcmp(k, "--stdout") ) { |
---|
139 | cflags |= F_STDOUT; |
---|
140 | } else if ( !strcmp(k, "--stderr") ) { |
---|
141 | cflags |= F_STDERR; |
---|
142 | } else if ( !strcmp(k, "--stdio") ) { |
---|
143 | cflags |= F_STDIN|F_STDOUT; |
---|
144 | } else if ( !strcmp(k, "--client-fh") ) { |
---|
145 | clientfh = atoi(argv[++i]); |
---|
146 | } else if ( !strcmp(k, "--proto") ) { |
---|
147 | proto = roar_str2proto(argv[++i]); |
---|
148 | } else if ( !strcmp(k, "--byteorder") ) { |
---|
149 | byteorder = roar_str2byteorder(argv[++i]); |
---|
150 | } else if ( !strcmp(k, "--listen") ) { |
---|
151 | flags |= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
152 | } else if ( !strcmp(k, "--command") ) { |
---|
153 | k = argv[++i]; |
---|
154 | if ( !strcasecmp(k, "passfh") ) { |
---|
155 | command = PASSFH; |
---|
156 | } else if ( !strcasecmp(k, "exec") ) { |
---|
157 | command = EXEC; |
---|
158 | } else { |
---|
159 | ROAR_ERR("unknown command: %s", k); |
---|
160 | return 1; |
---|
161 | } |
---|
162 | } else if ( !strcmp(k, "--mode") ) { |
---|
163 | k = argv[++i]; |
---|
164 | if ( !strcasecmp(k, "none") ) { |
---|
165 | mode = ROAR_SOCKET_MODE_NONE; |
---|
166 | } else if ( !strcasecmp(k, "listen") ) { |
---|
167 | mode = ROAR_SOCKET_MODE_LISTEN; |
---|
168 | flags |= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
169 | } else if ( !strcasecmp(k, "connect") ) { |
---|
170 | mode = ROAR_SOCKET_MODE_CONNECT; |
---|
171 | flags -= ROAR_CLIENTPASS_FLAG_LISTEN; |
---|
172 | } else { |
---|
173 | ROAR_ERR("unknown mode: %s", k); |
---|
174 | return 1; |
---|
175 | } |
---|
176 | } else if ( !strcmp(k, "--bind") ) { |
---|
177 | host = argv[++i]; |
---|
178 | } else if ( !strcmp(k, "--port") ) { |
---|
179 | port = atoi(argv[++i]); |
---|
180 | } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) { |
---|
181 | usage(); |
---|
182 | return 0; |
---|
183 | } else { |
---|
184 | ROAR_ERR("unknown argument: %s", k); |
---|
185 | usage(); |
---|
186 | return 1; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | if ( cflags & F_STDERR ) { |
---|
191 | #ifdef ROAR_HAVE_SYSLOG |
---|
192 | roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG); |
---|
193 | #else |
---|
194 | roar_debug_set_stderr_vio(roar_stderr); |
---|
195 | #endif |
---|
196 | } else { |
---|
197 | roar_debug_set_stderr_vio(roar_stderr); |
---|
198 | } |
---|
199 | |
---|
200 | if ( mode != ROAR_SOCKET_MODE_NONE ) { |
---|
201 | if ( clientfh != -1 ) { |
---|
202 | ROAR_ERR("Too may socket types given"); |
---|
203 | return 30; |
---|
204 | } |
---|
205 | |
---|
206 | clientfh = roar_socket_open(mode, type, host, port); |
---|
207 | |
---|
208 | if ( clientfh == -1 ) { |
---|
209 | ROAR_ERR("Unabled to open socket"); |
---|
210 | return 31; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | if ( clientfh == -1 ) { |
---|
215 | if ( cflags & F_STDIN ) { |
---|
216 | clientfh = ROAR_STDIN; |
---|
217 | } else if ( cflags & F_STDOUT ) { |
---|
218 | clientfh = ROAR_STDOUT; |
---|
219 | } else if ( cflags & F_STDERR ) { |
---|
220 | clientfh = ROAR_STDERR; |
---|
221 | } else { |
---|
222 | ROAR_ERR("No client socket given"); |
---|
223 | return 32; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | roar_client_new(&client); |
---|
228 | roar_client_set_proto(&client, proto, byteorder); |
---|
229 | |
---|
230 | if ( command != EXEC ) |
---|
231 | roar_client_set_fh(&client, clientfh); |
---|
232 | |
---|
233 | if ( roar_simple_connect(&con, server, "roarclientpass") == -1 ) { |
---|
234 | ROAR_ERR("Can not connect to server"); |
---|
235 | return 10; |
---|
236 | } |
---|
237 | |
---|
238 | switch (command) { |
---|
239 | case PASSFH: |
---|
240 | if ( roar_client_pass(&con, &client, flags) == -1 ) { |
---|
241 | ROAR_ERR("Can not pass client fh to server"); |
---|
242 | roar_disconnect(&con); |
---|
243 | return 20; |
---|
244 | } |
---|
245 | break; |
---|
246 | case EXEC: |
---|
247 | if ( roar_client_exec(&con, &client, flags) == -1 ) { |
---|
248 | ROAR_ERR("Can not exec client on server"); |
---|
249 | roar_disconnect(&con); |
---|
250 | return 20; |
---|
251 | } |
---|
252 | |
---|
253 | if ( run(client.fh, clientfh, cflags & F_STDOUT ? ROAR_STDOUT : clientfh) == -1 ) { |
---|
254 | ROAR_ERR("Can not run data copy runner"); |
---|
255 | return 20; |
---|
256 | } |
---|
257 | |
---|
258 | return 0; |
---|
259 | break; |
---|
260 | } |
---|
261 | |
---|
262 | roar_disconnect(&con); |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | //ll |
---|