Debian Codegraph @

librplay/rplay.c

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