author | alwin <alwin> | 2004-03-02 12:21:11 (UTC) |
---|---|---|
committer | alwin <alwin> | 2004-03-02 12:21:11 (UTC) |
commit | b6b1c97559c0ed9f2e33632272426bf98f289232 (patch) (unidiff) | |
tree | d3a9987704770cdf5eb14e1136f6e3ecb2f36a04 /noncore/comm/keypebble/krfblogin.cpp | |
parent | 0d59c780513da78033f4d9040475dee9db0256d4 (diff) | |
download | opie-b6b1c97559c0ed9f2e33632272426bf98f289232.zip opie-b6b1c97559c0ed9f2e33632272426bf98f289232.tar.gz opie-b6b1c97559c0ed9f2e33632272426bf98f289232.tar.bz2 |
applied the patch generated by the optimize_connect script from
TT.
Diffstat (limited to 'noncore/comm/keypebble/krfblogin.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/comm/keypebble/krfblogin.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/noncore/comm/keypebble/krfblogin.cpp b/noncore/comm/keypebble/krfblogin.cpp index 0b4a757..2bbd110 100644 --- a/noncore/comm/keypebble/krfblogin.cpp +++ b/noncore/comm/keypebble/krfblogin.cpp | |||
@@ -1,244 +1,244 @@ | |||
1 | #include <assert.h> | 1 | #include <assert.h> |
2 | 2 | ||
3 | 3 | ||
4 | extern "C" { | 4 | extern "C" { |
5 | #include "vncauth.h" | 5 | #include "vncauth.h" |
6 | } | 6 | } |
7 | 7 | ||
8 | #include "krfblogin.h" | 8 | #include "krfblogin.h" |
9 | #include "krfbconnection.h" | 9 | #include "krfbconnection.h" |
10 | #include <qtimer.h> | 10 | #include <qtimer.h> |
11 | 11 | ||
12 | // The length of the various messages (used to decide how many bytes to | 12 | // The length of the various messages (used to decide how many bytes to |
13 | // wait for). | 13 | // wait for). |
14 | const int ServerVersionLength = 12; | 14 | const int ServerVersionLength = 12; |
15 | const int ClientVersionLength = 12; | 15 | const int ClientVersionLength = 12; |
16 | const int AuthSchemeLength = 4; | 16 | const int AuthSchemeLength = 4; |
17 | const int FailureReasonSizeLength = 4; | 17 | const int FailureReasonSizeLength = 4; |
18 | const int ChallengeLength = 16; | 18 | const int ChallengeLength = 16; |
19 | const int AuthResultLength = 4; | 19 | const int AuthResultLength = 4; |
20 | 20 | ||
21 | // Authentication results | 21 | // Authentication results |
22 | enum AuthResult { | 22 | enum AuthResult { |
23 | AuthOk, | 23 | AuthOk, |
24 | AuthFailed, | 24 | AuthFailed, |
25 | AuthTooMany | 25 | AuthTooMany |
26 | }; | 26 | }; |
27 | 27 | ||
28 | typedef unsigned char CARD8; | 28 | typedef unsigned char CARD8; |
29 | typedef unsigned short CARD16; | 29 | typedef unsigned short CARD16; |
30 | typedef unsigned long CARD32; | 30 | typedef unsigned long CARD32; |
31 | 31 | ||
32 | const int endianTest = 1; | 32 | const int endianTest = 1; |
33 | 33 | ||
34 | // Endian stuff | 34 | // Endian stuff |
35 | #define Swap16IfLE(s) \ | 35 | #define Swap16IfLE(s) \ |
36 | (*(char *)&endianTest ? ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff)) : (s)) | 36 | (*(char *)&endianTest ? ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff)) : (s)) |
37 | 37 | ||
38 | #define Swap32IfLE(l) \ | 38 | #define Swap32IfLE(l) \ |
39 | (*(char *)&endianTest ? ((((l) & 0xff000000) >> 24) | \ | 39 | (*(char *)&endianTest ? ((((l) & 0xff000000) >> 24) | \ |
40 | (((l) & 0x00ff0000) >> 8) | \ | 40 | (((l) & 0x00ff0000) >> 8) | \ |
41 | (((l) & 0x0000ff00) << 8) | \ | 41 | (((l) & 0x0000ff00) << 8) | \ |
42 | (((l) & 0x000000ff) << 24)) : (l)) | 42 | (((l) & 0x000000ff) << 24)) : (l)) |
43 | 43 | ||
44 | KRFBLogin::KRFBLogin( KRFBConnection *con ) | 44 | KRFBLogin::KRFBLogin( KRFBConnection *con ) |
45 | : QObject( con, "RFB login manager" ) | 45 | : QObject( con, "RFB login manager" ) |
46 | { | 46 | { |
47 | assert( con ); | 47 | assert( con ); |
48 | this->con = con; | 48 | this->con = con; |
49 | currentState = AwaitingServerVersion; | 49 | currentState = AwaitingServerVersion; |
50 | 50 | ||
51 | connect( this, SIGNAL( error( const QString & ) ), | 51 | connect( this, SIGNAL( error(const QString&) ), |
52 | con, SIGNAL( error( const QString & ) ) ); | 52 | con, SIGNAL( error(const QString&) ) ); |
53 | 53 | ||
54 | 54 | ||
55 | qWarning( "Waiting for server version..." ); | 55 | qWarning( "Waiting for server version..." ); |
56 | 56 | ||
57 | static QString statusMsg = tr( "Waiting for server version..." ); | 57 | static QString statusMsg = tr( "Waiting for server version..." ); |
58 | emit status( statusMsg ); | 58 | emit status( statusMsg ); |
59 | 59 | ||
60 | // Kick off the state machine | 60 | // Kick off the state machine |
61 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotServerVersion() ) ); | 61 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotServerVersion() ) ); |
62 | con->waitForData( ServerVersionLength ); | 62 | con->waitForData( ServerVersionLength ); |
63 | } | 63 | } |
64 | 64 | ||
65 | KRFBLogin::~KRFBLogin() | 65 | KRFBLogin::~KRFBLogin() |
66 | { | 66 | { |
67 | 67 | ||
68 | } | 68 | } |
69 | 69 | ||
70 | KRFBLogin::State KRFBLogin::state() const | 70 | KRFBLogin::State KRFBLogin::state() const |
71 | { | 71 | { |
72 | return currentState; | 72 | return currentState; |
73 | } | 73 | } |
74 | 74 | ||
75 | void KRFBLogin::gotServerVersion() | 75 | void KRFBLogin::gotServerVersion() |
76 | { | 76 | { |
77 | qWarning( "Got server version" ); | 77 | qWarning( "Got server version" ); |
78 | 78 | ||
79 | disconnect( con, SIGNAL( gotEnoughData() ), | 79 | disconnect( con, SIGNAL( gotEnoughData() ), |
80 | this, SLOT( gotServerVersion() ) ); | 80 | this, SLOT( gotServerVersion() ) ); |
81 | 81 | ||
82 | // Read the server's version message | 82 | // Read the server's version message |
83 | char serverVersion[ ServerVersionLength + 1 ]; | 83 | char serverVersion[ ServerVersionLength + 1 ]; |
84 | con->read( serverVersion, ServerVersionLength ); | 84 | con->read( serverVersion, ServerVersionLength ); |
85 | serverVersion[ ServerVersionLength ] = '\0'; | 85 | serverVersion[ ServerVersionLength ] = '\0'; |
86 | 86 | ||
87 | QCString rfbString( serverVersion, ServerVersionLength + 1 ); | 87 | QCString rfbString( serverVersion, ServerVersionLength + 1 ); |
88 | versionString = rfbString; | 88 | versionString = rfbString; |
89 | 89 | ||
90 | QRegExp regexp( "RFB [0-9][0-9][0-9]\\.[0-9][0-9][0-9]\n" ); | 90 | QRegExp regexp( "RFB [0-9][0-9][0-9]\\.[0-9][0-9][0-9]\n" ); |
91 | 91 | ||
92 | if ( rfbString.find( regexp ) == -1 ) { | 92 | if ( rfbString.find( regexp ) == -1 ) { |
93 | static QString msg = tr( "Error: Invalid server version, %1" ).arg( rfbString ); | 93 | static QString msg = tr( "Error: Invalid server version, %1" ).arg( rfbString ); |
94 | 94 | ||
95 | qWarning( msg ); | 95 | qWarning( msg ); |
96 | emit error( msg ); | 96 | emit error( msg ); |
97 | currentState = Error; | 97 | currentState = Error; |
98 | return; | 98 | return; |
99 | } | 99 | } |
100 | 100 | ||
101 | // Calculate the actual version number | 101 | // Calculate the actual version number |
102 | serverMajor = (serverVersion[4] - '0') * 100 | 102 | serverMajor = (serverVersion[4] - '0') * 100 |
103 | + (serverVersion[5] - '0') * 10 | 103 | + (serverVersion[5] - '0') * 10 |
104 | + (serverVersion[6] - '0'); | 104 | + (serverVersion[6] - '0'); |
105 | serverMinor = (serverVersion[8] - '0') * 100 | 105 | serverMinor = (serverVersion[8] - '0') * 100 |
106 | + (serverVersion[9] - '0') * 10 | 106 | + (serverVersion[9] - '0') * 10 |
107 | + (serverVersion[10] - '0'); | 107 | + (serverVersion[10] - '0'); |
108 | 108 | ||
109 | qWarning("Server Version: %03d.%03d", serverMajor, serverMinor ); | 109 | qWarning("Server Version: %03d.%03d", serverMajor, serverMinor ); |
110 | 110 | ||
111 | if ( serverMajor != 3 ) { | 111 | if ( serverMajor != 3 ) { |
112 | QString msg = tr( "Error: Unsupported server version, %1" ) | 112 | QString msg = tr( "Error: Unsupported server version, %1" ) |
113 | .arg( rfbString ); | 113 | .arg( rfbString ); |
114 | 114 | ||
115 | qWarning( msg ); | 115 | qWarning( msg ); |
116 | emit error( msg ); | 116 | emit error( msg ); |
117 | currentState = Error; | 117 | currentState = Error; |
118 | return; | 118 | return; |
119 | } | 119 | } |
120 | 120 | ||
121 | if ( serverMinor != 3 ) { | 121 | if ( serverMinor != 3 ) { |
122 | qWarning( "Minor version mismatch: %d", serverMinor ); | 122 | qWarning( "Minor version mismatch: %d", serverMinor ); |
123 | } | 123 | } |
124 | 124 | ||
125 | // Setup for the next state | 125 | // Setup for the next state |
126 | sendClientVersion(); | 126 | sendClientVersion(); |
127 | 127 | ||
128 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotAuthScheme() ) ); | 128 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotAuthScheme() ) ); |
129 | con->waitForData( AuthSchemeLength ); | 129 | con->waitForData( AuthSchemeLength ); |
130 | } | 130 | } |
131 | 131 | ||
132 | void KRFBLogin::gotAuthScheme() | 132 | void KRFBLogin::gotAuthScheme() |
133 | { | 133 | { |
134 | disconnect( con, SIGNAL( gotEnoughData() ), | 134 | disconnect( con, SIGNAL( gotEnoughData() ), |
135 | this, SLOT( gotAuthScheme() ) ); | 135 | this, SLOT( gotAuthScheme() ) ); |
136 | 136 | ||
137 | // Got data | 137 | // Got data |
138 | CARD32 scheme; | 138 | CARD32 scheme; |
139 | con->read( &scheme, AuthSchemeLength ); | 139 | con->read( &scheme, AuthSchemeLength ); |
140 | scheme = Swap32IfLE( scheme ); | 140 | scheme = Swap32IfLE( scheme ); |
141 | 141 | ||
142 | static QString statusMsgOk = tr( "Logged in" ); | 142 | static QString statusMsgOk = tr( "Logged in" ); |
143 | 143 | ||
144 | switch ( scheme ) { | 144 | switch ( scheme ) { |
145 | case 0: | 145 | case 0: |
146 | qWarning( "Failed" ); | 146 | qWarning( "Failed" ); |
147 | // Handle failure | 147 | // Handle failure |
148 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotFailureReasonSize() ) ); | 148 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotFailureReasonSize() ) ); |
149 | con->waitForData( FailureReasonSizeLength ); | 149 | con->waitForData( FailureReasonSizeLength ); |
150 | break; | 150 | break; |
151 | case 1: | 151 | case 1: |
152 | // Handle no auth | 152 | // Handle no auth |
153 | emit status( statusMsgOk ); | 153 | emit status( statusMsgOk ); |
154 | con->gotRFBConnection(); | 154 | con->gotRFBConnection(); |
155 | break; | 155 | break; |
156 | case 2: | 156 | case 2: |
157 | // Handle VNC auth | 157 | // Handle VNC auth |
158 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotChallenge() ) ); | 158 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotChallenge() ) ); |
159 | con->waitForData( ChallengeLength ); | 159 | con->waitForData( ChallengeLength ); |
160 | break; | 160 | break; |
161 | default: | 161 | default: |
162 | qWarning( "Unknown authentication scheme, 0x%08lx", scheme ); | 162 | qWarning( "Unknown authentication scheme, 0x%08lx", scheme ); |
163 | currentState = Error; | 163 | currentState = Error; |
164 | break; | 164 | break; |
165 | }; | 165 | }; |
166 | } | 166 | } |
167 | 167 | ||
168 | void KRFBLogin::gotChallenge() | 168 | void KRFBLogin::gotChallenge() |
169 | { | 169 | { |
170 | disconnect( con, SIGNAL( gotEnoughData() ), | 170 | disconnect( con, SIGNAL( gotEnoughData() ), |
171 | this, SLOT( gotChallenge() ) ); | 171 | this, SLOT( gotChallenge() ) ); |
172 | 172 | ||
173 | QTimer::singleShot( 0, this, SLOT(getPassword()) ); | 173 | QTimer::singleShot( 0, this, SLOT(getPassword()) ); |
174 | } | 174 | } |
175 | 175 | ||
176 | void KRFBLogin::getPassword() | 176 | void KRFBLogin::getPassword() |
177 | { | 177 | { |
178 | // Got data | 178 | // Got data |
179 | CARD8 challenge[ ChallengeLength ]; | 179 | CARD8 challenge[ ChallengeLength ]; |
180 | con->read( challenge, ChallengeLength ); | 180 | con->read( challenge, ChallengeLength ); |
181 | 181 | ||
182 | // Last chance to enter a password | 182 | // Last chance to enter a password |
183 | if ( con->options_->password.isNull() ) { | 183 | if ( con->options_->password.isNull() ) { |
184 | qWarning( "krfblogin needs a password" ); | 184 | qWarning( "krfblogin needs a password" ); |
185 | emit passwordRequired( con ); | 185 | emit passwordRequired( con ); |
186 | } | 186 | } |
187 | 187 | ||
188 | if ( con->options_->password.isNull() ) { | 188 | if ( con->options_->password.isNull() ) { |
189 | QString msg = tr( "Error: This server requires a password, but none " | 189 | QString msg = tr( "Error: This server requires a password, but none " |
190 | "has been specified.\n" ); | 190 | "has been specified.\n" ); |
191 | 191 | ||
192 | emit error( msg ); | 192 | emit error( msg ); |
193 | return; | 193 | return; |
194 | } | 194 | } |
195 | 195 | ||
196 | vncEncryptBytes( (unsigned char *) challenge, QCString(con->options_->password.latin1()).data() ); | 196 | vncEncryptBytes( (unsigned char *) challenge, QCString(con->options_->password.latin1()).data() ); |
197 | con->write( challenge, ChallengeLength ); | 197 | con->write( challenge, ChallengeLength ); |
198 | 198 | ||
199 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotAuthResult() ) ); | 199 | connect( con, SIGNAL( gotEnoughData() ), SLOT( gotAuthResult() ) ); |
200 | con->waitForData( AuthResultLength ); | 200 | con->waitForData( AuthResultLength ); |
201 | } | 201 | } |
202 | 202 | ||
203 | void KRFBLogin::gotFailureReasonSize() | 203 | void KRFBLogin::gotFailureReasonSize() |
204 | { | 204 | { |
205 | disconnect( con, SIGNAL( gotEnoughData() ), this, | 205 | disconnect( con, SIGNAL( gotEnoughData() ), this, |
206 | SLOT( gotFailureReasonSize() ) ); | 206 | SLOT( gotFailureReasonSize() ) ); |
207 | } | 207 | } |
208 | 208 | ||
209 | void KRFBLogin::gotAuthResult() | 209 | void KRFBLogin::gotAuthResult() |
210 | { | 210 | { |
211 | // Got data | 211 | // Got data |
212 | disconnect( con, SIGNAL( gotEnoughData() ), this, | 212 | disconnect( con, SIGNAL( gotEnoughData() ), this, |
213 | SLOT( gotAuthResult() ) ); | 213 | SLOT( gotAuthResult() ) ); |
214 | 214 | ||
215 | long result; | 215 | long result; |
216 | con->read( &result, AuthResultLength ); | 216 | con->read( &result, AuthResultLength ); |
217 | result = Swap32IfLE( result ); | 217 | result = Swap32IfLE( result ); |
218 | 218 | ||
219 | qWarning( "Authentication Result is 0x%08lx", result ); | 219 | qWarning( "Authentication Result is 0x%08lx", result ); |
220 | 220 | ||
221 | static QString failed = tr( "Error: The password you specified was incorrect." ); | 221 | static QString failed = tr( "Error: The password you specified was incorrect." ); |
222 | static QString tooMany = tr( "Error: Too many invalid login attempts have been made\n" | 222 | static QString tooMany = tr( "Error: Too many invalid login attempts have been made\n" |
223 | "to this account, please try later." ); | 223 | "to this account, please try later." ); |
224 | 224 | ||
225 | static QString statusMsgOk = tr( "Logged in" ); | 225 | static QString statusMsgOk = tr( "Logged in" ); |
226 | static QString statusMsgFailed = tr( "Login Failed" ); | 226 | static QString statusMsgFailed = tr( "Login Failed" ); |
227 | static QString statusMsgTooMany = tr( "Too many failures" ); | 227 | static QString statusMsgTooMany = tr( "Too many failures" ); |
228 | 228 | ||
229 | switch( result ) { | 229 | switch( result ) { |
230 | case AuthOk: | 230 | case AuthOk: |
231 | emit status( statusMsgOk ); | 231 | emit status( statusMsgOk ); |
232 | con->gotRFBConnection(); | 232 | con->gotRFBConnection(); |
233 | break; | 233 | break; |
234 | case AuthFailed: | 234 | case AuthFailed: |
235 | qWarning( "Dammit" ); | 235 | qWarning( "Dammit" ); |
236 | emit status( statusMsgFailed ); | 236 | emit status( statusMsgFailed ); |
237 | emit error( failed ); | 237 | emit error( failed ); |
238 | break; | 238 | break; |
239 | case AuthTooMany: | 239 | case AuthTooMany: |
240 | emit status( statusMsgTooMany ); | 240 | emit status( statusMsgTooMany ); |
241 | emit error( tooMany ); | 241 | emit error( tooMany ); |
242 | break; | 242 | break; |
243 | default: | 243 | default: |
244 | qWarning( "Invalid authentication result, %lx", result ); | 244 | qWarning( "Invalid authentication result, %lx", result ); |