OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef BIN_TLS_SOCKET_H_ | |
6 #define BIN_TLS_SOCKET_H_ | |
7 | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <stdio.h> | |
11 #include <sys/types.h> | |
12 | |
13 #include <prinit.h> | |
14 #include <prerror.h> | |
15 #include <prnetdb.h> | |
16 | |
17 #include "bin/builtin.h" | |
18 #include "bin/dartutils.h" | |
19 #include "platform/globals.h" | |
20 #include "platform/thread.h" | |
21 | |
22 static void ReportError(const char* message) { | |
Søren Gjesse
2012/11/14 08:18:59
Rename to ThrowException to indicate that this wil
Bill Hesse
2012/11/14 13:33:30
Done.
| |
23 Dart_Handle socket_io_exception = | |
24 DartUtils::NewDartSocketIOException(message, Dart_Null()); | |
25 Dart_ThrowException(socket_io_exception); | |
26 } | |
27 | |
28 | |
29 /* Handle an error reported from the NSS library. */ | |
30 static void ReportPRError(const char* message) { | |
Søren Gjesse
2012/11/14 08:18:59
Ditto
Bill Hesse
2012/11/14 13:33:30
Done.
| |
31 PRErrorCode error_code = PR_GetError(); | |
32 int error_length = PR_GetErrorTextLength(); | |
33 char* error_message = static_cast<char*>(malloc(error_length + 1)); | |
34 ASSERT(error_message != NULL); | |
35 int copied_length = PR_GetErrorText(error_message); | |
36 ASSERT(copied_length == error_length); | |
37 error_message[error_length] = '\0'; | |
38 OSError os_error_struct(error_code, error_message, OSError::kNSS); | |
39 Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct); | |
40 Dart_Handle socket_io_exception = | |
41 DartUtils::NewDartSocketIOException(message, os_error); | |
42 free(error_message); | |
43 Dart_ThrowException(socket_io_exception); | |
44 } | |
45 | |
46 /* | |
47 * TlsFilter encapsulates the NSS SSL(TLS) code in a filter, that communicates | |
48 * with the containing _TlsFilterImpl Dart object through four shared | |
49 * ExternalByteArray buffers, for reading and writing plaintext, and | |
50 * reading and writing encrypted text. The filter handles handshaking | |
51 * and certificate verification. | |
52 */ | |
53 class TlsFilter { | |
54 public: | |
55 // These enums must agree with those in sdk/lib/io/tls_socket.dart. | |
56 enum BufferIndex { | |
57 kReadPlaintext, | |
58 kWritePlaintext, | |
59 kReadEncrypted, | |
60 kWriteEncrypted, | |
61 kNumBuffers | |
62 }; | |
63 | |
64 TlsFilter() | |
65 : string_start_(NULL), | |
66 string_length_(NULL), | |
67 handshake_start_(NULL), | |
68 handshake_finish_(NULL), | |
69 in_handshake_(false), | |
70 memio_(NULL) { } | |
71 | |
72 void Init(Dart_Handle dart_this); | |
73 void Connect(const char* host, int port); | |
74 void Destroy(); | |
75 void DestroyPlatformIndependent(); | |
76 void Handshake(); | |
77 void RegisterHandshakeCallbacks(Dart_Handle start, Dart_Handle finish); | |
78 static void InitializeLibrary(const char* pkcert_directory); | |
79 | |
80 intptr_t ProcessBuffer(int bufferIndex); | |
81 | |
82 private: | |
83 static const int kMemioBufferSize = 20 * KB; | |
84 static bool library_initialized_; | |
85 static dart::Mutex mutex_; // To protect library initialization. | |
86 | |
87 uint8_t* buffers_[kNumBuffers]; | |
88 int64_t buffer_size_; | |
89 Dart_Handle string_start_; | |
90 Dart_Handle string_length_; | |
91 Dart_Handle dart_buffer_objects_[kNumBuffers]; | |
92 Dart_Handle handshake_start_; | |
93 Dart_Handle handshake_finish_; | |
94 bool in_handshake_; | |
95 PRFileDesc* memio_; | |
96 | |
97 void InitializeBuffers(Dart_Handle dart_this); | |
98 void InitializePlatformData(); | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(TlsFilter); | |
101 }; | |
102 | |
103 #endif // BIN_TLS_SOCKET_H_ | |
OLD | NEW |