summaryrefslogtreecommitdiff
path: root/src/protocol/fqterm_socket.h
blob: 175907332b07f46eb27771ad12404ad161350971 (plain)
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
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/

#ifndef FQTERM_SOCKET_H
#define FQTERM_SOCKET_H

// _OS_X_ not defined if i don't include it
#include <QGlobalStatic>

// different
#if defined(Q_OS_WIN32) || defined(_OS_WIN32_)

#include <winsock2.h>

#elif defined(Q_OS_BSD4) || defined(_OS_FREEBSD_)   \
  || defined(Q_OS_MACX) || defined(Q_OS_DARWIN)

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#else

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>

#endif

#include <QAbstractSocket>
#include <QString>
#include <QStringList>
#include <QProcess>

#include "connect_info.h"

class QTcpSocket;
namespace FQTerm {
/*
 * Socket with proxy support. 
 *
 */
class FQTermSocketPrivate: public QObject {

  Q_OBJECT;
 public:
  FQTermSocketPrivate(QObject *parent_ = 0);

  ~FQTermSocketPrivate();

  void flush();
  void setProxy(int nProxyType, bool bAuth, const QString &strProxyHost,
                quint16 uProxyPort, const QString &strProxyUsr,
                const QString &strProxyPwd);
  void connectToHost(const QString &hostname, quint16 portnumber);
  void close();
  QByteArray readBlock(unsigned long maxlen);
  long writeBlock(const QByteArray &data);
  unsigned long bytesAvailable();

 signals:
  void connected();
  void hostFound();
  void connectionClosed();
  void delayedCloseFinished();
  void readyRead();
  void error(QAbstractSocket::SocketError);
  void socketState(int);

 protected slots:
  void socketConnected();
  void socketReadyRead();

 protected:
  // socks5 function
  void socks5_connect();
  void socks5_auth();
  void socks5_reply(const QByteArray &, int);

 private:
  // proxy
  int proxy_type;
  QString proxy_host;
  QString proxy_usr;
  quint16 proxy_port;
  QString proxy_pwd;
  QString host;
  quint16 port;
  int proxy_state;
  bool bauth;

  struct sockaddr_in addr_host;

  QTcpSocket *m_socket;
};

// Virtual base class for FQTermTelnetSocket and FQTermSSHSocket
class FQTermSocket: public QObject {

  Q_OBJECT;
protected:
  conn_info_t conn_info;

 public:
  FQTermSocket(QObject *parent = 0): QObject(parent) {}

  virtual ~FQTermSocket() {}

  virtual void flush() = 0;
  virtual void setProxy(int nProxyType, bool bAuth,
                        const QString &strProxyHost,
                        quint16 uProxyPort,
                        const QString &strProxyUsr,
                        const QString &strProxyPwd) =	0;
  virtual void connectToHost(const QString &host, quint16 port) = 0;
  virtual void close() = 0;
  virtual QByteArray readBlock(unsigned long maxlen) = 0;
  virtual long writeBlock(const QByteArray &data) = 0;
  virtual unsigned long bytesAvailable() = 0;
  virtual bool readyForInput() {return true;}
  virtual bool setTermSize(int col, int row) {return 0;}
  virtual conn_info_t * connectionInfo() { return &conn_info; }
 signals:
  void sshAuthOK();
  void connected();
  void hostFound();
  void connectionClosed();
  void delayedCloseFinished();
  void readyRead();
  void error(QAbstractSocket::SocketError);
  void errorMessage(QString);
  void socketState(int);
  void requestUserPwd(QString *user, QString *pwd, bool *isOK);
  void warnInsecure(const QString&, bool *isOK);
};


}  // namespace FQTerm

#endif  // FQTERM_SOCKET_H