Skip to content

Commit 372886f

Browse files
marcelveldtclaude
andcommitted
Implement pairing
Co-Authored-By: Claude <[email protected]>
1 parent 0ab6d92 commit 372886f

File tree

5 files changed

+165
-93
lines changed

5 files changed

+165
-93
lines changed

Makefile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ vpath %.c $(TOOLS):$(SRC):$(DMAP_PARSER):$(FETCHER)/src
3434
vpath %.cpp $(TOOLS):$(SRC):$(FETCHER)/src
3535

3636
INCLUDE = -I$(VALGRIND)/memcheck -I$(VALGRIND)/include \
37-
-I$(TOOLS) \
38-
-I$(DMAP_PARSER) \
39-
-I$(MDNS)/include/mdnssvc -I$(MDNS)/include/mdnssd \
40-
-I$(OPENSSL)/include \
41-
-I$(CODECS)/include/addons -I$(CODECS)/include/flac \
42-
-I$(CODECS)/include/shine -I$(CODECS)/include/faac \
43-
-I$(SRC) -I$(SRC)/inc \
44-
-I$(FETCHER)/include
37+
-I$(TOOLS) \
38+
-I$(DMAP_PARSER) \
39+
-I$(MDNS)/include/mdnssvc -I$(MDNS)/include/mdnssd \
40+
-I$(OPENSSL)/include \
41+
-I$(CODECS)/include/addons -I$(CODECS)/include/flac \
42+
-I$(CODECS)/include/shine -I$(CODECS)/include/faac \
43+
-I$(SRC) -I$(SRC)/inc \
44+
-I$(FETCHER)/include \
45+
-Icrosstools/src
4546

4647
CURVE25519_SOURCES = curve25519_dh.c curve25519_mehdi.c curve25519_order.c curve25519_utils.c custom_blind.c\
4748
ed25519_sign.c ed25519_verify.c \
@@ -53,11 +54,18 @@ SOURCES = raop_client.c rtsp_client.c \
5354
alac.c \
5455
http_fetcher.c http_error_codes.c
5556

56-
SOURCES_BIN = cross_log.c cross_ssl.c cross_util.c cross_net.c platform.c cliraop.c
57+
SOURCES_BIN = cross_log.c cross_ssl.c cross_util.c cross_net.c platform.c cliraop.c pairing.cpp bplist.cpp
58+
ifndef CXX
59+
CXX = g++
60+
endif
61+
CXXFLAGS ?= -Wall -O2 -std=c++17
5762

5863
OBJECTS = $(patsubst %.c,$(BUILDDIR)/%.o,$(filter %.c,$(SOURCES)))
5964
OBJECTS += $(patsubst %.cpp,$(BUILDDIR)/%.o,$(filter %.cpp,$(SOURCES)))
6065

66+
OBJECTS_BIN = $(patsubst %.c,$(BUILDDIR)/%.o,$(filter %.c,$(SOURCES_BIN)))
67+
OBJECTS_BIN += $(patsubst %.cpp,$(BUILDDIR)/%.o,$(filter %.cpp,$(SOURCES_BIN)))
68+
6169
LIBRARY = $(CODECS)/$(HOST)/$(PLATFORM)/libcodecs.a $(MDNS)/$(HOST)/$(PLATFORM)/libmdns.a
6270

6371
ifneq ($(STATIC),)
@@ -71,8 +79,8 @@ directory:
7179
@mkdir -p lib/$(HOST)/$(PLATFORM)
7280
@mkdir -p $(BUILDDIR)
7381

74-
$(EXECUTABLE): $(SOURCES_BIN:%.c=$(BUILDDIR)/%.o) $(LIB)
75-
$(CC) $^ $(LIBRARY) $(CFLAGS) $(LDFLAGS) -o $@
82+
$(EXECUTABLE): $(OBJECTS_BIN) $(LIB)
83+
$(CXX) $(OBJECTS_BIN) $(LIB) $(LIBRARY) $(LDFLAGS) -o $@
7684
ifeq ($(HOST),macos)
7785
rm -f $(CORE)
7886
lipo -create -output $(CORE) $$(ls $(CORE)* | grep -v '\-static')

src/cliraop.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <fcntl.h>
1414
#include <stdlib.h>
1515
#include <string.h>
16-
#include "platform.h"
16+
#include "../crosstools/src/platform.h"
1717
#include <assert.h>
1818

1919
#include <sys/stat.h>
@@ -32,13 +32,14 @@
3232
#endif
3333
#endif
3434

35-
#include "cross_thread.h"
35+
#include "../crosstools/src/cross_thread.h"
3636
#include "raop_client.h"
37-
#include "cross_net.h"
38-
#include "cross_ssl.h"
37+
#include "../crosstools/src/cross_net.h"
38+
#include "../crosstools/src/cross_ssl.h"
3939
#include "cross_util.h"
4040
#include "cross_log.h"
4141
#include "http_fetcher.h"
42+
#include "pairing.h"
4243

4344
#define RAOP_SEC(ntp) ((uint32_t)((ntp) >> 32))
4445
#define RAOP_FRAC(ntp) ((uint32_t)(ntp))
@@ -123,6 +124,7 @@ static int print_usage(char *argv[])
123124

124125
"\t[-if <ipaddress>] (IP of the interface to bind to)\n"
125126

127+
"\t[-pair] enter pairing mode for AppleTV\n"
126128
"\t[-debug <debug level>] (0 = silent)\n",
127129
name);
128130
return -1;
@@ -324,28 +326,34 @@ int main(int argc, char *argv[])
324326
int i, n = -1, level = 3;
325327
raop_crypto_t crypto = RAOP_CLEAR;
326328
uint64_t start = 0, start_at = 0, last = 0, frames = 0;
327-
bool alac = false, encryption = false, auth = false;
329+
bool alac = false, encryption = false, auth = false, pairing_mode = false;
328330
char *passwd = "", *secret = "", *md = "0,1,2", *et = "0,4", *am = "", *pk = "", *pw = "";
329331
char *iface = NULL;
330332
uint32_t glNetmask;
331333
char glInterface[16] = "?";
332334
static struct in_addr glHost;
335+
char *pair_udn = NULL;
336+
char *pair_secret = NULL;
333337

334338
// parse arguments
335339
for (i = 1; i < argc; i++)
336340
{
337-
if (!strcmp(argv[i], "-ntp"))
341+
if (!strcmp(argv[i], "-pair"))
342+
{
343+
pairing_mode = true;
344+
}
345+
else if (!strcmp(argv[i], "-ntp"))
338346
{
339347
uint64_t t = raopcl_get_ntp(NULL);
340348
printf("%" PRIu64 "\n", t);
341349
exit(0);
342350
}
343-
if (!strcmp(argv[i], "-check"))
351+
else if (!strcmp(argv[i], "-check"))
344352
{
345353
printf("cliraop check\n");
346354
exit(0);
347355
}
348-
if (!strcmp(argv[i], "-port"))
356+
else if (!strcmp(argv[i], "-port"))
349357
{
350358
player.port = atoi(argv[++i]);
351359
}
@@ -447,6 +455,23 @@ int main(int argc, char *argv[])
447455
LOG_INFO("Binding to %s [%s] with mask 0x%08x", inet_ntoa(glHost), iface, ntohl(glNetmask));
448456
NFREE(iface);
449457

458+
if (pairing_mode)
459+
{
460+
// Initialize platform/SSL for pairing
461+
netsock_init();
462+
cross_ssl_load();
463+
464+
if (AppleTVpairing(NULL, &pair_udn, &pair_secret))
465+
{
466+
printf("\nPairing successful!\nUDN: %s\nSecret: %s\n", pair_udn ? pair_udn : "(none)", pair_secret ? pair_secret : "(none)");
467+
}
468+
else
469+
{
470+
printf("Pairing failed.\n");
471+
}
472+
exit(0);
473+
}
474+
450475
if (!player.hostname)
451476
return print_usage(argv);
452477
if (!fname)

src/pairing.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ std::vector<uint8_t> computeM1(std::vector<uint8_t> pk, std::vector<uint8_t> sal
165165
return M1;
166166
}
167167

168+
extern "C" {
169+
168170
bool AppleTVpairing(struct mdnssd_handle_s* mDNShandle, char **pUDN, char **pSecret) {
169171
char response[32] = { };
170172
AppleTV *player = NULL;
@@ -226,7 +228,7 @@ bool AppleTVpairing(struct mdnssd_handle_s* mDNShandle, char **pUDN, char **pSec
226228

227229
kd_add(headers, "Connection", "keep-alive");
228230
kd_add(headers, "Content-Type", "application/octet-stream");
229-
231+
230232
char *buffer = http_send(sock, "POST /pair-pin-start HTTP/1.1", headers);
231233
//printf("%s", buffer);
232234
NFREE(buffer);
@@ -412,3 +414,5 @@ bool AppleTVpairing(struct mdnssd_handle_s* mDNShandle, char **pUDN, char **pSec
412414
if (!mDNShandle) mdnssd_close(mDNS);
413415
return true;
414416
}
417+
418+
} // extern "C"

src/pairing.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// pairing.h
2+
#pragma once
3+
#ifdef __cplusplus
4+
extern "C"
5+
{
6+
#endif
7+
bool AppleTVpairing(struct mdnssd_handle_s *mDNShandle, char **pUDN, char **pSecret);
8+
#ifdef __cplusplus
9+
}
10+
#endif

0 commit comments

Comments
 (0)