2015-02-05 20:57:01 +00:00
|
|
|
#include <fstream>
|
2015-02-05 11:38:34 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2015-02-05 20:57:01 +00:00
|
|
|
#include <getopt.h>
|
2015-02-05 16:36:25 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
|
|
#include "crypto.h"
|
2015-02-05 11:38:34 +00:00
|
|
|
|
2015-02-05 20:57:01 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
|
{"secret_key_filename", required_argument, NULL, 's'},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
std::string secret_key_filename;
|
|
|
|
|
{
|
|
|
|
|
int option, option_index;
|
|
|
|
|
while ((option = getopt_long(argc, argv, "s:", long_options, &option_index)) != -1) {
|
|
|
|
|
switch (option) {
|
|
|
|
|
case 's':
|
|
|
|
|
secret_key_filename = optarg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string secret_key;
|
|
|
|
|
{
|
|
|
|
|
std::fstream secret_key_file(secret_key_filename, std::fstream::in);
|
|
|
|
|
if (secret_key_file.fail()) {
|
|
|
|
|
std::cerr << "Failed to open secret key file" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
secret_key_file >> secret_key;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 16:36:25 +00:00
|
|
|
int fd = socket(PF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
|
2015-02-05 16:56:29 +00:00
|
|
|
int optval = 1;
|
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
|
2015-02-05 16:36:25 +00:00
|
|
|
|
|
|
|
|
struct sockaddr_in6 server_addr = {0};
|
|
|
|
|
server_addr.sin6_family = AF_INET6;
|
|
|
|
|
server_addr.sin6_addr = in6addr_any;
|
|
|
|
|
server_addr.sin6_port = htons(4990);
|
2015-02-05 11:38:34 +00:00
|
|
|
|
2015-02-05 16:36:25 +00:00
|
|
|
if (bind(fd, (struct sockaddr*) &server_addr, sizeof(server_addr))) {
|
|
|
|
|
perror("bind");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-02-05 11:38:34 +00:00
|
|
|
|
2015-02-05 16:36:25 +00:00
|
|
|
if (listen(fd, 256)) {
|
|
|
|
|
perror("listen");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-02-05 11:38:34 +00:00
|
|
|
|
2015-02-05 20:57:01 +00:00
|
|
|
CryptoPubServer server(fd, secret_key);
|
2015-02-05 16:36:25 +00:00
|
|
|
server.Loop();
|
2015-02-05 11:38:34 +00:00
|
|
|
}
|