Debian Codegraph @

librplay/rptp.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: rptp.c,v 1.4 1999/03/10 07:57:56 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "version.h"
30 #include "rplay.h"
31 #include <sys/errno.h>
32 #ifdef ultrix
33 #include <fcntl.h>
34 #else
35 #include <sys/fcntl.h>
36 #endif
37 #include <netdb.h>
38 #include <unistd.h>
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 #ifdef __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47 #include <errno.h>
48
49 int rptp_errno = RPTP_ERROR_NONE;
50
51 char *rptp_errlist[] =
52 {
53 "no error", /* RPTP_ERROR_NONE */
54 "out of memory", /* RPTP_ERROR_MEMORY */
55 "host not found", /* RPTP_ERROR_HOST */
56 "connection failed", /* RPTP_ERROR_CONNECT */
57 "cannot create socket", /* RPTP_ERROR_SOCKET */
58 "cannot open socket", /* RPTP_ERROR_OPEN */
59 "cannot read from socket", /* RPTP_ERROR_READ */
60 "cannot write to socket", /* RPTP_ERROR_WRITE */
61 "cannot ping rplay server", /* RPTP_ERROR_PING */
62 "connection timeout", /* RPTP_ERROR_TIMEOUT */
63 "RPTP protocol error", /* RPTP_ERROR_PROTOCOL */
64 };
65
66 #ifdef __STDC__
67 unsigned long inet_addr(char *rp);
68 #else
69 unsigned long inet_addr();
70 #endif
71
72 #ifndef HAVE_STRDUP
73 #ifdef __STDC__
74 static char *
75 strdup(char *str)
76 #else
77 static char *
78 strdup(str)
79 char *str;
80 #endif
81 {
82 char *p;
83
84 p = (char *) malloc(strlen(str) + 1);
85 if (p == NULL)
86 {
87 return NULL;
88 }
89 else
90 {
91 strcpy(p, str);
92 return p;
93 }
94 }
95 #endif /* HAVE_STRDUP */
96
97 /*
98 * open an RPTP connection to host using port
99 * the connection response will be stored in the response buffer
100 */
101 #ifdef __STDC__
102 int
103 rptp_open(char *host, int port, char *response, int response_size)
104 #else
105 int
106 rptp_open(host, port, response, response_size)
107 char *host;
108 int port;
109 char *response;
110 int response_size;
111 #endif
112 {
113 u_long addr;
114 struct sockaddr_in s;
115 struct hostent *hp;
116 int rptp_fd;
117 int i, n;
118
119 rptp_errno = RPTP_ERROR_NONE;
120
121 memset((char *) &s, 0, sizeof(s));
122
123 addr = inet_addr(host);
124 if (addr == 0xffffffff)
125 {
126 hp = gethostbyname(host);
127 if (hp == NULL)
128 {
129 rptp_errno = RPTP_ERROR_HOST;
130 return -1;
131 }
132 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
133 }
134 else
135 {
136 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
137 }
138
139 s.sin_port = htons(port);
140 s.sin_family = AF_INET;
141
142 for (i = 0; i < RPTP_CONNECT_ATTEMPTS; i++)
143 {
144 if (rplay_ping(host) < 0)
145 {
146 rptp_errno = RPTP_ERROR_PING;
147 return -1;
148 }
149
150 rptp_fd = socket(AF_INET, SOCK_STREAM, 0);
151 if (rptp_fd < 0)
152 {
153 rptp_errno = RPTP_ERROR_SOCKET;
154 return -1;
155 }
156
157 n = connect(rptp_fd, (struct sockaddr *) &s, sizeof(s));
158 if (n == 0)
159 {
160 /*
161 * successful connection
162 */
163 rptp_getline(rptp_fd, response, response_size);
164 if (response[0] == RPTP_ERROR)
165 {
166 rptp_errno = RPTP_ERROR_OPEN;
167 return -1;
168 }
169 return rptp_fd;
170 }
171
172 switch (errno)
173 {
174 case ECONNREFUSED:
175 case EINTR:
176 close(rptp_fd);
177 if (i + 1 != RPTP_CONNECT_ATTEMPTS)
178 {
179 sleep(RPTP_PING_DELAY);
180 }
181 break;
182
183 default:
184 rptp_errno = RPTP_ERROR_CONNECT;
185 return -1;
186 }
187 }
188
189 rptp_errno = RPTP_ERROR_CONNECT;
190
191 return -1;
192 }
193
194 /*
195 * read nbytes from the RPTP connection
196 *
197 * return the number of bytes actually read
198 *
199 * The routine was adapted from readn() in "Unix Network Programming"
200 * by W. Richard Stevens, page 279.
201 */
202 #ifdef __STDC__
203 int
204 rptp_read(int rptp_fd, char *ptr, int nbytes)
205 #else
206 int
207 rptp_read(rptp_fd, ptr, nbytes)
208 int rptp_fd;
209 char *ptr;
210 int nbytes;
211 #endif
212 {
213 int nleft, nread;
214
215 rptp_errno = RPTP_ERROR_NONE;
216
217 nleft = nbytes;
218
219 while (nleft > 0)
220 {
221 nread = read(rptp_fd, ptr, nleft);
222 if (nread < 0)
223 {
224 if (errno == EINTR)
225 {
226 continue;
227 }
228 rptp_errno = RPTP_ERROR_READ;
229 return -1;
230 }
231 if (nread == 0)
232 {
233 break;
234 }
235 nleft -= nread;
236 ptr += nread;
237 }
238
239 return nbytes - nleft;
240 }
241
242 /*
243 * write nbytes to the RPTP connection
244 *
245 * return the number of bytes actually written
246 *
247 * The routine was adapted from writen() in "Unix Network Programming"
248 * by W. Richard Stevens, page 279-80.
249 */
250 #ifdef __STDC__
251 int
252 rptp_write(int rptp_fd, char *ptr, int nbytes)
253 #else
254 int
255 rptp_write(rptp_fd, ptr, nbytes)
256 int rptp_fd;
257 char *ptr;
258 int nbytes;
259 #endif
260 {
261 int nleft, nwritten;
262
263 rptp_errno = RPTP_ERROR_NONE;
264
265 nleft = nbytes;
266
267 while (nleft > 0)
268 {
269 nwritten = write(rptp_fd, ptr, nleft);
270 if (nwritten < 0)
271 {
272 if (errno == EINTR)
273 {
274 continue;
275 }
276 rptp_errno = RPTP_ERROR_WRITE;
277 return -1;
278 }
279 else if (nwritten == 0)
280 {
281 rptp_errno = RPTP_ERROR_WRITE;
282 return -1;
283 }
284 nleft -= nwritten;
285 ptr += nwritten;
286 }
287
288 return nbytes - nleft;
289 }
290
291 /*
292 * write a line to the RPTP connection
293 *
294 * "\r\n" is appened to the string
295 */
296 #ifdef __STDC__
297 int
298 rptp_putline(int rptp_fd, char *fmt,...)
299 #else
300 int
301 rptp_putline(va_alist)
302 va_dcl
303 #endif
304 {
305 va_list args;
306 char buf[RPTP_MAX_LINE];
307
308 #ifdef __STDC__
309 va_start(args, fmt);
310 #else
311 int rptp_fd;
312 char *fmt;
313 va_start(args);
314 rptp_fd = va_arg(args, int);
315 fmt = va_arg(args, char *);
316 #endif
317
318 rptp_errno = RPTP_ERROR_NONE;
319
320 vsprintf(buf, fmt, args);
321 va_end(args);
322 strcat(buf, "\r\n");
323
324 return rptp_write(rptp_fd, buf, strlen(buf)) != strlen(buf) ? -1 : 0;
325 }
326
327 /*
328 * read a line from the RPTP connection
329 *
330 * "\r\n" is removed from the line
331 */
332 #ifdef __STDC__
333 int
334 rptp_getline(int rptp_fd, char *buf, int nbytes)
335 #else
336 int
337 rptp_getline(rptp_fd, buf, nbytes)
338 int rptp_fd;
339 char *buf;
340 int nbytes;
341 #endif
342 {
343 int i, n, nleft, x;
344 char *ptr;
345 char tmp_buf[RPTP_MAX_LINE];
346
347 rptp_errno = RPTP_ERROR_NONE;
348
349 nleft = nbytes;
350 ptr = buf;
351
352 while (nleft > 0)
353 {
354 /*
355 * peek at the message so only the necessary data
356 * is actually read
357 */
358 n = recv(rptp_fd, ptr, nleft, MSG_PEEK);
359 if (n < 0)
360 {
361 if (errno == EINTR)
362 {
363 continue;
364 }
365 rptp_errno = RPTP_ERROR_READ;
366 return -1;
367 }
368 else if (n == 0)
369 {
370 rptp_errno = RPTP_ERROR_READ;
371 return -1;
372 }
373 nleft -= n;
374 for (i = 0; i < n; i++)
375 {
376 if (ptr[i] == '\r')
377 {
378 ptr[i] = '\0';
379 }
380 else if (ptr[i] == '\n')
381 {
382 ptr[i] = '\0';
383 break;
384 }
385 }
386 again:
387 x = read(rptp_fd, tmp_buf, i == n ? n : i + 1);
388 if (x < 0)
389 {
390 if (errno == EINTR)
391 {
392 goto again;
393 }
394 rptp_errno = RPTP_ERROR_READ;
395 return -1;
396 }
397 else if (x == 0)
398 {
399 rptp_errno = RPTP_ERROR_READ;
400 return -1;
401 }
402 else if (i < n)
403 {
404 return 0;
405 }
406 else
407 {
408 ptr += n;
409 }
410 }
411
412 rptp_errno = RPTP_ERROR_READ;
413
414 return -1;
415 }
416
417 /*
418 * send the RPTP command to the RPTP connection storing the response in the
419 * response buffer
420 */
421 #ifdef __STDC__
422 int
423 rptp_command(int rptp_fd, char *command, char *response, int response_size)
424 #else
425 int
426 rptp_command(rptp_fd, command, response, response_size)
427 int rptp_fd;
428 char *command;
429 char *response;
430 int response_size;
431 #endif
432 {
433 rptp_errno = RPTP_ERROR_NONE;
434
435 if (rptp_putline(rptp_fd, command) < 0)
436 {
437 return -1;
438 }
439 if (rptp_getline(rptp_fd, response, response_size) < 0)
440 {
441 return -1;
442 }
443 switch (response[0])
444 {
445 case RPTP_TIMEOUT:
446 rptp_errno = RPTP_ERROR_TIMEOUT;
447 return -1;
448
449 case RPTP_ERROR:
450 return 1;
451
452 case RPTP_OK:
453 case RPTP_NOTIFY:
454 return 0;
455
456 default:
457 rptp_errno = RPTP_ERROR_PROTOCOL;
458 return -1;
459 }
460 }
461
462 /*
463 * close the RPTP connection
464 */
465 #ifdef __STDC__
466 int
467 rptp_close(int rptp_fd)
468 #else
469 int
470 rptp_close(rptp_fd)
471 int rptp_fd;
472 #endif
473 {
474 rptp_errno = RPTP_ERROR_NONE;
475
476 close(rptp_fd);
477
478 return 0;
479 }
480
481 /*
482 * report RPTP errors
483 */
484 #ifdef __STDC__
485 void
486 rptp_perror(char *message)
487 #else
488 void
489 rptp_perror(message)
490 char *message;
491 #endif
492 {
493 fprintf(stderr, "%s: %s\n", message, rptp_errlist[rptp_errno]);
494 }
495
496 /*
497 * List definitions used in rptp_parse.
498 */
499 typedef struct _list
500 {
501 struct _list *next;
502 char *name;
503 char *value;
504 }
505 LIST;
506
507 static LIST *list = NULL, **list_next = &list;
508
509 #define list_alloc() \
510 *list_next = (LIST *) malloc (sizeof(LIST)); \
511 if (!*list_next) \
512 { \
513 return NULL; \
514 }
515
516 #define list_add(xname, xvalue) \
517 list_alloc(); \
518 (*list_next)->name = xname; \
519 (*list_next)->value = xvalue; \
520 list_next = &(*list_next)->next; \
521 *list_next = NULL;
522
523 #define list_free() \
524 { \
525 LIST *l; \
526 for (; list; l = list, list = list->next, free ((char *)l)); \
527 list = NULL; \
528 list_next = &list; \
529 }
530
531 #ifdef __STDC__
532 char *
533 rptp_parse(char *response, char *name)
534 #else
535 char *
536 rptp_parse(response, name)
537 char *response;
538 char *name;
539 #endif
540 {
541 static char *buf;
542 static LIST *list_pos, *cache_pos;
543
544 /* No more name-value pairs. */
545 if (!response && !name && !list_pos)
546 {
547 return NULL;
548 }
549 /* Load the new `name=value' list. */
550 else if (response)
551 {
552 char *p;
553 char *response_name = "", *response_value = "";
554
555 list_free();
556 if (buf)
557 {
558 free((char *) buf);
559 }
560 buf = strdup(response);
561
562 p = buf;
563
564 /* Skip any initial RPTP characters. */
565 switch (*p)
566 {
567 case RPTP_ERROR:
568 case RPTP_OK:
569 case RPTP_NOTIFY:
570 p++;
571 break;
572 }
573
574 while (p && *p)
575 {
576 /* Skip white-space. */
577 if (isspace(*p))
578 {
579 for (p++; isspace(*p); p++) ;
580 continue;
581 }
582
583 /* `name' */
584 response_name = p;
585 p = strpbrk(p, "= \t\r\n");
586
587 /* `value' */
588 if (p && *p == '=')
589 {
590 int quoted = 0;
591
592 *p++ = '\0'; /* Remove the `=' */
593
594 if (*p == '"') /* Start of a quoted value */
595 {
596 p++;
597 quoted++;
598 }
599
600 response_value = p;
601 if (quoted)
602 {
603 p = strchr(p, '"');
604 }
605 else
606 {
607 p = strpbrk(p, " \t\r\n");
608 }
609 if (p)
610 {
611 *p++ = '\0';
612 }
613 }
614 else if (p)
615 {
616 *p++ = '\0';
617 }
618
619 list_add(response_name, response_value);
620 response_name = "";
621 response_value = "";
622 }
623
624 list_pos = list;
625 cache_pos = NULL;
626 }
627
628 /* Search for `name'. */
629 if (name)
630 {
631 LIST *l;
632 char *p;
633
634 /* Skip any leading dashes. */
635 while (*name == '-')
636 {
637 name++;
638 }
639
640 if (cache_pos)
641 {
642 for (p = cache_pos->name; *p && *p == '-'; p++) ; /* skip leading dashes */
643 if (strcmp(name, p) == 0)
644 {
645 return cache_pos->value;
646 }
647 }
648
649 for (l = list; l; l = l->next)
650 {
651 for (p = l->name; *p && *p == '-'; p++) ; /* skip leading dashes */
652 if (strcmp(p, name) == 0)
653 {
654 return l->value;
655 }
656 }
657 return NULL; /* Not found */
658 }
659 /* Cycle through the names. */
660 else if (list_pos)
661 {
662 cache_pos = list_pos;
663 list_pos = list_pos->next;
664 return cache_pos->name;
665 }
666
667 return NULL;
668 }