forked from telehash/telehash-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.c
140 lines (125 loc) · 3.36 KB
/
ping.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
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "switch.h"
#include "util.h"
#include "ext.h"
#include "util_unix.h"
#include "crypt.h"
int main(void)
{
unsigned char buf[2048];
switch_t s;
bucket_t seeds;
chan_t c, c2;
packet_t p;
path_t from;
int sock, len, blen;
struct sockaddr_in sad, sa;
crypt_init();
s = switch_new(0);
switch_init(s,util_file2packet("id.json"));
if(!s->id)
{
printf("failed to load id.json: %s\n", crypt_err());
return -1;
}
printf("loaded hashname %s\n",s->id->hexname);
seeds = bucket_load(s->index, "seeds.json");
if(!seeds || !bucket_get(seeds, 0))
{
printf("failed to load seeds.json: %s\n", crypt_err());
return -1;
}
// create a udp socket
if( (sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP) ) < 0 )
{
printf("failed to create socket\n");
return -1;
}
memset((char *)&sad,0,sizeof(sad));
memset((char *)&sa,0,sizeof(sa));
sa.sin_family = sad.sin_family = AF_INET;
sad.sin_port = htons(0);
sad.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (sock, (struct sockaddr *)&sad, sizeof(sad)) < 0)
{
printf("bind failed");
return -1;
}
// create/send a ping packet
c = chan_new(s, bucket_get(seeds, 0), "seek", 0);
p = chan_packet(c);
packet_set_str(p,"seek",s->id->hexname);
chan_send(c, p);
while((p = switch_sending(s)))
{
if(util_cmp(p->out->type,"ipv4")!=0)
{
packet_free(p);
continue;
}
printf("sending %s packet %d %s\n",p->json_len?"open":"line",packet_len(p),path_json(p->out));
path2sa(p->out, &sa);
if(sendto(sock, packet_raw(p), packet_len(p), 0, (struct sockaddr *)&sa, sizeof(sa))==-1)
{
printf("sendto failed\n");
return -1;
}
packet_free(p);
}
from = path_new("ipv4");
len = sizeof(sa);
if ((blen = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&sa, (socklen_t *)&len)) == -1)
{
printf("recvfrom failed\n");
return -1;
}
sa2path(&sa, from); // inits ip/port from sa
p = packet_parse(buf,blen);
printf("received %s packet %d %s\n", p->json_len?"open":"line", blen, path_json(from));
switch_receive(s,p,from);
while((p = switch_sending(s)))
{
if(util_cmp(p->out->type,"ipv4")!=0)
{
packet_free(p);
continue;
}
printf("Sending %s packet %d %s\n",p->json_len?"open":"line",packet_len(p),path_json(p->out));
path2sa(p->out, &sa);
if(sendto(sock, packet_raw(p), packet_len(p), 0, (struct sockaddr *)&sa, sizeof(sa))==-1)
{
printf("sendto failed\n");
return -1;
}
packet_free(p);
}
from = path_new("ipv4");
len = sizeof(sa);
while((blen = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&sa, (socklen_t *)&len)) != -1)
{
sa2path(&sa, from); // inits ip/port from sa
p = packet_parse(buf,blen);
printf("Received %s packet %d %s\n", p->json_len?"open":"line", blen, path_json(from));
switch_receive(s,p,from);
while((c2 = switch_pop(s)))
{
if(c2 == c)
{
printf("got pong state %d from %s see %s\n",c->ended,c->to->hexname,packet_get_str(chan_pop(c),"see"));
return 0;
}
while((p = chan_pop(c)))
{
printf("unhandled channel packet %.*s\n", p->json_len, p->json);
packet_free(p);
}
}
}
printf("recvfrom failed\n");
return -1;
return 0;
}