-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer2p.cpp
More file actions
255 lines (205 loc) · 6.1 KB
/
peer2p.cpp
File metadata and controls
255 lines (205 loc) · 6.1 KB
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
// Program 2
// EECE446
// Rainier Ring && Charles Clarke
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <filesystem>
#include <netdb.h>
#include <cstring>
#include <unistd.h>
#include <vector>
//Struct creation
struct SearchResult {
uint32_t peerID;
std::string ip;
uint16_t port;
};
namespace fs = std::filesystem;
//Function Prototypes
int lookup_and_connect( const char *host, const char *service );
int sendJoin(int fd, char * strId );
SearchResult search(int fd, char * fileName);
int publish(int fd);
int sendAll(int socketFd, char*buf, int len);
int recvAll(int fd, char*buf, int len);
// Main program, uses while loop for user input. Can publish, connect, and search for files
int main(int argc, char* argv[]) {
if(argc != 4) {
fprintf(stderr, "Usage: %s <registry address> <registry port#> <PeerID>\n", argv[0]);
return 1;
}
int fd;
if((fd = lookup_and_connect(argv[1],argv[2])) < 1) {
fprintf(stderr, "Error Connecting to %s",argv[1]);
}
bool joined = false;
std::string input;
while(input != "EXIT") {
std::cout << "\nEnter a command: ";
std::cin >> input;
if(input == "JOIN") {
if(!joined) {
if(sendJoin(fd,argv[3]) == -1) {
std::cerr <<"Error Joining Network";
}
joined = true;
}
else {
std::cout << "\nYou've Already Joined a Network this Session";
}
}
else if(input == "SEARCH") {
std::cout << "\nEnter a file name: ";
std::string fileName;
std::cin >> fileName;
SearchResult result = search(fd, const_cast<char*>(fileName.c_str()));
if(result.peerID != 0) {
std::cout << "Found file at \nPeer " << result.peerID
<< "\n" << result.ip << ":" << result.port << "\n";
}
else {
std::cout << "File not indexed by registry\n";
}
}
else if(input == "PUBLISH") {
if (publish(fd) == -1)
std::cerr << "Failed to publish";
}
}
close(fd);
}
//Provided by kkredo
int lookup_and_connect( const char *host, const char *service) {
struct addrinfo hints;
struct addrinfo *rp, *result;
int s;
/* Translate host name into peer's IP address */
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
if ( ( s = getaddrinfo( host, service, &hints, &result ) ) != 0 ) {
fprintf( stderr, "stream-talk-client: getaddrinfo: %s\n", gai_strerror( s ) );
return -1;
}
/* Iterate through the address list and try to connect */
for ( rp = result; rp != NULL; rp = rp->ai_next ) {
if ( ( s = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol ) ) == -1 ) {
continue;
}
if ( connect( s, rp->ai_addr, rp->ai_addrlen ) != -1 ) {
break;
}
close( s );
}
if ( rp == NULL ) {
perror( "stream-talk-client: connect" );
return -1;
}
freeaddrinfo( result );
return s;
}
//Uses sendAll function to send a join request to registry
int sendJoin(int fd, char * strId) {
uint32_t peerId = std::stoul(strId);
std::array<uint8_t, 5> buf;
buf[0] = 0;
uint32_t netPeerId = htonl(peerId);
memcpy(&buf[1], &netPeerId, 4);
int len = buf.size();
if (sendAll(fd, reinterpret_cast<char*>(buf.data()), len) == -1) {
return -1;
}
return 0;
}
//returns a struct containing peerID, port#, and IP#, uses send and recv All
SearchResult search(int fd, char * fileName) {
SearchResult result{0, "", 0};
size_t nameLen = std::strlen(fileName);
std::vector<char> buf( nameLen + 2);
buf[0] = 2;
memcpy(&buf[1], fileName, nameLen);
buf[1+nameLen] = '\0';
int msgLen = buf.size();
if (sendAll(fd, buf.data(), msgLen) == -1) {
std::cerr << "Send All failed";
return result;
}
uint8_t response[10];
if (recvAll(fd,reinterpret_cast<char *>(response),sizeof(response)) == -1) {
std::cerr << "Recv All failed";
return result;
}
uint32_t netPeerId, netIp;
uint16_t netPort;
std::memcpy(&netPeerId, response, 4);
std::memcpy(&netIp, response + 4, 4);
std::memcpy(&netPort, response + 8, 2);
char ipStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &netIp, ipStr, sizeof(ipStr));
result.peerID = ntohl(netPeerId);
result.ip = ipStr;
result.port = ntohs(netPort);
return result;
}
//publishes all files from SharedFiles dir. Uses sendAll
int publish(int fd) {
if(!fs::exists("SharedFiles")) return -1;
std::vector<std::string> files;
for (const auto& entry : fs::directory_iterator("SharedFiles")) {
if (entry.is_regular_file()) {
files.push_back(entry.path().filename().string());
}
}
if(files.empty()) {
return -1;
}
std::vector<char> buf;
buf.reserve(1200);
buf.push_back(1);
uint32_t count = htonl(static_cast<uint32_t>(files.size()));
buf.insert(buf.end(),
reinterpret_cast<char*>(&count),
reinterpret_cast<char*>(&count) + sizeof(count));
for (const auto& name : files) {
buf.insert(buf.end(), name.begin(), name.end());
buf.push_back('\0');
}
int msgLen = static_cast<int>(buf.size());
if (sendAll(fd, buf.data(), msgLen) == -1) {
std::cerr << "Send All failed";
return -1;
}
return 0;
}
//Sends data until all data is fully sent
int sendAll(int socketFd, char*buf, int len) {
int tot = 0;
int bytesLeft = len;
int n;
while(tot < len) {
n = send(socketFd,buf+tot, bytesLeft, 0);
if (n == -1) {
return -1;
}
bytesLeft -= n;
tot += n;
}
return tot;
}
//recvs data until all data has been collected
int recvAll(int fd, char*buf, int len) {
int tot = 0;
int n;
while(tot < len) {
n = recv(fd, buf + tot,len - tot, 0);
if (n <= 0) {
return -1;
}
tot += n;
}
return tot;
}