-
Notifications
You must be signed in to change notification settings - Fork 4
/
send_peer_local_chain_longer_and_send_blocks.cpp
289 lines (241 loc) · 11.6 KB
/
send_peer_local_chain_longer_and_send_blocks.cpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//clear; rm -frv send_peer_local_chain_longer_and_send_blocks.o; g++-7 -g -fPIC -Wall -c send_peer_local_chain_longer_and_send_blocks.cpp
//C_C++ Header
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstring>
//algorithm fürs string klein machen
#include <algorithm>
//Zum Casten von Char Hex Array nach string (bei md5sum)
#include <iomanip>
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <sstream>
#include <fstream>
#include <pthread.h>
#include <thread>
#include <time.h>
//Network Header
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
//TLS
#include <tls.h>
using namespace std;
extern vector<string> erreichbare_peers;
extern vector<string> config_file_parameter;
extern string blocks_from_db;
extern string redis_command_from_main;
extern string remote_ip;
extern string ip_fuer_block_senden;
void send_peer_local_chain_longer_and_send_blocks()
{
//cout<<"send_peer_local_chain_longer_and_send_blocks"<<endl;
//cout<<"\e[1m"<<"send_peer_local_chain_longer_and_send_blocks.cpp"<<"\e[0m"<<endl;
//cout<<"erreichbare_peers"<<erreichbare_peers[0]<<endl;
//cout<<"SENDE FEHLENDE BLÖCKE AN REMOTE PEER"<<endl;
//Sende BC Protokoll Kommando an remote Peer
//for(unsigned int i = 0 ; i < erreichbare_peers.size(); i++)
//{
//cout<<"Sende Bc-Protokoll Kommando an Remote Peer"<<endl;
//cout<<"erreichbare_peers#"<<erreichbare_peers[i]<<endl;
//TLS Initialisierung################################
//gnuTLS Initilisieren
int tls_init_status = 666;
tls_init_status = tls_init();
//cout<<"tls_init_status send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<tls_init_status<<endl;
//###
struct tls *tls = NULL;
tls = tls_client();
//cout<<"tls_client send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<tls<<endl;
//###
struct tls_config *config = NULL;
config = tls_config_new();
//cout<<"tls_config_new send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<config<<endl;
//###
tls_config_insecure_noverifycert(config);
tls_config_insecure_noverifyname(config);
tls_configure(tls, config);
//##################################################
string bc_peer;
//string bc_peer_port = "8888";
string bc_peer_port = config_file_parameter[1];
//Sende an 172.17.190.78 das die ketten LOKAL (172.17.190.84) Länger sind
//bc_peer = "172.17.190.86";
//bc_peer = erreichbare_peers[i];
bc_peer = ip_fuer_block_senden;
//bc_peer = remote_ip;
//cout<<"erreichbare_peers 1"<<bc_peer<<endl;
//cout<<"erreichbare_peers[i] in send_peer_local_chain_longer_and_send_block.cpp"<<erreichbare_peers[i]<<endl;
//cout<<"bc_peer#"<<bc_peer<<endl;
struct hostent *host;//Hostname struktuieren
host = gethostbyname(bc_peer.c_str()); //eventuell die IP in DNS Namen umwandeln
struct sockaddr_in addr;
addr.sin_addr = *(struct in_addr*)host->h_addr;
int s5;
s5 = socket(PF_INET, SOCK_STREAM, 0); //Socket aufbauen. wenn -1 dann ist fehlgeschlagen
int bc_peer_port_int = atoi(bc_peer_port.c_str());
addr.sin_port = htons(bc_peer_port_int);//Connection Portnummer des Servers festlegen
addr.sin_family = AF_INET;
int connect_status;
connect_status = 666;
connect_status = connect(s5,(struct sockaddr*)&addr, sizeof(addr)); //http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html
if (connect_status == -1)
{
//cout<<"Socket error auf der Gegenseite# "<<connect_status<<endl;
//continue;
//return;
cout<<"send_peer_local_chain_longer_and_send_blocks.cpp exit -1"<<endl;
exit(-1);
}
//An Peer Senden das unsere ketten länger sind
//und Peer veranlassen unsere Ketten zu laden, nach vlaidierung
string bc_command;
bc_command = "local chain longer send get chain command\r\n";
//cout<<"bc_command send to bc_server"<<bc_command<<endl;
int tls_connect_socket_status = 666;
tls_connect_socket_status = tls_connect_socket(tls, s5, "localhost");
//cout<<"tls_connect_socket_status in send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<tls_connect_socket_status<<endl;
//if (tls_connect_socket_status != 0){cout<<" tls_error(tls) in tls_connect_socket_status.cpp"<<tls_error(tls)<<endl; exit(-1);}
if (tls_connect_socket_status != 0){cout<<" tls_error(tls) in tls_connect_socket_status.cpp"<<tls_error(tls)<<endl;}
//bc_command = "local chain longer send get chain command";
//bc_command = "local chain longer send get chain command\n";
//cout<<"bc_command#"<<bc_command<<"#"<<endl;
//send(s5, bc_command.c_str(), bc_command.length(),0);//Daten senden
int tls_write_status = 666;
tls_write_status = tls_write(tls, bc_command.c_str(), strlen(bc_command.c_str()));
//cout<<"tls_write_status send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<tls_write_status<<endl;
if (tls_write_status == -1){cout<<"tls_write_status TLS Error# "<<tls_error(tls)<<endl;exit(-1);}
//cout<<"s#"<<s5<<endl;
close(s5);
tls_close(tls);
tls_free(tls);
tls_config_free(config);
/*
//##################################################################################################################################
//##################################################################################################################################
//##################################################################################################################################
//NUN die Fehlenden Blöcke senden ##############
//Protokoll: "set block to bc_server###"
cout<<"SENDE Fehlende Blöcke"<<endl;
//Eigenen Block aus DB holen und dann senden
//Kettenlänge ermitteln
redis_command_from_main = "";
redis_command_from_main = "LLEN blockchain_list\r\n";
void read_all_chains_from_db();
read_all_chains_from_db();
cout<<"Lokale Block Anzahl blocks_from_db"<<blocks_from_db<<endl;
//Kettenzahl nach long long konvertieren
long long anzahl_ketten_lokal = 0;
string anzahl_ketten_lokal_string = blocks_from_db;
//anzahl_ketten_lokal_string = anzahl_ketten_lokal_string.substr(1,anzahl_ketten_lokal_string.length());
try{anzahl_ketten_lokal_string = anzahl_ketten_lokal_string.substr(1,anzahl_ketten_lokal_string.length());}catch( std::out_of_range& exception ){cout<<"Error send_peer_local_chain_longer_and_send_blocks.cpp anzahl_ketten_lokal_string std::out_of_range"<<endl;}
anzahl_ketten_lokal = stoll(anzahl_ketten_lokal_string.c_str());
//cout<<"unsigned long long # anzahl_ketten_lokal#"<<anzahl_ketten_lokal<<endl;
//Blockanzahl mit for durchlaufen und jeden Block laden
//for (unsigned long long i = 0; i < anzahl_ketten_lokal; i++)
for (long long i = anzahl_ketten_lokal - 1; i > -1; i--)
{
string block_nummer;
stringstream strstream;
strstream << i;
strstream >> block_nummer;
redis_command_from_main = "";
redis_command_from_main = "LINDEX blockchain_list " + block_nummer + "\r\n";
//cout<<"redis_command_from_main Block Nummer#"<<redis_command_from_main<<endl;
void read_all_chains_from_db();
read_all_chains_from_db();
//cout<<"Block Nummer " + block_nummer +" #"<<blocks_from_db<<endl;
//##################################################################################################################################
//Block senden
//TLS Initialisierung################################
//gnuTLS Initilisieren
int tls_init_status = 666;
tls_init_status = tls_init();
//cout<<"tls_init_status validate_chains_peer_local_with_remote_ip_only.cpp 1#"<<tls_init_status<<endl;
//###
struct tls *tls = NULL;
tls = tls_client();
//cout<<"tls_client validate_chains_peer_local_with_remote_ip_only.cpp 1#"<<tls<<endl;
//###
struct tls_config *config = NULL;
config = tls_config_new();
//cout<<"tls_config_new validate_chains_peer_local_with_remote_ip_only.cpp 1#"<<config<<endl;
//###
tls_config_insecure_noverifycert(config);
tls_config_insecure_noverifyname(config);
tls_configure(tls, config);
//##################################################
string bc_peer;
//string bc_peer_port = "8888";
//cout<<"config_file_parameter[1]#"<<config_file_parameter[1]<<endl;
string bc_peer_port = config_file_parameter[1];
//bekommt falsche REMOTE IP, überdenken
bc_peer = ip_fuer_block_senden;
//bc_peer = "172.17.190.84";
cout<<"remote_ip 2#"<<bc_peer<<endl;
//string test_cin; cout<<"CIN"<<endl;cin>>test_cin;
struct hostent *host;//Hostname struktuieren
host = gethostbyname(bc_peer.c_str()); //eventuell die IP in DNS Namen umwandeln
struct sockaddr_in addr;
addr.sin_addr = *(struct in_addr*)host->h_addr;
int s12 = 666;
s12 = socket(PF_INET, SOCK_STREAM, 0); //Socket aufbauen. wenn -1 dann ist fehlgeschlagen
//cout<<"socket s12#"<<s12<<endl;
int bc_peer_port_int = atoi(bc_peer_port.c_str());
addr.sin_port = htons(bc_peer_port_int);//Connection Portnummer des Servers festlegen
addr.sin_family = AF_INET;
int connect_status;
connect_status = 666;
connect_status = connect(s12,(struct sockaddr*)&addr, sizeof(addr)); //http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html
cout<<"connect_status send_peer_local_chain_longer_and_send_blocks.cpp #"<<connect_status<<endl;
//string cin_input;
//cin>>cin_input;
//cout<<cin_input;
if (connect_status == -1)
{
cout<<"Socket error auf der Gegenseite 1 in send_peer_local_chain_longer_and_send_blocks.cpp# "<<connect_status<<endl;
//exit(-1);
}
int tls_connect_socket_status = 666;
tls_connect_socket_status = tls_connect_socket(tls, s12, "localhost");
cout<<"tls_connect_socket_status in send_peer_local_chain_longer_and_send_blocks.cpp 1#"<<tls_connect_socket_status<<endl;
//if (tls_connect_socket_status != 0){cout<<" tls_error(tls)"<<tls_error(tls)<<endl; exit(-1);}
if (tls_connect_socket_status != 0){cout<<" tls_error(tls)"<<tls_error(tls)<<endl;}
string bc_command;
//bc_command = "set block to bc_server### " + blocks_from_db + "###@###\r\n";
bc_command = "set block to peer ### " + blocks_from_db + "###@###\r\n";
cout<<"bc_command#"<<bc_command<<"#"<<endl;
int tls_write_status = 666;
tls_write_status = tls_write(tls, bc_command.c_str(), strlen(bc_command.c_str()));
if (tls_write_status == -1){cout<<"tls_write_status TLS Error send_peer_local_chain_longer_and_send_blocks.cpp# "<<tls_error(tls)<<endl;exit(-1);}
// //Sende Exit Command für "validate_chains_peer_local_with_remote_ip_only.cpp" -> exit_validate_chains_peer_local_with_remote_ip_only_cpp = true
// //Notwendig für "out of range" in validate_chains_peer_local_with_remote_ip_only_cpp Zeile 263
// //bc_command = "exit_validate_chains_peer_local_with_remote_ip_only_cpp " + exit_validate_chains_peer_local_with_remote_ip_only_cpp + " \r\n";
// bc_command = "exit_validate_chains_peer_local_with_remote_ip_only_cpp\r\n";
// cout<<"SENDE exit_validate_chains_peer_local_with_remote_ip_only_cpp"<<endl;
//
// tls_write_status = 666;
// tls_write_status = tls_write(tls, bc_command.c_str(), strlen(bc_command.c_str()));
//close(s12);
//tls_close(tls);
//tls_free(tls);
//tls_config_free(config);
}
//##################################################################################################################################
//##################################################################################################################################
//##################################################################################################################################
}
*/
cout<<"ENDE"<<endl;
//cout<<"getchar();"<<endl;getchar();
}