-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgComponent.cpp
More file actions
200 lines (181 loc) · 6.38 KB
/
msgComponent.cpp
File metadata and controls
200 lines (181 loc) · 6.38 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
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
198
199
200
/*
(c) Matthew Slocum 2015
msgComponent.cpp
msgComponent is resbonaible for sending and revieving messages
upon creation it spins off a thread to listen for incoming messages.
the parent process takes input from the user and executes commands or sends messages.
*/
#include <iostream>
#include <stdlib.h>
#include <boost/asio.hpp>
#include <pthread.h>
#include <string>
#include "msgComponent.h"
#include "regComponent.h"
#include "secComponent.h"
using namespace std;
using boost::asio::ip::tcp;
msgComponent::msgComponent() {
}
//Code derived from:
// daytime_server.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See
// at http://www.boost.org/LICENSE_1_0.txt)
//Function for listener thread
//This fucntion listens for incoming connections, recieves a message, decrypts it and displays it.
void* listen(void *threadid) {
try
{
extern regComponent* reg;
extern secComponent* sec;
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 6283);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
boost::system::error_code ec;
acceptor.accept(*stream.rdbuf(), ec);
//if the is an incomming connection
if (!ec)
{
string message = "";
string line;
char c;
char msg [1034];
int msg_count=0;
//get message
while(stream.get(c)) {
msg[msg_count++]=c;
}
//make holder for plaintext
char plaintext[msg_count];
//decrypt message and store in plaintext
sec->decrypt(msg, msg_count, reg->key, 20, plaintext);
//display plaintext message if it exists
if(msg_count>0) {
cout << endl << endl;
for(int i=0; i<msg_count-10; i++) {
cout << plaintext[i];
}
cout << endl;
}
//redraw user input prompt
cout.clear();cout.flush();
cout << endl << "TauNet [TO: " << reg->name_list[reg->dest] << "]> "; cout.clear(); cout.flush();
}
}
}
//catch any std exceptions
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
pthread_exit(NULL);
}
void msgComponent::run() {
//spin off thread for listener
pthread_t thread;
int rc;
rc = pthread_create(&thread, NULL, listen, NULL);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
command="";
//draw some ui
draw_header();
draw_dest();
//c&c loop
while(command!=":q") {
//get input
get_input();
//parse special commands
//list destinations
if(command==":dlist") {
reg->printDest();
}
//set destination
else if (command.find(":dset")==0) {
//cout << command.substr(6) << endl;
int d = atoi((command.substr(6)).c_str());
d--;
if(d>=0 && d<reg->dest_count) {
reg->setDest(d);
draw_dest();
} else {
cout << "Destination out of range." << endl;
}
}
//else, its a message, send it!
//CODE derived from daytime_client.cpp (Boost::asio)
//Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//Distributed under the Boost Software License, Version 1.0 at http://www.boost.org/LICENSE_1_0.txt)
else {
try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(reg->dest_list[reg->dest], "6283");
//attempt to open connection
tcp::iostream stream(reg->dest_list[reg->dest], "6283");
if (!stream)
{
std::cout << "Unable to connect: " << stream.error().message() << std::endl;
}
//build message
string message;
message = string("version: 0.2\r\n")
+ "from: " + reg->username + "\r\n"
+ "to: " + reg->name_list[reg->dest] + "\r\n"
+ "\r\n"
+ command + "\r\n";
//init holder for ciphertext
char ciphertext [message.length()+10];
//encrypt the message
sec->encrypt(message,reg->key,20, ciphertext);
//send the message
for(unsigned int i=0; i<message.length()+10; i++) {
stream << ciphertext[i];
}
//close the connection
stream.close();
}
catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
}
}
//let the msgComponent know about the regComponent
void msgComponent::bind_regComponent(regComponent * in) {
reg = in;
}
//let the msgComponent know about the secComponent
void msgComponent::bind_secComponent(secComponent * in) {
sec = in;
}
void msgComponent::draw_header() {
system("clear");
cout << "|------------TauNet Messenger------------|" << endl;
cout << "| Commands" << endl;
cout << "| :q - quit" << endl;
cout << "| :dlist - list destinations" << endl;
cout << "| :dset # - set destination" << endl;
cout << "|------------TauNet Messenger------------|" << endl;
}
void msgComponent::draw_dest() {
cout << "*** DESTINATION: [" << reg->name_list[reg->dest] << "]" << reg->dest_list[reg->dest] << " ***" << endl;
}
void msgComponent::get_input() {
cout << endl;
cin.clear();
cout << "TauNet [TO: " << reg->name_list[reg->dest] << "]> ";
getline(cin,command);
}
void msgComponent::draw_get_input() {
cout << "TauNet [TO: " << reg->name_list[reg->dest] << "]> ";
}