-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent.c
116 lines (87 loc) · 2.4 KB
/
agent.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
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include "bzrtp/bzrtp.h"
static void hexdump(const void *ptr, size_t size) {
const uint8_t *cptr = ptr;
size_t i;
for (i = 0; i < size; i++) {
printf("%02x", cptr[i]);
}
printf("\n");
}
static int sd;
static int bzrtp_sendData(void *clientData, const uint8_t *packetString, uint16_t packetLength) {
return write(sd, packetString, packetLength);
}
static int bzrtp_srtpSecretsAvailable(void *clientData, bzrtpSrtpSecrets_t *secrets,
uint8_t part) {
switch (part) {
case ZRTP_SRTP_SECRETS_FOR_RECEIVER:
printf("bzrtp SAS %s\n", secrets->sas);
break;
}
return 0;
}
static int bzrtp_startSrtpSession(void *clientData, const char* sas, int32_t verified) {
return 0;
}
static bzrtpCallbacks_t bzrtp_callbacks = {
NULL,
NULL,
bzrtp_sendData,
bzrtp_srtpSecretsAvailable,
bzrtp_startSrtpSession,
};
int main(int argc, char **argv) {
bzrtpContext_t *ctx;
int ret;
uint32_t self_ssrc;
const char *remote;
struct sockaddr_in addr;
char hostname[1024];
uint8_t buffer[1024];
uint16_t received;
uint64_t now;
remote = argv[1];
gethostname(hostname, sizeof(hostname));
self_ssrc = (hostname[0]<<8) + hostname[1];
sd = socket(AF_INET, SOCK_DGRAM, 0);
assert(sd != -1);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("0.0.0.0");
addr.sin_port = htons(0x1337);
ret = bind(sd, (struct sockaddr *) &addr, sizeof(addr));
assert(ret == 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(remote);
addr.sin_port = htons(0x1337);
ret = connect(sd, (struct sockaddr *) &addr, sizeof(addr));
assert(ret == 0);
ctx = bzrtp_createBzrtpContext(self_ssrc);
assert(ctx != NULL);
ret = bzrtp_setCallbacks(ctx, &bzrtp_callbacks);
assert(ret == 0);
bzrtp_initBzrtpContext(ctx);
bzrtp_setClientData(ctx, self_ssrc, ctx);
ret = bzrtp_startChannelEngine(ctx, self_ssrc);
assert(ret == 0);
for (now = 0; now += 50;) {
usleep(500000);
ret = recv(sd, buffer, sizeof(buffer), MSG_DONTWAIT);
if (ret > 0) {
received = ret;
bzrtp_processMessage(ctx, self_ssrc, buffer, received);
}
bzrtp_iterate(ctx, self_ssrc, now);
}
bzrtp_destroyBzrtpContext(ctx, self_ssrc);
return 0;
}