Commit graph

1528 commits

Author SHA1 Message Date
Andy Green
adc71469da just get hostname into canonical_hostname
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-14 17:11:22 +08:00
Andy Green
c2bb3dc7b3 unstaged server changes
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-14 11:25:44 +08:00
Andy Green
ff5dbf91b1 check for default protocol rx buf limit
This fixes

http://libwebsockets.org/trac/ticket/13

When using the default rx protocol buffer, the check is
performed against 0 not the default length.  That's the
case both in client and server code...

There's no problem if you actually give a max frame size
in the protocol definition.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-14 10:18:31 +08:00
Andy Green
0878b9ed05 fix missing cr from closing log
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-13 11:44:20 +08:00
Joakim Soderberg
f272cb0624 Fixed DLL compilation on Windows for CMake.
- Define LWS_DLL and LWS_INTERNAL when websockets_shared is compiled.

- The websocket_shared target compiles to websocket.lib / websocket.dll
  (websocket.lib contains the exported functions for websocket.dll, and is
  the file that is linked to when a program wants to use the dll)

- The websocket target compiles to websocket_static.lib on windows.

- Replaced any "extern" with "LWS_EXTERN" on libwebsockets.h for proper
  DLL function exports.

- Created a LIB_LIST with all the libwebsocket dependencies, instead of
  multiple calls to target_link_libraries, only one call is made for both
  the static and shared library version. This makes it easy to add other
  variants if wanted in the future.

- Added ZLIB as a dependency for the libs, so that the build order will be
  correct at all times.

- Added a dependency for the websockets lib to the test apps, so it is
  built before them.

- Fixed the test-server-extpoll app to include the emulated_poll, and link
  to winsock on Windows.

- Removed the global export of libwebsocket_internal_extensions, and added
  a function libwebsocket_get_internal_extensions() that returns it
  instead. Using the global would not work with the DLL export on Windows.
2013-02-13 09:29:26 +08:00
Andy Green
5b7312213e disable fstack usage
It's only workable on gcc 4.6+... and in fact it's only
going to be interesting typically to the maintainer, so it's
commented out in Makefile.am.

Currently the worst remaining ones for x86_64 are

libwebsockets.c:2250:6:_lws_log	496	static
libwebsockets.c:2203:13:lwsl_emit_stderr	352	static
client.c:700:1:libwebsockets_generate_client_handshake	320	static
server.c:42:1:libwebsockets_decode_ssl_error	288	static
server-handshake.c:31:1:handshake_0405	272	static
client.c:368:1:lws_client_interpret_server_handshake	272	static
libwebsockets.c:447:1:libwebsockets_get_peer_addresses	256	static
daemonize.c:93:1:lws_daemonize	240	static
output.c:536:5:libwebsockets_serve_http_file	224	static
sha-1.c:131:1:sha1_step	192	static
sha-1.c:316:1:SHA1	160	static
libwebsockets.c:1604:1:libwebsocket_create_context	160	static

The top two there are error assembly buffers, they could be made
static but then they won't be usable from threaded user code.

Reported-by: Anders Brander <anders@brander.dk>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-13 09:08:43 +08:00
Andy Green
94f94652ed security disallow repeated GET
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 14:16:06 +08:00
Andy Green
3ee9b31054 security harden http parser a bit
Drop the connection during parsing for a few more cases that can't be legit.

Take care about trying to free rxflow_buffer only if we reached a connmode
where it exists

Change behaviour on setting unknown HTTP method to kill connection

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 12:55:40 +08:00
Andy Green
d579a7d89a fix another escaape runon
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 12:53:36 +08:00
Andy Green
48258817a0 bump version to 1.2 and soname to 3
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 10:36:39 +08:00
Andy Green
508946cefd api make close api private only
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 10:19:08 +08:00
Andy Green
70edd6fb80 api remove hangup_on_client
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 10:15:25 +08:00
Andy Green
cecf5e73cf eliminate snprintf
The two cases where I introduced snprintf are either already
safe for buffer overflow or can be made so with one extra
statement, allowing sprintf.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 10:13:12 +08:00
Andy Green
7a506ba5cb fix info struct api docs for iface vs interface
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 10:13:02 +08:00
Joakim Soderberg
63ff120ba5 Fixed compilation on Windows. 2013-02-12 09:58:12 +08:00
Andy Green
224149ab58 fix ssl reject path for freeing header allocation
If the SSL connection failed before the headers came, we were not
dealing with deallocating the header malloc.  This takes care of it.

Using CyaSSL, we are then valgrind-clean for ssl client and server.

With OpenSSL, there is 88 bytes lost at init that never changes or
gets recovered.  AFAIK there's nothing to do about that.

OpenSSL also blows these during operation

==1059== Conditional jump or move depends on uninitialised value(s)
==1059==    at 0x4A0B131: bcmp (mc_replace_strmem.c:935)
==1059==    by 0x3014CDDBA8: ??? (in /usr/lib64/libcrypto.so.1.0.1c)
==1059==    by 0x3015430852: tls1_enc (in /usr/lib64/libssl.so.1.0.1c)
==1059==    by 0x3015428CEC: ssl3_read_bytes (in /usr/lib64/libssl.so.1.0.1c)
==1059==    by 0x30154264C5: ??? (in /usr/lib64/libssl.so.1.0.1c)
==1059==    by 0x4C3C596: lws_server_socket_service (server.c:153)
==1059==    by 0x4C32C1E: libwebsocket_service_fd (libwebsockets.c:927)
==1059==    by 0x4C33270: libwebsocket_service (libwebsockets.c:1225)
==1059==    by 0x401C84: main (in /usr/bin/libwebsockets-test-server)

However googling around

https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/60021
http://www.openssl.org/support/faq.html#PROG13
(also the next FAQ down)

it seems OpenSSL have a relaxed attitude to this and it's expected.
It's interesting CyaSSL works fine but doesn't have that problem...

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 21:43:41 +08:00
Andy Green
35517092ff fix busted debug format in ssl mode
CyaSSL blows (symptomless?) uninitialized memory accesses in
valgrind when using SSL_get_version()... don't need to do it...

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 20:10:56 +08:00
Andy Green
090789e87b fix string escape runon
Oh well it was a big patch to just be cosmetic ^^

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 20:03:59 +08:00
Andy Green
14f4729c0d fix without server
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 19:36:15 +08:00
Andy Green
b5b23199f6 style cleanup
This brings the library sources into compliance with checkpatch
style except for three or four exceptions like WIN32 related stuff
and one long string constant I don't want to break into multiple
sprintf calls.

There should be no functional or compilability change from all
this (hopefully).

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 17:13:32 +08:00
Andy Green
ad68639a7b valgrind openssl destroy as far as possible
OpenSSL doesn't seem to have a way to close out three allocations
it creates during SSL library init.

Even after doing the magic incantations suggested on the openssl list, we're
left with these.  Well, 88 bytes from ssl init is probably not critical,
but it's annoying

==15206== HEAP SUMMARY:
==15206==     in use at exit: 88 bytes in 3 blocks
==15206==   total heap usage: 13,566 allocs, 13,563 frees, 5,933,134 bytes allocated
==15206==
==15206== 24 bytes in 1 blocks are still reachable in loss record 1 of 3
==15206==    at 0x4A06409: malloc (vg_replace_malloc.c:270)
==15206==    by 0x3014C612B2: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1c)
==15206==    by 0x3015441B38: ??? (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x3015443A78: SSL_COMP_get_compression_methods (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x301544932B: SSL_library_init (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x4C340D4: libwebsocket_create_context (libwebsockets.c:1796)
==15206==    by 0x401C08: main (in /usr/bin/libwebsockets-test-server)
==15206==
==15206== 32 bytes in 1 blocks are still reachable in loss record 2 of 3
==15206==    at 0x4A06409: malloc (vg_replace_malloc.c:270)
==15206==    by 0x3014C612B2: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1c)
==15206==    by 0x3014CC91BE: sk_new (in /usr/lib64/libcrypto.so.1.0.1c)
==15206==    by 0x3015441AF9: ??? (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x3015443A78: SSL_COMP_get_compression_methods (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x301544932B: SSL_library_init (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x4C340D4: libwebsocket_create_context (libwebsockets.c:1796)
==15206==    by 0x401C08: main (in /usr/bin/libwebsockets-test-server)
==15206==
==15206== 32 bytes in 1 blocks are still reachable in loss record 3 of 3
==15206==    at 0x4A06409: malloc (vg_replace_malloc.c:270)
==15206==    by 0x3014C612B2: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1c)
==15206==    by 0x3014CC91DC: sk_new (in /usr/lib64/libcrypto.so.1.0.1c)
==15206==    by 0x3015441AF9: ??? (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x3015443A78: SSL_COMP_get_compression_methods (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x301544932B: SSL_library_init (in /usr/lib64/libssl.so.1.0.1c)
==15206==    by 0x4C340D4: libwebsocket_create_context (libwebsockets.c:1796)
==15206==    by 0x401C08: main (in /usr/bin/libwebsockets-test-server)
==15206==
==15206== LEAK SUMMARY:
==15206==    definitely lost: 0 bytes in 0 blocks
==15206==    indirectly lost: 0 bytes in 0 blocks
==15206==      possibly lost: 0 bytes in 0 blocks
==15206==    still reachable: 88 bytes in 3 blocks
==15206==         suppressed: 0 bytes in 0 blocks


Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 14:50:45 +08:00
Andy Green
fdd305a986 valgrind eliminate uninitialized warning on close
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 14:32:48 +08:00
Andy Green
1ae1b1fae8 valgrind also deallocate rx buf on close when client
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 14:12:32 +08:00
Andy Green
e77fb804dc client convert to new headers scheme eliminating mallocs
This removes all the direct wsi members specific to clients,
most of them are moved to being fake headers in the next 3-layer
header scheme, c_port moves to being a member of the u.hdr
unionized struct.

It gets rid of a lot of fiddly mallocs and frees(), despite it
adds a small internal API to create the fake headers, actually
the patch deletes more than it adds...

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 13:04:45 +08:00
Andy Green
bebc1bb852 valgrind free rx_user_buffer if entered CONNMODE_WS_SERVING
This seems to be enough to get a clean valgrind run for the
test server with 1 x chrome and 1 x libwebsockets-test-client
session being run for 10s

lwsts[19767]: libwebsockets-test-server exited cleanly
==19767==
==19767== HEAP SUMMARY:
==19767==     in use at exit: 0 bytes in 0 blocks
==19767==   total heap usage: 41,071 allocs, 41,071 frees, 27,464,834 bytes allocated
==19767==
==19767== All heap blocks were freed -- no leaks are possible
==19767==
==19767== For counts of detected and suppressed errors, rerun with: -v
==19767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

test client is another story...

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 12:37:13 +08:00
Andy Green
a7109e6ebf valgrind introduce protocol init and destroy user callbacks
To get a clean bill of health from valgrind, we have to have a way to
inform the user code that we're going down and it should free everything
it is holding that was malloc'd.

This patch introduces LWS_CALLBACK_PROTOCOL_DESTROY which each protocol
gets when the context is being destroyed and no more activity will come
after that call.  They can get rid of everything there.

To match it, LWS_CALLBACK_PROTOCOL_INIT is introduced which would allow
one-time init per protocol too.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 12:05:54 +08:00
Andy Green
7b92205086 valgrind context destroy close all conns properly
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:43:05 +08:00
Andy Green
4708a02f06 valgrind drop header allocation down http path
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:27:44 +08:00
Andy Green
16ab3185c4 replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.

It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure.  It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).

The 3-level thing is all in one struct

 - array indexed by the header enum, pointing to first "fragment" index
	(ie, header type to fragment lookup, or 0 for none)

 - array of fragments indexes, enough for 2 x the number of known headers
	(fragment array... note that fragments can point to a "next"
	fragment if the same header is spread across multiple entries)

 - linear char array where the known header payload gets written
	(fragments point into null-terminated strings stored in here,
	only the known header content is stored)

http headers can legally be split over multiple headers of the same
name which should be concatenated.  This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them.  There are apis to get the total length and copy out a
linear, concatenated version to a buffer.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:10:57 +08:00
Andy Green
a86f634d4f improve static allocation notice
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:10:56 +08:00
Andy Green
4659648159 valgrind free context allocations
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:04:01 +08:00
Andy Green
33fa908293 remove extension cruft from struct lws
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 10:31:17 +08:00
Andy Green
60fbc63513 use part of service buffer to make response not malloc
Done with an offset because the encoded key is stored at the
start of service_buffer at this time

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 10:19:59 +08:00
Andy Green
895d56d4ce remove minimum frame size for deflate
The idea here seems wrong, if we have a mixture of frames of varying
sizes above and below the limit, we segfault in deflate after skipping
it once.

If the protocol doesn't want compression because many frames are
small, it should veto the extension in the user callback.  If only
a few frames are tiny, the overhead for compressing it all is tiny.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 10:18:31 +08:00
Andy Green
a3957ef804 stop O2 override
Now we are building with -O0 -g and debug enabled by default.
--disable-debug in configure will get you a -04 without -g

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 09:31:43 +08:00
Andy Green
aedc953a8f dont close in user callback wrapper let ancestor do it
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:43 +08:00
Andy Green
5ab9c68687 fix error path in file transfer
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:43 +08:00
Andy Green
acfa1a879e throw out lws_websocket_related cruft
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:43 +08:00
Andy Green
d91d5e8d22 optimize wsi using bitfields and enums to chars
Also max protocols to 5 (overridable by configure) and max extensions
from 10 to 3 by default (also overridable by configure).

wsi is down to 608 on x86_64 with this.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:43 +08:00
Andy Green
e48ba315b8 use context service buffer instead of stack for clent_connect
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:43 +08:00
Andy Green
f54a94b494 use context service buffer instead of stack for clent_connect_2
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:42 +08:00
Andy Green
e310b0c443 use context service buffer instead of stack for create_context
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 00:34:42 +08:00
Andy Green
2b40b79e6d fix non ssl changes missed from context api change
http://libwebsockets.org/trac/ticket/11#comment:5

Reported-by: amn
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 22:22:01 +08:00
Andy Green
c97067cf69 use context service buffer instead of stack for lws_client_socket_service
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 11:03:32 +08:00
Andy Green
0390608a39 use context service buffer instead of stack for server_socket_service
Reduces lws_server_socket_service from 4208 to 80 stack allocation
on x86_64

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 10:49:15 +08:00
Andy Green
5f348a80a0 add static stack analysis
You can get a worst-first list of stack allocators like this

$ cat ./lib/.libs/*.su | sort -k2g | tac

On x86_64, currently the ones above 100 bytes are

server.c:126:5:lws_server_socket_service	4208	static
client.c:42:5:lws_client_socket_service	1584	static
libwebsockets.c:1539:1:libwebsocket_create_context	1136	static
libwebsockets.c:783:1:libwebsocket_service_fd	656	static
client-handshake.c:3:22:__libwebsocket_client_connect_2	592	static
libwebsockets.c:2149:6:_lws_log	496	static
server-handshake.c:33:1:handshake_0405	464	static
libwebsockets.c:2102:13:lwsl_emit_stderr	352	static
client.c:351:1:lws_client_interpret_server_handshake	240	static
daemonize.c:93:1:lws_daemonize	224	static
libwebsockets.c:434:1:libwebsockets_get_peer_addresses	208	static
client.c:694:1:libwebsockets_generate_client_handshake	208	static
output.c:534:5:libwebsockets_serve_http_file	192	static
output.c:51:6:lwsl_hexdump	176	static
sha-1.c:316:1:SHA1	160	static
libwebsockets.c:157:1:libwebsocket_close_and_free_session	144	static

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 10:49:15 +08:00
Andy Green
f0b79e238c dont try figure out listen_service_fd position if unset
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 10:49:11 +08:00
Andy Green
a47865fa19 dont try set per socket keepalive timing on bsds
As per http://libwebsockets.org/trac/ticket/10
BSD doesn't support setting keepalive info per-socket

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 09:39:47 +08:00
Andy Green
9e4c917c27 fix broken listen socket piggybacking
As reported here

http://libwebsockets.org/trac/ticket/11

the code for connection storm handling had rotted, fds[0] is no longer
always related to the listen socket when serving.

This patch updates it to determine the listen socket correctly to stop
infinite recursion here.

Reported-by: amn
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 09:06:38 +08:00
Andy Green
a690cd066e introduce keepalive option and make common socket options function
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-09 14:27:38 +08:00