forked from mikejuni/shairport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shairport.c
1228 lines (1119 loc) · 36.5 KB
/
shairport.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
/*
* ShairPort Initializer - Network Initializer/Mgr for Hairtunes RAOP
* Copyright (c) M. Andrew Webster 2011
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include "socketlib.h"
#include "shairport.h"
#include "hairtunes.h"
#include "common.h"
#include "vol.h"
#include "audio.h"
// TEMP
int bufferStartFill = -1;
#ifdef _WIN32
#define DEVNULL "nul"
#else
#define DEVNULL "/dev/null"
#endif
static void handleClient(int pSock, char *pPassword, char *pHWADDR);
static void writeDataToClient(int pSock, struct shairbuffer *pResponse);
static int readDataFromClient(int pSock, struct shairbuffer *pClientBuffer);
static int parseMessage(struct connection *pConn,unsigned char *pIpBin, unsigned int pIpBinLen, char *pHWADDR);
static void propogateCSeq(struct connection *pConn);
static void cleanupBuffers(struct connection *pConnection);
static void cleanup(struct connection *pConnection);
static int startAvahi(const char *pHwAddr, const char *pServerName, int pPort);
static int getAvailChars(struct shairbuffer *pBuf);
static void addToShairBuffer(struct shairbuffer *pBuf, char *pNewBuf);
static void addNToShairBuffer(struct shairbuffer *pBuf, char *pNewBuf, int pNofNewBuf);
static char *getTrimmedMalloc(char *pChar, int pSize, int pEndStr, int pAddNL);
static char *getTrimmed(char *pChar, int pSize, int pEndStr, int pAddNL, char *pTrimDest);
static void initConnection(struct connection *pConn, struct keyring *pKeys, struct comms *pComms, int pSocket, char *pPassword);
static void closePipe(int *pPipe);
static void initBuffer(struct shairbuffer *pBuf, int pNumChars);
static void setKeys(struct keyring *pKeys, char *pIV, char* pAESKey, char *pFmtp);
static RSA *loadKey(void);
static int g_avahi_pid=-1;
static void handle_sigchld(int signo) {
int status;
waitpid(-1, &status, WNOHANG);
}
char tPidFile[255] = "/var/run/shairport.pid";
static void handle_sigterm(int signo) {
if (g_avahi_pid>0)
{
syslog(LOG_INFO,"SIGTERM caught, killing avahi-publish-service (%d) before exit\n", g_avahi_pid);
kill(g_avahi_pid, SIGTERM);
}
int pid=0;
FILE* fd=fopen(tPidFile,"r");
if (fd==NULL)
{
syslog(LOG_ERR, "PID File: %s not found\n",tPidFile);
} else {
fscanf(fd,"%d",&pid);
fclose(fd);
if (getpid()==pid){
syslog(LOG_INFO,"Removing PID file\n");
if (remove(tPidFile)!=0)
{
syslog(LOG_ERR,"Cannot remove PID file %s\n",tPidFile);
} else {
syslog(LOG_INFO,"PID file: %s removed.\n",tPidFile);
}
} else {
syslog(LOG_ERR,"PID file mismatch, please check\n");
}
}
exit(0);
}
char tAoDriver[56] = "";
char tAoDeviceName[56] = "";
char tAoDeviceId[56] = "";
int main(int argc, char **argv)
{
// unfortunately we can't just IGN on non-SysV systems
openlog("SHAIRPORT", LOG_PID | LOG_PERROR, LOG_USER);
// Register SIGCHLD to avoid zombee process
struct sigaction sa;
sa.sa_handler = handle_sigchld;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGCHLD, &sa, NULL) < 0) {
perror("sigaction");
return 1;
}
// Register SIGTERM to kill avahi process
struct sigaction sa_term;
sa_term.sa_handler=handle_sigterm;
sa.sa_flags=0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGTERM, &sa_term, NULL) < 0)
{
perror("sigaction on sigterm");
return 1;
}
char tHWID[HWID_SIZE] = {0,51,52,53,54,55};
char tHWID_Hex[HWID_SIZE * 2 + 1];
memset(tHWID_Hex, 0, sizeof(tHWID_Hex));
char tServerName[56] = "ShairPort";
char tPassword[56] = "";
struct addrinfo *tAddrInfo;
int tSimLevel = 0;
int tUseKnownHWID = FALSE;
int tDaemonize = FALSE;
int tPort = PORT;
int tLogLvl = LOG_ERR;
char *arg;
while ( (arg = *++argv) ) {
parse_vol_arg(arg);
parse_audio_arg(arg);
if(!strcmp(arg, "-a"))
{
strncpy(tServerName, *++argv, 55);
argc--;
}
else if(!strncmp(arg, "--apname=", 9))
{
strncpy(tServerName, arg+9, 55);
}
else if(!strncmp(arg, "--apname=", 9))
{
strncpy(tServerName, arg+9, 55);
}
else if(!strcmp(arg, "-p"))
{
strncpy(tPassword, *++argv, 55);
argc--;
}
else if(!strncmp(arg, "--password=",11 ))
{
strncpy(tPassword, arg+11, 55);
}
else if(!strncmp(arg, "--pid_file=",11 ))
{
strncpy(tPidFile, arg+11, 55);
}
else if(!strcmp(arg, "-o"))
{
tPort = atoi(*++argv);
argc--;
}
else if(!strncmp(arg, "--server_port=", 14))
{
tPort = atoi(arg+14);
}
else if(!strcmp(arg, "-b"))
{
bufferStartFill = atoi(*++argv);
argc--;
}
else if(!strncmp(arg, "--buffer=", 9))
{
bufferStartFill = atoi(arg + 9);
}
else if(!strncmp(arg, "--buffer_size=", 14))
{
int fsize = atoi(arg + 14);
set_buffer_frames(fsize);
}
else if(!strcmp(arg, "-k"))
{
tUseKnownHWID = TRUE;
}
else if(!strcmp(arg, "-q") || !strncmp(arg, "--quiet", 7))
{
tLogLvl=LOG_CRIT;
}
else if(!strcmp(arg, "-d"))
{
tDaemonize = TRUE;
}
else if(!strcmp(arg, "-v"))
{
tLogLvl=LOG_WARNING;
}
else if(!strcmp(arg, "-v2"))
{
tLogLvl=LOG_INFO;
}
else if(!strcmp(arg, "-vv") || !strcmp(arg, "-v3"))
{
tLogLvl=LOG_DEBUG;
}
else if(!strcmp(arg, "-h") || !strcmp(arg, "--help"))
{
printf("ShairPort version 0.05 C port - Airport Express emulator\n");
printf("Usage:\nshairport [OPTION...]\n\nOptions:\n");
printf(" -h Show this help\n");
printf(" -a, --apname=AirPort Sets Airport name\n");
printf(" -p, --password=secret Sets Password (not working)\n");
printf(" -o, --server_port=5002 Sets Port for Avahi/dns-sd/howl\n");
printf(" -b, --buffer=282 Sets Number of frames to buffer before beginning playback\n");
printf(" --buffer_size=1024 Sets buffer size\n");
printf(" -d Daemonize\n");
printf(" --pid_file=/var/run/shairport.pid Sets PID file in -d mode\n");
print_audio_args();
print_vol_args();
printf(" -d Daemon mode\n");
printf(" -q, --quiet Supresses all output.\n");
printf(" -v,-v2,-v3,-vv Various debugging levels\n\n");
return 0;
}
}
if ( (bufferStartFill >= 0 && bufferStartFill < 30) || bufferStartFill > get_buffer_frames() ) {
syslog(LOG_ERR, "buffer value must be > 30 and < %d\n", get_buffer_frames());
return(-1);
}
setlogmask(LOG_UPTO(tLogLvl));
if(tDaemonize)
{
closelog();
openlog("SHAIRPORT", LOG_PID, LOG_DAEMON);
setlogmask(LOG_UPTO(tLogLvl));
int tPid = fork();
if(tPid < 0)
{
exit(1); // Error on fork
}
else if(tPid > 0)
{
syslog(LOG_INFO,"Writing PID file %s\n",tPidFile);
FILE* fd=fopen(tPidFile,"w");
if (fd==NULL)
{
syslog(LOG_WARNING,"Cannot update PID file %s\n", tPidFile);
}
else
{
fprintf(fd,"%d\n",tPid);
fclose(fd);
}
exit(0);
}
else
{
setsid();
int tIdx = 0;
for(tIdx = getdtablesize(); tIdx >= 0; --tIdx)
{
close(tIdx);
}
tIdx = open(DEVNULL, O_RDWR);
dup(tIdx);
dup(tIdx);
}
}
srandom ( time(NULL) );
// Copy over empty 00's
//tPrintHWID[tIdx] = tAddr[0];
int tIdx = 0;
for(tIdx=0;tIdx<HWID_SIZE;tIdx++)
{
if(tIdx > 0)
{
if(!tUseKnownHWID)
{
int tVal = ((random() % 80) + 33);
tHWID[tIdx] = tVal;
}
//tPrintHWID[tIdx] = tAddr[tIdx];
}
sprintf(tHWID_Hex+(tIdx*2), "%02X",tHWID[tIdx]);
}
//tPrintHWID[HWID_SIZE] = '\0';
syslog(LOG_DEBUG, "LogLevel: %d\n", tLogLvl);
syslog(LOG_INFO, "AirName: %s\n", tServerName);
syslog(LOG_DEBUG, "HWID: %.*s\n", HWID_SIZE, tHWID+1);
syslog(LOG_DEBUG, "HWID_Hex(%d): %s\n", (int)strlen(tHWID_Hex), tHWID_Hex);
if(tSimLevel >= 1)
{
#ifdef SIM_INCL
sim(tSimLevel, tTestValue, tHWID);
#endif
return(1);
}
else
{
g_avahi_pid=startAvahi(tHWID_Hex, tServerName, tPort);
syslog(LOG_INFO, "Starting connection server: specified server port: %d\n", tPort);
int tServerSock = setupListenServer(&tAddrInfo, tPort);
if(tServerSock < 0)
{
freeaddrinfo(tAddrInfo);
syslog(LOG_ERR, "Error setting up server socket on port %d, try specifying a different port\n", tPort);
exit(1);
}
int tClientSock = 0;
while(1)
{
syslog(LOG_INFO, "Waiting for clients to connect\n");
tClientSock = acceptClient(tServerSock, tAddrInfo);
if(tClientSock > 0)
{
int tPid = fork();
if(tPid == 0)
{
freeaddrinfo(tAddrInfo);
tAddrInfo = NULL;
syslog(LOG_DEBUG, "...Accepted Client Connection..\n");
close(tServerSock);
handleClient(tClientSock, tPassword, tHWID);
//close(tClientSock);
return 0;
}
else
{
syslog(LOG_DEBUG, "Child now busy handling new client\n");
close(tClientSock);
}
}
else
{
// failed to init server socket....try waiting a moment...
sleep(2);
}
}
}
syslog(LOG_DEBUG, "Finished, and waiting to clean everything up\n");
sleep(1);
if(tAddrInfo != NULL)
{
freeaddrinfo(tAddrInfo);
}
return 0;
}
static int findEnd(char *tReadBuf)
{
// find \n\n, \r\n\r\n, or \r\r is found
int tIdx = 0;
int tLen = strlen(tReadBuf);
for(tIdx = 0; tIdx < tLen; tIdx++)
{
if(tReadBuf[tIdx] == '\r')
{
if(tIdx + 1 < tLen)
{
if(tReadBuf[tIdx+1] == '\r')
{
return (tIdx+1);
}
else if(tIdx+3 < tLen)
{
if(tReadBuf[tIdx+1] == '\n' &&
tReadBuf[tIdx+2] == '\r' &&
tReadBuf[tIdx+3] == '\n')
{
return (tIdx+3);
}
}
}
}
else if(tReadBuf[tIdx] == '\n')
{
if(tIdx + 1 < tLen && tReadBuf[tIdx+1] == '\n')
{
return (tIdx + 1);
}
}
}
// Found nothing
return -1;
}
static void handleClient(int pSock, char *pPassword, char *pHWADDR)
{
fflush(stdout);
socklen_t len;
struct sockaddr_storage addr;
#ifdef AF_INET6
unsigned char ipbin[INET6_ADDRSTRLEN];
#else
unsigned char ipbin[INET_ADDRSTRLEN];
#endif
unsigned int ipbinlen;
int port;
char ipstr[64];
len = sizeof addr;
getsockname(pSock, (struct sockaddr*)&addr, &len);
// deal with both IPv4 and IPv6:
if (addr.ss_family == AF_INET) {
syslog(LOG_DEBUG, "Constructing ipv4 address\n");
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
port = ntohs(s->sin_port);
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
memcpy(ipbin, &s->sin_addr, 4);
ipbinlen = 4;
} else { // AF_INET6
syslog(LOG_DEBUG, "Constructing ipv6 address\n");
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
port = ntohs(s->sin6_port);
inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr);
union {
struct sockaddr_in6 s;
unsigned char bin[sizeof(struct sockaddr_in6)];
} addr;
memcpy(&addr.s, &s->sin6_addr, sizeof(struct sockaddr_in6));
if(memcmp(&addr.bin[0], "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\xff\xff", 12) == 0)
{
// its ipv4...
memcpy(ipbin, &addr.bin[12], 4);
ipbinlen = 4;
}
else
{
memcpy(ipbin, &s->sin6_addr, 16);
ipbinlen = 16;
}
}
syslog(LOG_DEBUG, "Peer IP address: %s\n", ipstr);
syslog(LOG_DEBUG, "Peer port : %d\n", port);
int tMoreDataNeeded = 1;
struct keyring tKeys;
struct comms tComms;
struct connection tConn;
initConnection(&tConn, &tKeys, &tComms, pSock, pPassword);
while(1)
{
tMoreDataNeeded = 1;
initBuffer(&tConn.recv, 80); // Just a random, small size to seed the buffer with.
initBuffer(&tConn.resp, 80);
int tError = FALSE;
while(1 == tMoreDataNeeded)
{
tError = readDataFromClient(pSock, &(tConn.recv));
if(!tError && strlen(tConn.recv.data) > 0)
{
syslog(LOG_DEBUG, "Finished Reading some data from client\n");
// parse client request
tMoreDataNeeded = parseMessage(&tConn, ipbin, ipbinlen, pHWADDR);
if(1 == tMoreDataNeeded)
{
syslog(LOG_DEBUG, "\n\nNeed to read more data\n");
}
else if(-1 == tMoreDataNeeded) // Forked process down below ended.
{
syslog(LOG_DEBUG, "Forked Process ended...cleaning up\n");
cleanup(&tConn);
// pSock was already closed
return;
}
// if more data needed,
}
else
{
syslog(LOG_DEBUG, "Error reading from socket, closing client\n");
// Error reading data....quit.
cleanup(&tConn);
return;
}
}
syslog(LOG_DEBUG, "Writing: %d chars to socket\n", tConn.resp.current);
//tConn->resp.data[tConn->resp.current-1] = '\0';
writeDataToClient(pSock, &(tConn.resp));
// Finished reading one message...
cleanupBuffers(&tConn);
}
cleanup(&tConn);
fflush(stdout);
}
static void writeDataToClient(int pSock, struct shairbuffer *pResponse)
{
syslog(LOG_DEBUG, "\n----Beg Send Response Header----\n%.*s\n", pResponse->current, pResponse->data);
send(pSock, pResponse->data, pResponse->current,0);
syslog(LOG_DEBUG, "----Send Response Header----\n");
}
static int readDataFromClient(int pSock, struct shairbuffer *pClientBuffer)
{
char tReadBuf[MAX_SIZE];
tReadBuf[0] = '\0';
int tRetval = 1;
int tEnd = -1;
while(tRetval > 0 && tEnd < 0)
{
// Read from socket until \n\n, \r\n\r\n, or \r\r is found
syslog(LOG_DEBUG, "Waiting To Read...\n");
fflush(stdout);
tRetval = read(pSock, tReadBuf, MAX_SIZE);
// if new buffer contains the end of request string, only copy partial buffer?
tEnd = findEnd(tReadBuf);
if(tEnd >= 0)
{
if(pClientBuffer->marker == 0)
{
pClientBuffer->marker = tEnd+1; // Marks start of content
}
syslog(SOCKET_LOG_LEVEL, "Found end of http request at: %d\n", tEnd);
fflush(stdout);
}
else
{
tEnd = MAX_SIZE;
syslog(SOCKET_LOG_LEVEL, "Read %d of data so far\n%s\n", tRetval, tReadBuf);
fflush(stdout);
}
if(tRetval > 0)
{
// Copy read data into tReceive;
syslog(SOCKET_LOG_LEVEL, "Read %d data, using %d of it\n", tRetval, tEnd);
addNToShairBuffer(pClientBuffer, tReadBuf, tRetval);
syslog(LOG_DEBUG, "Finished copying data\n");
}
else
{
syslog(LOG_DEBUG, "Error reading data from socket, got: %d bytes", tRetval);
return tRetval;
}
}
if(tEnd + 1 != tRetval)
{
syslog(SOCKET_LOG_LEVEL, "Read more data after end of http request. %d instead of %d\n", tRetval, tEnd+1);
}
syslog(SOCKET_LOG_LEVEL, "Finished Reading Data:\n%s\nEndOfData\n", pClientBuffer->data);
fflush(stdout);
return 0;
}
static char *getFromBuffer(char *pBufferPtr, const char *pField, int pLenAfterField, int *pReturnSize, char *pDelims)
{
syslog(LOG_DEBUG, "GettingFromBuffer: %s\n", pField);
char* tFound = strstr(pBufferPtr, pField);
int tSize = 0;
if(tFound != NULL)
{
tFound += (strlen(pField) + pLenAfterField);
int tIdx = 0;
char tDelim = pDelims[tIdx];
char *tShortest = NULL;
char *tEnd = NULL;
while(tDelim != '\0')
{
tDelim = pDelims[tIdx++]; // Ensures that \0 is also searched.
tEnd = strchr(tFound, tDelim);
if(tEnd != NULL && (NULL == tShortest || tEnd < tShortest))
{
tShortest = tEnd;
}
}
tSize = (int) (tShortest - tFound);
syslog(LOG_DEBUG, "Found %.*s length: %d\n", tSize, tFound, tSize);
if(pReturnSize != NULL)
{
*pReturnSize = tSize;
}
}
else
{
syslog(LOG_DEBUG, "Not Found\n");
}
return tFound;
}
static char *getFromHeader(char *pHeaderPtr, const char *pField, int *pReturnSize)
{
return getFromBuffer(pHeaderPtr, pField, 2, pReturnSize, "\r\n");
}
static char *getFromContent(char *pContentPtr, const char* pField, int *pReturnSize)
{
return getFromBuffer(pContentPtr, pField, 1, pReturnSize, "\r\n");
}
static char *getFromSetup(char *pContentPtr, const char* pField, int *pReturnSize)
{
return getFromBuffer(pContentPtr, pField, 1, pReturnSize, ";\r\n");
}
// Handles compiling the Apple-Challenge, HWID, and Server IP Address
// Into the response the airplay client is expecting.
static int buildAppleResponse(struct connection *pConn, unsigned char *pIpBin,
unsigned int pIpBinLen, char *pHWID)
{
// Find Apple-Challenge
char *tResponse = NULL;
int tFoundSize = 0;
char* tFound = getFromHeader(pConn->recv.data, "Apple-Challenge", &tFoundSize);
if(tFound != NULL)
{
char tTrim[tFoundSize + 2];
getTrimmed(tFound, tFoundSize, TRUE, TRUE, tTrim);
syslog(LOG_DEBUG, "HeaderChallenge: [%s] len: %d sizeFound: %d\n", tTrim, (int)strlen(tTrim), tFoundSize);
int tChallengeDecodeSize = 16;
char *tChallenge = decode_base64((unsigned char *)tTrim, tFoundSize, &tChallengeDecodeSize);
syslog(LOG_DEBUG, "Challenge Decode size: %d expected 16\n", tChallengeDecodeSize);
int tCurSize = 0;
unsigned char tChalResp[38];
memcpy(tChalResp, tChallenge, tChallengeDecodeSize);
tCurSize += tChallengeDecodeSize;
memcpy(tChalResp+tCurSize, pIpBin, pIpBinLen);
tCurSize += pIpBinLen;
memcpy(tChalResp+tCurSize, pHWID, HWID_SIZE);
tCurSize += HWID_SIZE;
int tPad = 32 - tCurSize;
if (tPad > 0)
{
memset(tChalResp+tCurSize, 0, tPad);
tCurSize += tPad;
}
char *tTmp = encode_base64((unsigned char *)tChalResp, tCurSize);
syslog(LOG_DEBUG, "Full sig: %s\n", tTmp);
free(tTmp);
// RSA Encrypt
RSA *rsa = loadKey(); // Free RSA
int tSize = RSA_size(rsa);
unsigned char tTo[tSize];
RSA_private_encrypt(tCurSize, (unsigned char *)tChalResp, tTo, rsa, RSA_PKCS1_PADDING);
// Wrap RSA Encrypted binary in Base64 encoding
tResponse = encode_base64(tTo, tSize);
int tLen = strlen(tResponse);
while(tLen > 1 && tResponse[tLen-1] == '=')
{
tResponse[tLen-1] = '\0';
}
free(tChallenge);
RSA_free(rsa);
}
if(tResponse != NULL)
{
// Append to current response
addToShairBuffer(&(pConn->resp), "Apple-Response: ");
addToShairBuffer(&(pConn->resp), tResponse);
addToShairBuffer(&(pConn->resp), "\r\n");
free(tResponse);
return TRUE;
}
return FALSE;
}
//parseMessage(tConn->recv.data, tConn->recv.mark, &tConn->resp, ipstr, pHWADDR, tConn->keys);
static int parseMessage(struct connection *pConn, unsigned char *pIpBin, unsigned int pIpBinLen, char *pHWID)
{
int tReturn = 0; // 0 = good, 1 = Needs More Data, -1 = close client socket.
if(pConn->resp.data == NULL)
{
initBuffer(&(pConn->resp), MAX_SIZE);
}
char *tContent = getFromHeader(pConn->recv.data, "Content-Length", NULL);
if(tContent != NULL)
{
int tContentSize = atoi(tContent);
if(pConn->recv.marker == 0 || strlen(pConn->recv.data+pConn->recv.marker) != tContentSize)
{
syslog(HEADER_LOG_LEVEL, "Content-Length: %s value -> %d\n", tContent, tContentSize);
if(pConn->recv.marker != 0)
{
syslog(HEADER_LOG_LEVEL, "ContentPtr has %d, but needs %d\n",
(int)strlen(pConn->recv.data+pConn->recv.marker), tContentSize);
}
// check if value in tContent > 2nd read from client.
return 1; // means more content-length needed
}
}
else
{
syslog(LOG_DEBUG, "No content, header only\n");
}
// "Creates" a new Response Header for our response message
addToShairBuffer(&(pConn->resp), "RTSP/1.0 200 OK\r\n");
int tLen = strchr(pConn->recv.data, ' ') - pConn->recv.data;
if(tLen < 0 || tLen > 20)
{
tLen = 20;
}
syslog(LOG_INFO, "********** RECV %.*s **********\n", tLen, pConn->recv.data);
if(pConn->password != NULL)
{
}
if(buildAppleResponse(pConn, pIpBin, pIpBinLen, pHWID)) // need to free sig
{
syslog(LOG_DEBUG, "Added AppleResponse to Apple-Challenge request\n");
}
// Find option, then based on option, do different actions.
if(strncmp(pConn->recv.data, "OPTIONS", 7) == 0)
{
propogateCSeq(pConn);
addToShairBuffer(&(pConn->resp),
"Public: ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER\r\n");
}
else if(!strncmp(pConn->recv.data, "ANNOUNCE", 8))
{
char *tContent = pConn->recv.data + pConn->recv.marker;
int tSize = 0;
char *tHeaderVal = getFromContent(tContent, "a=aesiv", &tSize); // Not allocated memory, just pointing
if(tSize > 0)
{
int tKeySize = 0;
char tEncodedAesIV[tSize + 2];
getTrimmed(tHeaderVal, tSize, TRUE, TRUE, tEncodedAesIV);
syslog(LOG_DEBUG, "AESIV: [%.*s] Size: %d Strlen: %d\n", tSize, tEncodedAesIV, tSize, (int)strlen(tEncodedAesIV));
char *tDecodedIV = decode_base64((unsigned char*) tEncodedAesIV, tSize, &tSize);
// grab the key, copy it out of the receive buffer
tHeaderVal = getFromContent(tContent, "a=rsaaeskey", &tKeySize);
char tEncodedAesKey[tKeySize + 2]; // +1 for nl, +1 for \0
getTrimmed(tHeaderVal, tKeySize, TRUE, TRUE, tEncodedAesKey);
syslog(LOG_DEBUG, "AES KEY: [%s] Size: %d Strlen: %d\n", tEncodedAesKey, tKeySize, (int)strlen(tEncodedAesKey));
// remove base64 coding from key
char *tDecodedAesKey = decode_base64((unsigned char*) tEncodedAesKey,
tKeySize, &tKeySize); // Need to free DecodedAesKey
// Grab the formats
int tFmtpSize = 0;
char *tFmtp = getFromContent(tContent, "a=fmtp", &tFmtpSize); // Don't need to free
tFmtp = getTrimmedMalloc(tFmtp, tFmtpSize, TRUE, FALSE); // will need to free
syslog(LOG_DEBUG, "Format: %s\n", tFmtp);
RSA *rsa = loadKey();
// Decrypt the binary aes key
char *tDecryptedKey = malloc(RSA_size(rsa) * sizeof(char)); // Need to Free Decrypted key
//char tDecryptedKey[RSA_size(rsa)];
if(RSA_private_decrypt(tKeySize, (unsigned char *)tDecodedAesKey,
(unsigned char*) tDecryptedKey, rsa, RSA_PKCS1_OAEP_PADDING) >= 0)
{
syslog(LOG_DEBUG, "Decrypted AES key from RSA Successfully\n");
}
else
{
syslog(LOG_INFO, "Error Decrypting AES key from RSA\n");
}
free(tDecodedAesKey);
RSA_free(rsa);
setKeys(pConn->keys, tDecodedIV, tDecryptedKey, tFmtp);
propogateCSeq(pConn);
}
}
else if(!strncmp(pConn->recv.data, "SETUP", 5))
{
// Setup pipes
struct comms *tComms = pConn->hairtunes;
if (! (pipe(tComms->in) == 0 && pipe(tComms->out) == 0))
{
syslog(LOG_INFO, "Error setting up hairtunes communications...some things probably wont work very well.\n");
}
// Setup fork
char tPort[8] = "6000"; // get this from dup()'d stdout of child pid
int tPid = fork();
if(tPid == 0)
{
int tDataport=0;
char tCPortStr[8] = "59010";
char tTPortStr[8] = "59012";
int tSize = 0;
char *tFound =getFromSetup(pConn->recv.data, "control_port", &tSize);
getTrimmed(tFound, tSize, 1, 0, tCPortStr);
tFound = getFromSetup(pConn->recv.data, "timing_port", &tSize);
getTrimmed(tFound, tSize, 1, 0, tTPortStr);
syslog(LOG_DEBUG, "converting %s and %s from str->int\n", tCPortStr, tTPortStr);
int tControlport = atoi(tCPortStr);
int tTimingport = atoi(tTPortStr);
syslog(LOG_DEBUG, "Got %d for CPort and %d for TPort\n", tControlport, tTimingport);
char *tRtp = NULL;
char *tPipe = NULL;
// *************************************************
// ** Setting up Pipes, AKA no more debug/output **
// *************************************************
dup2(tComms->in[0],0); // Input to child
closePipe(&(tComms->in[0]));
closePipe(&(tComms->in[1]));
dup2(tComms->out[1], 1); // Output from child
closePipe(&(tComms->out[1]));
closePipe(&(tComms->out[0]));
struct keyring *tKeys = pConn->keys;
pConn->keys = NULL;
pConn->hairtunes = NULL;
// Free up any recv buffers, etc..
if(pConn->clientSocket != -1)
{
close(pConn->clientSocket);
pConn->clientSocket = -1;
}
cleanupBuffers(pConn);
hairtunes_init(tKeys->aeskey, tKeys->aesiv, tKeys->fmt, tControlport, tTimingport,
tDataport, tRtp, tPipe, bufferStartFill);
// Quit when finished.
syslog(LOG_DEBUG, "Returned from hairtunes init....returning -1, should close out this whole side of the fork\n");
return -1;
}
else if(tPid >0)
{
// Ensure Connection has access to the pipe.
closePipe(&(tComms->in[0]));
closePipe(&(tComms->out[1]));
char tFromHairtunes[80];
int tRead = read(tComms->out[0], tFromHairtunes, 80);
if(tRead <= 0)
{
syslog(LOG_ERR, "Error reading port from hairtunes function, assuming default port: %s\n", tPort);
}
else
{
int tSize = 0;
char *tPortStr = getFromHeader(tFromHairtunes, "port", &tSize);
if(tPortStr != NULL)
{
getTrimmed(tPortStr, tSize, TRUE, FALSE, tPort);
}
else
{
syslog(LOG_ERR, "Read %d bytes, Error translating %s into a port\n", tRead, tFromHairtunes);
}
}
// READ Ports from here?close(pConn->hairtunes_pipes[0]);
propogateCSeq(pConn);
int tSize = 0;
char *tTransport = getFromHeader(pConn->recv.data, "Transport", &tSize);
addToShairBuffer(&(pConn->resp), "Transport: ");
addNToShairBuffer(&(pConn->resp), tTransport, tSize);
// Append server port:
addToShairBuffer(&(pConn->resp), ";server_port=");
addToShairBuffer(&(pConn->resp), tPort);
addToShairBuffer(&(pConn->resp), "\r\nSession: DEADBEEF\r\n");
}
else
{
syslog(LOG_ERR, "Error forking process....dere' be errors round here.\n");
return -1;
}
}
else if(!strncmp(pConn->recv.data, "TEARDOWN", 8))
{
// Be smart? Do more finish up stuff...
addToShairBuffer(&(pConn->resp), "Connection: close\r\n");
propogateCSeq(pConn);
close(pConn->hairtunes->in[1]);
syslog(LOG_DEBUG, "Tearing down connection, closing pipes\n");
//close(pConn->hairtunes->out[0]);
tReturn = -1; // Close client socket, but sends an ACK/OK packet first
}
else if(!strncmp(pConn->recv.data, "FLUSH", 5))
{
write(pConn->hairtunes->in[1], "flush\n", 6);
propogateCSeq(pConn);
}
else if(!strncmp(pConn->recv.data, "SET_PARAMETER", 13))
{
propogateCSeq(pConn);
int tSize = 0;
char *tVol = getFromHeader(pConn->recv.data, "volume", &tSize);
syslog(LOG_DEBUG, "About to write [vol: %.*s] data to hairtunes\n", tSize, tVol);
write(pConn->hairtunes->in[1], "vol: ", 5);
write(pConn->hairtunes->in[1], tVol, tSize);
write(pConn->hairtunes->in[1], "\n", 1);
syslog(LOG_DEBUG, "Finished writing data write data to hairtunes\n");
}
else
{
syslog(LOG_DEBUG, "\n\nUn-Handled recv: %s\n", pConn->recv.data);
propogateCSeq(pConn);
}
addToShairBuffer(&(pConn->resp), "\r\n");
return tReturn;
}
// Copies CSeq value from request, and adds standard header values in.
static void propogateCSeq(struct connection *pConn) //char *pRecvBuffer, struct shairbuffer *pConn->recp.data)
{
int tSize=0;
char *tRecPtr = getFromHeader(pConn->recv.data, "CSeq", &tSize);
addToShairBuffer(&(pConn->resp), "Audio-Jack-Status: connected; type=analog\r\n");
addToShairBuffer(&(pConn->resp), "CSeq: ");
addNToShairBuffer(&(pConn->resp), tRecPtr, tSize);
addToShairBuffer(&(pConn->resp), "\r\n");
}
void cleanupBuffers(struct connection *pConn)
{
if(pConn->recv.data != NULL)
{
free(pConn->recv.data);
pConn->recv.data = NULL;
}
if(pConn->resp.data != NULL)
{
free(pConn->resp.data);
pConn->resp.data = NULL;
}
}
static void cleanup(struct connection *pConn)
{
cleanupBuffers(pConn);
if(pConn->hairtunes != NULL)
{
closePipe(&(pConn->hairtunes->in[0]));
closePipe(&(pConn->hairtunes->in[1]));
closePipe(&(pConn->hairtunes->out[0]));
closePipe(&(pConn->hairtunes->out[1]));
}
if(pConn->keys != NULL)
{
if(pConn->keys->aesiv != NULL)
{
free(pConn->keys->aesiv);
}
if(pConn->keys->aeskey != NULL)
{
free(pConn->keys->aeskey);
}
if(pConn->keys->fmt != NULL)