Commit 9e44ba71 authored by Yury Popov's avatar Yury Popov

Base server code

parents
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:SCEPServer.xcodeproj">
</FileRef>
</Workspace>
//
// main.cpp
// SCEPServer
//
// Created by Yury Popov on 12.02.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <openssl/crypto.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
RSA *cakey;
X509 *cacer;
int sock;
char checkbuf(const char* buf) {
size_t len = strlen(buf);
if (len < 6) return 0;
if (strncmp("GET ", buf, 4) == 0) {
if (strncmp("\r\n\r\n", &buf[len-4], 4)==0) return 1;
if (strncmp("\n\r\n\r", &buf[len-4], 4)==0) return 1;
if (strncmp("\n\n", &buf[len-2], 2)==0) return 1;
return 0;
}
return -1;
}
void do_500(int sock) {
const char* buf = "HTTP/1.1 500 Internal Server Error\n\rConnection: close\n\rContent-Length: 0\n\r\n\r";
ssize_t off = 0, len;
while (1) {
len = send(sock, &buf[off], strlen(buf)-off, 0);
if (len < 0) break;
off += len;
if (off == strlen(buf)) break;
}
}
void do_op(int sock, const char* op, const char* msg) {
if (strcmp(op, "GetCACer")==0) {
size_t len = i2d_X509(cacer, 0);
printf("%lu\n",len);
do_500(sock);
return;
}
printf("REQUEST %s %s\n",op,msg);
do_500(sock);
}
static void* client_proc (void* arg) {
int sock = *(int*)arg;
std::time_t start = std::time(0);
char buf[4096]; memset(buf, 0, 4096);
ssize_t off = 0, len;
while (1) {
len = recv(sock, &buf[off], 4096-off, MSG_DONTWAIT);
if (len > 0) off += len;
else if (len == 0) break;
else if (len == -1) {
if (errno == 35) {
char bc = checkbuf(buf);
if (bc == 1) break;
if (bc == -1) {
printf("Unexpected data: %d\n",sock);
close(sock);
pthread_exit(0);
}
if ((std::time(0) - start) > 60) {
printf("Read timeout: %d\n",sock);
close(sock);
pthread_exit(0);
}
usleep(100);
continue;
}
printf("err %d\n",errno);
break;
}
}
char* get = strstr(buf, "GET ");
if (get) {
get = strstr(get, " "); get++;
get = strstr(get, "?");
if (get) {
get++;
len = strcspn(get, " ");
char addr[len+1]; addr[len]=0;
strncpy(addr,get,len);
char* op = strstr(addr, "operation=");
char* msg = strstr(addr, "message=");
if (op) {
op += 10;
len = strcspn(op, "&");
if (len != -1) op[len] = 0;
if (msg) {
msg += 8;
len = strcspn(msg, "&");
if (len != -1) msg[len] = 0;
} else msg = 0;
do_op(sock,op,msg);
} else do_500(sock);
} else do_500(sock);
} else do_500(sock);
close(sock);
return 0;
}
static void* socket_proc (void* arg) {
while (1) {
int s = accept(sock, 0, 0);
if (s < 0) continue;
pthread_t th;
pthread_create(&th, 0, &client_proc, &s);
}
return 0;
}
int main(int argc, const char * argv[]) {
const char *cacerf, *cakeyf, *sockf;
if (argc == 4) {
cacerf = argv[1];
cakeyf = argv[2];
sockf = argv[3];
} else return 1;
OPENSSL_init();
printf("Loading keys...");
{
FILE *fp;
fp = fopen(cakeyf, "r");
PEM_read_RSAPrivateKey(fp, &cakey, 0, 0);
if (!cakey) d2i_RSAPrivateKey_fp(fp, &cakey);
if (!cakey) return 1;
fclose(fp);
fp = fopen(cacerf, "r");
PEM_read_X509(fp, &cacer, 0, 0);
if (!cacer) d2i_X509_fp(fp, &cacer);
if (!cacer) return 1;
fclose(fp);
printf(" [ DONE ]\n");
}
printf("Starting server...");
{
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) return 1;
sockaddr_un sadr;
sadr.sun_family = AF_UNIX;
sadr.sun_len = strlen(sockf);
strcpy(sadr.sun_path, sockf);
sadr.sun_len += sizeof(sadr.sun_len)+sizeof(sadr.sun_family);
remove(sockf);
if (bind(sock, (const struct sockaddr *)&sadr, sadr.sun_len) == -1) return 1;
if (listen(sock, 10) == -1) return 1;
printf(" [ DONE ]\n");
}
printf("Start receiving connections...");
pthread_t th;
pthread_create(&th, 0, &socket_proc, 0);
printf(" [ DONE ]\n");
pthread_join(th, 0);
return 0;
}
#pragma GCC diagnostic pop
//
// main.cpp
// SCEPTest
//
// Created by Yury Popov on 12.02.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment