-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
48 lines (35 loc) · 1.17 KB
/
server.cpp
File metadata and controls
48 lines (35 loc) · 1.17 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
#include <cstdio>
#include <thread>
#include <windows.h>
#include "server.hpp"
#include "rsa.hpp"
#ifdef WIN32
#define MSG_NOSIGNAL 0
#define socklen_t int
#endif
// createHostKey guides the client to chat.
void createHostKey(){
int accept_socket, socketfd;
struct sockaddr_in addr;
socketfd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
bind(socketfd, (struct sockaddr *)&addr, sizeof(addr));
listen(socketfd, 10);
// int accept_socket is the client socket descriptor
socklen_t addrlen;
accept_socket = accept(socketfd, (struct sockaddr *)&addr, &addrlen);
// int key is our public key
uint64_t key = establishHostKey(accept_socket);
printf("This should be the same: %lld\n", key);
closesocket(socketfd);
}
uint64_t establishHostKey(int accept_socket) {
RSA host;
char buffer[256];
sprintf(buffer, "%lld", host.getPubKey());
send(accept_socket, buffer, sizeof(buffer), 0);
recv(accept_socket, buffer, sizeof(buffer), 0);
return host.Decrypt(atoi(buffer));
}