Debian Codegraph @

librplay/rplay.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: rplay.c,v 1.6 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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include "version.h"
28 #include "rplay.h"
29 #include <sys/param.h>
30 #include <netdb.h>
31 #include <string.h>
32 #include <arpa/inet.h>
33
34 #ifdef __STDC__
35 #include <stdarg.h>
36 #else
37 #include <varargs.h>
38 #endif
39 #ifndef __FreeBSD__
40 #include <malloc.h>
41 #endif
42 #include <stdlib.h>
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #include <unistd.h>
47 #ifdef HAVE_MEMORY_H
48 #include <memory.h>
49 #endif
50 #include <time.h>
51
52 /* Make sure MAXHOSTNAMELEN is defined. */
53 #ifndef MAXHOSTNAMELEN
54 #define MAXHOSTNAMELEN 256
55 #endif
56
57 int rplay_errno;
58
59 char *rplay_errlist[] =
60 {
61 "no error", /* RPLAY_ERROR_NONE */
62 "out of memory", /* RPLAY_ERROR_MEMORY */
63 "host not found", /* RPLAY_ERROR_HOST */
64 "cannot connect to socket", /* RPLAY_ERROR_CONNECT */
65 "cannot create socket", /* RPLAY_ERROR_SOCKET */
66 "error writing to socket", /* RPLAY_ERROR_WRITE */
67 "error closing socket", /* RPLAY_ERROR_CLOSE */
68 "max packet size exceeded", /* RPLAY_ERROR_PACKET_SIZE */
69 "cannot enable broadcast", /* RPLAY_ERROR_BROADCAST */
70 "unknown rplay attribute", /* RPLAY_ERROR_ATTRIBUTE */
71 "unknown rplay command", /* RPLAY_ERROR_COMMAND */
72 "illegal rplay index value", /* RPLAY_ERROR_INDEX */
73 "unknown rplay modifier", /* RPLAY_ERROR_MODIFIER */
74 };
75 /*
76 #ifdef __STDC__
77 unsigned long inet_addr (char *rp);
78 #else
79 unsigned long inet_addr ();
80 #endif
81 */
82 /* A simple version a strdup. */
83 #ifndef HAVE_STRDUP
84 #ifdef __STDC__
85 static char *
86 strdup(char *str)
87 #else
88 static char *
89 strdup(str)
90 char *str;
91 #endif
92 {
93 char *p;
94
95 p = (char *) malloc(strlen(str) + 1);
96 if (p == NULL)
97 {
98 return NULL;
99 }
100 else
101 {
102 strcpy(p, str);
103 return p;
104 }
105 }
106 #endif /* HAVE_STRDUP */
107
108 /*
109 * create an rplay attributes object
110 */
111 #ifdef __STDC__
112 static RPLAY_ATTRS *
113 rplay_attrs_create(void)
114 #else
115 static RPLAY_ATTRS *
116 rplay_attrs_create()
117 #endif
118 {
119 RPLAY_ATTRS *attrs;
120
121 attrs = (RPLAY_ATTRS *) malloc(sizeof(RPLAY_ATTRS));
122 if (attrs == NULL)
123 {
124 return NULL;
125 }
126 attrs->next = NULL;
127 attrs->sound = "";
128 attrs->volume = RPLAY_DEFAULT_VOLUME;
129 attrs->count = RPLAY_DEFAULT_COUNT;
130 attrs->rptp_server = NULL;
131 attrs->rptp_server_port = RPTP_PORT;
132 attrs->rptp_search = TRUE;
133 attrs->sample_rate = RPLAY_DEFAULT_SAMPLE_RATE;
134 attrs->client_data = "";
135
136 return attrs;
137 }
138
139 /*
140 * free memory used by an rplay attributes object
141 */
142 #ifdef __STDC__
143 static void
144 rplay_attrs_destroy(RPLAY_ATTRS *attrs)
145 #else
146 static void
147 rplay_attrs_destroy(attrs)
148 RPLAY_ATTRS *attrs;
149 #endif
150 {
151 if (*attrs->sound)
152 {
153 free((char *) attrs->sound);
154 }
155 if (attrs->rptp_server)
156 {
157 free((char *) attrs->rptp_server);
158 }
159 if (*attrs->client_data)
160 {
161 free((char *) attrs->client_data);
162 }
163 free((char *) attrs);
164 }
165
166 #define COPY_SIZE 128
167 #define COPY(rp, p, n) \
168 grow = 0; \
169 while (rp->len + n > rp->size) \
170 { \
171 grow++; \
172 rp->size += COPY_SIZE; \
173 if (rp->size > MAX_PACKET) \
174 { \
175 rplay_errno = RPLAY_ERROR_PACKET_SIZE; \
176 return -1; \
177 } \
178 } \
179 if (grow) \
180 { \
181 rp->buf = realloc (rp->buf, rp->size); \
182 if (rp->buf == NULL) \
183 { \
184 rplay_errno = RPLAY_ERROR_MEMORY; \
185 return -1; \
186 } \
187 } \
188 memcpy ((char *) rp->buf + rp->len, (char *) p, n); \
189 rp->len += n; \
190
191 /*
192 * pack the rplay object into the internal buffer
193 *
194 * this routine is called everytime rplay_set is used
195 */
196 #ifdef __STDC__
197 int
198 rplay_pack(RPLAY *rp)
199 #else
200 int
201 rplay_pack(rp)
202 RPLAY *rp;
203 #endif
204 {
205 RPLAY_ATTRS *attrs;
206 int len, grow = 0, i;
207 unsigned char val;
208 unsigned long lval;
209 unsigned short sval;
210 short size;
211
212 rp->len = 0;
213 val = RPLAY_PACKET_ID;
214 COPY(rp, &val, sizeof(val));
215 val = rp->command;
216 COPY(rp, &val, sizeof(val));
217
218 if (rp->count != RPLAY_DEFAULT_LIST_COUNT)
219 {
220 val = RPLAY_LIST_COUNT;
221 COPY(rp, &val, sizeof(val));
222 val = rp->count;
223 COPY(rp, &val, sizeof(val));
224 }
225
226 if (rp->priority != RPLAY_DEFAULT_PRIORITY)
227 {
228 val = RPLAY_PRIORITY;
229 COPY(rp, &val, sizeof(val));
230 val = rp->priority;
231 COPY(rp, &val, sizeof(val));
232 }
233
234 if (*rp->list_name)
235 {
236 val = RPLAY_LIST_NAME;
237 COPY(rp, &val, sizeof(val));
238 len = strlen(rp->list_name) + 1;
239 COPY(rp, rp->list_name, len);
240 }
241
242 if (rp->id != RPLAY_NULL)
243 {
244 val = RPLAY_ID;
245 COPY(rp, &val, sizeof(val));
246 lval = htonl(rp->id);
247 COPY(rp, &lval, sizeof(lval));
248 }
249
250 if (rp->sequence != -1)
251 {
252 val = RPLAY_SEQUENCE;
253 COPY(rp, &val, sizeof(val));
254 lval = htonl(rp->sequence);
255 COPY(rp, &lval, sizeof(lval));
256 }
257
258 if (rp->data != RPLAY_NULL && rp->data_size > 0)
259 {
260 val = RPLAY_DATA_SIZE;
261 COPY(rp, &val, sizeof(val));
262 sval = htons(rp->data_size);
263 COPY(rp, &sval, sizeof(sval));
264
265 val = RPLAY_DATA;
266 COPY(rp, &val, sizeof(val));
267 COPY(rp, rp->data, rp->data_size);
268 }
269
270 for (i = 0, attrs = rp->attrs; attrs; attrs = attrs->next, i++)
271 {
272 if (rp->random_sound != RPLAY_DEFAULT_RANDOM_SOUND)
273 {
274 if (i != rp->random_sound)
275 {
276 continue;
277 }
278 }
279
280 if (*attrs->sound)
281 {
282 val = RPLAY_SOUND;
283 COPY(rp, &val, sizeof(val));
284 len = strlen(attrs->sound) + 1;
285 COPY(rp, attrs->sound, len);
286 }
287
288 if (attrs->volume != RPLAY_DEFAULT_VOLUME)
289 {
290 val = RPLAY_VOLUME;
291 COPY(rp, &val, sizeof(val));
292 val = attrs->volume;
293 COPY(rp, &val, sizeof(val));
294 }
295
296 if (attrs->count != RPLAY_DEFAULT_COUNT)
297 {
298 val = RPLAY_COUNT;
299 COPY(rp, &val, sizeof(val));
300 val = attrs->count;
301 COPY(rp, &val, sizeof(val));
302 }
303
304 if (attrs->rptp_server)
305 {
306 val = RPLAY_RPTP_SERVER;
307 COPY(rp, &val, sizeof(val));
308 len = strlen(attrs->rptp_server) + 1;
309 COPY(rp, attrs->rptp_server, len);
310 }
311
312 if (attrs->rptp_server_port != RPTP_PORT)
313 {
314 val = RPLAY_RPTP_SERVER_PORT;
315 COPY(rp, &val, sizeof(val));
316 size = htons(attrs->rptp_server_port);
317 COPY(rp, &size, sizeof(size));
318 }
319
320 if (attrs->rptp_search == FALSE)
321 {
322 val = RPLAY_RPTP_SEARCH;
323 COPY(rp, &val, sizeof(val));
324 val = attrs->rptp_search;
325 COPY(rp, &val, sizeof(val));
326 }
327
328 if (attrs->sample_rate != RPLAY_DEFAULT_SAMPLE_RATE)
329 {
330 val = RPLAY_SAMPLE_RATE;
331 COPY(rp, &val, sizeof(val));
332 lval = htonl(attrs->sample_rate);
333 COPY(rp, &lval, sizeof(lval));
334 }
335
336 if (*attrs->client_data)
337 {
338 val = RPLAY_CLIENT_DATA;
339 COPY(rp, &val, sizeof(val));
340 len = strlen(attrs->client_data) + 1;
341 COPY(rp, attrs->client_data, len);
342 }
343
344 val = RPLAY_NULL;
345 COPY(rp, &val, sizeof(val));
346 }
347
348 if (i == 0)
349 {
350 val = RPLAY_NULL;
351 COPY(rp, &val, sizeof(val));
352 }
353
354 val = RPLAY_NULL;
355 COPY(rp, &val, sizeof(val));
356
357 return 0;
358 }
359
360 /*
361 * unpack an rplay packet into an rplay object
362 */
363 #ifdef __STDC__
364 RPLAY *
365 rplay_unpack(char *packet)
366 #else
367 RPLAY *
368 rplay_unpack(packet)
369 char *packet;
370 #endif
371 {
372 RPLAY *rp;
373 int still_going = 1;
374 int version;
375
376 rplay_errno = RPLAY_ERROR_NONE;
377
378 version = *packet++;
379 rp = rplay_create(*packet++);
380 if (rp == NULL)
381 {
382 rplay_errno = RPLAY_ERROR_MEMORY;
383 return NULL;
384 }
385
386 *(rp->attrsp) = rplay_attrs_create();
387 if (*(rp->attrsp) == NULL)
388 {
389 rplay_errno = RPLAY_ERROR_MEMORY;
390 return NULL;
391 }
392
393 while (still_going)
394 {
395 switch (*packet++)
396 {
397 case RPLAY_LIST_COUNT:
398 rp->count = (unsigned char) *packet++;
399 break;
400
401 case RPLAY_LIST_NAME:
402 rp->list_name = strdup(packet);
403 packet += strlen(packet) + 1;
404 break;
405
406 case RPLAY_PRIORITY:
407 rp->priority = (unsigned char) *packet++;
408 break;
409
410 case RPLAY_SOUND:
411 (*rp->attrsp)->sound = strdup(packet);
412 packet += strlen(packet) + 1;
413 break;
414
415 case RPLAY_VOLUME:
416 (*rp->attrsp)->volume = (unsigned char) *packet++;
417 break;
418
419 case RPLAY_COUNT:
420 (*rp->attrsp)->count = (unsigned char) *packet++;
421 break;
422
423 case RPLAY_RPTP_SERVER:
424 (*rp->attrsp)->rptp_server = strdup(packet);
425 packet += strlen(packet) + 1;
426 break;
427
428 case RPLAY_RPTP_SERVER_PORT:
429 memcpy((char *) &(*rp->attrsp)->rptp_server_port, packet, sizeof((*rp->attrsp)->rptp_server_port));
430 (*rp->attrsp)->rptp_server_port = ntohs((*rp->attrsp)->rptp_server_port);
431 packet += sizeof((*rp->attrsp)->rptp_server_port);
432 break;
433
434
435 case RPLAY_RPTP_SEARCH:
436 (*rp->attrsp)->rptp_search = (unsigned char) *packet++;
437 break;
438
439 case RPLAY_SAMPLE_RATE:
440 memcpy((char *) &(*rp->attrsp)->sample_rate, packet,
441 sizeof((*rp->attrsp)->sample_rate));
442 (*rp->attrsp)->sample_rate = ntohl((*rp->attrsp)->sample_rate);
443 packet += sizeof((*rp->attrsp)->sample_rate);
444 break;
445
446 case RPLAY_CLIENT_DATA:
447 (*rp->attrsp)->client_data = strdup(packet);
448 packet += strlen(packet) + 1;
449 break;
450
451 case RPLAY_ID:
452 memcpy((char *) &rp->id, packet, sizeof(rp->id));
453 rp->id = ntohl(rp->id);
454 packet += sizeof(rp->id);
455 break;
456
457 case RPLAY_SEQUENCE:
458 memcpy((char *) &rp->sequence, packet, sizeof(rp->sequence));
459 rp->sequence = ntohl(rp->sequence);
460 packet += sizeof(rp->sequence);
461 break;
462
463 case RPLAY_DATA_SIZE:
464 memcpy((char *) &rp->data_size, packet, sizeof(rp->data_size));
465 rp->data_size = ntohs(rp->data_size);
466 packet += sizeof(rp->data_size);
467 break;
468
469 case RPLAY_DATA:
470 rp->data = (char *) malloc(rp->data_size);
471 memcpy(rp->data, packet, rp->data_size);
472 packet += rp->data_size;
473 break;
474
475 case RPLAY_NULL:
476 rp->nsounds++;
477 rp->attrsp = &(*rp->attrsp)->next;
478 if (*packet == RPLAY_NULL)
479 {
480 still_going = 0;
481 }
482 else
483 {
484 *(rp->attrsp) = rplay_attrs_create();
485 if (*(rp->attrsp) == NULL)
486 {
487 rplay_errno = RPLAY_ERROR_MEMORY;
488 return NULL;
489 }
490 }
491 break;
492
493 default:
494 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
495 #if 1
496 printf("unpack: unknown attr '%d'\n", *packet);
497 #endif
498 return NULL;
499 }
500 }
501
502 return rp;
503 }
504
505 /*
506 * create an rplay object for the given command
507 *
508 * currently all commands use the same internal representation but this can
509 * be changed in the future
510 */
511 #ifdef __STDC__
512 RPLAY *
513 rplay_create(int command)
514 #else
515 RPLAY *
516 rplay_create(command)
517 int command;
518 #endif
519 {
520 RPLAY *rp;
521
522 rplay_errno = RPLAY_ERROR_NONE;
523
524 rp = (RPLAY *) malloc(sizeof(RPLAY));
525 if (rp == NULL)
526 {
527 rplay_errno = RPLAY_ERROR_MEMORY;
528 return NULL;
529 }
530
531 rp->attrs = NULL;
532 rp->attrsp = &rp->attrs;
533 rp->buf = (char *) malloc(COPY_SIZE);
534 if (rp->buf == NULL)
535 {
536 rplay_errno = RPLAY_ERROR_MEMORY;
537 return NULL;
538 }
539
540 rp->len = 0;
541 rp->size = 0;
542 rp->command = RPLAY_NULL;
543 rp->nsounds = 0;
544 rp->count = RPLAY_DEFAULT_LIST_COUNT;
545 rp->priority = RPLAY_DEFAULT_PRIORITY;
546 rp->random_sound = RPLAY_DEFAULT_RANDOM_SOUND;
547 rp->list_name = "";
548 rp->id = RPLAY_NULL;
549 rp->sequence = -1;
550 rp->data = NULL;
551 rp->data_size = 0;
552
553 switch (command)
554 {
555 case RPLAY_PLAY:
556 case RPLAY_STOP:
557 case RPLAY_PAUSE:
558 case RPLAY_CONTINUE:
559 case RPLAY_PING:
560 case RPLAY_RESET:
561 case RPLAY_DONE:
562 case RPLAY_PUT:
563 rp->command = command;
564 break;
565
566 default:
567 rplay_errno = RPLAY_ERROR_COMMAND;
568 return NULL;
569 }
570
571 return rp;
572 }
573
574 /*
575 * return the rplay attributes for the given index
576 */
577 #ifdef __STDC__
578 static RPLAY_ATTRS *
579 get_attrs(RPLAY_ATTRS *attrs, int index)
580 #else
581 static RPLAY_ATTRS *
582 get_attrs(attrs, index)
583 RPLAY_ATTRS *attrs;
584 int index;
585 #endif
586 {
587 int i;
588
589 if (index < 0)
590 {
591 return NULL;
592 }
593
594 for (i = 0; i < index && attrs; i++, attrs = attrs->next) ;
595
596 return attrs;
597 }
598
599 /*
600 * set rplay attributes for the given rplay object
601 *
602 * The argument list should be NULL terminated but this is
603 * not always checked.
604 */
605 #ifdef __STDC__
606 long
607 rplay_set(RPLAY *rp,...)
608 #else
609 long
610 rplay_set(va_alist)
611 va_dcl
612 #endif
613 {
614 va_list args;
615 RPLAY_ATTRS *attrs = NULL, *prev = NULL, *curr;
616 int index, i, modifier;
617 char *data;
618 time_t seed;
619
620 #ifdef __STDC__
621 va_start(args, rp);
622 #else
623 RPLAY *rp;
624 va_start(args);
625 rp = va_arg(args, RPLAY *);
626 #endif
627
628 rplay_errno = RPLAY_ERROR_NONE;
629
630 modifier = va_arg(args, long);
631
632 switch (modifier)
633 {
634 case RPLAY_APPEND:
635 *(rp->attrsp) = attrs = rplay_attrs_create();
636 if (attrs == NULL)
637 {
638 rplay_errno = RPLAY_ERROR_MEMORY;
639 return -1;
640 }
641 rp->attrsp = &attrs->next;
642 rp->nsounds++;
643 break;
644
645 case RPLAY_INSERT:
646 index = va_arg(args, long);
647 if (index < 0)
648 {
649 rplay_errno = RPLAY_ERROR_INDEX;
650 return -1;
651 }
652 for (i = 0, curr = rp->attrs; i < index && curr; i++)
653 {
654 prev = curr;
655 curr = curr->next;
656 }
657 if (curr == NULL && i != index)
658 {
659 rplay_errno = RPLAY_ERROR_INDEX;
660 return -1;
661 }
662 attrs = rplay_attrs_create();
663 if (attrs == NULL)
664 {
665 rplay_errno = RPLAY_ERROR_MEMORY;
666 return -1;
667 }
668 if (prev)
669 {
670 prev->next = attrs;
671 }
672 else
673 {
674 *(rp->attrsp) = attrs;
675 }
676 attrs->next = curr;
677 if (attrs->next == NULL)
678 {
679 rp->attrsp = &attrs->next;
680 }
681 rp->nsounds++;
682 break;
683
684 case RPLAY_DELETE:
685 index = va_arg(args, long);
686 if (index < 0)
687 {
688 rplay_errno = RPLAY_ERROR_INDEX;
689 return -1;
690 }
691 for (i = 0, curr = rp->attrs; i < index && curr; i++)
692 {
693 prev = curr;
694 curr = curr->next;
695 }
696 if (curr == NULL)
697 {
698 rplay_errno = RPLAY_ERROR_INDEX;
699 return -1;
700 }
701 if (prev)
702 {
703 prev->next = curr->next;
704 if (prev->next == NULL)
705 {
706 rp->attrsp = &prev->next;
707 }
708 }
709 else
710 {
711 rp->attrs = curr->next;
712 if (rp->attrs == NULL)
713 {
714 rp->attrsp = &rp->attrs;
715 }
716 }
717 rplay_attrs_destroy(curr);
718 rp->nsounds--;
719 break;
720
721 case RPLAY_CHANGE:
722 index = va_arg(args, long);
723 attrs = get_attrs(rp->attrs, index);
724 if (attrs == NULL)
725 {
726 rplay_errno = RPLAY_ERROR_INDEX;
727 return -1;
728 }
729 break;
730
731 case RPLAY_LIST_COUNT:
732 rp->count = va_arg(args, long);
733 break;
734
735 case RPLAY_LIST_NAME:
736 if (*rp->list_name)
737 {
738 free((char *) rp->list_name);
739 }
740 rp->list_name = strdup(va_arg(args, char *));
741 break;
742
743 case RPLAY_PRIORITY:
744 rp->priority = va_arg(args, long);
745 break;
746
747 case RPLAY_RANDOM_SOUND:
748 seed = time(0);
749 srandom((int) seed);
750 rp->random_sound = (int) (random() % rp->nsounds);
751 break;
752
753 case RPLAY_ID:
754 rp->id = va_arg(args, long);
755 break;
756
757 case RPLAY_SEQUENCE:
758 rp->sequence = va_arg(args, unsigned long);
759 break;
760
761 case RPLAY_DATA_SIZE:
762 rplay_errno = RPLAY_ERROR_MODIFIER;
763 return -1;
764
765 case RPLAY_DATA:
766 if (rp->data)
767 {
768 free((char *) rp->data);
769 }
770
771 data = va_arg(args, char *);
772 rp->data_size = va_arg(args, long);
773 rp->data = (char *) malloc(rp->data_size);
774 if (rp->data == NULL)
775 {
776 rplay_errno = RPLAY_ERROR_MEMORY;
777 return -1;
778 }
779 memcpy(rp->data, data, rp->data_size);
780 break;
781
782 default:
783 rplay_errno = RPLAY_ERROR_MODIFIER;
784 return -1;
785 }
786
787 if (attrs)
788 {
789 int attribute;
790
791 while ((attribute = va_arg(args, long)))
792 {
793 switch (attribute)
794 {
795 case RPLAY_LIST_COUNT:
796 rp->count = va_arg(args, long);
797 break;
798
799 case RPLAY_LIST_NAME:
800 if (*rp->list_name)
801 {
802 free((char *) rp->list_name);
803 }
804 rp->list_name = strdup(va_arg(args, char *));
805 break;
806
807 case RPLAY_PRIORITY:
808 rp->priority = va_arg(args, long);
809 break;
810
811 case RPLAY_SOUND:
812 if (*attrs->sound)
813 {
814 free((char *) attrs->sound);
815 }
816 attrs->sound = strdup(va_arg(args, char *));
817 break;
818
819 case RPLAY_VOLUME:
820 attrs->volume = va_arg(args, long);
821 break;
822
823 case RPLAY_COUNT:
824 attrs->count = va_arg(args, long);
825 break;
826
827 case RPLAY_RPTP_SERVER:
828 case RPLAY_RPTP_FROM_SENDER:
829 {
830 struct hostent *hp;
831 u_long addr;
832 struct sockaddr_in s;
833 char *host = va_arg(args, char *);
834 char hostname[MAXHOSTNAMELEN];
835
836 if (attribute == RPLAY_RPTP_FROM_SENDER)
837 {
838 if (gethostname(hostname, sizeof(hostname)) < 0)
839 {
840 rplay_errno = RPLAY_ERROR_HOST;
841 return -1;
842 }
843 host = hostname;
844 }
845 else
846 {
847 host = va_arg(args, char *);
848 }
849
850 memset((char *) &s, 0, sizeof(s));
851 addr = inet_addr(host);
852 if (addr == 0xffffffff)
853 {
854 hp = gethostbyname(host);
855 if (hp == NULL)
856 {
857 rplay_errno = RPLAY_ERROR_HOST;
858 return -1;
859 }
860 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
861 }
862 else
863 {
864 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
865 }
866 attrs->rptp_server = strdup((char *) inet_ntoa(s.sin_addr));
867 break;
868 }
869
870 case RPLAY_RPTP_SERVER_PORT:
871 attrs->rptp_server_port = (unsigned short) va_arg(args, long);
872 break;
873
874 case RPLAY_RPTP_SEARCH:
875 attrs->rptp_search = va_arg(args, long);
876 break;
877
878 case RPLAY_SAMPLE_RATE:
879 attrs->sample_rate = va_arg(args, unsigned long);
880 break;
881
882 case RPLAY_CLIENT_DATA:
883 if (*attrs->client_data)
884 {
885 free((char *) attrs->client_data);
886 }
887 attrs->client_data = strdup(va_arg(args, char *));
888 break;
889
890 default:
891 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
892 return -1;
893 }
894 }
895 }
896
897 return rplay_pack(rp);
898 }
899
900 /*
901 * retrieve rplay attributes from the given rplay object
902 */
903 #ifdef __STDC__
904 long
905 rplay_get(RPLAY *rp,...)
906 #else
907 long
908 rplay_get(va_alist)
909 va_dcl
910 #endif
911 {
912 va_list args;
913 RPLAY_ATTRS *attrs;
914 int get, index;
915
916 #ifdef __STDC__
917 va_start(args, rp);
918 #else
919 RPLAY *rp;
920 va_start(args);
921 rp = va_arg(args, RPLAY *);
922 #endif
923 rplay_errno = RPLAY_ERROR_NONE;
924
925 get = va_arg(args, long);
926
927 switch (get)
928 {
929 case RPLAY_NSOUNDS:
930 return rp->nsounds;
931
932 case RPLAY_COMMAND:
933 return rp->command;
934
935 case RPLAY_LIST_COUNT:
936 return rp->count;
937
938 case RPLAY_LIST_NAME:
939 return (long) rp->list_name;
940
941 case RPLAY_PRIORITY:
942 return rp->priority;
943
944 case RPLAY_RANDOM_SOUND:
945 return rp->random_sound;
946
947 case RPLAY_ID:
948 return rp->id;
949
950 case RPLAY_SEQUENCE:
951 return rp->sequence;
952
953 case RPLAY_DATA_SIZE:
954 return rp->data_size;
955
956 case RPLAY_DATA:
957 return (long) rp->data;
958
959 case RPLAY_SOUND:
960 index = va_arg(args, long);
961 attrs = get_attrs(rp->attrs, index);
962 if (attrs == NULL)
963 {
964 rplay_errno = RPLAY_ERROR_INDEX;
965 return -1;
966 }
967 return (long) attrs->sound;
968
969 case RPLAY_VOLUME:
970 index = va_arg(args, long);
971 attrs = get_attrs(rp->attrs, index);
972 if (attrs == NULL)
973 {
974 rplay_errno = RPLAY_ERROR_INDEX;
975 return -1;
976 }
977 return attrs->volume;
978
979 case RPLAY_COUNT:
980 index = va_arg(args, long);
981 attrs = get_attrs(rp->attrs, index);
982 if (attrs == NULL)
983 {
984 rplay_errno = RPLAY_ERROR_INDEX;
985 return -1;
986 }
987 return attrs->count;
988
989 case RPLAY_RPTP_SERVER:
990 index = va_arg(args, long);
991 attrs = get_attrs(rp->attrs, index);
992 if (attrs == NULL)
993 {
994 rplay_errno = RPLAY_ERROR_INDEX;
995 return -1;
996 }
997 return (long) attrs->rptp_server;
998
999 case RPLAY_RPTP_SERVER_PORT:
1000 index = va_arg(args, long);
1001 attrs = get_attrs(rp->attrs, index);
1002 if (attrs == NULL)
1003 {
1004 rplay_errno = RPLAY_ERROR_INDEX;
1005 return -1;
1006 }
1007 return (long) attrs->rptp_server_port;
1008
1009 case RPLAY_RPTP_SEARCH:
1010 index = va_arg(args, long);
1011 attrs = get_attrs(rp->attrs, index);
1012 if (attrs == NULL)
1013 {
1014 rplay_errno = RPLAY_ERROR_INDEX;
1015 return -1;
1016 }
1017 return attrs->rptp_search;
1018
1019 case RPLAY_SAMPLE_RATE:
1020 index = va_arg(args, long);
1021 attrs = get_attrs(rp->attrs, index);
1022 if (attrs == NULL)
1023 {
1024 rplay_errno = RPLAY_ERROR_INDEX;
1025 return -1;
1026 }
1027 return attrs->sample_rate;
1028
1029 case RPLAY_CLIENT_DATA:
1030 index = va_arg(args, int);
1031 attrs = get_attrs(rp->attrs, index);
1032 if (attrs == NULL)
1033 {
1034 rplay_errno = RPLAY_ERROR_INDEX;
1035 return -1;
1036 }
1037 return (long) attrs->client_data;
1038
1039 default:
1040 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
1041 return -1;
1042 }
1043 }
1044
1045 /*
1046 * free memory used by the given rplay object
1047 */
1048 #ifdef __STDC__
1049 void
1050 rplay_destroy(RPLAY *rp)
1051 #else
1052 void
1053 rplay_destroy(rp)
1054 RPLAY *rp;
1055 #endif
1056 {
1057 RPLAY_ATTRS *p, *q;
1058
1059 rplay_errno = RPLAY_ERROR_NONE;
1060
1061 for (p = rp->attrs; p; q = p, p = p->next, rplay_attrs_destroy(q)) ;
1062 if (*rp->list_name)
1063 {
1064 free((char *) rp->list_name);
1065 }
1066 if (rp->data)
1067 {
1068 free((char *) rp->data);
1069 }
1070 free((char *) rp->buf);
1071 free((char *) rp);
1072 }
1073
1074 /*
1075 * support routine to convert rplay 2.0 packets to rplay 3.0
1076 */
1077 #ifdef OLD_RPLAY
1078 #ifdef __STDC__
1079 char *
1080 rplay_convert(char *p)
1081 #else
1082 char *
1083 rplay_convert(p)
1084 char *p;
1085 #endif
1086 {
1087 static char buf[MAX_PACKET];
1088 char *q = buf;
1089 int len;
1090
1091 *q++ = RPLAY_PACKET_ID;
1092 switch (*p++)
1093 {
1094 case OLD_RPLAY_PLAY:
1095 *q++ = RPLAY_PLAY;
1096 break;
1097
1098 case OLD_RPLAY_STOP:
1099 *q++ = RPLAY_STOP;
1100 break;
1101
1102 case OLD_RPLAY_PAUSE:
1103 *q++ = RPLAY_PAUSE;
1104 break;
1105
1106 case OLD_RPLAY_CONTINUE:
1107 *q++ = RPLAY_CONTINUE;
1108 break;
1109 }
1110
1111 do
1112 {
1113 *q++ = RPLAY_SOUND;
1114 strcpy(q, p);
1115 len = strlen(p) + 1;
1116 p += len;
1117 q += len;
1118 *q++ = RPLAY_VOLUME;
1119 *q++ = *p++;
1120 *q++ = RPLAY_NULL;
1121 }
1122 while (*p);
1123
1124 *q++ = RPLAY_NULL;
1125
1126 return buf;
1127 }
1128 #endif /* OLD_RPLAY */
1129
1130 static int
1131 default_rplay_port()
1132 {
1133 struct servent *sp;
1134 int port;
1135
1136 sp = getservbyname("rplay", "udp");
1137 if (sp)
1138 {
1139 port = ntohs(sp->s_port); /* htons is used later */
1140 }
1141 else
1142 {
1143 port = RPLAY_PORT;
1144 }
1145
1146 return port;
1147 }
1148
1149 #ifdef __STDC__
1150 int
1151 rplay_open(char *host)
1152 #else
1153 int
1154 rplay_open(host)
1155 char *host;
1156 #endif
1157 {
1158 int port;
1159
1160 port = default_rplay_port();
1161
1162 return rplay_open_port(host, port);
1163 }
1164
1165 /*
1166 * Open a UDP socket for the given host and port.
1167 *
1168 * The host can be either a name or an IP address.
1169 */
1170 #ifdef __STDC__
1171 int
1172 rplay_open_port(char *host, int port)
1173 #else
1174 int
1175 rplay_open_port(host, port)
1176 char *host;
1177 int port;
1178 #endif
1179 {
1180 struct hostent *hp;
1181 u_long addr;
1182 struct sockaddr_in s;
1183
1184 rplay_errno = RPLAY_ERROR_NONE;
1185
1186 memset((char *) &s, 0, sizeof(s));
1187
1188 addr = inet_addr(host);
1189 if (addr == 0xffffffff)
1190 {
1191 hp = gethostbyname(host);
1192 if (hp == NULL)
1193 {
1194 rplay_errno = RPLAY_ERROR_HOST;
1195 return -1;
1196 }
1197 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
1198 }
1199 else
1200 {
1201 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
1202 }
1203
1204 s.sin_port = htons(port);
1205 s.sin_family = AF_INET;
1206
1207 return rplay_open_sockaddr_in(&s);
1208 }
1209
1210 #ifdef __STDC__
1211 int
1212 rplay_open_sockaddr_in(struct sockaddr_in *saddr)
1213 #else
1214 int
1215 rplay_open_sockaddr_in(saddr)
1216 struct sockaddr_in *saddr;
1217 #endif
1218 {
1219 int rplay_fd;
1220 int on = 1;
1221
1222 rplay_fd = socket(AF_INET, SOCK_DGRAM, 0);
1223 if (rplay_fd < 0)
1224 {
1225 rplay_errno = RPLAY_ERROR_SOCKET;
1226 return -1;
1227 }
1228
1229 /*
1230 * enable broadcasting
1231 */
1232 if (setsockopt(rplay_fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on)) < 0)
1233 {
1234 rplay_errno = RPLAY_ERROR_BROADCAST;
1235 return -1;
1236 }
1237
1238 if (connect(rplay_fd, (struct sockaddr *) saddr, sizeof(*saddr)) < 0)
1239 {
1240 rplay_errno = RPLAY_ERROR_CONNECT;
1241 return -1;
1242 }
1243
1244 return rplay_fd;
1245 }
1246
1247 /*
1248 * send an rplay packet to a host
1249 */
1250 #ifdef __STDC__
1251 int
1252 rplay(int rplay_fd, RPLAY *rp)
1253 #else
1254 int
1255 rplay(rplay_fd, rp)
1256 int rplay_fd;
1257 RPLAY *rp;
1258 #endif
1259 {
1260 rplay_errno = RPLAY_ERROR_NONE;
1261
1262 if (write(rplay_fd, rp->buf, rp->len) != rp->len)
1263 {
1264 rplay_errno = RPLAY_ERROR_WRITE;
1265 return -1;
1266 }
1267
1268 return 0;
1269 }
1270
1271 /*
1272 * close the socket
1273 */
1274 #ifdef __STDC__
1275 int
1276 rplay_close(int rplay_fd)
1277 #else
1278 int
1279 rplay_close(rplay_fd)
1280 int rplay_fd;
1281 #endif
1282 {
1283 rplay_errno = RPLAY_ERROR_NONE;
1284
1285 if (close(rplay_fd) < 0)
1286 {
1287 rplay_errno = RPLAY_ERROR_CLOSE;
1288 return -1;
1289 }
1290
1291 return 0;
1292 }
1293
1294 /*
1295 * report rplay error messages
1296 */
1297 #ifdef __STDC__
1298 void
1299 rplay_perror(char *s)
1300 #else
1301 void
1302 rplay_perror(s)
1303 char *s;
1304 #endif
1305 {
1306 fprintf(stderr, "%s: %s\n", s, rplay_errlist[rplay_errno]);
1307 }
1308
1309 /*
1310 * open a socket using the current X display
1311 * (uses the DISPLAY environment variable)
1312 */
1313 #ifdef __STDC__
1314 int
1315 rplay_open_display(void)
1316 #else
1317 int
1318 rplay_open_display()
1319 #endif
1320 {
1321 char *display, *p;
1322 char host[MAXHOSTNAMELEN];
1323
1324 display = getenv("DISPLAY");
1325 if (display == NULL || display[0] == ':')
1326 {
1327 strcpy(host, "localhost");
1328 }
1329 else
1330 {
1331 strcpy(host, display);
1332 p = strchr(host, ':');
1333 if (p)
1334 {
1335 *p = '\0';
1336 }
1337 if (strcmp(host, "unix") == 0 || strcmp(host, "local") == 0 || strcmp(host, "X") == 0)
1338 {
1339 strcpy(host, "localhost");
1340 }
1341 }
1342
1343 return rplay_open(host);
1344 }
1345
1346 /*
1347 * play a sound on the current X display
1348 */
1349 #ifdef __STDC__
1350 int
1351 rplay_display(char *sound)
1352 #else
1353 int
1354 rplay_display(sound)
1355 char *sound;
1356 #endif
1357 {
1358 int rplay_fd;
1359
1360 rplay_fd = rplay_open_display();
1361 if (rplay_fd < 0)
1362 {
1363 return -1;
1364 }
1365
1366 return rplay_sound(rplay_fd, sound);
1367 }
1368
1369 /*
1370 * play a sound on the localhost
1371 */
1372 #ifdef __STDC__
1373 int
1374 rplay_local(char *sound)
1375 #else
1376 int
1377 rplay_local(sound)
1378 char *sound;
1379 #endif
1380 {
1381 return rplay_host("localhost", sound);
1382 }
1383
1384 /*
1385 * play a sound on a host
1386 */
1387 #ifdef __STDC__
1388 int
1389 rplay_host(char *host, char *sound)
1390 #else
1391 int
1392 rplay_host(host, sound)
1393 char *host;
1394 char *sound;
1395 #endif
1396 {
1397 int rplay_fd;
1398
1399 rplay_fd = rplay_open(host);
1400 if (rplay_fd < 0)
1401 {
1402 return -1;
1403 }
1404
1405 return rplay_sound(rplay_fd, sound);
1406 }
1407
1408 /*
1409 * play a sound given a socket
1410 */
1411 #ifdef __STDC__
1412 int
1413 rplay_sound(int rplay_fd, char *sound)
1414 #else
1415 int
1416 rplay_sound(rplay_fd, sound)
1417 int rplay_fd;
1418 char *sound;
1419 #endif
1420 {
1421 RPLAY *rp;
1422
1423 rp = rplay_create(RPLAY_PLAY);
1424 if (rp == NULL)
1425 {
1426 return -1;
1427 }
1428
1429 if (rplay_set(rp, RPLAY_APPEND, RPLAY_SOUND, sound, NULL) < 0)
1430 {
1431 return -1;
1432 }
1433
1434 if (rplay(rplay_fd, rp) < 0)
1435 {
1436 return -1;
1437 }
1438
1439 rplay_destroy(rp);
1440
1441 return 0;
1442 }
1443
1444 /*
1445 * ping an rplay server
1446 */
1447 #ifdef __STDC__
1448 int
1449 rplay_ping(char *host)
1450 #else
1451 int
1452 rplay_ping(host)
1453 char *host;
1454 #endif
1455 {
1456 int rplay_fd;
1457 int port;
1458 int error1 = 0, error2 = 0;
1459
1460 /* Ping the default port. */
1461 port = default_rplay_port();
1462 rplay_fd = rplay_open_port(host, port);
1463 if (rplay_fd < 0)
1464 {
1465 return -1;
1466 }
1467 error1 = rplay_ping_sockfd(rplay_fd);
1468
1469 #ifdef OTHER_RPLAY_PORTS
1470 /* Pick an alternative port. */
1471 if (port == RPLAY_PORT)
1472 {
1473 port = OLD_RPLAY_PORT;
1474 }
1475 else
1476 {
1477 port = RPLAY_PORT;
1478 }
1479
1480 /* Ping the alternative port. */
1481 rplay_fd = rplay_open_port(host, port);
1482 if (rplay_fd < 0)
1483 {
1484 return -1;
1485 }
1486 error2 = rplay_ping_sockfd(rplay_fd);
1487 #endif /* OTHER_RPLAY_PORTS */
1488
1489 /* Only return -1 if both pings fail. */
1490 if (error1 < 0 && error2 < 0)
1491 {
1492 return -1;
1493 }
1494 else
1495 {
1496 return 0;
1497 }
1498 }
1499
1500 #ifdef __STDC__
1501 int
1502 rplay_ping_sockfd(int rplay_fd)
1503 #else
1504 int
1505 rplay_ping_sockfd(rplay_fd)
1506 int rplay_fd;
1507 #endif
1508 {
1509 RPLAY *rp;
1510
1511 rp = rplay_create(RPLAY_PING);
1512 if (rp == NULL)
1513 {
1514 return -1;
1515 }
1516
1517 if (rplay_pack(rp) < 0)
1518 {
1519 return -1;
1520 }
1521
1522 if (rplay(rplay_fd, rp) < 0)
1523 {
1524 return -1;
1525 }
1526
1527 rplay_close(rplay_fd);
1528 rplay_destroy(rp);
1529
1530 return 0;
1531 }
1532
1533 #ifdef __STDC__
1534 int
1535 rplay_ping_sockaddr_in(struct sockaddr_in *saddr)
1536 #else
1537 int
1538 rplay_ping_sockaddr_in(saddr)
1539 struct sockaddr_in *saddr;
1540 #endif
1541 {
1542 int rplay_fd;
1543
1544 rplay_fd = rplay_open_sockaddr_in(saddr);
1545 if (rplay_fd < 0)
1546 {
1547 return -1;
1548 }
1549
1550 return rplay_ping_sockfd(rplay_fd);
1551 }
1552
1553 /*
1554 * play a sound on a host with a specific volume
1555 */
1556 #ifdef __STDC__
1557 int
1558 rplay_host_volume(char *host, char *sound, int volume)
1559 #else
1560 int
1561 rplay_host_volume(host, sound, volume)
1562 char *host;
1563 char *sound;
1564 int volume;
1565 #endif
1566 {
1567 int rplay_fd;
1568 RPLAY *rp;
1569
1570 rplay_fd = rplay_open(host);
1571 if (rplay_fd < 0)
1572 {
1573 return -1;
1574 }
1575
1576 rp = rplay_create(RPLAY_PLAY);
1577 if (rp == NULL)
1578 {
1579 return -1;
1580 }
1581
1582 if (rplay_set(rp, RPLAY_APPEND, RPLAY_SOUND, sound, RPLAY_VOLUME, volume, NULL) < 0)
1583 {
1584 return -1;
1585 }
1586
1587 if (rplay(rplay_fd, rp) < 0)
1588 {
1589 return -1;
1590 }
1591
1592 rplay_destroy(rp);
1593
1594 return 0;
1595 }
1596
1597 /*
1598 * obtain the default rplay host
1599 */
1600 #ifdef __STDC__
1601 char *
1602 rplay_default_host(void)
1603 #else
1604 char *
1605 rplay_default_host()
1606 #endif
1607 {
1608 char *host;
1609
1610 host = getenv("RPLAY_HOST");
1611
1612 return host ? host : "localhost";
1613 }
1614
1615 /*
1616 * play a sound on the default rplay host
1617 */
1618 #ifdef __STDC__
1619 int
1620 rplay_default(char *sound)
1621 #else
1622 int
1623 rplay_default(sound)
1624 char *sound;
1625 #endif
1626 {
1627 return rplay_host(rplay_default_host(), sound);
1628 }
1629
1630 /*
1631 * open the default host
1632 */
1633 #ifdef __STDC__
1634 int
1635 rplay_open_default(void)
1636 #else
1637 int
1638 rplay_open_default()
1639 #endif
1640 {
1641 return rplay_open(rplay_default_host());
1642 }