summaryrefslogtreecommitdiffstats
path: root/rfbplayer/RfbProto.cxx
blob: add2096dc5eba10bc66ff44e2cd9014bde8b9c8d (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
/* Copyright (C) 2004 TightVNC Team.  All Rights Reserved.
 *    
 * This 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 software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- RFB Protocol

#include <rfb/Exception.h>
#include <rfb/LogWriter.h>

#include <rfbplayer/RfbProto.h>

using namespace rfb;

static LogWriter vlog("RfbProto");

//
// Constructor
//

RfbProto::RfbProto(char *fileName) {
  is = NULL;
  reader = NULL;
  newSession(fileName);
}

//
// Destructor
//

RfbProto::~RfbProto() {
  if (is) delete is;
  is = 0;
  if (reader) delete reader;
  reader = 0;
}

void RfbProto::newSession(char *fileName) {
  // Close the previous session
  if (is) {
    delete is;
    is = NULL;
    delete reader;
    reader = NULL;
  }

  // Begin the new session
  if (fileName != NULL) {
    is = new FbsInputStream(fileName);
    reader = new CMsgReaderV3(this, is);
    initialiseProtocol();
  }
}

void RfbProto::initialiseProtocol() {
  state_ = RFBSTATE_PROTOCOL_VERSION;
}

void RfbProto::processMsg()
{
  switch (state_) {

  case RFBSTATE_PROTOCOL_VERSION: processVersionMsg();       break;
  case RFBSTATE_SECURITY:         processSecurityMsg();      break;
  case RFBSTATE_INITIALISATION:   processInitMsg();          break;
  case RFBSTATE_NORMAL:           reader->readMsg();         break;
  default:
    throw rfb::Exception("RfbProto::processMsg: invalid state");
  }
}

void RfbProto::processVersionMsg()
{
  vlog.debug("reading protocol version");
  memset(&cp, 0, sizeof(cp));
  bool done;
  if (!cp.readVersion(is, &done)) {
    state_ = RFBSTATE_INVALID;
    throw rfb::Exception("reading version failed: wrong file format?", "RfbPlayer");
  }
  if (!done) return;

  // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8
  if (!cp.isVersion(3,3) && !cp.isVersion(3,7) && !cp.isVersion(3,8)) {
    char msg[256];
    sprintf(msg,"File have unsupported RFB protocol version %d.%d",
            cp.majorVersion, cp.minorVersion);
    state_ = RFBSTATE_INVALID;
    throw rfb::Exception(msg, "RfbPlayer Error");
  }

  state_ = RFBSTATE_SECURITY;

  vlog.info("Using RFB protocol version %d.%d",
            cp.majorVersion, cp.minorVersion);
}

void RfbProto::processSecurityMsg()
{
  vlog.debug("processing security types message");

  int secType = secTypeInvalid;

  // legacy 3.3 server may only offer "vnc authentication" or "none"
  secType = is->readU32();
  if (secType == secTypeInvalid) {
    int reasonLen = is->readU32();
    char *reason = new char[reasonLen];
    is->readBytes(reason, reasonLen);
    throw rfb::Exception(reason, "RfbPlayer"); 
  }

  if (secType != secTypeNone) {
    throw rfb::Exception("Wrong authentication type in the session file", 
                         "RfbPlayer Error");
  }

  state_ = RFBSTATE_INITIALISATION;
}

void RfbProto::processInitMsg() {
  vlog.debug("reading server initialisation");
  reader->readServerInit(); 
}

void RfbProto::serverInit()
{
  state_ = RFBSTATE_NORMAL;
  vlog.debug("initialisation done");
}