File: Data.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (190 lines) | stat: -rw-r--r-- 3,619 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include "OS/Sync.h"
#include "Core/Str.h"
#include "Core/Io/Stream.h"

namespace ssl {

	/**
	 * Generic data utilities.
	 */

	class SSLSession;
	class WinSSLCert;
	class WinSSLCertKey;
	class OpenSSLCert;
	class OpenSSLCertKey;

	/**
	 * Generic refcount class.
	 */
	class RefCount {
	public:
		// Create, initializes refs to 1.
		RefCount() : refs(1) {}

		// Destroy.
		virtual ~RefCount() {}

		// Add a reference.
		void ref() {
			atomicIncrement(refs);
		}

		// Release a reference.
		void unref() {
			if (atomicDecrement(refs) == 0) {
				delete this;
			}
		}

	private:
		// References.
		size_t refs;
	};

	/**
	 * Auto ptr for refcounts. Useful in implementation code, but might not work as data members in Storm classes.
	 */
	template <class T>
	class RefPtr {
	public:
		// Create for null.
		RefPtr() : data(null) {}

		// Create. Takes ownership.
		RefPtr(T *p) : data(p) {}

		// Destroy.
		~RefPtr() {
			if (data)
				data->unref();
		}

		// Copy.
		RefPtr(const RefPtr &o) : data(o.data) {
			if (data)
				data->ref();
		}

		// Assign.
		RefPtr &operator =(const RefPtr &o) {
			if (o.data)
				o.data->ref();

			if (data)
				data->unref();
			data = o.data;

			return *this;
		}

#ifdef USE_MOVE
		RefPtr(RefPtr &&o) : data(o.data) {
			o.data = null;
		}

		RefPtr &operator =(RefPtr &&o) {
			std::swap(data, o.data);
			return *this;
		}
#endif

		// Access the data.
		T &operator *() const {
			return *data;
		}
		T *operator ->() const {
			return data;
		}
		T *get() const {
			return data;
		}

		operator bool() const {
			return data != null;
		}

	private:
		T *data;
	};


	/**
	 * Data for an SSL context. Internal to the Context, but convenient to have outside the class
	 * declaration.
	 */
	class SSLContext : public RefCount {
	public:
		// Create a session for this context.
		virtual SSLSession *createSession() = 0;
	};

	/**
	 * Data for an individual session.
	 */
	class SSLSession : public RefCount {
	public:
		// Lock for the session.
		os::Lock lock;

		// Connect this session. Returns any additional data needed to be kept alive by the GC.
		virtual void *connect(IStream *input, OStream *output, Str *host) = 0;

		// More data available?
		virtual Bool more(void *gcData) = 0;

		// Read data.
		virtual void read(Buffer &to, void *gcData) = 0;

		// Peek data.
		virtual void peek(Buffer &to, void *gcData) = 0;

		// Write data.
		virtual Nat write(const Buffer &from, Nat start, void *gcData) = 0;

		// Flush the stream.
		virtual Bool flush(void *gcData) = 0;

		// Shut down the session.
		virtual void shutdown(void *gcData) = 0;

		// Close the underlying streams.
		virtual void close(void *gcData) = 0;
	};


	/**
	 * Some representation of a certificate.
	 *
	 * The certificate inside is expected to be read-only after creation.
	 */
	class SSLCert : public RefCount {
	public:
		// Get the windows SSL implementation (might convert it, might throw).
		virtual WinSSLCert *windows() = 0;

		// Get the OpenSSL implementation version (might convert it, might throw).
		virtual OpenSSLCert *openSSL() = 0;

		// Extract some sensible string data.
		virtual void output(StrBuf *to) = 0;
	};


	/**
	 * Some representation of a certificate key.
	 */
	class SSLCertKey : public RefCount {
	public:
		// Check so that the key is valid for a particular certificate. Returns an error message on error, or null.
		virtual const wchar *validate(SSLCert *cert) = 0;

		// Get a windows version of the data.
		virtual WinSSLCertKey *windows() = 0;

		// Get the OpenSSL version of the data.
		virtual OpenSSLCertKey *openSSL() = 0;
	};

}