forked from googleprojectzero/Jackalope
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.h
140 lines (100 loc) · 3.39 KB
/
server.h
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
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <vector>
#include <string>
#include <unordered_map>
#include "sample.h"
#include "mutex.h"
#include "coverage.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#include <windows.h>
#include "winsock.h"
typedef SOCKET socket_type;
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef int socket_type;
#define INVALID_SOCKET (-1)
#define closesocket close
#endif
#define MAX_CONNECTIONS 8
#define DEFAULT_SERVER_PORT 8000
// save state every 20 minutes
#define SERVER_SAVE_INERVAL (5 * 60)
#define MAX_SERVER_IDENTICAL_CRASHES 4
class CoverageServer;
class ClientSocket {
public:
socket_type client_socket;
CoverageServer *server;
};
class ServerCommon {
protected:
int Read(socket_type sock, void *buf, size_t size);
int Write(socket_type sock, const char *buf, size_t size);
int SendSample(socket_type sock, Sample &sample);
int RecvSample(socket_type sock, Sample &sample);
int SendString(socket_type sock, std::string &str);
int RecvString(socket_type sock, std::string &str);
int SendCoverage(socket_type sock, Coverage &coverage);
int RecvCoverage(socket_type sock, Coverage &coverage);
};
class CoverageServer : public ServerCommon {
public:
CoverageServer() : server_timestamp(0), server_port(DEFAULT_SERVER_PORT), num_samples(0), num_crashes(0), num_unique_crashes(0) { }
// for incremental updates
struct TimestampIndex {
uint64_t timestamp;
uint64_t index;
};
struct ServerCorpus {
std::vector<Sample> samples;
std::vector<TimestampIndex> timestamps;
};
ServerCorpus corpus;
Coverage total_coverage;
std::string out_dir;
size_t num_crashes;
size_t num_unique_crashes;
std::string crash_dir;
std::string sample_dir;
size_t num_samples;
Mutex connection_mutex;
size_t num_connections;
int ServeUpdates(socket_type sock);
int ReportCrash(socket_type sock);
int ReportNewCoverage(socket_type sock);
uint64_t GetIndex(std::vector<TimestampIndex> ×tamps, uint64_t timestamp, uint64_t last_index);
void SaveState();
void RestoreState();
bool OnNewCoverage(Coverage *client_coverage);
void UpdateModuleCoverage(ModuleCoverage *client_coverage);
bool HasNewCoverage(Coverage *client_coverage, Coverage *new_coverage);
void RunServer();
int HandleConnection(socket_type sock);
void StatusThread();
void Init(int argc, char **argv);
void SetupDirectories();
bool CheckFilename(std::string& filename);
uint64_t server_timestamp;
ReadWriteMutex mutex;
// separate mutex for writing crashes
Mutex crash_mutex;
std::unordered_map<std::string, int> unique_crashes;
std::string server_ip;
uint16_t server_port;
};