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
|
/*
(c) Copyright 2012 - 2018 Anton Sviridenko
https://picapica.im
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef PICA_NODE_H
#define PICA_NODE_H
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/time.h>
#define SOCKET int
#define SOCKET_ERROR -1
#endif
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <time.h>
#include "PICA_nodeaddrlist.h"
#include "PICA_id.h"
#include "PICA_proto.h"
#include <event2/event.h>
#define PICA_COMM_PORT 51914
//#define PICA_DATA_PORT 51915
//#define PICA_NODE_PORT 51916
#define PICA_NEWCONN_WAITANONTLSACCEPT 1
#define PICA_NEWCONN_WAITREQUEST 2
#define PICA_NEWCONN_SENDINGOK 3
#define PICA_NEWCONN_SENDINGREJECT 4
#define PICA_NEWCONN_WAITANONTLSSHUTDOWN 5
#define PICA_CLSTATE_WAITANONTLSSHUTDOWN 1
//#define PICA_CLSTATE_SENDINGRESP 1
#define PICA_CLSTATE_TLSNEGOTIATION 2
#define PICA_CLSTATE_CONNECTED 3
#define PICA_CLSTATE_MULTILOGIN_SECONDARY 4
//LOCAL - both clients are connected to the same node
//N2NCLR - caller client is connected to this node
//N2NCLE - callee client is connected to this node
#define PICA_CCLINK_LOCAL_WAITREP 1
#define PICA_CCLINK_LOCAL_WAITCONNCLE 2
#define PICA_CCLINK_LOCAL_WAITCONNCLEANONTLSSHUTDOWN 3
#define PICA_CCLINK_LOCAL_WAITCONNCLR 4
#define PICA_CCLINK_LOCAL_WAITCONNCLRANONTLSSHUTDOWN 5
#define PICA_CCLINK_LOCAL_ACTIVE 6
#define PICA_CCLINK_N2NCLR_WAITSEARCH 7
#define PICA_CCLINK_N2NCLR_WAITREP 8
#define PICA_CCLINK_N2NCLR_WAITCONNCLR 9
#define PICA_CCLINK_N2NCLR_WAITCONNCLRANONTLSSHUTDOWN 10
#define PICA_CCLINK_N2NCLR_ACTIVE 11
#define PICA_CCLINK_N2NCLE_WAITREP 12
#define PICA_CCLINK_N2NCLE_WAITCONNCLE 13
#define PICA_CCLINK_N2NCLE_WAITCONNCLEANONTLSSHUTDOWN 14
#define PICA_CCLINK_N2NCLE_ACTIVE 15
#define PICA_NETWORKNAME "TESTNET" // TEMP //CONF
#define NEWCONN_BUFSIZE 58 //FIXME this is the size of the CONNID packet, which is followed by TLS session negotiation data.
//TLS data must not be interpreted as a PICA packet by PICA_processdatastream(),
//so the buffer size is limited by the size of CONNID.
#define DEFAULT_BUF_SIZE 4096
//Relay buffers of a c2c link. procmsg_N2NMSG() drops the whole link when an
//incoming N2NMSG does not fit into the free space left in these, and the
//announced payload length of an N2NMSG is a 16 bit field less the two peer ids
//(65535 - 2 * PICA_ID_SIZE), so anything below 64K makes that a routine event
//instead of an exceptional one - a link carrying a call has a backlog in both
//directions most of the time. Deliberately not DEFAULT_BUF_SIZE: that one is
//also the r_buf/w_buf granularity paid for by every connected client.
#define C2C_BUF_SIZE 65536
struct nodelink;
struct client
{
unsigned char id[PICA_ID_SIZE];
struct client *up;
struct client *left;
struct client *right;
struct client *next_multi;
SOCKET sck_comm;
struct sockaddr_in addr;
SSL *ssl_comm;
unsigned char *r_buf;
unsigned char *w_buf;
unsigned int buflen_r;
unsigned int buflen_w;
unsigned int r_pos;
unsigned int w_pos;
unsigned int btw_ssl;//number of bytes to send, passed to SSL_write
//If SSL_write has to be retried, the same argument values must be passed, while w_pos may be increased
//
int state;
struct client* next;//a client structure can be in a tree and a linked list at the same time
struct client* prev;
time_t tmst;
int disconnect_ticking;//on sending a message TO the client this var is set to 1, on receiving any message FROM the client it is reset to 0
struct event *ev_read;
struct event *ev_write;
//Pending "get my UDP address" request. The token is what ties a datagram
//arriving at the UDP socket to this connection, see process_udp_read()
unsigned char udpaddr_token[PICA_PROTO_MYUDPADDR_TOKENSIZE];
int udpaddr_token_issued;
time_t udpaddr_token_tmst;
struct client *udpaddr_token_next;//next client in the same bucket of udpaddr_tokens[]
//how many more MYUDPADDRREQ messages are served right now, see client_udpaddr_req_allowed()
unsigned int udpaddr_req_allowance;
time_t udpaddr_req_tmst;
};
struct cclink //describes a connection between two clients through the server
{
//unsigned int cclink_id;
unsigned int state;
time_t tmst;
struct client *p1;//caller
struct client *p2;//callee
unsigned char *caller_id;
unsigned char *callee_id;
struct nodelink *caller_node;
struct nodelink *callee_node;
SOCKET sck_p1;
SOCKET sck_p2;
int jam_p1p2;
int jam_p2p1;
char* buf_p1p2;
char* buf_p2p1;
unsigned int buflen_p1p2;
unsigned int buflen_p2p1;
unsigned int bufpos_p1p2;
unsigned int bufpos_p2p1;
struct cclink *next;
struct cclink *prev;
int disconnect_ticking;
struct event *ev_read_p1;
struct event *ev_read_p2;
struct event *ev_write_p1;
struct event *ev_write_p2;
};
enum newconn_type
{
NEWCONN_UNKNOWN,
NEWCONN_C2N,
NEWCONN_C2C,
NEWCONN_N2N
};
struct newconn //describes a new (client) connection
{
SOCKET sck;
SSL *anonssl;
struct sockaddr_in addr;
time_t tmst;
int pos;
int state;
enum newconn_type type;
unsigned char buf[NEWCONN_BUFSIZE];
union
{
struct client *cl;
struct cclink *cc;
} iconn;
struct event *ev;
};
struct nodelink
{
struct sockaddr_in addr;
SOCKET sck;
SSL *anonssl;
unsigned int addr_type;//node address type - PICA_PROTO_NEWNODE_IPV4,PICA_PROTO_NEWNODE_IPV6,...
void *node_addr;//pointer to the address structure {PICA_nodeaddr_ipv4, PICA_nodeaddr_ipv6,PICA_nodeaddr_dns,..}
unsigned char *r_buf;
unsigned char *w_buf;
unsigned int buflen_r;
unsigned int buflen_w;
unsigned int r_pos;
unsigned int w_pos;
unsigned int btw_ssl;
struct nodelink *next;
struct nodelink *prev;
time_t tmst;
int disconnect_ticking;
struct event *ev_read;
struct event *ev_write;
};
extern SSL_CTX *anon_ctx;
#endif
|