forked from M-griffin/EtherTerm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketState.hpp
174 lines (143 loc) · 3.37 KB
/
socketState.hpp
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
#ifndef __MY_SOCKET__H
#define __MY_SOCKET__H
// EtherTerm SVN: $Id$
// Source: $HeadURL$
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
#include "renderer.hpp"
#ifdef TARGET_OS_MAC
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#elif _WIN32
#include <SDL.h>
#include <SDL_net.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#endif
#include <libssh/libssh.h>
#include <iostream>
#include <string>
/*
* Virtual Class Container for Common access to different
* Socket And connection types.
*/
class SocketState
{
public:
virtual ~SocketState()
{
std::cout << "Shutting Down SocketState" << std::endl;
}
virtual int sendSocket(unsigned char *message, Uint32 len) = 0;
virtual int recvSocket(char *message)= 0;
virtual int pollSocket() = 0;
virtual bool onEnter() = 0;
virtual bool onExit() = 0;
};
/*
* Class for TCP Telnet Sockets
*/
class SDL_Socket : public SocketState
{
public:
SDL_Socket(std::string _host, int _port) :
host(_host),
port(_port),
sock(nullptr),
set(nullptr)
{ }
virtual ~SDL_Socket()
{
std::cout << "Shutting Down SDL_SocketState" << std::endl;
}
virtual int sendSocket(unsigned char *message, Uint32 len);
virtual int recvSocket(char *message);
virtual int pollSocket();
virtual bool onEnter();
virtual bool onExit();
private:
std::string host;
int port;
TCPsocket sock;
SDLNet_SocketSet set;
};
/*
* Class for TCP FTP Sockets
*/
class FTP_Socket : public SocketState
{
public:
FTP_Socket(std::string _host, int _port, std::string _user, std::string _pass)
:
host(_host),
port(_port),
userId(_user),
password(_pass),
controlSocket(nullptr),
listenSocket(nullptr),
dataSocket(nullptr),
set(nullptr)
{ }
virtual ~FTP_Socket()
{
std::cout << "Shutting Down FTP SDL_SocketState" << std::endl;
}
virtual int sendSocket(unsigned char *message, Uint32 len);
virtual int recvSocket(char *message);
virtual int pollSocket();
virtual bool onEnter();
virtual bool onExit();
private:
std::string host;
int port;
std::string userId;
std::string password;
TCPsocket controlSocket;
TCPsocket listenSocket;
TCPsocket dataSocket;
SDLNet_SocketSet set;
};
/*
* Class for TCP SSH Connections
*/
class SSH_Socket : public SocketState
{
public:
SSH_Socket(std::string _host, int _port, std::string _user, std::string _pass)
:
host(_host),
port(_port),
userId(_user),
password(_pass),
sshChannel(nullptr),
session(nullptr)
{ }
virtual ~SSH_Socket()
{
std::cout << "Shutting Down SSH_SocketState" << std::endl;
}
virtual int sendSocket(unsigned char *message, Uint32 len);
virtual int recvSocket(char *message);
virtual int pollSocket();
virtual bool onEnter();
virtual bool onExit();
// Specific Functions for SSH
int verify_knownhost();
int authenticate_console();
int authenticate_kbdint();
void error()
{
std::cout << "Authentication failed: "
<< ssh_get_error(session) << std::endl;
}
private:
std::string host;
int port;
std::string userId;
std::string password;
ssh_channel sshChannel;
ssh_session session;
};
#endif