-
Notifications
You must be signed in to change notification settings - Fork 4
/
derpnet_example.c
164 lines (136 loc) · 3.16 KB
/
derpnet_example.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
#define _CRT_SECURE_NO_DEPRECATE
#define DERPNET_STATIC
#include "derpnet.h"
#include <stdio.h>
#include <string.h>
static void PrintHelpAndExit(char* argv0)
{
printf(
"USAGE: %s s other_key message\n"
"Sends message to other user:\n"
" - other_key = PUBLIC key of user to send to\n"
" - message = arbitrart text to send\n"
"\n"
"USAGE: %s r\n"
"Receives messages from other users\n"
"\n"
, argv0, argv0);
exit(0);
}
static void PrintKey(const DerpKey* Key)
{
for (size_t i=0; i<32; i++)
{
printf("%02hhx", Key->Bytes[i]);
}
}
static DerpKey HexToKey(const char* Hex)
{
DERPNET_ASSERT(strlen(Hex) == 64);
DerpKey Key;
for (size_t i=0; i<32; i++)
{
sscanf(&Hex[2*i], "%02hhx", &Key.Bytes[i]);
}
return Key;
}
// choose any hostname from https://login.tailscale.com/derpmap/default
// both peers do not need to be connected to the same server
// but they must be connected to the same region
#define DERP_SERVER_HOST "derp1f.tailscale.com"
int main(int argc, char* argv[])
{
if (argc < 2)
{
PrintHelpAndExit(argv[0]);
}
if (strcmp(argv[1], "g") == 0)
{
DerpKey SecretKey;
DerpNet_CreateNewKey(&SecretKey);
DerpKey PublicKey;
DerpNet_GetPublicKey(&SecretKey, &PublicKey);
printf("Your PRIVATE key is: ");
PrintKey(&SecretKey);
printf(" - do NOT share this with anyone!\n");
printf("Your PUBLIC key is: ");
PrintKey(&PublicKey);
printf(" - give this to others\n");
}
else if (strcmp(argv[1], "s") == 0)
{
if (argc != 4)
{
PrintHelpAndExit(argv[0]);
}
DerpKey MySecretKey;
DerpNet_CreateNewKey(&MySecretKey);
DerpKey OtherUser = HexToKey(argv[2]);
const char* Message = argv[3];
DerpNet Net;
printf("Connecting to server... ");
if (!DerpNet_Open(&Net, DERP_SERVER_HOST, &MySecretKey))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
printf("Sending message... ");
if (!DerpNet_Send(&Net, &OtherUser, Message, strlen(Message)))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
DerpNet_Close(&Net);
}
else if (strcmp(argv[1], "r") == 0)
{
if (argc != 2)
{
PrintHelpAndExit(argv[0]);
}
DerpKey MySecretKey;
DerpNet_CreateNewKey(&MySecretKey);
DerpKey MyPublicKey;
DerpNet_GetPublicKey(&MySecretKey, &MyPublicKey);
printf("My PUBLIC key is: ");
PrintKey(&MyPublicKey);
printf("\n");
DerpNet Net;
printf("Connecting to server... ");
if (!DerpNet_Open(&Net, DERP_SERVER_HOST, &MySecretKey))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
printf("Waiting for messages...\n");
for (;;)
{
uint8_t* ReceiveData;
uint32_t ReceiveSize;
DerpKey ReceiveUser;
// true in last argument means to block and wait for incoming data in socket
int ReceiveResult = DerpNet_Recv(&Net, &ReceiveUser, &ReceiveData, &ReceiveSize, true);
if (ReceiveResult == 0)
{
// this will never happen here
// but in case you do non-blocking Derp_Recv call, this would mean no more incoming data
break;
}
if (ReceiveResult < 0)
{
printf("ERROR!\n");
exit(1);
}
PrintKey(&ReceiveUser);
printf(": %.*s\n", (int)ReceiveSize, (char*)ReceiveData);
}
DerpNet_Close(&Net);
}
else
{
PrintHelpAndExit(argv[0]);
}
}