-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
198 lines (150 loc) · 4.83 KB
/
main.cpp
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
//
// Created by Sebastian Balz on 1/13/17.
//
#include <search.h>
#include "main.h"
#include "PipeReader.h"
#include <unistd.h>
#include <logging.h>
using namespace std;
/**
* PipeRelay.v.2
* Version 2
* support multithreading and pcap detection
*
* this application has the goal to connect several ingoing pipes with one outgoing pipe
* The ingoing pipes are filled with remote captured from several probes. Each probes data is stored in one singe named pipe.
* The data of each probe can be grabbed by an ssh connection witch is piping the tcpdump-data the named pipe
* Wireshark e.g is able the read data from a singe named pipe,
* but if there is more than on probe, the data of each probe has to be bundled to a singe outgoing pipe witch can be captured
* by Wireshark or by an other application
*
*
* This program is the binding link of ingoing pipes and the outpoing pipe.
* Each ingoing pipe has an Thread witch is reader pcap package. If the pcap is completely read, the thread sends the pcap to the outgoing pipe
* to avoid simultaneous writes to the outgoing pipe each write is locked with an mutex
*/
/*
* - ssh configFile
* - pipe number
* - ch channel
* - location
* - log loglevel
* - encap
* - remoteSetup
*/
int main(int argc, char *argv[]) {
Log::log("asd",Error);
configuration(argc,argv); // read parameter
// open Writer
wr = new PipeWriter(new string(location + "Out"), new string("PipeWriter"));
wr->open();
// open ssh
if(doSSH){
handleSSH(configFile);
}
// open named pipes
if(numberOfPipes > 0)
handleNamedPipe(numberOfPipes);
sleep(1);
// wait for finish
for(int i = 0; i<l->size(); i++){
l->at(i)->join();
}
return 0;
}
void handleSSH(string s){
Log::log("main open ssh with config file: \t" +s,Message);
ReadConfig *r = new ReadConfig(&s);
r->open();
while(r->hasNext()){
ReadConfig::entry *entry = r->getNextValid();
Log::log("main : establish new ssh conection to : \033[1;34m"+entry->client+ "\033[0m with the user : \033[1;34m"
+entry->user +"\033[0m and run there : [ \033[1;34m" + entry->execute+"\033[0m ]",Message);
ssh *s = new ssh(entry);
delete entry;
l->push_back(new thread(startSSHReader, s));
}
}
void handleNamedPipe(int numberOfThreads){
Log::log("main" + ("cread " + to_string(numberOfThreads )+" named pipes" ),Message);
threadStartPipe *w;
thread *list[numberOfThreads];
// creat all threads
for(int i = 0; i <numberOfThreads; i++) {
string *loc = new string(location + "In" + to_string(i));
string *name = new string("Reader: " + to_string(i));
w = new threadStartPipe(loc, wr, name);
l->push_back(new thread(startPipeReader, w));
}
}
void startPipeReader(threadStartPipe *w){
Log::log("main:pipe : startThread",Message);
PipeReader *r = new PipeReader(w->location,w->name,w->message, w->log);
r->open();
while(true){
char c;
r->read(&c,1);
wr->write(&c,1);
}
Log::log("main:pipe" + *w->name + " : leaf",Message);
delete w;
delete r;
return;
}
void startSSHReader(ssh *s){
Log::log("main : ssh startThread",Info);
s->open();
while(true){
char c;
s->read(&c,1);
wr->write(&c,1);
}
delete s;
}
void printUsage(){
cout<<"usage:\t\033[1;32m"
<<"\n\t[-ssh <configfile> ] \t\tread config file "
<<"\n\t[-pipe <nuberOf>] \t\topen <numberOf named pipes und store them at <location>"
<<"\n\t[-l <location>] \t\tchange the default location to <location> and apend In and Out<n>"
<<"\n\t[-log <loglevel>] \t\tset loglevel default : 0"
<<"\033[0m"<<endl;
exit(-1);
}
void wrongsage(){
cout<<"wrong usage!\t use : \033[1;31m"
<<endl;
printUsage();
exit(-1);
}
/*
* -ssh configFile
* -pipe number
* -ch channel
* -l location
*/
void configuration(int numberOfArg, char *argv[]) {
Log::log("main read configuration", Info);
for (int i = 1; i < numberOfArg; i += 2) {
bool hasNext = i + 1 < numberOfArg;
string *s = new string(argv[i]);
if (*s == "-ssh" & hasNext) {
doSSH = true;
configFile = string(argv[i + 1]);
} else if (*s == "-pipe" & hasNext) {
numberOfPipes = atoi(argv[i + 1]);
doPipe = true;
} else if (*s == "-l" & hasNext)
location = string(argv[i + 1]);
else if (*s == "-h")
printUsage();
else if (*s == "-log")
Log::setLogLevel((LogLevel)atoi(argv[i + 1]));
else
wrongsage();
}
if (!(doPipe || doSSH)) {
numberOfPipes = 2;
Log::log("main there was no input configuration -> 2 pipes", Info);
}
}