mp3_afh.c: set bitrate, frequency and channels of struct audio_file_info
[paraslash.git] / client_common.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file client_common.c common functions of para_client and para_audiod */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "client.cmdline.h"
25 #include "crypt.h"
26 #include "rc4.h"
27 #include "net.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "client.cmdline.h"
31 #include "client.h"
32 #include "error.h"
33
34 /*
35 * rc4 encrypt data before sending
36 *
37 * \param len the number of bytes to encrypt
38 * \param indata pointer to the input data of length \a len to be encrypted
39 * \param outdata pointer that holds the encrypted data after return
40 * \param private_data pointer to the private client data containing
41 * the rc4 key
42 * */
43 static void rc4_send(unsigned long len, const unsigned char *indata,
44 unsigned char *outdata, void *private_data)
45 {
46 struct private_client_data *pcd = private_data;
47 RC4(&pcd->rc4_send_key, len, indata, outdata);
48 }
49
50 /*
51 * rc4 decrypt received data
52 *
53 * \param len the number of bytes to decrypt
54 * \param indata pointer to the input data of length \a len
55 * \param outdata pointer that holds the decrypted data after return
56 * \param private_data pointer to the private client data containing
57 * the rc4 key
58 * */
59 static void rc4_recv(unsigned long len, const unsigned char *indata,
60 unsigned char *outdata, void *private_data)
61 {
62 struct private_client_data *pcd = private_data;
63 RC4(&pcd->rc4_recv_key, len, indata, outdata);
64 }
65
66 /**
67 * close the connection to para_server and free all resources
68 *
69 * \param pcd pointer to the client data
70 *
71 * \sa client_open.
72 * */
73 void client_close(struct private_client_data *pcd)
74 {
75 if (!pcd)
76 return;
77 if (pcd->fd >= 0) {
78 disable_crypt(pcd->fd);
79 close(pcd->fd);
80 }
81 free(pcd->user);
82 free(pcd->config_file);
83 free(pcd->key_file);
84 client_cmdline_parser_free(&pcd->conf);
85 free(pcd);
86 }
87
88 static int client_connect(struct private_client_data *pcd)
89 {
90 int ret;
91 struct hostent *he;
92 struct sockaddr_in their_addr;
93
94 pcd->fd = -1;
95 ret = get_host_info(pcd->conf.hostname_arg, &he);
96 if (ret < 0)
97 return ret;
98 /* get new socket */
99 ret = get_socket();
100 if (ret < 0)
101 return ret;
102 pcd->fd = ret;
103 /* init their_addr */
104 init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
105 ret = para_connect(pcd->fd, &their_addr);
106 if (ret < 0)
107 goto err_out;
108 pcd->status = CL_CONNECTED;
109 ret = mark_fd_nonblock(pcd->fd);
110 if (ret < 0)
111 goto err_out;
112 pcd->task.pre_select = client_pre_select;
113 pcd->task.post_select = client_post_select;
114 pcd->task.private_data = pcd;
115 sprintf(pcd->task.status, "client");
116 register_task(&pcd->task);
117 return 1;
118 err_out:
119 close(pcd->fd);
120 pcd->fd = -1;
121 return ret;
122 }
123
124 /**
125 * open connection to para_server
126 *
127 * \param argc usual argument count
128 * \param argv usual argument vector
129 * \param pcd_ptr points to dynamically allocated and initialized private client data
130 * upon successful return
131 *
132 * Check the command line options given by \a argc and argv, set default values
133 * for user name and rsa key file, read further option from the config file.
134 * Finally, establish a connection to para_server.
135 *
136 * \return Positive on success, negative on errors.
137 */
138 int client_open(int argc, char *argv[], struct private_client_data **pcd_ptr)
139 {
140 char *home = para_homedir();
141 struct stat statbuf;
142 int ret;
143 struct private_client_data *pcd =
144 para_calloc(sizeof(struct private_client_data));
145
146 *pcd_ptr = pcd;
147 pcd->fd = -1;
148 ret = client_cmdline_parser(argc, argv, &pcd->conf);
149 HANDLE_VERSION_FLAG("client", pcd->conf);
150 ret = -E_CLIENT_SYNTAX;
151 if (!pcd->conf.inputs_num)
152 goto out;
153 pcd->user = pcd->conf.user_given?
154 para_strdup(pcd->conf.user_arg) : para_logname();
155
156 pcd->key_file = pcd->conf.key_file_given?
157 para_strdup(pcd->conf.key_file_arg) :
158 make_message("%s/.paraslash/key.%s", home, pcd->user);
159
160 pcd->config_file = pcd->conf.config_file_given?
161 para_strdup(pcd->conf.config_file_arg) :
162 make_message("%s/.paraslash/client.conf", home);
163 ret = stat(pcd->config_file, &statbuf);
164 if (ret && pcd->conf.config_file_given) {
165 ret = -E_NO_CONFIG;
166 goto out;
167 }
168 if (!ret)
169 client_cmdline_parser_configfile(pcd->config_file,
170 &pcd->conf, 0, 0, 0);
171 ret = 1;
172 PARA_INFO_LOG("loglevel: %d\n", pcd->conf.loglevel_arg);
173 PARA_INFO_LOG("config_file: %s\n", pcd->config_file);
174 PARA_INFO_LOG("key_file: %s\n", pcd->key_file);
175 PARA_NOTICE_LOG("connecting %s:%d\n", pcd->conf.hostname_arg,
176 pcd->conf.server_port_arg);
177 ret = client_connect(pcd);
178 out:
179 free(home);
180 if (ret < 0) {
181 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
182 client_close(pcd);
183 *pcd_ptr = NULL;
184 }
185 return ret;
186 }
187
188 /**
189 * the preselect hook for server commands
190 *
191 * \param s pointer to the scheduler
192 * \param t pointer to the task struct for this command
193 *
194 * The task pointer must contain a pointer to the initialized client data
195 * structure as it is returned by client_open().
196 *
197 * This function checks the state of the connection and adds the file descriptor
198 * of the connection to the read or write fd set of \a s accordingly.
199 *
200 * \sa register_task() client_open(), struct sched, struct task
201 */
202 void client_pre_select(struct sched *s, struct task *t)
203 {
204 struct private_client_data *pcd = t->private_data;
205
206 t->ret = 1;
207 pcd->check_r = 0;
208 pcd->check_w = 0;
209 if (pcd->fd < 0)
210 return;
211 switch (pcd->status) {
212 case CL_CONNECTED:
213 case CL_SENT_AUTH:
214 case CL_SENT_CH_RESPONSE:
215 case CL_SENT_COMMAND:
216 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
217 pcd->check_r = 1;
218 return;
219
220 case CL_RECEIVED_WELCOME:
221 case CL_RECEIVED_CHALLENGE:
222 case CL_RECEIVED_PROCEED:
223 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
224 pcd->check_w = 1;
225 return;
226
227 case CL_RECEIVING:
228 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
229 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
230 pcd->check_r = 1;
231 }
232 return;
233 case CL_SENDING:
234 if (*pcd->in_loaded) {
235 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
236 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
237 pcd->check_w = 1;
238 } else {
239 if (*pcd->in_eof) {
240 t->ret = -E_INPUT_EOF;
241 s->timeout.tv_sec = 0;
242 s->timeout.tv_usec = 1;
243 }
244 }
245 return;
246 }
247 }
248
249 static ssize_t client_recv_buffer(struct private_client_data *pcd)
250 {
251 ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
252 CLIENT_BUFSIZE - pcd->loaded);
253 if (!ret)
254 return -E_SERVER_EOF;
255 if (ret > 0)
256 pcd->loaded += ret;
257 return ret;
258
259 }
260
261 /**
262 * the post select hook for client commands
263 *
264 * \param s pointer to the scheduler
265 * \param t pointer to the task struct for this command
266 *
267 * Depending on the current state of the connection and the status of the read
268 * and write fd sets of \a s, this function performs the neccessary steps to
269 * authenticate the connection, to send the commmand given by \a
270 * t->private_data and to receive para_server's output, if any.
271 *
272 * \sa struct sched, struct task
273 */
274 void client_post_select(struct sched *s, struct task *t)
275 {
276 struct private_client_data *pcd = t->private_data;
277
278 // PARA_INFO_LOG("status %d\n", pcd->status);
279 t->ret = 1;
280 if (pcd->fd < 0)
281 return;
282 if (!pcd->check_r && !pcd->check_w)
283 return;
284 if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
285 return;
286 if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
287 return;
288 switch (pcd->status) {
289 case CL_CONNECTED: /* receive welcome message */
290 t->ret = client_recv_buffer(pcd);
291 if (t->ret > 0)
292 pcd->status = CL_RECEIVED_WELCOME;
293 return;
294 case CL_RECEIVED_WELCOME: /* send auth command */
295 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
296 "" : "rc4 ", pcd->user);
297 PARA_INFO_LOG("--> %s\n", pcd->buf);
298 t->ret = send_buffer(pcd->fd, pcd->buf);
299 if (t->ret >= 0)
300 pcd->status = CL_SENT_AUTH;
301 return;
302 case CL_SENT_AUTH: /* receive challenge number */
303 pcd->loaded = 0;
304 t->ret = client_recv_buffer(pcd);
305 if (t->ret < 0)
306 return;
307 if (t->ret != 64) {
308 t->ret = -E_INVALID_CHALLENGE;
309 PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
310 return;
311 }
312 PARA_INFO_LOG("%s", "<-- [challenge]\n");
313 /* decrypt challenge number */
314 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
315 (unsigned char *) pcd->buf, 64);
316 if (t->ret > 0)
317 pcd->status = CL_RECEIVED_CHALLENGE;
318 return;
319 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
320 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
321 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
322 pcd->challenge_nr);
323 if (t->ret > 0)
324 pcd->status = CL_SENT_CH_RESPONSE;
325 return;
326 case CL_SENT_CH_RESPONSE: /* read server response */
327 {
328 size_t bytes_received;
329 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
330 pcd->loaded = 0;
331 t->ret = client_recv_buffer(pcd);
332 if (t->ret < 0)
333 return;
334 bytes_received = t->ret;
335 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
336 "info ++++\n", pcd->buf);
337 /* check if server has sent "Proceed" message */
338 t->ret = -E_CLIENT_AUTH;
339 if (!strstr(pcd->buf, PROCEED_MSG))
340 return;
341 t->ret = 1;
342 pcd->status = CL_RECEIVED_PROCEED;
343 if (bytes_received < PROCEED_MSG_LEN + 32)
344 return;
345 PARA_INFO_LOG("%s", "decrypting session key\n");
346 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
347 (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
348 bytes_received - PROCEED_MSG_LEN - 1);
349 if (t->ret < 0)
350 return;
351 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
352 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
353 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
354 }
355 case CL_RECEIVED_PROCEED: /* concat args and send command */
356 {
357 int i;
358 char *command = NULL;
359 for (i = 0; i < pcd->conf.inputs_num; i++) {
360 char *tmp = command;
361 command = make_message("%s\n%s", command?
362 command : "", pcd->conf.inputs[i]);
363 free(tmp);
364 }
365 command = para_strcat(command, EOC_MSG "\n");
366 PARA_DEBUG_LOG("--> %s\n", command);
367 t->ret = send_buffer(pcd->fd, command);
368 free(command);
369 if (t->ret > 0)
370 pcd->status = CL_SENT_COMMAND;
371 return;
372 }
373 case CL_SENT_COMMAND:
374 pcd->loaded = 0;
375 t->ret = client_recv_buffer(pcd);
376 if (t->ret < 0)
377 return;
378 t->ret = -E_HANDSHAKE_COMPLETE;
379 if (strstr(pcd->buf, AWAITING_DATA_MSG))
380 pcd->status = CL_SENDING;
381 else
382 pcd->status = CL_RECEIVING;
383 return;
384 case CL_SENDING: /* FIXME: might block */
385 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
386 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
387 if (t->ret < 0)
388 return;
389 *pcd->in_loaded = 0; /* FIXME: short writes */
390 return;
391 case CL_RECEIVING:
392 t->ret = client_recv_buffer(pcd);
393 return;
394 }
395 }