-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.cpp
192 lines (167 loc) · 6.09 KB
/
server.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
#include "block.h"
#include "crypto.h"
#include "bdb.h"
#include "id.h"
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char const *argv[]) {
char pub_pem_ext[] = "-pub.pem";
RSA *rsa_pub;
RSA *rsa_pri;
DB *db;
int ret;
ret = db_create(&db, NULL, 0);
if(ret!=0) {
printf("Error in db creation.\n");
}
ret = db->open(db,NULL,"chain.db", NULL, DB_HASH, DB_CREATE, 0);
if(ret!=0) {
printf("Error in db opening.\n");
}
DBT key, data;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
int sockfd, newsockfd, portNo;
unsigned int clilen;
struct sockaddr_in serv_addr, cli_addr;
if(argc < 2){
printf("Port not specified\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
printf("Socket not opened\n");
return -1;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portNo = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portNo);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("127.0.0.1");
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
printf("ERROR on binding\n");
listen(sockfd, 5);
clilen = (unsigned int)sizeof(cli_addr);
uint8_t tophash[32], tophash_rec[32], tophash_temp[32];
block a;
FILE *fp;
uint8_t req_type;
uint32_t maxSumOfDiff = 0x00, sumdiff_temp = 0x00;
printf("Generating genesis block\n");
genesisblk(&a);
printf("Generated genesis block\n");
printblk(a);
hashblk(tophash, &a);
fp = fopen("tophash.txt", "w");
for(int i = 0; i<32; i++) {
fprintf(fp, "%02x ", (tophash[i]));
}
printf("Written to tophash.txt\n");
fclose(fp);
printf("Adding block to local db\n");
key.data = tophash;
key.size = 32;
data.data = &a;
data.size = sizeof(block);
if(db->put(db, NULL, &key, &data, DB_NOOVERWRITE)!=0) {
printf("db->put() returns non-zero\n");
}
else {
printf("Added block to local db\n");
}
while(newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen)) {
fp = fopen("tophash.txt", "r");
for(int i = 0; i<32; i++) {
fscanf(fp, " %hhx", (tophash+i));
}
fclose(fp);
printf("tophash updated from file \n");
printf("tophash: ");
for(int i = 0; i<32; i++) {
printf("%02x", (tophash[i]));
}
printf("\n");
read(newsockfd, tophash_rec,sizeof(tophash_rec));
printf("Recieved tophash_rec\ntophash_rec: ");
for(int i = 0; i<32; i++) {
printf("%02x", (tophash_rec[i]));
}
printf("\n");
if(hashcmp(tophash_rec,tophash)) {
printf("tophash_rec not identical to tophash\nSending block request\n");
req_type = 0x01;
printf("Sending req_type 0x01 to client\n");
write(newsockfd, &req_type, sizeof(req_type));
read(newsockfd, &a,sizeof(block));
if(verifyHash(&a)) {
printf("Block hash verified\n");
if((a.src)[0]==0x00) {
printblk(a);
hashblk(tophash_temp, &a);
}
else {
printblk(a);
hashblk(tophash_temp, &a);
}
printf("Adding block to local db\n");
key.data = tophash_temp;
key.size = 32;
data.data = &a;
data.size = sizeof(block);
if(db->put(db, NULL, &key, &data, DB_NOOVERWRITE)!=0) {
printf("db->put() returns non-zero\n");
}
else {
printf("Added block to local db\n");
}
sumdiff_temp = sumOfDiff(tophash_temp, db);
printf("sumdiff_temp: %04x\n", sumdiff_temp);
if(sumdiff_temp > maxSumOfDiff) {
maxSumOfDiff = sumdiff_temp;
printf("maxSumOfDiff: %04x\n", maxSumOfDiff);
memcpy(tophash, tophash_temp, 32);
printf("tophash updated\n");
printf("tophash: ");
for(int i = 0; i<32; i++) {
printf("%02x", (tophash[i]));
}
printf("\n");
fp = fopen("tophash.txt", "w");
for(int i = 0; i<32; i++) {
fprintf(fp, "%02x ", (tophash[i]));
}
printf("Written to tophash.txt\n");
fclose(fp);
}
else {
printf("maxSumOfDiff: %04x\n", maxSumOfDiff);
printf("tophash not updated\n");
printf("tophash: ");
for(int i = 0; i<32; i++) {
printf("%02x", (tophash[i]));
}
printf("\n");
}
}
else {
printf("Block rejected!\n");
}
}
else {
req_type = 0x02;
printf("tophash_rec identical to tophash\n");
printf("Sending req_type 0x02 to client\n");
write(newsockfd, &req_type, sizeof(req_type));
}
close(newsockfd);
}
if (newsockfd < 0) {
printf("Error accepting\n");
return -1;
}
close(sockfd);
printchain(tophash, db);
return 0;
}