summaryrefslogtreecommitdiff
path: root/proto.c
blob: 3fb53adad923d38e4096436e62e231bf5fbfcbae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

#include "esd-server.h"
#include <errno.h>

/*******************************************************************/
/* globals */

int esd_forced_standby = 0;	/* whether we're forcing the standby */

/*******************************************************************/
/* prototypes */
int esd_check_endian( esd_client_t *client, unsigned int *endian );
int esd_validate_source( esd_client_t *client, 
			 octet *submitted_key, int owner_only );
int esd_proto_unimplemented( esd_client_t *client );
int esd_proto_connect( esd_client_t *client );
int esd_proto_lock( esd_client_t *client );
int esd_proto_unlock( esd_client_t *client );
int esd_proto_standby( esd_client_t *client );
int esd_proto_resume( esd_client_t *client );
int esd_proto_stream_play( esd_client_t *client );
int esd_proto_stream_recorder( esd_client_t *client );
int esd_proto_stream_monitor( esd_client_t *client );
int esd_proto_stream_filter( esd_client_t *client );
int esd_proto_sample_cache( esd_client_t *client );
int esd_proto_sample_getid(esd_client_t *client);
int esd_proto_sample_free( esd_client_t *client );
int esd_proto_sample_play( esd_client_t *client );
int esd_proto_sample_loop( esd_client_t *client );
int esd_proto_sample_stop( esd_client_t *client );
int esd_proto_server_info( esd_client_t *client );
int esd_proto_all_info( esd_client_t *client );
int esd_proto_stream_pan( esd_client_t *client );
int esd_proto_sample_pan( esd_client_t *client );
int esd_proto_standby_mode( esd_client_t *client );
int esd_proto_get_latency( esd_client_t *client );
int poll_client_requests(void);

/*******************************************************************/
/* protocol handlers as function pointers: synch with esd_proto_t enum */
typedef int esd_proto_handler_t( esd_client_t *client );

/* data type to hold the per protocol handler info */
typedef struct esd_proto_handler_info {
    int 		data_length;
    esd_proto_handler_t *handler;
    const char *        description;
} esd_proto_handler_info_t;

/* the big map of protocol handler info */
esd_proto_handler_info_t esd_proto_map[ ESD_PROTO_MAX ] = 
{
    { ESD_KEY_LEN + sizeof(int), &esd_proto_connect, "connect" },
    { ESD_KEY_LEN + sizeof(int), &esd_proto_lock, "lock" },
    { ESD_KEY_LEN + sizeof(int), &esd_proto_unlock, "unlock" },

    { ESD_NAME_MAX + 2 * sizeof(int), &esd_proto_stream_play, "stream play" },
    { ESD_NAME_MAX + 2 * sizeof(int), &esd_proto_stream_recorder, "stream rec" },
    { ESD_NAME_MAX + 2 * sizeof(int), &esd_proto_stream_monitor, "stream mon" },

    { ESD_NAME_MAX + 3 * sizeof(int), &esd_proto_sample_cache, "sample cache" },
    { sizeof(int), &esd_proto_sample_free, "sample free" },
    { sizeof(int), &esd_proto_sample_play, "sample play" },
    { sizeof(int), &esd_proto_sample_loop, "sample loop" },
    { sizeof(int), &esd_proto_sample_stop, "sample stop" },
    { -1, &esd_proto_unimplemented, "TODO: sample kill" },

    { ESD_KEY_LEN + sizeof(int), &esd_proto_standby, "standby" },
    { ESD_KEY_LEN + sizeof(int), &esd_proto_resume, "resume" },

    { ESD_NAME_MAX, &esd_proto_sample_getid, "sample getid" },
    { ESD_NAME_MAX + 2 * sizeof(int), &esd_proto_stream_filter, "stream filter" },

    { sizeof(int), &esd_proto_server_info, "server info" },
    { sizeof(int), &esd_proto_all_info, "all info" },
    { -1, &esd_proto_unimplemented, "TODO: subscribe" },
    { -1, &esd_proto_unimplemented, "TODO: unsubscribe" },

    { 3 * sizeof(int), &esd_proto_stream_pan, "stream pan"},
    { 3 * sizeof(int), &esd_proto_sample_pan, "sample pan" },

    { sizeof(int), &esd_proto_standby_mode, "standby mode" },
    { 0, &esd_proto_get_latency, "get latency" }
};

/***********************************************************************/
/* returns the latency between audio stream data being write() 'en to  */
/* esd and when it finally comes out the speakers of your audio system */
int esd_proto_get_latency( esd_client_t *client )
{
    int lag, amount, actual;
    int buf_size = esound_getblksize();

    ESDBG_TRACE( printf( "(%02d) proto: get latency\n", client->fd ); );
  
    if (esd_audio_format & ESD_STEREO)
      {
	if (esd_audio_format & ESD_BITS16)
	  amount = (44100 * (buf_size + 64)) / esd_audio_rate;
	else
	  amount = (44100 * (buf_size + 128)) / esd_audio_rate;
      }
    else
      {
	if (esd_audio_format & ESD_BITS16)
	  amount = (2 * 44100 * (buf_size + 128)) / esd_audio_rate;
	else
	  amount = (2 * 44100 * (buf_size + 256)) / esd_audio_rate;
      }
    lag = maybe_swap_32( client->swap_byte_order, amount );

    /* send back the server information */
    ESD_WRITE_INT( client->fd, &lag, sizeof(lag), actual, "lag buf" );
    if ( sizeof( lag ) != actual )
	return 0;
    return 1;
}

/*******************************************************************/
/* checks for client/server endianness */
int esd_check_endian( esd_client_t *client, unsigned int *endian )
{
    if ( *endian == ESD_ENDIAN_KEY ) {
	ESDBG_TRACE( printf( "(%02d) same endian order.\n", client->fd ); );
	client->swap_byte_order = 0;
    } else if ( *endian == ESD_SWAP_ENDIAN_KEY ) {
	ESDBG_TRACE( printf( "(%02d) different endian order!\n", client->fd ); );
	client->swap_byte_order = 1;
    } else {
	ESDBG_TRACE( 
	    printf( "(%02d) unknown endian key: 0x%08x"
		    " (same = 0x%08x, diff = 0x%08x)\n",
		    client->fd, *endian, 
		    ESD_ENDIAN_KEY, ESD_SWAP_ENDIAN_KEY ); );
	return 0;
    }

    /* now we're done */
    return 1;
}

/*******************************************************************/
/* checks for authorization to use the sound daemon */
int esd_validate_source( esd_client_t *client, 
			 octet *submitted_key,
			 int owner_only )
{
  int ok;
    if( !esd_is_owned ) {
	/* noone owns it yet, the first client claims ownership */
	ESDBG_TRACE( printf( "(%02d) esd auth: claiming ownership of esd, auth ok\n", 
			     client->fd ); );

	esd_is_locked = 1;
	memcpy( esd_owner_key, submitted_key, ESD_KEY_LEN );
	esd_is_owned = 1;
        ok = 1;
        write(client->fd, &ok, sizeof(ok));
	return 1;
    }

    if( !esd_is_locked && !owner_only ) {
	/* anyone can connect to it */
	ESDBG_TRACE( printf( "(%02d) esd auth: not locked nor owner only, auth ok.\n", 
			     client->fd ); );
        ok = 1;
        write(client->fd, &ok, sizeof(ok));
	return 1;
    }

    if( !memcmp( esd_owner_key, submitted_key, ESD_KEY_LEN ) ) {
	/* the client key matches the owner, trivial acceptance */
	ESDBG_TRACE( printf( "(%02d) esd auth: key matches, auth ok.\n", 
			     client->fd ); );
        ok = 1;
        write(client->fd, &ok, sizeof(ok));
	return 1;
    }

    /* TODO: maybe check based on source ip? */ 
    /* done with LIBWRAP when client first connects to daemon */
    /* if ( !owner_only ) { check_ip_etc( client->source ); } */

    /* the client is not authorized to connect to the server */
    ESDBG_TRACE( printf( "(%02d) esd auth: NOT authorized to use esd, closing conn.\n",
			 client->fd ); );
    ok = 0;
    write(client->fd, &ok, sizeof(ok));
    return 0;
}

/*******************************************************************/
/* place holder during development */
int esd_proto_unimplemented( esd_client_t *client )
{
    fprintf( stderr, 
	     "(%02d) proto: unimplemented protocol request:  0x%08x\n",
	     client->fd, client->request );
    return 0;
}

/*******************************************************************/
/* initial connection handler, return boolean ok */
int esd_proto_connect( esd_client_t *client )
{
    int ok;

    if ( esd_validate_source( client, client->proto_data, 0 ) )
    {
	ok = esd_check_endian( client, (unsigned int*)(client->proto_data + ESD_KEY_LEN) );
    } else {
	ok = 0;
    }
    
    ESDBG_TRACE( printf( "(%02d) connecting to sound daemon = %d\n", 
			 client->fd, ok ); );
    return ok;
}

/*******************************************************************/
/* daemon rejects untrusted clients, return boolean ok */
int esd_proto_lock( esd_client_t *client )
{
    int ok, client_ok, actual;

    ok = esd_validate_source( client, client->proto_data, 1 );
    client_ok = maybe_swap_32( client->swap_byte_order, ok );

    if ( ok ) {
	ESDBG_TRACE( printf( "(%02d) locking sound daemon\n", client->fd ); );
	esd_is_locked = 1;
    }

    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "lock ok" );
    
    return ok;
}

/*******************************************************************/
/* allows anyone to connect to the sound daemon, return boolean ok */
int esd_proto_unlock( esd_client_t *client )
{
    int ok, client_ok, actual;

    ok = esd_validate_source( client, client->proto_data, 1 );
    client_ok = maybe_swap_32( client->swap_byte_order, ok );

    if ( ok ) {
	ESDBG_TRACE( printf( "(%02d) unlocking sound daemon\n", client->fd ); );
	esd_is_locked = 0;
    }

    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "unlock ok" );
    return ok;
}

/*******************************************************************/
/* daemon eats sound data, without playing anything, return boolean ok */
int esd_proto_standby( esd_client_t *client )
{
    int ok, client_ok, actual;

    ok = esd_validate_source( client, client->proto_data, 1 );

    if ( ok ) ok = esd_server_standby();
    if(!esd_on_autostandby)
	    esd_forced_standby = 1;

    client_ok = maybe_swap_32( client->swap_byte_order, ok );
    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "stdby ok" );
    return ok;
}

/*******************************************************************/
/* daemon eats sound data, without playing anything, return boolean ok */
int esd_proto_resume( esd_client_t *client )
{
    int ok, client_ok, actual;

    ok = esd_validate_source( client, client->proto_data, 1 );

    if ( ok ) ok = esd_server_resume();
    if ( ok ) esd_forced_standby = 0;

    client_ok = maybe_swap_32( client->swap_byte_order, ok );
    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "resum ok" );
    return ok;
}

/*******************************************************************/
/* add another stream player to the list, returns boolean ok */
int esd_proto_stream_play( esd_client_t *client )
{
    /* spawn a new player to handle this stream */
    esd_player_t *player = NULL;
    player = new_stream_player( client );
    
    /* we got one, right? */
    if ( !player ) {
	return 0;
    }
    
    /* add to the list of players */
    player->parent = client;
    add_player( player );

    client->state = ESD_STREAMING_DATA;
    return 1;
}

/*******************************************************************/
/* manage recorder clients, return boolean ok */
int esd_proto_stream_recorder( esd_client_t *client )
{
    esd_player_t *recorder = NULL;
    
    /* wake up if we're asleep */
    if ( esd_on_autostandby  && !esd_forced_standby ) {
	ESDBG_TRACE( printf( "stuff to record, waking up.\n" ); );
	esd_server_resume();
    }

    /* if we're in standby mode, go away */
    if ( esd_on_standby )
	return 0;

    /* sign up the new recorder client */
    recorder = new_stream_player( client );
    /* we got one, right? */
    if ( !recorder )
	return 0;
    
    if (!(esd_audio_format & ESD_RECORD)) {
	/* let the device know we want to record */
	ESDBG_TRACE( printf( "closing audio for a sec...\n" ); );
	esd_audio_close();
	usleep(100);
	esd_audio_format |= ESD_RECORD;
	ESDBG_TRACE( printf( "reopening audio to record...\n" ); );
	if (esd_audio_open() < 0) {
            /* Failed to record */
            free_player( recorder );
            esd_audio_format &= ~ESD_RECORD;
            usleep(100);
            /* If we fail here, we have a oops */
            esd_audio_open();
            return 0;
        }
	ESDBG_TRACE( printf( "reopened?\n" ); );
    }
    
    add_recorder(recorder);
    
    /* flesh out the recorder */
    recorder->parent = client;
    recorder->translate_func 
	    = get_translate_func( esd_audio_format, esd_audio_rate,
				  recorder->format, recorder->rate );

    ESDBG_TRACE( printf ( "(%02d) recording on client\n", client->fd ); );
    
    client->state = ESD_STREAMING_DATA;
    return 1;
}

/*******************************************************************/
/* manage the single monitoring client, return boolean ok */
int esd_proto_stream_monitor( esd_client_t *client )
{
    esd_player_t *monitor;

    /* sign up the new monitor client */
    monitor = new_stream_player( client );
    if ( monitor != NULL ) {
	/* flesh out the monitor */
	monitor->parent = client;
	monitor->next = esd_monitor_list;
	esd_monitor_list = monitor;

	monitor->translate_func 
	    = get_translate_func( esd_audio_format, esd_audio_rate,
				  monitor->format, monitor->rate );

	ESDBG_TRACE( printf ( "(%02d) monitoring on client\n", client->fd ); );
    } else {
	/* failed to initialize the recorder, kill its client */
	return 0;
    }

    client->state = ESD_STREAMING_DATA;
    return 1;
}

/*******************************************************************/
/* manage the filter client, return boolean ok */
int esd_proto_stream_filter( esd_client_t *client )
{
    esd_player_t *filter;
    
    /* sign up the new filter client */
    filter = new_stream_player( client );
    if ( filter != NULL ) {
	/* flesh out the filter */
	filter->parent = client;
	filter->next = esd_filter_list;
	esd_filter_list = filter;

	ESDBG_TRACE( printf ( "(%02d) filter on client\n", client->fd ); );
    } else {
	/* failed to initialize the filter, kill its client */
	return 0;
    }

    client->state = ESD_STREAMING_DATA;
    return 1;
}

/*******************************************************************/
/* cache a sample from the client, return boolean ok  */
int esd_proto_sample_cache( esd_client_t *client )
{
    esd_sample_t *sample;
    int length;
    int client_id;

    ESDBG_TRACE( printf( "(%02d) proto: caching sample\n", client->fd ); );

    if ( client->state == ESD_CACHING_SAMPLE ) {
	sample = find_caching_sample( client );
    } else {
	sample = new_sample( client );
	add_sample( sample );
    }

    /* add to the list of sample */
    if ( sample != NULL ) {
	sample->parent = client;
	if ( !read_sample( sample ) )
	{
	    return 0;	/* something failed during the read, just bail */
	}

	if ( sample->cached_length < sample->sample_length ) {
	    client->state = ESD_CACHING_SAMPLE;
	    ESDBG_TRACE( printf( "(%02d) continue caching sample next trip\n", 
				 client->fd ); );
	    return 1;
	} else {
	    ESDBG_TRACE( printf( "(%02d) sample cached, moving on\n", 
				 client->fd ); );
	    client->state = ESD_NEXT_REQUEST;
	}
    } else {
	fprintf( stderr, "(%02d) not enough mem for sample, closing\n", 
		 client->fd );
	return 0;
    }

    client_id = maybe_swap_32( client->swap_byte_order, 
			       sample->sample_id );
    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   length, "smp cach" );

    return 1;
}

/*******************************************************************/
/* check for an existing sample name */
int esd_proto_sample_getid(esd_client_t *client)
{
    int client_id, length;
    esd_sample_t *sample = esd_samples_list;
    char namebuf[ESD_NAME_MAX];

    strncpy( namebuf, client->proto_data, ESD_NAME_MAX );
    namebuf[ESD_NAME_MAX - 1] = '\0';

    ESDBG_TRACE( printf( "(%02d) proto: getting sample ID: %s\n", 
			 client->fd, namebuf ); );

    while(sample) {
	if(!strcmp(sample->name, namebuf))
	    break;
	sample = sample->next;
    }

    if(sample)
	client_id = maybe_swap_32( client->swap_byte_order, 
				   sample->sample_id );
    else
	client_id = maybe_swap_32(client->swap_byte_order, -1);

    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   length, "smp getid" );
    return 1;
}


/*******************************************************************/
/* free a sample cached by the client, return boolean ok */
int esd_proto_sample_free( esd_client_t *client )
{
    int sample_id, client_id, actual;

    client_id = *(int*)(client->proto_data);
    sample_id = maybe_swap_32( client->swap_byte_order, client_id );

    ESDBG_TRACE( printf( "(%02d) proto: erasing sample <%d>\n", 
			 client->fd, sample_id ); );

    erase_sample( sample_id, 0 );

    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   actual, "smp free" );
    if ( sizeof( client_id ) != actual )
	return 0;

    return 1;
}

/*******************************************************************/
/* play a sample cached by the client, return boolean ok */
int esd_proto_sample_play( esd_client_t *client )
{
    int sample_id, client_id, actual;
    
    client_id = *(int*)(client->proto_data);
    sample_id = maybe_swap_32( client->swap_byte_order, client_id );

    ESDBG_TRACE( printf( "(%02d) proto: playing sample <%d>\n", 
			 client->fd, sample_id ); );

    if ( !play_sample( sample_id, 0 ) )
	sample_id = 0;

    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   actual, "smp play" );
    if ( sizeof( client_id ) != actual )
	return 0;

    return 1;
}

/*******************************************************************/
/* play a sample cached by the client, return boolean ok */
int esd_proto_sample_loop( esd_client_t *client )
{
    int sample_id, client_id, actual;

    client_id = *(int*)(client->proto_data);
    sample_id = maybe_swap_32( client->swap_byte_order, client_id );

    ESDBG_TRACE( printf( "(%02d) proto: looping sample <%d>\n", 
			 client->fd, sample_id ); );

    if ( !play_sample( sample_id, 1 ) )
	sample_id = 0;

    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   actual, "smp loop" );
    if ( sizeof( sample_id ) != actual )
	return 0;

    return 1;
}

/*******************************************************************/
/* play a sample cached by the client, return boolean ok */
int esd_proto_sample_stop( esd_client_t *client )
{
    int sample_id, client_id, actual;

    client_id = *(int*)(client->proto_data);
    sample_id = maybe_swap_32( client->swap_byte_order, client_id );

    ESDBG_TRACE( printf( "(%02d) proto: stopping sample <%d>\n", 
			 client->fd, sample_id ); );

    if ( !stop_sample( sample_id ) )
	sample_id = 0;

    ESD_WRITE_INT( client->fd, &client_id, sizeof(client_id), 
		   actual, "smp stop" );
    if ( sizeof( client_id ) != actual )
	return 0;

    return 1;
}

/*******************************************************************/
/* play a sample cached by the client, return boolean ok */
int esd_proto_server_info( esd_client_t *client )
{
    int version, rate, format, actual;

    version = maybe_swap_32( client->swap_byte_order, 0 );
    rate = maybe_swap_32( client->swap_byte_order, esd_audio_rate );
    format = maybe_swap_32( client->swap_byte_order, esd_audio_format );

    ESDBG_TRACE( printf( "(%02d) proto: server info\n", client->fd ); );

    /* send back the server information */
    ESD_WRITE_INT( client->fd, &version, sizeof(version), actual, "si ver" );
    ESD_WRITE_INT( client->fd, &rate, sizeof(rate), actual, "si rate" );
    ESD_WRITE_INT( client->fd, &format, sizeof(format), actual, "si fmt" );
    if ( sizeof( format ) != actual )
	return 0;

    return 1;
}

/*******************************************************************/
/* play a sample cached by the client, return boolean ok */
int esd_proto_all_info( esd_client_t *client )
{
    int version, rate, left, right, format;
    int actual, source_id, sample_id, length;
    esd_player_t *player;
    esd_sample_t *sample;
    const char *name;
    char no_name[ ESD_NAME_MAX ] = "";

    version = maybe_swap_32( client->swap_byte_order, 0 );
    rate = maybe_swap_32( client->swap_byte_order, esd_audio_rate );
    format = maybe_swap_32( client->swap_byte_order, esd_audio_format );

    ESDBG_TRACE( printf( "(%02d) proto: server info\n", client->fd ); );

    /* send back the server information */
    ESD_WRITE_INT( client->fd, &version, sizeof(version), actual, "ai ver" );
    ESD_WRITE_INT( client->fd, &rate, sizeof(rate), actual, "ai rate" );
    ESD_WRITE_INT( client->fd, &format, sizeof(format), actual, "ai fmt" );
    if ( sizeof( format ) != actual )
	return 0;

    /* send back the player information */
    for ( player = esd_players_list; /* NULL breaks */ ; player = player->next )
    {
	if ( player ) {
	    source_id = maybe_swap_32( client->swap_byte_order, 
				       player->source_id );
	    name = ( (player->format & ESD_MASK_MODE) == ESD_STREAM ) 
		? player->name : ( (esd_sample_t*) (player->parent) )->name;
	    rate = maybe_swap_32( client->swap_byte_order, 
				  player->rate );
	    left = maybe_swap_32( client->swap_byte_order, 
				  player->left_vol_scale );
	    right = maybe_swap_32( client->swap_byte_order, 
				   player->right_vol_scale );
	    format = maybe_swap_32( client->swap_byte_order, 
				    player->format );
	} else {
	    source_id = rate = format = 0;
	    name = no_name;
	}

	ESD_WRITE_INT( client->fd, &source_id, sizeof(source_id), 
		       actual, "ai p.id" );
	ESD_WRITE_BIN( client->fd, name, ESD_NAME_MAX, 
		       actual, "ai p.nm" );
	ESD_WRITE_INT( client->fd, &rate, sizeof(rate), 
		       actual, "ai p.rate" );
	ESD_WRITE_INT( client->fd, &left, sizeof(left), 
		       actual, "ai p.lt" );
	ESD_WRITE_INT( client->fd, &right, sizeof(right), 
		       actual, "ai p.rt" );
	ESD_WRITE_INT( client->fd, &format, sizeof(format), 
		       actual, "ai p.fmt" );
	if ( sizeof( format ) != actual )
	    return 0;

	if ( !player ) break;
    }

    /* send back the sample information */
    for ( sample = esd_samples_list; /* NULL breaks */ ; sample = sample->next )
    {
	if ( sample ) {
	    sample_id = maybe_swap_32( client->swap_byte_order, 
				       sample->sample_id );
	    name = sample->name;
	    rate = maybe_swap_32( client->swap_byte_order, 
				  sample->rate );
	    left = maybe_swap_32( client->swap_byte_order, 
				  sample->left_vol_scale );
	    right = maybe_swap_32( client->swap_byte_order, 
				   sample->right_vol_scale );
	    format = maybe_swap_32( client->swap_byte_order, 
				    sample->format );
	    length = maybe_swap_32( client->swap_byte_order, 
				    sample->sample_length );
	} else {
	    sample_id = rate = format = length = 0;
	    name = no_name;
	}

	ESD_WRITE_INT( client->fd, &sample_id, sizeof(sample_id), 
		       actual, "ai s.id" );
	ESD_WRITE_BIN( client->fd, name, ESD_NAME_MAX, 
		       actual, "ai s.nm" );
	ESD_WRITE_INT( client->fd, &rate, sizeof(rate), 
		       actual, "ai s.rate" );
	ESD_WRITE_INT( client->fd, &left, sizeof(left), 
		       actual, "ai s.lt" );
	ESD_WRITE_INT( client->fd, &right, sizeof(right), 
		       actual, "ai s.rt" );
	ESD_WRITE_INT( client->fd, &format, sizeof(format), 
		       actual, "ai s.fmt" );
	ESD_WRITE_INT( client->fd, &length, sizeof(length), 
		       actual, "ai s.fmt" );
	if ( sizeof( length ) != actual )
	    return 0;

	if ( !sample ) break;
    }

    return 1;
}


/*******************************************************************/
/* set the stereo panning for a stream */
int esd_proto_stream_pan( esd_client_t *client )
{
    int client_id, client_left, client_right, client_ok, actual;
    int stream_id, left, right, ok;
    esd_player_t *player;
    
    client_id = *(int*)(client->proto_data);
    client_left = *(int*)(client->proto_data + sizeof(int));
    client_right = *(int*)(client->proto_data + 2 * sizeof(int));

    stream_id = maybe_swap_32( client->swap_byte_order, client_id );
    left = maybe_swap_32( client->swap_byte_order, client_left );
    right = maybe_swap_32( client->swap_byte_order, client_right );

    ESDBG_TRACE( printf( "(%02d) proto: panning stream <%d> [%d, %d]\n", 
			 client->fd, stream_id, left, right ); );

    /* find the stream, and reset panning */
    ok = 0;
    for ( player = esd_players_list ; player != NULL ; player = player->next )
    {
	if ( player->source_id == stream_id 
	     && ( (player->format & ESD_MASK_MODE) == ESD_STREAM ) )
	{
	    player->left_vol_scale = left;
	    player->right_vol_scale = right;
	    player->mix_func = get_mix_func( player );
	    ok = 1;
	    break;
	}
    }

    /* let the client know how it went */
    client_ok = maybe_swap_32( client->swap_byte_order, ok );
    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "panstr ok" );
    if ( sizeof( client_ok ) != actual )
	return 0;

    return 1;
}


/*******************************************************************/
/* set the default panning for a stream */
int esd_proto_sample_pan( esd_client_t *client )
{
    int client_id, client_left, client_right, client_ok, actual;
    int sample_id, left, right, ok;
    esd_sample_t *sample;
    
    client_id = *(int*)(client->proto_data);
    client_left = *(int*)(client->proto_data + sizeof(int));
    client_right = *(int*)(client->proto_data + 2 * sizeof(int));

    sample_id = maybe_swap_32( client->swap_byte_order, client_id );
    left = maybe_swap_32( client->swap_byte_order, client_left );
    right = maybe_swap_32( client->swap_byte_order, client_right );

    ESDBG_TRACE( printf( "(%02d) proto: panning sample <%d> [%d, %d]\n", 
			 client->fd, sample_id, left, right ); );

    /* find the stream, and reset panning */
    ok = 0;
    for ( sample = esd_samples_list ; sample != NULL ; sample = sample->next )
    {
	if ( sample->sample_id == sample_id )
	{
	    sample->left_vol_scale = left;
	    sample->right_vol_scale = right;
	    ok = 1;
	    break;
	}
    }

    /* let the client know how it went */
    client_ok = maybe_swap_32( client->swap_byte_order, ok );
    ESD_WRITE_INT( client->fd, &client_ok, sizeof(client_ok), 
		   actual, "panstr ok" );
    if ( sizeof( client_ok ) != actual )
	return 0;

    return 1;
}


/*******************************************************************/
/* daemon rejects untrusted clients, return boolean ok */
int esd_proto_standby_mode( esd_client_t *client )
{
    int ok = 1, mode, client_mode, actual;

    if ( esd_on_autostandby && !esd_forced_standby ) 
	mode = ESM_ON_AUTOSTANDBY;
    else if ( esd_on_standby )
	mode = ESM_ON_STANDBY;
    else
	mode = ESM_RUNNING;

    client_mode = maybe_swap_32( client->swap_byte_order, mode );
    ESDBG_TRACE( printf( "(%02d) getting standby mode\n", client->fd ); );

    ESD_WRITE_INT( client->fd, &client_mode, sizeof(client_mode), 
		   actual, "stby mode" );
    if ( sizeof( client_mode ) != actual )
	return 0;
    
    return ok;
}


/*******************************************************************/
/* checks for new client requiests - returns 1 */
int poll_client_requests(void)
{
    int can_read, length = 0, is_ok = 0;
    esd_client_t *client = NULL;
    esd_client_t *erase = NULL;
    fd_set rd_fds;
    struct timeval timeout;

    /* check all clients, as some may become readable between the
       previous blocking select() and now */

    /* for each client */
    client = esd_clients_list;
    while ( client != NULL )
    {
	/* if it's a streaming client connection, just skip it 
	   data will be read (if available) during the mix phase */
	if ( client->state == ESD_STREAMING_DATA ) {
	    client = client->next;
	    continue;
	}
	    
	/* find out if this client wants to do anything yet */
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	FD_ZERO( &rd_fds );
	FD_SET( client->fd, &rd_fds );
	can_read = select( client->fd + 1, &rd_fds, NULL, NULL, &timeout );
	if ( !can_read ) {
	    client = client->next;
	    continue;
	}

	/* see what the client needs to do next */
	ESDBG_TRACE( printf( "(%02d) client state %d.\n", 
			     client->fd, client->state ); );
	switch ( client->state ) {

	case ESD_NEEDS_REQDATA:

	    /* check for insanity */
	    if ( client->proto_data_length 
		 > esd_proto_map[ client->request ].data_length )
	    {
		ESDBG_TRACE( 
		    printf( "(%02d) REQDATA insanity detected, expecting %d, got %d\n",
			    client->fd,
			    esd_proto_map[ client->request ].data_length,
			    client->proto_data_length); );
		is_ok = 0;
		break;
	    }

	    /* read another chunk of data, if any more is required */
	    if ( esd_proto_map[ client->request ].data_length ) {
		ESD_READ_BIN( client->fd, 
			      client->proto_data + client->proto_data_length, 
			      esd_proto_map[ client->request ].data_length 
			      - client->proto_data_length , length, "req dat" );
		client->proto_data_length += length;
	    }

	    /* check length, as EOF returns readable */
	    if ( !length 
		 || ( length < 0 && errno != EAGAIN && errno != EINTR ) )
	    {
		ESDBG_TRACE( printf( "(%02d) interrupted request %d, %s.\n", 
				     client->fd, client->request, 
				     esd_proto_map[ client->request ].description ); );
		is_ok = 0;
		break;
	    }

	    /* see if we have it all */
	    if ( client->proto_data_length 
		 == esd_proto_map[ client->request ].data_length )
	    {
		ESDBG_TRACE( printf( "(%02d) handling request %d, %s.\n", 
				     client->fd, client->request, 
				     esd_proto_map[ client->request ].description ); );
		client->state = ESD_NEXT_REQUEST; /* handler may override */
		is_ok = esd_proto_map[ client->request ].handler( client );
	    } else {
		ESDBG_TRACE( printf( "(%02d) need more data.\n", client->fd ); );
		is_ok = 1;
	    }

	    break;

	case ESD_CACHING_SAMPLE:
	    is_ok = esd_proto_sample_cache(client);
	    break;

	case ESD_NEXT_REQUEST:

 	    /* make sure there's a request as EOF may return as readable */
	    ESDBG_COMMS( printf( "--------------------------------\n" ); );
	    ESD_READ_INT( client->fd, &client->request, 
			  sizeof(client->request), length, "request" );

	    if ( client->swap_byte_order )
		client->request = swap_endian_32( client->request );

 	    if ( length == 0
		 || ( length < 0 && errno != EAGAIN && errno != EINTR ) )
	    {
 		/* no more data available from that client, close it */
		ESDBG_TRACE( printf( "(%02d) no more protocol requests for client\n", 
				     client->fd ); );
 		is_ok = 0;
		break;
 	    } 
	    else if ( client->request > ESD_PROTO_CONNECT 
		       && client->request < ESD_PROTO_MAX ) 
	    {
		/* set up to read more data */
		client->state = ESD_NEEDS_REQDATA;
		client->proto_data_length = 0;
		/* TODO: do one read, and handle if we get all the data */
		/* this should fix the "all handlers requrie data oddity */
		is_ok = 1;
	        /* if this request requires no additional param data... */
	        if (esd_proto_map[client->request].data_length == 0)
		  {
		    ESDBG_TRACE( printf( "(%02d) handling request %d, %s.\n", 
					client->fd, client->request, 
					esd_proto_map[ client->request ].description ); );
		    client->state = ESD_NEXT_REQUEST; /* handler may override */
		    is_ok = esd_proto_map[ client->request ].handler( client );
		  }
	    } 
	    else {
		ESDBG_TRACE( printf( "(%02d) invalid request: %d\n",
				     client->fd, client->request ); );
		is_ok = 0;
	    }

	    break;

	default:
	    ESDBG_TRACE( printf( "(%02d) invalid state: %d\n",
				 client->fd, client->state ); );
	    is_ok = 0;
	}

	/* if there was a problem, erase the client */
	if ( !is_ok ) {
	    ESDBG_TRACE( printf( "(%02d) error handling request %d, %s.\n", 
				 client->fd, client->request, 
				 esd_proto_map[ client->request ].description ); );
	    erase = client; 
	}

	/* update the iterator before removing */
	client = client->next;
	if ( erase != NULL ) {
	    erase_client( erase );
	    erase = NULL;
	}
    }

    return 1;
}