Commit graph

306 commits

Author SHA1 Message Date
Patrick Gansterer
ac49f1e97f Remove useless usage of ssize_t 2014-03-30 10:18:51 +02:00
Patrick Gansterer
b47f87b04b Introduce LWS_POLLIN, LWS_POLLOUT and LWS_POLLHUP 2014-03-30 09:18:05 +02:00
Patrick Gansterer
73882e49fc Remove websock-w32.h 2014-03-29 08:25:58 +01:00
Andy Green
145ecece10 cyassl skip setting SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
It's already the default and no "SSL_set_mode" in CYASSL

Reported by Chris Conlon <chris@wolfssl.com>

Signed-off-by: Andy Green <andy.green@linaro.org>
2014-03-28 14:00:01 +08:00
Andrew Canaday
9769f4f293 libev port
merged by andy@warmcat.com via https://github.com/gaby64/libwebsockets-libev

To use, you need to both

 - cmake ---> -DLWS_USE_LIBEV=1

 - info->options must have LWS_SERVER_OPTION_LIBEV set when creating the context

this is so a single library can be built for distros to support apps that use
normal polling and apps that use libev polling.
2014-03-23 13:25:07 +08:00
Andy Green
35c80f9885 ssl add SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER to server and client
gaby64 at githib found this option is necessary when using SSL with the
truncated send support

Signed-off-by: Andy Green <andy.green@linaro.org>
2014-03-23 11:53:07 +08:00
Patrick Gansterer
2dbd837c41 WSAGetLastError() instead of errno on Windows
Error codes set by Windows Sockets are not made available through the errno
variable. Checking them via WSAGetLastError() is the corret solution.
2014-02-28 20:08:55 +08:00
Patrick Gansterer
cd9d6c5149 Correctly server non-ASCII filenames on Windows
Convert the filename from UTF-8 to UTF-16 and use CreateFileW() to open the file.
2014-02-28 08:59:58 +08:00
Patrick Gansterer
e5720a347e Cleanup include statements
Remove useless includes and add some preprocessor conditions for platform specific headers.
2014-02-28 08:51:35 +08:00
Patrick Gansterer
fce64cda4c Fix Content-Type header of http status response
The header for setting the mime type is called ‘Content-Type’ and not ‘Mime-Type’.
2014-02-27 21:29:48 +08:00
Patrick Gansterer
81338aa886 Use native file functions on Windows
Add a special implementation with CreateFile(), ReadFile() and CloseFile()
for serving HTTP file request to allow compilation on all Windows platforms.
2014-02-27 21:20:36 +08:00
Andy Green
83725d8cd4 revert wrong fix visibility of return_http_status
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-27 07:19:21 +08:00
Andy Green
6afb19ac2c fix visibility of return_http_status
From "DevYourWorld" on github

https://github.com/warmcat/libwebsockets/issues/64#issuecomment-35720037

Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-21 18:51:38 +08:00
Andy Green
92f9b16c9e interface_to_sa no longer server only
Via "DevYourWorld" on github

https://github.com/warmcat/libwebsockets/issues/64

Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-21 18:43:42 +08:00
Andy Green
76b6ea191c add outermost wsi lifetime callbacks
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-15 19:25:50 +08:00
Andy Green
176de27df6 add timeout between accept and negotiation
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-15 14:36:02 +08:00
Alexandre Erwin Ittner
d578f57f7f Fix documentation for LWS_CALLBACK_FILTER_NETWORK_CONNECTION
At the time callback LWS_CALLBACK_FILTER_NETWORK_CONNECTION is called,
there is no client connection information yet, so the parameter wsi
still pointing to the main server connection. Add an description of
this behavior to the documentation.
2014-02-15 14:02:30 +08:00
Andy Green
91f19d8d79 unify all pollfd lock management
This provides a single place for pollfd event changing,
external locking for that and extpoll management.

It saves about 85 lines of duplication and simplifies the callers.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-12-21 11:18:34 +08:00
Andy Green
7a1327977a add locking callback for fds
This adds two new callbacks in protocols[0] that are optional for allowing limited thread
access to libwebsockets, LWS_CALLBACK_LOCK_POLL and LWS_CALLBACK_UNLOCK_POLL.

If you use them, they protect internal and external poll list changes, but if you want to use
external thread access to libwebsocket_callback_on_writable() you have to implement your
locking here even if you don't use external poll support.

If you will use another thread for this, take a lot of care about managing your list of
live wsi by doing it from ESTABLISHED and CLOSED callbacks (with your own locking).

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-12-18 09:48:26 +08:00
James Devine
5b34c975ae Optionally allow non-SSL connections on same port as SSL
If enabled one listening socket will accept both SSL and plain HTTP connections.

Do not enable if you regard SSL handshake as some kind of security, eg, use
client-side certs to restrict access.

AG: changed flag names, added extra comments, changelog, add -a in test server

Signed-off-by: James Devine <fxmulder@gmail.com>
Signed-off-by: Andy Green <andy@warmcat.com>
2013-12-14 11:41:29 +08:00
Andy Green
2764eba89f handle EAGAIN during send
This patch deploys the truncated send work to buffer output in case
either send() or the SSL send return a temporary "unable to send"
condition even though they signalled as writeable.

I added a by-default #if 0 test jig which enforces only half of what
you want to send is sendable, this is working when enabled.

One subtle change is that the pipe reports choked if there is any
pending remaining truncated send.  Otherwise it should be transparent.

Hopefully...

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-12-09 14:16:17 +08:00
Andy Green
4e7a13314d real http status codes update attack.sh
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-11-11 07:30:33 +08:00
Andy Green
d1eac60f81 interface improve visibility fail if not found
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-11-09 08:07:38 +08:00
Andreas Pakulat
68bd4bd7bf Support _WIN32 in addition to WIN32 define
MSVC8 and MSVC9 set only _WIN32, so support that define as well to be able
to compile libwebsockets with those compilers.
2013-11-04 10:04:31 +08:00
Andy Green
7cf6cb00c2 trac 28 - differentiate between http serving and accepted http connection
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-05-19 14:04:10 +08:00
Bob Roberts
ac04911dde cygwin compatibility
Signed-off-by: Bob Roberts <bobroberts177@gmail.com>
2013-04-25 09:16:30 +08:00
Joakim Soderberg
b82b0dd118 Print SSL error codes as well as the string.
CYASSL doesn't compile with the error strings in some configurations, so
it is nice to have the error code on SSL errors as well.
2013-02-22 09:28:15 +08:00
Edwin van den Oetelaar
8c8a8e149d provide socketfd at in param for LWS_CALLBACK_FILTER_NETWORK_CONNECTION
Signed-off-by: Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-20 20:56:59 +08:00
Andy Green
0c2f4d865a update missed extpoll calls to use correct args for ssl
http://libwebsockets.org/trac/ticket/15#comment:16

Reported-by: Joakim Soderberg <joakim.soderberg@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-19 19:19:51 +08:00
Andy Green
cc7cb68ded remove header name buffer
The header name buffer and its max length handling has actually
been unused since the minilex parser was introduced.  We hold
parsing state in the lex-type parts and don't need to store or
worry about max length, since the parser will let us know as
soon as it can't be a match for the valid header names.

This strips it out reducing the per-connection allocation for
x86_64 with default configure from 224 to 160.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-18 10:22:42 +08:00
Andy Green
54cb346320 introduce LWS_CALLBACK_HTTP_WRITEABLE
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-14 22:23:54 +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
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
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
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
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
Andy Green
5449511d3e remove fixed rx buffer allow definition per protocol
A new protocol member is defined that controls the size of rx
buffer allocation per connection.  For compatibility 0 size
allocates 4096, but you should adapt your protocol definition
array in the user code to declare an appropriate value.

See the changelog for more detail.

The advantage is the rx frame buffer size is now tailored to
what is expected from the protocol, rather than being fixed
to a default of 4096.  If your protocol only sends frames of
a dozen bytes this allows you to only allocate an rx frame
buffer of the same size.

For example the per-connection allocation (excluding headers)
for the test server fell from ~4500 to < 750 bytes with this.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 13:16:07 +08:00
Andy Green
b8b247d3e1 remove need for filepath buffer on http file serve
This gets rid of the stack buffer while serving files, and the
PATH_MAX char array that used to hold the filepath in the wsi.

It holds an extra file descriptor open while serving the file,
however it attempts to stuff the socket with as much of the
file as it can take.  For files of a few KB, that typically
completes (without blocking) in the call to
libwebsockets_serve_http_file() and then closes the file
descriptor before returning.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 12:19:01 +08:00
Andy Green
23c5f2ecd0 add autotools bits for cyassl
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Andy Green
68a672bb44 unionize header token array
This reduces the size of struct libwebscocket from 4840 to 4552
on x86_64

There are also big benefits on malloc pool fragmentation and
allocation, the header allocations only exist between the first
peer communication and websocket connection establishment for
both server and client.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Andy Green
2b57a34677 headers deleted after websocket established
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Joakim Soderberg
b378ce9d18 Added support for CyaSSL replacement of OpenSSL.
http://www.yassl.com/yaSSL/Products-cyassl.html
- Small Size: 20-100kB
- Runtime Memory:  1-36kB
- 20X smaller than OpenSSL

So far only tested on Linux.

Note that this requires a bugfix in cyassl, otherwise it will crash. Pull request has been made to the official repos, in the meantime the following repos can be used: git://github.com/JoakimSoderberg/cyassl.git
2013-02-06 15:49:12 +09:00
Joakim Soderberg
4c53123677 CMake support + fixed windows build.
- Finalized CMake support (tested on windows only so far).
  - Uses a generated lws_config.h that is included in
  private-libwebsocket to pass defines, only used if CMAKE_BUILD is set.
  - Support for SSL on Windows.
  - Initial support for CyaSSL replacement of OpenSSL (This has been added
    to my older CMake-fork but haven't been tested on this version yet).
- Fixed windows build (see below for details).
- Fixed at least the 32-bit Debug build for the existing Visual Studio
  Project. (Not to keen fixing all the others when we have CMake support
  anyway (which can generate much better project files)...)
- BUGFIXES:
  - handshake.c
    - used C99 definition of handshake_0405 function
  - libwebsocket.c
    - syslog not available on windows, put in ifdefs.
    - Fixed previous known crash bug on Windows where WSAPoll in
      Ws2_32.dll would not be present, causing the poll function pointer
      being set to NULL.
    - Uninitialized variable context->listen_service_extraseen would
      result in stack overflow because of infinite recursion. Fixed by
      initializing in libwebsocket_create_context
    - SO_REUSADDR means something different on Windows compared to Unix.
    - Setting a socket to nonblocking is done differently on Windows.
      (This should probably broken out into a helper function instead)
    - lwsl_emit_syslog -> lwsl_emit_stderr on Windows.
  - private-libwebsocket.h
    - PATH_MAX is not available on Windows, define as MAX_PATH
    - Always define LWS_NO_DAEMONIZE on windows.
    - Don't define lws_latency as inline that does nothing. inline is not
      support by the Microsoft compiler, replaced with an empty define
      instead. (It's __inline in MSVC)
  - server.c
    - Fixed nonblock call on windows
  - test-ping.c
    - Don't use C99 features (Microsoft compiler does not support it).
    - Move non-win32 headers into ifdefs.
    - Skip use of sighandler on Windows.
  - test-server.c
    - ifdef syslog parts on Windows.
2013-02-06 15:49:12 +09:00
Andy Green
e000a709b3 instrument latency
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-30 08:49:12 +08:00
Andy Green
6f520a5195 evict all broadcast support
Libwebsockets is fundamentally singlethreaded... the existence of the
fork and broadcast support, especially in the sample server is
giving the wrong idea about how to use it.

This replaces broadcast in the sample server with
libwebsocket_callback_on_writable_all_protocol().  The whole idea of
'broadcast' is removed.

All of the broadcast proxy stuff is removed: data must now be sent
from the callback only.  Doing othherwise is not reliable since the
service loop may close the socket and free the wsi at any time,
invalidating a wsi pointer held by another thread (don't do that!)

Likewise the confirm_legit_wsi api added recently does not help the
other thread case, since if the wsi has been freed dereferencing the
wsi to study if it is legit or not will segfault in that case.  So
this is removed too.

The overall effect is to push user code to only operate inside the
protocol callbacks or external poll loops, ie, single thread context.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-29 22:13:55 +08:00
Edwin van den Oetelaar
0794af94eb fix ssl bits outside of ifdef coverage
Signed-off-by: Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
2013-01-28 21:53:53 +08:00
Andy Green
1167dd4a6e force ssl rw bios nonblocking
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-28 17:49:52 +08:00
Andy Green
ba85a7dbbd timeout coverage for ssl accept
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-28 17:49:51 +08:00