Skip to content

Commit 14081a1

Browse files
committed
Clean up of Windows and SSL files
Filled in all the liblacewing SSL error reporting holes. Added error number when loading the SSL certificate files fails. Fixed a Debug-only crash when reporting error in writing to socket, due to missing argument. I'm keeping the client SSL files so they're not absent should someone use the Lacewing folder as a base for their client SSL, or should I extend Lacewing clients later for SSL support. But some SSL files aren't used at all, or weren't referenced in the vcxproj file correctly. Removed the webstate enum - it wasn't used. I was planning a multi-stage switch over, so the raw socket port could accept websocket clients, but with webserver taking the HTTP side and upgrade, all that fluff with webstate wasn't necessary. In theory, the ports can be merged, if you add a layer between to send raw to Lacewing or to HTTP parsers - in practice, that's ignoring the point of ports, that protocols become apparent by responses to their port.
1 parent ce8cab8 commit 14081a1

File tree

9 files changed

+69
-58
lines changed

9 files changed

+69
-58
lines changed

Lacewing/Lacewing.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,17 +2053,6 @@ struct relayserver
20532053
// Edit relayserverinternal::client::getimplementation if you add more lines
20542054
};
20552055

2056-
enum class webstate {
2057-
// Not a websocket
2058-
rawsocket = -1,
2059-
// In Lacewing websocket mode
2060-
websocket = 0,
2061-
// Websocket handshake part 1: Waiting for client to send HTTP upgrade request
2062-
httprequestpending,
2063-
// Websocket handshake part 2: Waiting for client to reply to the HTTP upgrade approval in a Lacewing way
2064-
httprespackpending,
2065-
};
2066-
20672056
mutable lacewing::readwritelock lock;
20682057

20692058
void * tag = nullptr;

Lacewing/src/stream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void lwp_stream_init (lw_stream, const lw_streamdef *, lw_pump);
165165
void lwp_stream_push (lw_stream, const char * buffer, size_t size);
166166

167167

168-
/* Extended (internal) versions of lw_stream_write* */
168+
/* Extended (internal) versions of lw_stream_write */
169169

170170
#define lwp_stream_write_ignore_filters 1
171171
#define lwp_stream_write_ignore_busy 2

Lacewing/src/unix/global2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ lw_bool lw_file_exists (const char * filename)
4444
if (stat(filename, &attr) == 0)
4545
return !S_ISDIR(attr.st_mode);
4646

47-
always_log("%s stat failed", filename);
47+
always_log("%s stat failed, errno %d: %s", filename, errno, strerror(errno));
4848
return lw_false;
4949
}
5050

Lacewing/src/windows/fdstream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ static size_t def_sink_data (lw_stream _ctx, const char * buffer, size_t size)
448448
#ifdef _lacewing_debug
449449
lw_error err = lw_error_new();
450450
lw_error_add(err, error);
451-
lwp_trace("Failed to write to socket %p, got error %s", lw_error_tostring(err));
451+
lwp_trace("Failed to write to socket %p, got error %s", ctx, lw_error_tostring(err));
452452
lw_error_delete(err);
453453
#endif
454454

Lacewing/src/windows/server.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ void * lw_server_tag (lw_server ctx)
122122
{
123123
return ctx->tag;
124124
}
125+
void on_ssl_error (lw_server ctx, lw_stream client, lw_error error)
126+
{
127+
lw_error_addf(error, "SSL client error");
128+
if (ctx->on_error)
129+
ctx->on_error(ctx, error);
130+
131+
// SSL errors are generally unrecoverable
132+
lw_stream_close(client, lw_true);
133+
}
125134

126135
lw_server_client lwp_server_client_new (lw_server ctx, SOCKET socket)
127136
{
@@ -144,6 +153,8 @@ lw_server_client lwp_server_client_new (lw_server ctx, SOCKET socket)
144153
if (ctx->cert_loaded)
145154
{
146155
lwp_serverssl_init (&client->ssl, ctx->ssl_creds, client);
156+
client->ssl.ssl.server = ctx;
157+
client->ssl.ssl.handle_error = on_ssl_error;
147158
}
148159

149160
lw_fdstream_set_fd ((lw_fdstream) client, (HANDLE) socket, 0, lw_true, lw_true);

Lacewing/src/windows/ssl/ssl.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ static size_t def_upstream_sink_data (lw_stream upstream,
5151

5252
if (status != SEC_E_OK)
5353
{
54-
/* TODO : error? */
55-
56-
return size;
54+
lw_error err = lw_error_new();
55+
lw_error_add(err, status);
56+
lw_error_addf(err, "Encrypting message failed");
57+
if (ctx->handle_error)
58+
ctx->handle_error(ctx->server, ctx->orig_stream, err);
59+
lw_error_delete(err);
60+
61+
return size;
5762
}
5863

5964
lw_stream_data (upstream, (char *) buffers [0].pvBuffer, buffers [0].cbBuffer);
@@ -90,16 +95,12 @@ static size_t def_downstream_sink_data (lw_stream downstream,
9095
SECPKG_ATTR_STREAM_SIZES,
9196
&ctx->sizes)) != SEC_E_OK)
9297
{
93-
/* Lacewing::Error Error;
94-
95-
Error.Add(WSAGetLastError ());
96-
Error.Add("Secure handshake failure");
97-
98-
if (ctx->Server.Handlers.Error)
99-
ctx->Server.Handlers.Error (ctx->Server.Public, Error);
100-
101-
ctx->Public.Disconnect(); */
102-
98+
lw_error err = lw_error_new();
99+
lw_error_add(err, ctx->status);
100+
lw_error_addf(err, "Secure handshake failure");
101+
if (ctx->handle_error)
102+
ctx->handle_error(ctx->server, ctx->orig_stream, err);
103+
lw_error_delete(err);
103104
return size;
104105
}
105106

@@ -137,8 +138,13 @@ size_t proc_message_data (lwp_ssl ctx, const char * buffer, size_t size)
137138

138139
if (ctx->status == _HRESULT_TYPEDEF_ (0x00090317L)) /* SEC_I_CONTENT_EXPIRED */
139140
{
140-
/* ctx->Public.Disconnect(); */
141-
return size;
141+
lw_error err = lw_error_new();
142+
lw_error_add(err, ctx->status);
143+
lw_error_addf(err, "Secure content expired");
144+
if (ctx->handle_error)
145+
ctx->handle_error(ctx->server, ctx->orig_stream, err);
146+
lw_error_delete(err);
147+
return size;
142148
}
143149

144150
if (ctx->status == SEC_I_RENEGOTIATE)
@@ -156,20 +162,20 @@ size_t proc_message_data (lwp_ssl ctx, const char * buffer, size_t size)
156162
* http://msdn.microsoft.com/en-us/library/aa374781%28v=VS.85%29.aspx
157163
*/
158164

159-
return size;
165+
// TODO: Phi note: when TLS cert expires, this may trigger for existing connections. I've rewritten this, but not tested yet.
166+
ctx->handshake_complete = lw_false;
167+
return ctx->proc_handshake_data(ctx, NULL, 0);
160168
}
161169

162170
if (FAILED (ctx->status))
163171
{
164-
/* Error decrypting the message */
165-
166-
/* Lacewing::Error Error;
167-
Error.Add(Status);
168-
lwp_trace("Error decrypting the message: %s", Error.ToString ());
169-
170-
ctx->Public.Disconnect(); */
171-
172-
return size;
172+
lw_error err = lw_error_new();
173+
lw_error_add(err, ctx->status);
174+
lw_error_addf(err, "Error decrypting the message");
175+
if (ctx->handle_error)
176+
ctx->handle_error(ctx->server, ctx->orig_stream, err);
177+
lw_error_delete(err);
178+
return size;
173179
}
174180

175181
/* Find the decrypted data
@@ -185,7 +191,7 @@ size_t proc_message_data (lwp_ssl ctx, const char * buffer, size_t size)
185191
}
186192
}
187193

188-
/* Check for any trailing data that wasn't part of the messagei
194+
/* Check for any trailing data that wasn't part of the message
189195
*/
190196
for (int i = 0; i < 4; ++ i)
191197
{

Lacewing/src/windows/ssl/ssl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
typedef struct _lwp_ssl
1717
{
1818
DWORD status;
19+
lw_server server;
20+
lw_stream orig_stream;
21+
void (*handle_error)(lw_server, lw_stream, lw_error);
1922
lw_bool handshake_complete;
2023

2124
lw_bool got_context;

bluewing-cpp-server.vcxproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@
236236
<ClCompile Include="Lacewing\src\windows\file.c" />
237237
<ClCompile Include="Lacewing\src\windows\global2.c" />
238238
<ClCompile Include="Lacewing\src\windows\server.c" />
239-
<ClCompile Include="Lacewing\src\windows\sslclient.c" />
240-
<ClCompile Include="Lacewing\src\windows\ssl\clientssl.c" />
241239
<ClCompile Include="Lacewing\src\windows\ssl\serverssl.c" />
242240
<ClCompile Include="Lacewing\src\windows\ssl\ssl.c" />
243241
<ClCompile Include="Lacewing\src\windows\sync.c" />
@@ -278,7 +276,8 @@
278276
<ClInclude Include="Lacewing\src\windows\common.h" />
279277
<ClInclude Include="Lacewing\src\windows\compat.h" />
280278
<ClInclude Include="Lacewing\src\windows\fdstream.h" />
281-
<ClInclude Include="Lacewing\src\windows\sslclient.h" />
279+
<ClInclude Include="Lacewing\src\windows\ssl\serverssl.h" />
280+
<ClInclude Include="Lacewing\src\windows\ssl\ssl.h" />
282281
<ClInclude Include="Lacewing\src\windows\typeof.h" />
283282
</ItemGroup>
284283
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

bluewing-cpp-server.vcxproj.filters

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
<Filter Include="Source Files\Lacewing\deps\http-parser">
6868
<UniqueIdentifier>{0645920b-bacc-45ab-b3c6-93ce994cb50d}</UniqueIdentifier>
6969
</Filter>
70+
<Filter Include="Source Files\Lacewing\src\windows\ssl">
71+
<UniqueIdentifier>{6937536c-e755-4074-9a4d-d6d20a3fedf6}</UniqueIdentifier>
72+
</Filter>
73+
<Filter Include="Header Files\Lacewing\src\windows\ssl">
74+
<UniqueIdentifier>{4d0e779d-53d8-419b-819a-a6e2edbf09ce}</UniqueIdentifier>
75+
</Filter>
7076
</ItemGroup>
7177
<ItemGroup>
7278
<ClCompile Include="Lacewing\RelayServer.cc">
@@ -189,18 +195,6 @@
189195
<ClCompile Include="Lacewing\src\windows\server.c">
190196
<Filter>Source Files\Lacewing\src\windows</Filter>
191197
</ClCompile>
192-
<ClCompile Include="Lacewing\src\windows\sslclient.c">
193-
<Filter>Source Files\Lacewing\src\windows</Filter>
194-
</ClCompile>
195-
<ClCompile Include="Lacewing\src\windows\ssl\clientssl.c">
196-
<Filter>Source Files\Lacewing\src\windows</Filter>
197-
</ClCompile>
198-
<ClCompile Include="Lacewing\src\windows\ssl\serverssl.c">
199-
<Filter>Source Files\Lacewing\src\windows</Filter>
200-
</ClCompile>
201-
<ClCompile Include="Lacewing\src\windows\ssl\ssl.c">
202-
<Filter>Source Files\Lacewing\src\windows</Filter>
203-
</ClCompile>
204198
<ClCompile Include="Lacewing\src\windows\sync.c">
205199
<Filter>Source Files\Lacewing\src\windows</Filter>
206200
</ClCompile>
@@ -264,6 +258,12 @@
264258
<ClCompile Include="Lacewing\src\cxx\webserver2.cc">
265259
<Filter>Source Files\Lacewing\src\cxx</Filter>
266260
</ClCompile>
261+
<ClCompile Include="Lacewing\src\windows\ssl\ssl.c">
262+
<Filter>Source Files\Lacewing\src\windows\ssl</Filter>
263+
</ClCompile>
264+
<ClCompile Include="Lacewing\src\windows\ssl\serverssl.c">
265+
<Filter>Source Files\Lacewing\src\windows\ssl</Filter>
266+
</ClCompile>
267267
</ItemGroup>
268268
<ItemGroup>
269269
<ClInclude Include="ConsoleColors.hpp">
@@ -323,9 +323,6 @@
323323
<ClInclude Include="Lacewing\src\windows\fdstream.h">
324324
<Filter>Header Files\Lacewing\src\windows</Filter>
325325
</ClInclude>
326-
<ClInclude Include="Lacewing\src\windows\sslclient.h">
327-
<Filter>Header Files\Lacewing\src\windows</Filter>
328-
</ClInclude>
329326
<ClInclude Include="Lacewing\src\windows\typeof.h">
330327
<Filter>Header Files\Lacewing\src\windows</Filter>
331328
</ClInclude>
@@ -365,5 +362,11 @@
365362
<ClInclude Include="Lacewing\src\webserver\multipart.h">
366363
<Filter>Header Files\Lacewing\src\webserver</Filter>
367364
</ClInclude>
365+
<ClInclude Include="Lacewing\src\windows\ssl\serverssl.h">
366+
<Filter>Header Files\Lacewing\src\windows\ssl</Filter>
367+
</ClInclude>
368+
<ClInclude Include="Lacewing\src\windows\ssl\ssl.h">
369+
<Filter>Header Files\Lacewing\src\windows\ssl</Filter>
370+
</ClInclude>
368371
</ItemGroup>
369372
</Project>

0 commit comments

Comments
 (0)