Changeset View
Changeset View
Standalone View
Standalone View
protocols/jabber/libjingle/talk/base/opensslstreamadapter.cc
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Line(s) | |||||
82 | 82 | | |||
83 | static int stream_write(BIO* h, const char* buf, int num); | 83 | static int stream_write(BIO* h, const char* buf, int num); | ||
84 | static int stream_read(BIO* h, char* buf, int size); | 84 | static int stream_read(BIO* h, char* buf, int size); | ||
85 | static int stream_puts(BIO* h, const char* str); | 85 | static int stream_puts(BIO* h, const char* str); | ||
86 | static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); | 86 | static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); | ||
87 | static int stream_new(BIO* h); | 87 | static int stream_new(BIO* h); | ||
88 | static int stream_free(BIO* data); | 88 | static int stream_free(BIO* data); | ||
89 | 89 | | |||
90 | #if OPENSSL_VERSION_NUMBER < 0x10100000L | ||||
90 | static BIO_METHOD methods_stream = { | 91 | static BIO_METHOD methods_stream = { | ||
91 | BIO_TYPE_BIO, | 92 | BIO_TYPE_BIO, | ||
92 | "stream", | 93 | "stream", | ||
93 | stream_write, | 94 | stream_write, | ||
94 | stream_read, | 95 | stream_read, | ||
95 | stream_puts, | 96 | stream_puts, | ||
96 | 0, | 97 | 0, | ||
97 | stream_ctrl, | 98 | stream_ctrl, | ||
98 | stream_new, | 99 | stream_new, | ||
99 | stream_free, | 100 | stream_free, | ||
100 | NULL, | 101 | NULL, | ||
101 | }; | 102 | }; | ||
102 | 103 | | |||
103 | static BIO_METHOD* BIO_s_stream() { return(&methods_stream); } | 104 | static BIO_METHOD* BIO_s_stream() { return(&methods_stream); } | ||
105 | #else | ||||
106 | static BIO_METHOD *methods_stream = NULL; | ||||
107 | | ||||
108 | static const BIO_METHOD * BIO_s_stream(void) { | ||||
109 | if (methods_stream == NULL) { | ||||
110 | methods_stream = BIO_meth_new (BIO_TYPE_BIO | BIO_get_new_index (), "stream"); | ||||
111 | if (methods_stream == NULL || | ||||
112 | BIO_meth_set_write (methods_stream, stream_write) || | ||||
113 | BIO_meth_set_read (methods_stream, stream_read) || | ||||
114 | BIO_meth_set_puts (methods_stream, stream_puts) || | ||||
115 | BIO_meth_set_gets (methods_stream, 0) || | ||||
116 | BIO_meth_set_ctrl (methods_stream, stream_ctrl) || | ||||
117 | BIO_meth_set_create (methods_stream, stream_new) || | ||||
118 | BIO_meth_set_destroy (methods_stream, stream_free)) | ||||
119 | return NULL; | ||||
120 | } | ||||
121 | return methods_stream; | ||||
122 | } | ||||
123 | #endif | ||||
104 | 124 | | |||
105 | static BIO* BIO_new_stream(StreamInterface* stream) { | 125 | static BIO* BIO_new_stream(StreamInterface* stream) { | ||
106 | BIO* ret = BIO_new(BIO_s_stream()); | 126 | BIO* ret = BIO_new(BIO_s_stream()); | ||
107 | if (ret == NULL) | 127 | if (ret == NULL) | ||
108 | return NULL; | 128 | return NULL; | ||
109 | ret->ptr = stream; | 129 | BIO_set_data(ret, stream); | ||
110 | return ret; | 130 | return ret; | ||
111 | } | 131 | } | ||
112 | 132 | | |||
113 | // bio methods return 1 (or at least non-zero) on success and 0 on failure. | 133 | // bio methods return 1 (or at least non-zero) on success and 0 on failure. | ||
114 | 134 | | |||
135 | #if OPENSSL_VERSION_NUMBER < 0x10100000L | ||||
115 | static int stream_new(BIO* b) { | 136 | static int stream_new(BIO* b) { | ||
116 | b->shutdown = 0; | 137 | b->shutdown = 0; | ||
117 | b->init = 1; | 138 | b->init = 1; | ||
118 | b->num = 0; // 1 means end-of-stream | 139 | b->num = 0; // 1 means end-of-stream | ||
119 | b->ptr = 0; | 140 | b->ptr = 0; | ||
120 | return 1; | 141 | return 1; | ||
121 | } | 142 | } | ||
143 | #else | ||||
144 | static int stream_new(BIO* b) { | ||||
145 | BIO_set_shutdown(b, 0); | ||||
146 | BIO_set_init(b, 1); | ||||
147 | BIO_set_data(b, 0); | ||||
148 | return 1; | ||||
149 | } | ||||
150 | #endif | ||||
122 | 151 | | |||
123 | static int stream_free(BIO* b) { | 152 | static int stream_free(BIO* b) { | ||
124 | if (b == NULL) | 153 | if (b == NULL) | ||
125 | return 0; | 154 | return 0; | ||
126 | return 1; | 155 | return 1; | ||
127 | } | 156 | } | ||
128 | 157 | | |||
129 | static int stream_read(BIO* b, char* out, int outl) { | 158 | static int stream_read(BIO* b, char* out, int outl) { | ||
130 | if (!out) | 159 | if (!out) | ||
131 | return -1; | 160 | return -1; | ||
132 | StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); | 161 | StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b)); | ||
133 | BIO_clear_retry_flags(b); | 162 | BIO_clear_retry_flags(b); | ||
134 | size_t read; | 163 | size_t read; | ||
135 | int error; | 164 | int error; | ||
136 | StreamResult result = stream->Read(out, outl, &read, &error); | 165 | StreamResult result = stream->Read(out, outl, &read, &error); | ||
137 | if (result == SR_SUCCESS) { | 166 | if (result == SR_SUCCESS) { | ||
138 | return read; | 167 | return read; | ||
139 | } else if (result == SR_EOS) { | | |||
140 | b->num = 1; | | |||
141 | } else if (result == SR_BLOCK) { | 168 | } else if (result == SR_BLOCK) { | ||
142 | BIO_set_retry_read(b); | 169 | BIO_set_retry_read(b); | ||
143 | } | 170 | } | ||
144 | return -1; | 171 | return -1; | ||
145 | } | 172 | } | ||
146 | 173 | | |||
147 | static int stream_write(BIO* b, const char* in, int inl) { | 174 | static int stream_write(BIO* b, const char* in, int inl) { | ||
148 | if (!in) | 175 | if (!in) | ||
149 | return -1; | 176 | return -1; | ||
150 | StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); | 177 | StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b)); | ||
151 | BIO_clear_retry_flags(b); | 178 | BIO_clear_retry_flags(b); | ||
152 | size_t written; | 179 | size_t written; | ||
153 | int error; | 180 | int error; | ||
154 | StreamResult result = stream->Write(in, inl, &written, &error); | 181 | StreamResult result = stream->Write(in, inl, &written, &error); | ||
155 | if (result == SR_SUCCESS) { | 182 | if (result == SR_SUCCESS) { | ||
156 | return written; | 183 | return written; | ||
157 | } else if (result == SR_BLOCK) { | 184 | } else if (result == SR_BLOCK) { | ||
158 | BIO_set_retry_write(b); | 185 | BIO_set_retry_write(b); | ||
159 | } | 186 | } | ||
160 | return -1; | 187 | return -1; | ||
161 | } | 188 | } | ||
162 | 189 | | |||
163 | static int stream_puts(BIO* b, const char* str) { | 190 | static int stream_puts(BIO* b, const char* str) { | ||
164 | return stream_write(b, str, strlen(str)); | 191 | return stream_write(b, str, strlen(str)); | ||
165 | } | 192 | } | ||
166 | 193 | | |||
167 | static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) { | 194 | static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) { | ||
168 | UNUSED(num); | 195 | UNUSED(num); | ||
169 | UNUSED(ptr); | 196 | UNUSED(ptr); | ||
170 | 197 | | |||
171 | switch (cmd) { | 198 | switch (cmd) { | ||
172 | case BIO_CTRL_RESET: | 199 | case BIO_CTRL_RESET: | ||
173 | return 0; | 200 | return 0; | ||
174 | case BIO_CTRL_EOF: | 201 | case BIO_CTRL_EOF: | ||
175 | return b->num; | | |||
176 | case BIO_CTRL_WPENDING: | 202 | case BIO_CTRL_WPENDING: | ||
177 | case BIO_CTRL_PENDING: | 203 | case BIO_CTRL_PENDING: | ||
178 | return 0; | 204 | return 0; | ||
179 | case BIO_CTRL_FLUSH: | 205 | case BIO_CTRL_FLUSH: | ||
180 | return 1; | 206 | return 1; | ||
181 | default: | 207 | default: | ||
182 | return 0; | 208 | return 0; | ||
183 | } | 209 | } | ||
▲ Show 20 Lines • Show All 755 Lines • Show Last 20 Lines |