-
Notifications
You must be signed in to change notification settings - Fork 28
/
shout.c
1318 lines (1049 loc) · 29.2 KB
/
shout.c
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
/* -*- c-basic-offset: 8; -*- */
/* shout.c: Implementation of public libshout interface shout.h
*
* Copyright (C) 2002-2004 the Icecast team <team@icecast.org>,
* Copyright (C) 2012 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <shout/shout.h>
#include <net/sock.h>
#include "timing/timing.h"
#include "httpp/httpp.h"
#include "shout_private.h"
#include "util.h"
#ifdef _MSC_VER
# ifndef va_copy
# define va_copy(ap1, ap2) memcpy(&ap1, &ap2, sizeof(va_list))
# endif
# define vsnprintf _vsnprintf
# define inline _inline
#endif
/* -- local prototypes -- */
static int queue_data(shout_queue_t *queue, const unsigned char *data, size_t len);
static int queue_str(shout_t *self, const char *str);
static int queue_printf(shout_t *self, const char *fmt, ...);
static inline void queue_free(shout_queue_t *queue);
static int send_queue(shout_t *self);
static int get_response(shout_t *self);
static int try_connect (shout_t *self);
static int try_write (shout_t *self, const void *data, size_t len);
static int create_request(shout_t *self);
static int create_http_request(shout_t *self);
static int create_xaudiocast_request(shout_t *self);
static int create_icy_request(shout_t *self);
static int parse_response(shout_t *self);
static int parse_http_response(shout_t *self);
static int parse_xaudiocast_response(shout_t *self);
static char *http_basic_authorization(shout_t *self);
/* -- static data -- */
static int _initialized = 0;
/* -- public functions -- */
void shout_init(void)
{
if (_initialized)
return;
sock_initialize();
_initialized = 1;
}
void shout_shutdown(void)
{
if (!_initialized)
return;
sock_shutdown();
_initialized = 0;
}
shout_t *shout_new(void)
{
shout_t *self;
/* in case users haven't done this explicitly. Should we error
* if not initialized instead? */
shout_init();
if (!(self = (shout_t *)calloc(1, sizeof(shout_t)))) {
return NULL;
}
if (shout_set_host(self, LIBSHOUT_DEFAULT_HOST) != SHOUTERR_SUCCESS) {
shout_free(self);
return NULL;
}
if (shout_set_user(self, LIBSHOUT_DEFAULT_USER) != SHOUTERR_SUCCESS) {
shout_free(self);
return NULL;
}
if (shout_set_agent(self, LIBSHOUT_DEFAULT_USERAGENT) != SHOUTERR_SUCCESS) {
shout_free(self);
return NULL;
}
if (!(self->audio_info = _shout_util_dict_new())) {
shout_free(self);
return NULL;
}
if (!(self->meta = _shout_util_dict_new())) {
shout_free(self);
return NULL;
}
if (shout_set_meta(self, "name", "no name") != SHOUTERR_SUCCESS) {
shout_free(self);
return NULL;
}
self->port = LIBSHOUT_DEFAULT_PORT;
self->format = LIBSHOUT_DEFAULT_FORMAT;
self->protocol = LIBSHOUT_DEFAULT_PROTOCOL;
return self;
}
void shout_free(shout_t *self)
{
if (!self) return;
if (self->host) free(self->host);
if (self->password) free(self->password);
if (self->mount) free(self->mount);
if (self->user) free(self->user);
if (self->useragent) free(self->useragent);
if (self->audio_info) _shout_util_dict_free (self->audio_info);
if (self->meta) _shout_util_dict_free (self->meta);
free(self);
}
int shout_open(shout_t *self)
{
/* sanity check */
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return SHOUTERR_CONNECTED;
if (!self->host || !self->password || !self->port)
return self->error = SHOUTERR_INSANE;
if (self->format == SHOUT_FORMAT_OGG && self->protocol != SHOUT_PROTOCOL_HTTP)
return self->error = SHOUTERR_UNSUPPORTED;
return self->error = try_connect(self);
}
int shout_close(shout_t *self)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state == SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_UNCONNECTED;
if (self->state == SHOUT_STATE_CONNECTED && self->close)
self->close(self);
sock_close(self->socket);
self->state = SHOUT_STATE_UNCONNECTED;
self->starttime = 0;
self->senttime = 0;
queue_free(&self->rqueue);
queue_free(&self->wqueue);
return self->error = SHOUTERR_SUCCESS;
}
int shout_send(shout_t *self, const unsigned char *data, size_t len)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_CONNECTED)
return self->error = SHOUTERR_UNCONNECTED;
if (self->starttime <= 0)
self->starttime = timing_get_time();
if (!len)
return send_queue(self);
return self->send(self, data, len);
}
ssize_t shout_send_raw(shout_t *self, const unsigned char *data, size_t len)
{
ssize_t ret;
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_CONNECTED)
return SHOUTERR_UNCONNECTED;
self->error = SHOUTERR_SUCCESS;
/* send immediately if possible (should be the common case) */
if (len && ! self->wqueue.len) {
if ((ret = try_write(self, data, len)) < 0)
return self->error;
if (ret < (ssize_t)len) {
self->error = queue_data(&self->wqueue, data + ret, len - ret);
if (self->error != SHOUTERR_SUCCESS)
return self->error;
}
return len;
}
self->error = queue_data(&self->wqueue, data, len);
if (self->error != SHOUTERR_SUCCESS)
return self->error;
ret = send_queue(self);
if (ret == SHOUTERR_SUCCESS || (len && ret == SHOUTERR_BUSY))
return len;
return ret;
}
ssize_t shout_queuelen(shout_t *self)
{
if (!self)
return SHOUTERR_INSANE;
return (ssize_t)self->wqueue.len;
}
void shout_sync(shout_t *self)
{
int64_t sleep;
if (!self)
return;
if (self->senttime == 0)
return;
sleep = self->senttime / 1000 - (timing_get_time() - self->starttime);
if (sleep > 0)
timing_sleep((uint64_t)sleep);
}
int shout_delay(shout_t *self)
{
if (!self)
return 0;
if (self->senttime == 0)
return 0;
return self->senttime / 1000 - (timing_get_time() - self->starttime);
}
shout_metadata_t *shout_metadata_new(void)
{
return _shout_util_dict_new();
}
void shout_metadata_free(shout_metadata_t *self)
{
if (!self)
return;
_shout_util_dict_free(self);
}
int shout_metadata_add(shout_metadata_t *self, const char *name, const char *value)
{
if (!self || !name)
return SHOUTERR_INSANE;
return _shout_util_dict_set(self, name, value);
}
/* open second socket to server, send HTTP request to change metadata.
* TODO: prettier error-handling. */
int shout_set_metadata(shout_t *self, shout_metadata_t *metadata)
{
sock_t socket;
int rv;
char *encvalue;
if (!self || !metadata)
return SHOUTERR_INSANE;
if (!(encvalue = _shout_util_dict_urlencode(metadata, '&')))
return SHOUTERR_MALLOC;
if ((socket = sock_connect(self->host, self->port)) <= 0)
return SHOUTERR_NOCONNECT;
if (self->protocol == SHOUT_PROTOCOL_ICY)
rv = sock_write(socket, "GET /admin.cgi?mode=updinfo&pass=%s&%s HTTP/1.0\r\nUser-Agent: %s (Mozilla compatible)\r\n\r\n",
self->password, encvalue, shout_get_agent(self));
else if (self->protocol == SHOUT_PROTOCOL_HTTP) {
char *auth = http_basic_authorization(self);
rv = sock_write(socket, "GET /admin/metadata?mode=updinfo&mount=%s&%s HTTP/1.0\r\nUser-Agent: %s\r\n%s\r\n",
self->mount, encvalue, shout_get_agent(self), auth ? auth : "");
free(auth);
} else
rv = sock_write(socket, "GET /admin.cgi?mode=updinfo&pass=%s&mount=%s&%s HTTP/1.0\r\nUser-Agent: %s\r\n\r\n",
self->password, self->mount, encvalue, shout_get_agent(self));
free(encvalue);
if (!rv) {
sock_close(socket);
return SHOUTERR_SOCKET;
}
sock_close(socket);
return SHOUTERR_SUCCESS;
}
/* getters/setters */
const char *shout_version(int *major, int *minor, int *patch)
{
if (major)
*major = LIBSHOUT_MAJOR;
if (minor)
*minor = LIBSHOUT_MINOR;
if (patch)
*patch = LIBSHOUT_MICRO;
return VERSION;
}
int shout_get_errno(shout_t *self)
{
return self->error;
}
const char *shout_get_error(shout_t *self)
{
if (!self)
return "Invalid shout_t";
switch (self->error) {
case SHOUTERR_SUCCESS:
return "No error";
case SHOUTERR_INSANE:
return "Nonsensical arguments";
case SHOUTERR_NOCONNECT:
return "Couldn't connect";
case SHOUTERR_NOLOGIN:
return "Login failed";
case SHOUTERR_SOCKET:
return "Socket error";
case SHOUTERR_MALLOC:
return "Out of memory";
case SHOUTERR_CONNECTED:
return "Cannot set parameter while connected";
case SHOUTERR_UNCONNECTED:
return "Not connected";
case SHOUTERR_BUSY:
return "Socket is busy";
case SHOUTERR_UNSUPPORTED:
return "This libshout doesn't support the requested option";
default:
return "Unknown error";
}
}
/* Returns:
* SHOUTERR_CONNECTED if the connection is open,
* SHOUTERR_UNCONNECTED if it has not yet been opened,
* or an error from try_connect, including SHOUTERR_BUSY
*/
int shout_get_connected(shout_t *self)
{
int rc;
if (!self)
return SHOUTERR_INSANE;
if (self->state == SHOUT_STATE_CONNECTED)
return SHOUTERR_CONNECTED;
if (self->state != SHOUT_STATE_UNCONNECTED) {
if ((rc = try_connect(self)) == SHOUTERR_SUCCESS)
return SHOUTERR_CONNECTED;
return rc;
}
return SHOUTERR_UNCONNECTED;
}
int shout_set_host(shout_t *self, const char *host)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (self->host)
free(self->host);
if (!(self->host = _shout_util_strdup(host)))
return self->error = SHOUTERR_MALLOC;
return self->error = SHOUTERR_SUCCESS;
}
const char *shout_get_host(shout_t *self)
{
if (!self)
return NULL;
return self->host;
}
int shout_set_port(shout_t *self, unsigned short port)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
self->port = port;
return self->error = SHOUTERR_SUCCESS;
}
unsigned short shout_get_port(shout_t *self)
{
if (!self)
return 0;
return self->port;
}
int shout_set_password(shout_t *self, const char *password)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (self->password)
free (self->password);
if (!(self->password = _shout_util_strdup(password)))
return self->error = SHOUTERR_MALLOC;
return self->error = SHOUTERR_SUCCESS;
}
const char* shout_get_password(shout_t *self)
{
if (!self)
return NULL;
return self->password;
}
int shout_set_mount(shout_t *self, const char *mount)
{
size_t len;
if (!self || !mount)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (self->mount)
free(self->mount);
len = strlen (mount) + 1;
if (mount[0] != '/')
len++;
if (!(self->mount = malloc(len)))
return self->error = SHOUTERR_MALLOC;
sprintf (self->mount, "%s%s", mount[0] == '/' ? "" : "/", mount);
return self->error = SHOUTERR_SUCCESS;
}
const char *shout_get_mount(shout_t *self)
{
if (!self)
return NULL;
return self->mount;
}
int shout_set_name(shout_t *self, const char *name)
{
return shout_set_meta(self, "name", name);
}
const char *shout_get_name(shout_t *self)
{
return shout_get_meta(self, "name");
}
int shout_set_url(shout_t *self, const char *url)
{
return shout_set_meta(self, "url", url);
}
const char *shout_get_url(shout_t *self)
{
return shout_get_meta(self, "url");
}
int shout_set_genre(shout_t *self, const char *genre)
{
return shout_set_meta(self, "genre", genre);
}
const char *shout_get_genre(shout_t *self)
{
return shout_get_meta(self, "genre");
}
int shout_set_agent(shout_t *self, const char *agent)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (self->useragent)
free(self->useragent);
if (! (self->useragent = _shout_util_strdup (agent)))
return self->error = SHOUTERR_MALLOC;
return self->error = SHOUTERR_SUCCESS;
}
const char *shout_get_agent(shout_t *self)
{
if (!self)
return NULL;
return self->useragent;
}
int shout_set_user(shout_t *self, const char *username)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (self->user)
free(self->user);
if (! (self->user = _shout_util_strdup (username)))
return self->error = SHOUTERR_MALLOC;
return self->error = SHOUTERR_SUCCESS;
}
const char *shout_get_user(shout_t *self)
{
if (!self)
return NULL;
return self->user;
}
int shout_set_description(shout_t *self, const char *description)
{
return shout_set_meta(self, "description", description);
}
const char *shout_get_description(shout_t *self)
{
return shout_get_meta(self, "description");
}
int shout_set_dumpfile(shout_t *self, const char *dumpfile)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return SHOUTERR_CONNECTED;
if (self->dumpfile)
free(self->dumpfile);
if (! (self->dumpfile = _shout_util_strdup (dumpfile)))
return self->error = SHOUTERR_MALLOC;
return self->error = SHOUTERR_SUCCESS;
}
const char *shout_get_dumpfile(shout_t *self)
{
if (!self)
return NULL;
return self->dumpfile;
}
int shout_set_audio_info(shout_t *self, const char *name, const char *value)
{
if (!self)
return SHOUTERR_INSANE;
return self->error = _shout_util_dict_set(self->audio_info, name, value);
}
const char *shout_get_audio_info(shout_t *self, const char *name)
{
if (!self)
return NULL;
return _shout_util_dict_get(self->audio_info, name);
}
int shout_set_meta(shout_t *self, const char *name, const char *value)
{
size_t i;
if (!self || !name)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
for (i = 0; name[i]; i++)
if ((name[i] < 'a' || name[i] > 'z') && (name[i] < '0' || name[i] > '9'))
return self->error = SHOUTERR_INSANE;
return self->error = _shout_util_dict_set(self->meta, name, value);
}
const char *shout_get_meta(shout_t *self, const char *name)
{
if (!self)
return NULL;
return _shout_util_dict_get(self->meta, name);
}
int shout_set_public(shout_t *self, unsigned int public)
{
if (!self || (public != 0 && public != 1))
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
self->public = public;
return self->error = SHOUTERR_SUCCESS;
}
unsigned int shout_get_public(shout_t *self)
{
if (!self)
return 0;
return self->public;
}
int shout_set_format(shout_t *self, unsigned int format)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (format != SHOUT_FORMAT_OGG
&& format != SHOUT_FORMAT_MP3
&& format != SHOUT_FORMAT_WEBM
&& format != SHOUT_FORMAT_WEBMAUDIO)
return self->error = SHOUTERR_UNSUPPORTED;
self->format = format;
return self->error = SHOUTERR_SUCCESS;
}
unsigned int shout_get_format(shout_t* self)
{
if (!self)
return 0;
return self->format;
}
int shout_set_protocol(shout_t *self, unsigned int protocol)
{
if (!self)
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
if (protocol != SHOUT_PROTOCOL_HTTP &&
protocol != SHOUT_PROTOCOL_XAUDIOCAST &&
protocol != SHOUT_PROTOCOL_ICY)
return self->error = SHOUTERR_UNSUPPORTED;
self->protocol = protocol;
return self->error = SHOUTERR_SUCCESS;
}
unsigned int shout_get_protocol(shout_t *self)
{
if (!self)
return 0;
return self->protocol;
}
int shout_set_nonblocking(shout_t *self, unsigned int nonblocking)
{
if (!self || (nonblocking != 0 && nonblocking != 1))
return SHOUTERR_INSANE;
if (self->state != SHOUT_STATE_UNCONNECTED)
return self->error = SHOUTERR_CONNECTED;
self->nonblocking = nonblocking;
return SHOUTERR_SUCCESS;
}
unsigned int shout_get_nonblocking(shout_t *self)
{
if (!self)
return 0;
return self->nonblocking;
}
/* -- static function definitions -- */
/* queue data in pages of SHOUT_BUFSIZE bytes */
static int queue_data(shout_queue_t *queue, const unsigned char *data, size_t len)
{
shout_buf_t *buf;
size_t plen;
if (!len)
return SHOUTERR_SUCCESS;
if (!queue->len) {
queue->head = calloc(1, sizeof (shout_buf_t));
if (! queue->head)
return SHOUTERR_MALLOC;
}
for (buf = queue->head; buf->next; buf = buf->next);
/* Maybe any added data should be freed if we hit a malloc error?
* Otherwise it'd be impossible to tell where to start requeueing.
* (As if anyone ever tried to recover from a malloc error.) */
while (len > 0) {
if (buf->len == SHOUT_BUFSIZE) {
buf->next = calloc(1, sizeof (shout_buf_t));
if (! buf->next)
return SHOUTERR_MALLOC;
buf->next->prev = buf;
buf = buf->next;
}
plen = len > SHOUT_BUFSIZE - buf->len ? SHOUT_BUFSIZE - buf->len : len;
memcpy (buf->data + buf->len, data, plen);
buf->len += plen;
data += plen;
len -= plen;
queue->len += plen;
}
return SHOUTERR_SUCCESS;
}
static inline int queue_str(shout_t *self, const char *str)
{
return queue_data(&self->wqueue, (const unsigned char*)str, strlen(str));
}
/* this should be shared with sock_write. Create libicecommon. */
static int queue_printf(shout_t *self, const char *fmt, ...)
{
char buffer[1024];
char *buf;
va_list ap, ap_retry;
int len;
buf = buffer;
va_start(ap, fmt);
va_copy(ap_retry, ap);
len = vsnprintf(buf, sizeof(buffer), fmt, ap);
self->error = SHOUTERR_SUCCESS;
if (len > 0) {
if ((size_t)len < sizeof(buffer))
queue_data(&self->wqueue, (unsigned char*)buf, len);
else {
buf = malloc(++len);
if (buf) {
len = vsnprintf(buf, len, fmt, ap_retry);
queue_data(&self->wqueue, (unsigned char*)buf, len);
free(buf);
} else
self->error = SHOUTERR_MALLOC;
}
}
va_end(ap_retry);
va_end(ap);
return self->error;
}
static inline void queue_free(shout_queue_t *queue)
{
shout_buf_t *prev;
while (queue->head) {
prev = queue->head;
queue->head = queue->head->next;
free(prev);
}
queue->len = 0;
}
static int get_response(shout_t *self)
{
char buf[1024];
int rc, blen;
char *pc;
shout_buf_t *queue;
int newlines = 0;
rc = sock_read_bytes(self->socket, buf, sizeof(buf));
if (rc < 0 && sock_recoverable(sock_error()))
return SHOUTERR_BUSY;
if (rc <= 0)
return SHOUTERR_SOCKET;
if ((rc = queue_data(&self->rqueue, (unsigned char*)buf, rc)))
return rc;
/* work from the back looking for \r?\n\r?\n. Anything else means more
* is coming. */
for (queue = self->rqueue.head; queue->next; queue = queue->next);
pc = (char*)queue->data + queue->len - 1;
blen = queue->len;
while (blen) {
if (*pc == '\n')
newlines++;
/* we may have to scan the entire queue if we got a response with
* data after the head line (this can happen with eg 401) */
else if (*pc != '\r')
newlines = 0;
if (newlines == 2)
return SHOUTERR_SUCCESS;
blen--;
pc--;
if (!blen && queue->prev) {
queue = queue->prev;
pc = (char*)queue->data + queue->len - 1;
blen = queue->len;
}
}
return SHOUTERR_BUSY;
}
static int try_connect (shout_t *self)
{
int rc;
int port;
/* the breaks between cases are omitted intentionally */
switch (self->state) {
case SHOUT_STATE_UNCONNECTED:
port = self->port;
if (shout_get_protocol(self) == SHOUT_PROTOCOL_ICY)
port++;
if (shout_get_nonblocking(self)) {
if ((self->socket = sock_connect_non_blocking(self->host, port)) < 0)
return self->error = SHOUTERR_NOCONNECT;
self->state = SHOUT_STATE_CONNECT_PENDING;
} else {
if ((self->socket = sock_connect(self->host, port)) < 0)
return self->error = SHOUTERR_NOCONNECT;
if ((rc = create_request(self)) != SHOUTERR_SUCCESS)
return rc;
self->state = SHOUT_STATE_REQ_PENDING;
}
case SHOUT_STATE_CONNECT_PENDING:
if (shout_get_nonblocking(self)) {
if ((rc = sock_connected(self->socket, 0)) < 1) {
if (rc == SOCK_ERROR) {
rc = SHOUTERR_SOCKET;
goto failure;
} else
return SHOUTERR_BUSY;
}
if ((rc = create_request(self)) != SHOUTERR_SUCCESS)
goto failure;
}
self->state = SHOUT_STATE_REQ_PENDING;
case SHOUT_STATE_REQ_PENDING:
do
rc = send_queue(self);
while (!shout_get_nonblocking(self) && rc == SHOUTERR_BUSY);
if (rc == SHOUTERR_BUSY)
return rc;
if (rc != SHOUTERR_SUCCESS)
goto failure;
self->state = SHOUT_STATE_RESP_PENDING;
case SHOUT_STATE_RESP_PENDING:
do
rc = get_response(self);
while (!shout_get_nonblocking(self) && rc == SHOUTERR_BUSY);
if (rc == SHOUTERR_BUSY)
return rc;
if (rc != SHOUTERR_SUCCESS)
goto failure;
if ((rc = parse_response(self)) != SHOUTERR_SUCCESS)
goto failure;
switch (self->format) {
case SHOUT_FORMAT_OGG:
if ((rc = self->error = shout_open_ogg(self)) != SHOUTERR_SUCCESS)
goto failure;
break;
case SHOUT_FORMAT_MP3:
if ((rc = self->error = shout_open_mp3(self)) != SHOUTERR_SUCCESS)
goto failure;
break;
case SHOUT_FORMAT_WEBM:
case SHOUT_FORMAT_WEBMAUDIO:
if ((rc = self->error = shout_open_webm(self)) != SHOUTERR_SUCCESS)
goto failure;
break;
default:
rc = SHOUTERR_INSANE;
goto failure;
}
case SHOUT_STATE_CONNECTED:
self->state = SHOUT_STATE_CONNECTED;
}