Debian Codegraph @

librplay/rplay.c

raw symbols ◀ ▶ SCIP
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 * rplay_unpack() is affected by CVE-2025-62672, which can not be fixed in this function
362 * therefore this function is deprecated and only returns an error
363 * please use rplay_unpack_n(char *packet, int length) instead
364 */
365 /*
366 * unpack an rplay packet into an rplay object
367 */
368 #ifdef __STDC__
369 RPLAY *
370 rplay_unpack(char *packet)
371 #else
372 RPLAY *
373 rplay_unpack(packet)
374 char *packet;
375 #endif
376 {
•377 rplay_errno = RPLAY_ERROR_MEMORY;
378 return NULL;
379 }
380
381 /*
382 * unpack an rplay packet into an rplay object
383 */
384 #ifdef __STDC__
385 RPLAY *
386 rplay_unpack_n(char *packet, int length)
387 #else
388 RPLAY *
389 rplay_unpack_n(packet, length)
390 char *packet;
391 int length;
392 #endif
393 {
394 RPLAY *rp;
395 int still_going = 1;
396 int version;
397
398 rplay_errno = RPLAY_ERROR_NONE;
399
400 version = *packet++;
401 rp = rplay_create(*packet++);
402 if (rp == NULL)
403 {
404 rplay_errno = RPLAY_ERROR_MEMORY;
405 return NULL;
406 }
407
408 *(rp->attrsp) = rplay_attrs_create();
409 if (*(rp->attrsp) == NULL)
410 {
411 rplay_errno = RPLAY_ERROR_MEMORY;
412 return NULL;
413 }
414
415 while (still_going)
416 {
417 switch (*packet++)
418 {
419 case RPLAY_LIST_COUNT:
420 rp->count = (unsigned char) *packet++;
421 break;
422
423 case RPLAY_LIST_NAME:
424 rp->list_name = strdup(packet);
425 packet += strlen(packet) + 1;
426 break;
427
428 case RPLAY_PRIORITY:
429 rp->priority = (unsigned char) *packet++;
430 break;
431
432 case RPLAY_SOUND:
433 (*rp->attrsp)->sound = strdup(packet);
434 packet += strlen(packet) + 1;
435 break;
436
437 case RPLAY_VOLUME:
438 (*rp->attrsp)->volume = (unsigned char) *packet++;
439 break;
440
441 case RPLAY_COUNT:
442 (*rp->attrsp)->count = (unsigned char) *packet++;
443 break;
444
445 case RPLAY_RPTP_SERVER:
446 (*rp->attrsp)->rptp_server = strdup(packet);
447 packet += strlen(packet) + 1;
448 break;
449
450 case RPLAY_RPTP_SERVER_PORT:
451 memcpy((char *) &(*rp->attrsp)->rptp_server_port, packet, sizeof((*rp->attrsp)->rptp_server_port));
452 (*rp->attrsp)->rptp_server_port = ntohs((*rp->attrsp)->rptp_server_port);
453 packet += sizeof((*rp->attrsp)->rptp_server_port);
454 break;
455
456
457 case RPLAY_RPTP_SEARCH:
458 (*rp->attrsp)->rptp_search = (unsigned char) *packet++;
459 break;
460
461 case RPLAY_SAMPLE_RATE:
462 memcpy((char *) &(*rp->attrsp)->sample_rate, packet,
463 sizeof((*rp->attrsp)->sample_rate));
464 (*rp->attrsp)->sample_rate = ntohl((*rp->attrsp)->sample_rate);
465 packet += sizeof((*rp->attrsp)->sample_rate);
466 break;
467
468 case RPLAY_CLIENT_DATA:
469 (*rp->attrsp)->client_data = strdup(packet);
470 packet += strlen(packet) + 1;
471 break;
472
473 case RPLAY_ID:
474 memcpy((char *) &rp->id, packet, sizeof(rp->id));
475 rp->id = ntohl(rp->id);
476 packet += sizeof(rp->id);
477 break;
478
479 case RPLAY_SEQUENCE:
480 memcpy((char *) &rp->sequence, packet, sizeof(rp->sequence));
481 rp->sequence = ntohl(rp->sequence);
482 packet += sizeof(rp->sequence);
483 break;
484
485 case RPLAY_DATA_SIZE:
486 memcpy((char *) &rp->data_size, packet, sizeof(rp->data_size));
487 rp->data_size = ntohs(rp->data_size);
488 packet += sizeof(rp->data_size);
489 break;
490
491 case RPLAY_DATA:
•492 if (rp->data_size>length) {
493 rplay_errno = RPLAY_ERROR_MEMORY;
494 return NULL;
495 }
496 rp->data = (char *) malloc(rp->data_size);
497 memcpy(rp->data, packet, rp->data_size);
498 packet += rp->data_size;
499 break;
500
501 case RPLAY_NULL:
502 rp->nsounds++;
503 rp->attrsp = &(*rp->attrsp)->next;
504 if (*packet == RPLAY_NULL)
505 {
506 still_going = 0;
507 }
508 else
509 {
510 *(rp->attrsp) = rplay_attrs_create();
511 if (*(rp->attrsp) == NULL)
512 {
513 rplay_errno = RPLAY_ERROR_MEMORY;
514 return NULL;
515 }
516 }
517 break;
518
519 default:
520 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
521 #if 1
522 printf("unpack: unknown attr '%d'\n", *packet);
523 #endif
524 return NULL;
525 }
526 }
527
528 return rp;
529 }
530
531 /*
532 * create an rplay object for the given command
533 *
534 * currently all commands use the same internal representation but this can
535 * be changed in the future
536 */
537 #ifdef __STDC__
538 RPLAY *
539 rplay_create(int command)
540 #else
541 RPLAY *
542 rplay_create(command)
543 int command;
544 #endif
545 {
546 RPLAY *rp;
547
548 rplay_errno = RPLAY_ERROR_NONE;
549
550 rp = (RPLAY *) malloc(sizeof(RPLAY));
551 if (rp == NULL)
552 {
553 rplay_errno = RPLAY_ERROR_MEMORY;
554 return NULL;
555 }
556
557 rp->attrs = NULL;
558 rp->attrsp = &rp->attrs;
559 rp->buf = (char *) malloc(COPY_SIZE);
560 if (rp->buf == NULL)
561 {
562 rplay_errno = RPLAY_ERROR_MEMORY;
563 return NULL;
564 }
565
566 rp->len = 0;
567 rp->size = 0;
568 rp->command = RPLAY_NULL;
569 rp->nsounds = 0;
570 rp->count = RPLAY_DEFAULT_LIST_COUNT;
571 rp->priority = RPLAY_DEFAULT_PRIORITY;
572 rp->random_sound = RPLAY_DEFAULT_RANDOM_SOUND;
573 rp->list_name = "";
574 rp->id = RPLAY_NULL;
575 rp->sequence = -1;
576 rp->data = NULL;
577 rp->data_size = 0;
578
579 switch (command)
580 {
581 case RPLAY_PLAY:
582 case RPLAY_STOP:
583 case RPLAY_PAUSE:
584 case RPLAY_CONTINUE:
585 case RPLAY_PING:
586 case RPLAY_RESET:
587 case RPLAY_DONE:
588 case RPLAY_PUT:
589 rp->command = command;
590 break;
591
592 default:
593 rplay_errno = RPLAY_ERROR_COMMAND;
594 return NULL;
595 }
596
597 return rp;
598 }
599
600 /*
601 * return the rplay attributes for the given index
602 */
603 #ifdef __STDC__
604 static RPLAY_ATTRS *
605 get_attrs(RPLAY_ATTRS *attrs, int index)
606 #else
607 static RPLAY_ATTRS *
608 get_attrs(attrs, index)
609 RPLAY_ATTRS *attrs;
610 int index;
611 #endif
612 {
613 int i;
614
615 if (index < 0)
616 {
617 return NULL;
618 }
619
620 for (i = 0; i < index && attrs; i++, attrs = attrs->next) ;
621
622 return attrs;
623 }
624
625 /*
626 * set rplay attributes for the given rplay object
627 *
628 * The argument list should be NULL terminated but this is
629 * not always checked.
630 */
631 #ifdef __STDC__
632 long
633 rplay_set(RPLAY *rp,...)
634 #else
635 long
636 rplay_set(va_alist)
637 va_dcl
638 #endif
639 {
640 va_list args;
641 RPLAY_ATTRS *attrs = NULL, *prev = NULL, *curr;
642 int index, i, modifier;
643 char *data;
644 time_t seed;
645
646 #ifdef __STDC__
647 va_start(args, rp);
648 #else
649 RPLAY *rp;
650 va_start(args);
651 rp = va_arg(args, RPLAY *);
652 #endif
653
654 rplay_errno = RPLAY_ERROR_NONE;
655
656 modifier = va_arg(args, long);
657
658 switch (modifier)
659 {
660 case RPLAY_APPEND:
661 *(rp->attrsp) = attrs = rplay_attrs_create();
662 if (attrs == NULL)
663 {
664 rplay_errno = RPLAY_ERROR_MEMORY;
665 return -1;
666 }
667 rp->attrsp = &attrs->next;
668 rp->nsounds++;
669 break;
670
671 case RPLAY_INSERT:
672 index = va_arg(args, long);
673 if (index < 0)
674 {
675 rplay_errno = RPLAY_ERROR_INDEX;
676 return -1;
677 }
678 for (i = 0, curr = rp->attrs; i < index && curr; i++)
679 {
680 prev = curr;
681 curr = curr->next;
682 }
683 if (curr == NULL && i != index)
684 {
685 rplay_errno = RPLAY_ERROR_INDEX;
686 return -1;
687 }
688 attrs = rplay_attrs_create();
689 if (attrs == NULL)
690 {
691 rplay_errno = RPLAY_ERROR_MEMORY;
692 return -1;
693 }
694 if (prev)
695 {
696 prev->next = attrs;
697 }
698 else
699 {
700 *(rp->attrsp) = attrs;
701 }
702 attrs->next = curr;
703 if (attrs->next == NULL)
704 {
705 rp->attrsp = &attrs->next;
706 }
707 rp->nsounds++;
708 break;
709
710 case RPLAY_DELETE:
711 index = va_arg(args, long);
712 if (index < 0)
713 {
714 rplay_errno = RPLAY_ERROR_INDEX;
715 return -1;
716 }
717 for (i = 0, curr = rp->attrs; i < index && curr; i++)
718 {
719 prev = curr;
720 curr = curr->next;
721 }
722 if (curr == NULL)
723 {
724 rplay_errno = RPLAY_ERROR_INDEX;
725 return -1;
726 }
727 if (prev)
728 {
729 prev->next = curr->next;
730 if (prev->next == NULL)
731 {
732 rp->attrsp = &prev->next;
733 }
734 }
735 else
736 {
737 rp->attrs = curr->next;
738 if (rp->attrs == NULL)
739 {
740 rp->attrsp = &rp->attrs;
741 }
742 }
743 rplay_attrs_destroy(curr);
744 rp->nsounds--;
745 break;
746
747 case RPLAY_CHANGE:
748 index = va_arg(args, long);
749 attrs = get_attrs(rp->attrs, index);
750 if (attrs == NULL)
751 {
752 rplay_errno = RPLAY_ERROR_INDEX;
753 return -1;
754 }
755 break;
756
757 case RPLAY_LIST_COUNT:
758 rp->count = va_arg(args, long);
759 break;
760
761 case RPLAY_LIST_NAME:
762 if (*rp->list_name)
763 {
764 free((char *) rp->list_name);
765 }
766 rp->list_name = strdup(va_arg(args, char *));
767 break;
768
769 case RPLAY_PRIORITY:
770 rp->priority = va_arg(args, long);
771 break;
772
773 case RPLAY_RANDOM_SOUND:
774 seed = time(0);
775 srandom((int) seed);
776 rp->random_sound = (int) (random() % rp->nsounds);
777 break;
778
779 case RPLAY_ID:
780 rp->id = va_arg(args, long);
781 break;
782
783 case RPLAY_SEQUENCE:
784 rp->sequence = va_arg(args, unsigned long);
785 break;
786
787 case RPLAY_DATA_SIZE:
788 rplay_errno = RPLAY_ERROR_MODIFIER;
789 return -1;
790
791 case RPLAY_DATA:
792 if (rp->data)
793 {
794 free((char *) rp->data);
795 }
796
797 data = va_arg(args, char *);
798 rp->data_size = va_arg(args, long);
799 rp->data = (char *) malloc(rp->data_size);
800 if (rp->data == NULL)
801 {
802 rplay_errno = RPLAY_ERROR_MEMORY;
803 return -1;
804 }
805 memcpy(rp->data, data, rp->data_size);
806 break;
807
808 default:
809 rplay_errno = RPLAY_ERROR_MODIFIER;
810 return -1;
811 }
812
813 if (attrs)
814 {
815 int attribute;
816
817 while ((attribute = va_arg(args, long)))
818 {
819 switch (attribute)
820 {
821 case RPLAY_LIST_COUNT:
822 rp->count = va_arg(args, long);
823 break;
824
825 case RPLAY_LIST_NAME:
826 if (*rp->list_name)
827 {
828 free((char *) rp->list_name);
829 }
830 rp->list_name = strdup(va_arg(args, char *));
831 break;
832
833 case RPLAY_PRIORITY:
834 rp->priority = va_arg(args, long);
835 break;
836
837 case RPLAY_SOUND:
838 if (*attrs->sound)
839 {
840 free((char *) attrs->sound);
841 }
842 attrs->sound = strdup(va_arg(args, char *));
843 break;
844
845 case RPLAY_VOLUME:
846 attrs->volume = va_arg(args, long);
847 break;
848
849 case RPLAY_COUNT:
850 attrs->count = va_arg(args, long);
851 break;
852
853 case RPLAY_RPTP_SERVER:
854 case RPLAY_RPTP_FROM_SENDER:
855 {
856 struct hostent *hp;
857 u_long addr;
858 struct sockaddr_in s;
859 char *host = va_arg(args, char *);
860 char hostname[MAXHOSTNAMELEN];
861
862 if (attribute == RPLAY_RPTP_FROM_SENDER)
863 {
864 if (gethostname(hostname, sizeof(hostname)) < 0)
865 {
866 rplay_errno = RPLAY_ERROR_HOST;
867 return -1;
868 }
869 host = hostname;
870 }
871 else
872 {
873 host = va_arg(args, char *);
874 }
875
876 memset((char *) &s, 0, sizeof(s));
877 addr = inet_addr(host);
878 if (addr == 0xffffffff)
879 {
880 hp = gethostbyname(host);
881 if (hp == NULL)
882 {
883 rplay_errno = RPLAY_ERROR_HOST;
884 return -1;
885 }
886 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
887 }
888 else
889 {
890 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
891 }
892 attrs->rptp_server = strdup((char *) inet_ntoa(s.sin_addr));
893 break;
894 }
895
896 case RPLAY_RPTP_SERVER_PORT:
897 attrs->rptp_server_port = (unsigned short) va_arg(args, long);
898 break;
899
900 case RPLAY_RPTP_SEARCH:
901 attrs->rptp_search = va_arg(args, long);
902 break;
903
904 case RPLAY_SAMPLE_RATE:
905 attrs->sample_rate = va_arg(args, unsigned long);
906 break;
907
908 case RPLAY_CLIENT_DATA:
909 if (*attrs->client_data)
910 {
911 free((char *) attrs->client_data);
912 }
913 attrs->client_data = strdup(va_arg(args, char *));
914 break;
915
916 default:
917 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
918 return -1;
919 }
920 }
921 }
922
923 return rplay_pack(rp);
924 }
925
926 /*
927 * retrieve rplay attributes from the given rplay object
928 */
929 #ifdef __STDC__
930 long
931 rplay_get(RPLAY *rp,...)
932 #else
933 long
934 rplay_get(va_alist)
935 va_dcl
936 #endif
937 {
938 va_list args;
939 RPLAY_ATTRS *attrs;
940 int get, index;
941
942 #ifdef __STDC__
943 va_start(args, rp);
944 #else
945 RPLAY *rp;
946 va_start(args);
947 rp = va_arg(args, RPLAY *);
948 #endif
949 rplay_errno = RPLAY_ERROR_NONE;
950
951 get = va_arg(args, long);
952
953 switch (get)
954 {
955 case RPLAY_NSOUNDS:
956 return rp->nsounds;
957
958 case RPLAY_COMMAND:
959 return rp->command;
960
961 case RPLAY_LIST_COUNT:
962 return rp->count;
963
964 case RPLAY_LIST_NAME:
965 return (long) rp->list_name;
966
967 case RPLAY_PRIORITY:
968 return rp->priority;
969
970 case RPLAY_RANDOM_SOUND:
971 return rp->random_sound;
972
973 case RPLAY_ID:
974 return rp->id;
975
976 case RPLAY_SEQUENCE:
977 return rp->sequence;
978
979 case RPLAY_DATA_SIZE:
980 return rp->data_size;
981
982 case RPLAY_DATA:
983 return (long) rp->data;
984
985 case RPLAY_SOUND:
986 index = va_arg(args, long);
987 attrs = get_attrs(rp->attrs, index);
988 if (attrs == NULL)
989 {
990 rplay_errno = RPLAY_ERROR_INDEX;
991 return -1;
992 }
993 return (long) attrs->sound;
994
995 case RPLAY_VOLUME:
996 index = va_arg(args, long);
997 attrs = get_attrs(rp->attrs, index);
998 if (attrs == NULL)
999 {
1000 rplay_errno = RPLAY_ERROR_INDEX;
1001 return -1;
1002 }
1003 return attrs->volume;
1004
1005 case RPLAY_COUNT:
1006 index = va_arg(args, long);
1007 attrs = get_attrs(rp->attrs, index);
1008 if (attrs == NULL)
1009 {
1010 rplay_errno = RPLAY_ERROR_INDEX;
1011 return -1;
1012 }
1013 return attrs->count;
1014
1015 case RPLAY_RPTP_SERVER:
1016 index = va_arg(args, long);
1017 attrs = get_attrs(rp->attrs, index);
1018 if (attrs == NULL)
1019 {
1020 rplay_errno = RPLAY_ERROR_INDEX;
1021 return -1;
1022 }
1023 return (long) attrs->rptp_server;
1024
1025 case RPLAY_RPTP_SERVER_PORT:
1026 index = va_arg(args, long);
1027 attrs = get_attrs(rp->attrs, index);
1028 if (attrs == NULL)
1029 {
1030 rplay_errno = RPLAY_ERROR_INDEX;
1031 return -1;
1032 }
1033 return (long) attrs->rptp_server_port;
1034
1035 case RPLAY_RPTP_SEARCH:
1036 index = va_arg(args, long);
1037 attrs = get_attrs(rp->attrs, index);
1038 if (attrs == NULL)
1039 {
1040 rplay_errno = RPLAY_ERROR_INDEX;
1041 return -1;
1042 }
1043 return attrs->rptp_search;
1044
1045 case RPLAY_SAMPLE_RATE:
1046 index = va_arg(args, long);
1047 attrs = get_attrs(rp->attrs, index);
1048 if (attrs == NULL)
1049 {
1050 rplay_errno = RPLAY_ERROR_INDEX;
1051 return -1;
1052 }
1053 return attrs->sample_rate;
1054
1055 case RPLAY_CLIENT_DATA:
1056 index = va_arg(args, int);
1057 attrs = get_attrs(rp->attrs, index);
1058 if (attrs == NULL)
1059 {
1060 rplay_errno = RPLAY_ERROR_INDEX;
1061 return -1;
1062 }
1063 return (long) attrs->client_data;
1064
1065 default:
1066 rplay_errno = RPLAY_ERROR_ATTRIBUTE;
1067 return -1;
1068 }
1069 }
1070
1071 /*
1072 * free memory used by the given rplay object
1073 */
1074 #ifdef __STDC__
1075 void
1076 rplay_destroy(RPLAY *rp)
1077 #else
1078 void
1079 rplay_destroy(rp)
1080 RPLAY *rp;
1081 #endif
1082 {
1083 RPLAY_ATTRS *p, *q;
1084
1085 rplay_errno = RPLAY_ERROR_NONE;
1086
1087 for (p = rp->attrs; p; q = p, p = p->next, rplay_attrs_destroy(q)) ;
1088 if (*rp->list_name)
1089 {
1090 free((char *) rp->list_name);
1091 }
1092 if (rp->data)
1093 {
1094 free((char *) rp->data);
1095 }
1096 free((char *) rp->buf);
1097 free((char *) rp);
1098 }
1099
1100 /*
1101 * support routine to convert rplay 2.0 packets to rplay 3.0
1102 */
1103 #ifdef OLD_RPLAY
1104 #ifdef __STDC__
1105 char *
1106 rplay_convert(char *p)
1107 #else
1108 char *
1109 rplay_convert(p)
1110 char *p;
1111 #endif
1112 {
1113 static char buf[MAX_PACKET];
1114 char *q = buf;
1115 int len;
1116
1117 *q++ = RPLAY_PACKET_ID;
1118 switch (*p++)
1119 {
1120 case OLD_RPLAY_PLAY:
1121 *q++ = RPLAY_PLAY;
1122 break;
1123
1124 case OLD_RPLAY_STOP:
1125 *q++ = RPLAY_STOP;
1126 break;
1127
1128 case OLD_RPLAY_PAUSE:
1129 *q++ = RPLAY_PAUSE;
1130 break;
1131
1132 case OLD_RPLAY_CONTINUE:
1133 *q++ = RPLAY_CONTINUE;
1134 break;
1135 }
1136
1137 do
1138 {
1139 *q++ = RPLAY_SOUND;
1140 strcpy(q, p);
1141 len = strlen(p) + 1;
1142 p += len;
1143 q += len;
1144 *q++ = RPLAY_VOLUME;
1145 *q++ = *p++;
1146 *q++ = RPLAY_NULL;
1147 }
1148 while (*p);
1149
1150 *q++ = RPLAY_NULL;
1151
1152 return buf;
1153 }
1154 #endif /* OLD_RPLAY */
1155
1156 static int
1157 default_rplay_port()
1158 {
1159 struct servent *sp;
1160 int port;
1161
1162 sp = getservbyname("rplay", "udp");
1163 if (sp)
1164 {
1165 port = ntohs(sp->s_port); /* htons is used later */
1166 }
1167 else
1168 {
1169 port = RPLAY_PORT;
1170 }
1171
1172 return port;
1173 }
1174
1175 #ifdef __STDC__
1176 int
1177 rplay_open(char *host)
1178 #else
1179 int
1180 rplay_open(host)
1181 char *host;
1182 #endif
1183 {
1184 int port;
1185
1186 port = default_rplay_port();
1187
1188 return rplay_open_port(host, port);
1189 }
1190
1191 /*
1192 * Open a UDP socket for the given host and port.
1193 *
1194 * The host can be either a name or an IP address.
1195 */
1196 #ifdef __STDC__
1197 int
1198 rplay_open_port(char *host, int port)
1199 #else
1200 int
1201 rplay_open_port(host, port)
1202 char *host;
1203 int port;
1204 #endif
1205 {
1206 struct hostent *hp;
1207 u_long addr;
1208 struct sockaddr_in s;
1209
1210 rplay_errno = RPLAY_ERROR_NONE;
1211
1212 memset((char *) &s, 0, sizeof(s));
1213
1214 addr = inet_addr(host);
1215 if (addr == 0xffffffff)
1216 {
1217 hp = gethostbyname(host);
1218 if (hp == NULL)
1219 {
1220 rplay_errno = RPLAY_ERROR_HOST;
1221 return -1;
1222 }
1223 memcpy((char *) &s.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
1224 }
1225 else
1226 {
1227 memcpy((char *) &s.sin_addr.s_addr, (char *) &addr, sizeof(addr));
1228 }
1229
1230 s.sin_port = htons(port);
1231 s.sin_family = AF_INET;
1232
1233 return rplay_open_sockaddr_in(&s);
1234 }
1235
1236 #ifdef __STDC__
1237 int
1238 rplay_open_sockaddr_in(struct sockaddr_in *saddr)
1239 #else
1240 int
1241 rplay_open_sockaddr_in(saddr)
1242 struct sockaddr_in *saddr;
1243 #endif
1244 {
1245 int rplay_fd;
1246 int on = 1;
1247
1248 rplay_fd = socket(AF_INET, SOCK_DGRAM, 0);
1249 if (rplay_fd < 0)
1250 {
1251 rplay_errno = RPLAY_ERROR_SOCKET;
1252 return -1;
1253 }
1254
1255 /*
1256 * enable broadcasting
1257 */
1258 if (setsockopt(rplay_fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on)) < 0)
1259 {
1260 rplay_errno = RPLAY_ERROR_BROADCAST;
1261 return -1;
1262 }
1263
1264 if (connect(rplay_fd, (struct sockaddr *) saddr, sizeof(*saddr)) < 0)
1265 {
1266 rplay_errno = RPLAY_ERROR_CONNECT;
1267 return -1;
1268 }
1269
1270 return rplay_fd;
1271 }
1272
1273 /*
1274 * send an rplay packet to a host
1275 */
1276 #ifdef __STDC__
1277 int
1278 rplay(int rplay_fd, RPLAY *rp)
1279 #else
1280 int
1281 rplay(rplay_fd, rp)
1282 int rplay_fd;
1283 RPLAY *rp;
1284 #endif
1285 {
1286 rplay_errno = RPLAY_ERROR_NONE;
1287
1288 if (write(rplay_fd, rp->buf, rp->len) != rp->len)
1289 {
1290 rplay_errno = RPLAY_ERROR_WRITE;
1291 return -1;
1292 }
1293
1294 return 0;
1295 }
1296
1297 /*
1298 * close the socket
1299 */
1300 #ifdef __STDC__
1301 int
1302 rplay_close(int rplay_fd)
1303 #else
1304 int
1305 rplay_close(rplay_fd)
1306 int rplay_fd;
1307 #endif
1308 {
1309 rplay_errno = RPLAY_ERROR_NONE;
1310
1311 if (close(rplay_fd) < 0)
1312 {
1313 rplay_errno = RPLAY_ERROR_CLOSE;
1314 return -1;
1315 }
1316
1317 return 0;
1318 }
1319
1320 /*
1321 * report rplay error messages
1322 */
1323 #ifdef __STDC__
1324 void
1325 rplay_perror(char *s)
1326 #else
1327 void
1328 rplay_perror(s)
1329 char *s;
1330 #endif
1331 {
1332 fprintf(stderr, "%s: %s\n", s, rplay_errlist[rplay_errno]);
1333 }
1334
1335 /*
1336 * open a socket using the current X display
1337 * (uses the DISPLAY environment variable)
1338 */
1339 #ifdef __STDC__
1340 int
1341 rplay_open_display(void)
1342 #else
1343 int
1344 rplay_open_display()
1345 #endif
1346 {
1347 char *display, *p;
1348 char host[MAXHOSTNAMELEN];
1349
1350 display = getenv("DISPLAY");
1351 if (display == NULL || display[0] == ':')
1352 {
1353 strcpy(host, "localhost");
1354 }
1355 else
1356 {
1357 strcpy(host, display);
1358 p = strchr(host, ':');
1359 if (p)
1360 {
1361 *p = '\0';
1362 }
1363 if (strcmp(host, "unix") == 0 || strcmp(host, "local") == 0 || strcmp(host, "X") == 0)
1364 {
1365 strcpy(host, "localhost");
1366 }
1367 }
1368
1369 return rplay_open(host);
1370 }
1371
1372 /*
1373 * play a sound on the current X display
1374 */
1375 #ifdef __STDC__
1376 int
1377 rplay_display(char *sound)
1378 #else
1379 int
1380 rplay_display(sound)
1381 char *sound;
1382 #endif
1383 {
1384 int rplay_fd;
1385
1386 rplay_fd = rplay_open_display();
1387 if (rplay_fd < 0)
1388 {
1389 return -1;
1390 }
1391
1392 return rplay_sound(rplay_fd, sound);
1393 }
1394
1395 /*
1396 * play a sound on the localhost
1397 */
1398 #ifdef __STDC__
1399 int
1400 rplay_local(char *sound)
1401 #else
1402 int
1403 rplay_local(sound)
1404 char *sound;
1405 #endif
1406 {
1407 return rplay_host("localhost", sound);
1408 }
1409
1410 /*
1411 * play a sound on a host
1412 */
1413 #ifdef __STDC__
1414 int
1415 rplay_host(char *host, char *sound)
1416 #else
1417 int
1418 rplay_host(host, sound)
1419 char *host;
1420 char *sound;
1421 #endif
1422 {
1423 int rplay_fd;
1424
1425 rplay_fd = rplay_open(host);
1426 if (rplay_fd < 0)
1427 {
1428 return -1;
1429 }
1430
1431 return rplay_sound(rplay_fd, sound);
1432 }
1433
1434 /*
1435 * play a sound given a socket
1436 */
1437 #ifdef __STDC__
1438 int
1439 rplay_sound(int rplay_fd, char *sound)
1440 #else
1441 int
1442 rplay_sound(rplay_fd, sound)
1443 int rplay_fd;
1444 char *sound;
1445 #endif
1446 {
1447 RPLAY *rp;
1448
1449 rp = rplay_create(RPLAY_PLAY);
1450 if (rp == NULL)
1451 {
1452 return -1;
1453 }
1454
1455 if (rplay_set(rp, RPLAY_APPEND, RPLAY_SOUND, sound, NULL) < 0)
1456 {
1457 return -1;
1458 }
1459
1460 if (rplay(rplay_fd, rp) < 0)
1461 {
1462 return -1;
1463 }
1464
1465 rplay_destroy(rp);
1466
1467 return 0;
1468 }
1469
1470 /*
1471 * ping an rplay server
1472 */
1473 #ifdef __STDC__
1474 int
1475 rplay_ping(char *host)
1476 #else
1477 int
1478 rplay_ping(host)
1479 char *host;
1480 #endif
1481 {
1482 int rplay_fd;
1483 int port;
1484 int error1 = 0, error2 = 0;
1485
1486 /* Ping the default port. */
1487 port = default_rplay_port();
1488 rplay_fd = rplay_open_port(host, port);
1489 if (rplay_fd < 0)
1490 {
1491 return -1;
1492 }
1493 error1 = rplay_ping_sockfd(rplay_fd);
1494
1495 #ifdef OTHER_RPLAY_PORTS
1496 /* Pick an alternative port. */
1497 if (port == RPLAY_PORT)
1498 {
1499 port = OLD_RPLAY_PORT;
1500 }
1501 else
1502 {
1503 port = RPLAY_PORT;
1504 }
1505
1506 /* Ping the alternative port. */
1507 rplay_fd = rplay_open_port(host, port);
1508 if (rplay_fd < 0)
1509 {
1510 return -1;
1511 }
1512 error2 = rplay_ping_sockfd(rplay_fd);
1513 #endif /* OTHER_RPLAY_PORTS */
1514
1515 /* Only return -1 if both pings fail. */
1516 if (error1 < 0 && error2 < 0)
1517 {
1518 return -1;
1519 }
1520 else
1521 {
1522 return 0;
1523 }
1524 }
1525
1526 #ifdef __STDC__
1527 int
1528 rplay_ping_sockfd(int rplay_fd)
1529 #else
1530 int
1531 rplay_ping_sockfd(rplay_fd)
1532 int rplay_fd;
1533 #endif
1534 {
1535 RPLAY *rp;
1536
1537 rp = rplay_create(RPLAY_PING);
1538 if (rp == NULL)
1539 {
1540 return -1;
1541 }
1542
1543 if (rplay_pack(rp) < 0)
1544 {
1545 return -1;
1546 }
1547
1548 if (rplay(rplay_fd, rp) < 0)
1549 {
1550 return -1;
1551 }
1552
1553 rplay_close(rplay_fd);
1554 rplay_destroy(rp);
1555
1556 return 0;
1557 }
1558
1559 #ifdef __STDC__
1560 int
1561 rplay_ping_sockaddr_in(struct sockaddr_in *saddr)
1562 #else
1563 int
1564 rplay_ping_sockaddr_in(saddr)
1565 struct sockaddr_in *saddr;
1566 #endif
1567 {
1568 int rplay_fd;
1569
1570 rplay_fd = rplay_open_sockaddr_in(saddr);
1571 if (rplay_fd < 0)
1572 {
1573 return -1;
1574 }
1575
1576 return rplay_ping_sockfd(rplay_fd);
1577 }
1578
1579 /*
1580 * play a sound on a host with a specific volume
1581 */
1582 #ifdef __STDC__
1583 int
1584 rplay_host_volume(char *host, char *sound, int volume)
1585 #else
1586 int
1587 rplay_host_volume(host, sound, volume)
1588 char *host;
1589 char *sound;
1590 int volume;
1591 #endif
1592 {
1593 int rplay_fd;
1594 RPLAY *rp;
1595
1596 rplay_fd = rplay_open(host);
1597 if (rplay_fd < 0)
1598 {
1599 return -1;
1600 }
1601
1602 rp = rplay_create(RPLAY_PLAY);
1603 if (rp == NULL)
1604 {
1605 return -1;
1606 }
1607
1608 if (rplay_set(rp, RPLAY_APPEND, RPLAY_SOUND, sound, RPLAY_VOLUME, volume, NULL) < 0)
1609 {
1610 return -1;
1611 }
1612
1613 if (rplay(rplay_fd, rp) < 0)
1614 {
1615 return -1;
1616 }
1617
1618 rplay_destroy(rp);
1619
1620 return 0;
1621 }
1622
1623 /*
1624 * obtain the default rplay host
1625 */
1626 #ifdef __STDC__
1627 char *
1628 rplay_default_host(void)
1629 #else
1630 char *
1631 rplay_default_host()
1632 #endif
1633 {
1634 char *host;
1635
1636 host = getenv("RPLAY_HOST");
1637
1638 return host ? host : "localhost";
1639 }
1640
1641 /*
1642 * play a sound on the default rplay host
1643 */
1644 #ifdef __STDC__
1645 int
1646 rplay_default(char *sound)
1647 #else
1648 int
1649 rplay_default(sound)
1650 char *sound;
1651 #endif
1652 {
1653 return rplay_host(rplay_default_host(), sound);
1654 }
1655
1656 /*
1657 * open the default host
1658 */
1659 #ifdef __STDC__
1660 int
1661 rplay_open_default(void)
1662 #else
1663 int
1664 rplay_open_default()
1665 #endif
1666 {
1667 return rplay_open(rplay_default_host());
1668 }