提交 6fa2fd36 authored 作者: Michael Jerris's avatar Michael Jerris

add ws

上级 ef5c1256
output/
win32port/ipch/
win32port/Debug*/
win32port/Release*/
win32port/server/Debug*/
win32port/server/Release*/
win32port/client/Debug*/
win32port/client/Release*/
win32port/libwebsocketswin32/Debug*/
win32port/libwebsocketswin32/Release*/
win32port/zlib/Debug*/
win32port/zlib/Release*/
*.vcxproj.user
*.opensdf
*.sdf
*.suo
差异被折叠。
差异被折叠。
差异被折叠。
Using test-server as a quickstart
---------------------------------
You need to regenerate the autotools and libtoolize stuff for your system
$ autoreconf
$ libtoolize
Then for a Fedora x86_86 box, the following config line was
needed:
./configure --prefix=/usr --libdir=/usr/lib64 --enable-openssl
For Apple systems, Christopher Baker reported that this is needed
(and I was told separately enabling openssl makes trouble somehow)
./configure CC="gcc -arch i386 -arch x86_64" CXX="g++ -arch i386 -arch
x86_64" CPP="gcc -E" CXXCPP="g++ -E" --enable-nofork
For mingw build, I did the following to get working build, ping test is
disabled when building this way
1) install mingw64_w32 compiler packages from Fedora
2) additionally install mingw64-zlib package
3) ./configure --prefix=/usr --enable-mingw --host=x86_64-w64-mingw32
4) make
otherwise if /usr/local/... and /usr/local/lib are OK then...
$ ./configure
$ make clean
$ make
$ sudo make install
$ libwebsockets-test-server
should be enough to get a test server listening on port 7861.
There are a couple of other possible configure options
--enable-nofork disables the fork into the background API
and removes all references to fork() and
pr_ctl() from the sources. Use it if your
platform doesn't support forking.
--enable-libcrypto by default libwebsockets uses its own
built-in md5 and sha-1 implementation for
simplicity. However the libcrypto ones
may be faster, and in a distro context it
may be highly desirable to use a common
library implementation for ease of security
upgrades. Give this configure option
to disable the built-in ones and force use
of the libcrypto (part of openssl) ones.
--with-client-cert-dir=dir tells the client ssl support where to
look for trust certificates to validate
the remote certificate against.
--enable-noping Don't try to build the ping test app
It needs some unixy environment that
may choke in other build contexts, this
lets you cleanly stop it being built
--enable-x-google-mux Enable experimental x-google-mux support
in the build (see notes later in document)
Testing server with a browser
-----------------------------
If you point your browser (eg, Chrome) to
http://127.0.0.1:7681
It will fetch a script in the form of test.html, and then run the
script in there on the browser to open a websocket connection.
Incrementing numbers should appear in the browser display.
Using SSL on the server side
----------------------------
To test it using SSL/WSS, just run the test server with
$ libwebsockets-test-server --ssl
and use the URL
https://127.0.0.1:7681
The connection will be entirely encrypted using some generated
certificates that your browser will not accept, since they are
not signed by any real Certificate Authority. Just accept the
certificates in the browser and the connection will proceed
in first https and then websocket wss, acting exactly the
same.
test-server.c is all that is needed to use libwebsockets for
serving both the script html over http and websockets.
Forkless operation
------------------
If your target device does not offer fork(), you can use
libwebsockets from your own main loop instead. Use the
configure option --nofork and simply call libwebsocket_service()
from your own main loop as shown in the test app sources.
Testing websocket client support
--------------------------------
If you run the test server as described above, you can also
connect to it using the test client as well as a browser.
$ libwebsockets-test-client localhost
will by default connect to the test server on localhost:7681
and print the dumb increment number from the server at the
same time as drawing random circles in the mirror protocol;
if you connect to the test server using a browser at the
same time you will be able to see the circles being drawn.
Testing SSL on the client side
------------------------------
To test SSL/WSS client action, just run the client test with
$ libwebsockets-test-client localhost --ssl
By default the client test applet is set to accept selfsigned
certificates used by the test server, this is indicated by the
use_ssl var being set to 2. Set it to 1 to reject any server
certificate that it doesn't have a trusted CA cert for.
Using the websocket ping utility
--------------------------------
libwebsockets-test-ping connects as a client to a remote
websocket server using 04 protocol and pings it like the
normal unix ping utility.
$ libwebsockets-test-ping localhost
handshake OK for protocol lws-mirror-protocol
Websocket PING localhost.localdomain (127.0.0.1) 64 bytes of data.
64 bytes from localhost: req=1 time=0.1ms
64 bytes from localhost: req=2 time=0.1ms
64 bytes from localhost: req=3 time=0.1ms
64 bytes from localhost: req=4 time=0.2ms
64 bytes from localhost: req=5 time=0.1ms
64 bytes from localhost: req=6 time=0.2ms
64 bytes from localhost: req=7 time=0.2ms
64 bytes from localhost: req=8 time=0.1ms
^C
--- localhost.localdomain websocket ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7458ms
rtt min/avg/max = 0.110/0.185/0.218 ms
$
By default it sends 64 byte payload packets using the 04
PING packet opcode type. You can change the payload size
using the -s= flag, up to a maximum of 125 mandated by the
04 standard.
Using the lws-mirror protocol that is provided by the test
server, libwebsockets-test-ping can also use larger payload
sizes up to 4096 is BINARY packets; lws-mirror will copy
them back to the client and they appear as a PONG. Use the
-m flag to select this operation.
The default interval between pings is 1s, you can use the -i=
flag to set this, including fractions like -i=0.01 for 10ms
interval.
Before you can even use the PING opcode that is part of the
standard, you must complete a handshake with a specified
protocol. By default lws-mirror-protocol is used which is
supported by the test server. But if you are using it on
another server, you can specify the protcol to handshake with
by --protocol=protocolname
Fraggle test app
----------------
By default it runs in server mode
$ libwebsockets-test-fraggle
libwebsockets test fraggle
(C) Copyright 2010-2011 Andy Green <andy@warmcat.com> licensed under LGPL2.1
Compiled with SSL support, not using it
Listening on port 7681
server sees client connect
accepted v06 connection
Spamming 360 random fragments
Spamming session over, len = 371913. sum = 0x2D3C0AE
Spamming 895 random fragments
Spamming session over, len = 875970. sum = 0x6A74DA1
...
You need to run a second session in client mode, you have to
give the -c switch and the server address at least:
$ libwebsockets-test-fraggle -c localhost
libwebsockets test fraggle
(C) Copyright 2010-2011 Andy Green <andy@warmcat.com> licensed under LGPL2.1
Client mode
Connecting to localhost:7681
denied deflate-stream extension
handshake OK for protocol fraggle-protocol
client connects to server
EOM received 371913 correctly from 360 fragments
EOM received 875970 correctly from 895 fragments
EOM received 247140 correctly from 258 fragments
EOM received 695451 correctly from 692 fragments
...
The fraggle test sends a random number up to 1024 fragmented websocket frames
each of a random size between 1 and 2001 bytes in a single message, then sends
a checksum and starts sending a new randomly sized and fragmented message.
The fraggle test client receives the same message fragments and computes the
same checksum using websocket framing to see when the message has ended. It
then accepts the server checksum message and compares that to its checksum.
proxy support
-------------
The http_proxy environment variable is respected by the client
connection code for both ws:// and wss://. It doesn't support
authentication yet.
You use it like this
export http_proxy=myproxy.com:3128
libwebsockets-test-client someserver.com
Websocket version supported
---------------------------
The websocket client code is 04 and 05 version, the server
supports 00/76 in text mode and 04 and 05 dynamically
per-connection depending on the version of the
client / browser.
External Polling Loop support
-----------------------------
libwebsockets maintains an internal poll() array for all of its
sockets, but you can instead integrate the sockets into an
external polling array. That's needed if libwebsockets will
cooperate with an existing poll array maintained by another
server.
Four callbacks LWS_CALLBACK_ADD_POLL_FD, LWS_CALLBACK_DEL_POLL_FD,
LWS_CALLBACK_SET_MODE_POLL_FD and LWS_CALLBACK_CLEAR_MODE_POLL_FD
appear in the callback for protocol 0 and allow interface code to
manage socket descriptors in other poll loops.
x-google-mux support
--------------------
Experimental and super-preliminary x-google-mux support is available if
enabled in ./configure with --enable-x-google-mux. Note that when changing
configurations, you will need to do a make distclean before, then the new
configure and then make ; make install. Don't forget the necessary other
flags for your platform as described at the top of the readme.
It has the following notes:
1) To enable it, reconfigure with --enable-x-google-mux
2) It deviates from the google standard by sending full
headers in the addchannel subcommand rather than just
changed ones from original connect
3) Quota is not implemented yet
However despite those caveats, in fact it can run the
test client reliably over one socket (both dumb-increment
and lws-mirror-protocol), you can open a browser on the
same test server too and see the circles, etc.
It also works compatibly with deflate-stream automatically.
2012-04-12 Andy Green <andy@warmcat.com>
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.66])
AC_INIT(libwebsockets, 0.3, andy@warmcat.com)
AC_CONFIG_SRCDIR([test-server/test-server.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
LT_INIT(shared)
#AX_PTHREAD
# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CONFIG_MACRO_DIR([m4])
#
#
#
AC_ARG_ENABLE(openssl,
[ --enable-openssl Enables https support and needs openssl libs],
[ openssl=yes
])
if test "x$openssl" = "xyes" ; then
AC_CHECK_LIB([ssl], [SSL_library_init])
CFLAGS="$CFLAGS -DLWS_OPENSSL_SUPPORT"
fi
#
#
#
AC_ARG_ENABLE(nofork,
[ --enable-nofork Disables fork-related options],
[ nofork=yes
])
if test "x$nofork" = "xyes" ; then
CFLAGS="$CFLAGS -DLWS_NO_FORK"
else
AC_FUNC_FORK
fi
#
#
#
AC_ARG_ENABLE(libcrypto,
[ --enable-libcrypto Use libcrypto MD5 and SHA1 implementations],
[ libcrypto=yes
])
if test "x$libcrypto" = "xyes" ; then
CFLAGS="$CFLAGS -DLWS_LIBCRYPTO"
LDFLAGS="$LDFLAGS -lcrypto"
fi
AM_CONDITIONAL(LIBCRYPTO, test x$libcrypto = xyes)
#
#
#
AC_ARG_ENABLE(x-google-mux,
[ --enable-x-google-mux Build experimental x-google-mux],
[ x_google_mux=yes
])
if test "x$x_google_mux" = "xyes" ; then
CFLAGS="$CFLAGS -DLWS_EXT_GOOGLE_MUX"
fi
AM_CONDITIONAL(EXT_GOOGLE_MUX, test x$x_google_mux = xyes)
#
#
#
AC_ARG_ENABLE(mingw,
[ --enable-mingw Using mingw compilers, disables ping test build],
[ mingw=yes
noping=yes
])
if test "x$mingw" = "xyes" ; then
CFLAGS="$CFLAGS -DLWS_MINGW_SUPPORT"
fi
AM_CONDITIONAL(MINGW, test x$mingw = xyes)
#
#
#
AC_ARG_WITH([client-cert-dir],
[AS_HELP_STRING([--with-client-cert-dir],[directory containing client certs, defaults to /etc/pki/tls/certs/])],
[clientcertdir=$withval],
[clientcertdir=/etc/pki/tls/certs/]
)
AC_SUBST([clientcertdir])
AC_SUBST([CFLAGS])
#
#
#
AC_ARG_ENABLE(noping,
[ --enable-noping Do not build ping test app, which has some unixy stuff in sources],
[ noping=yes
])
AM_CONDITIONAL(NOPING, test x$noping = xyes)
# Checks for header files.
AC_CHECK_HEADERS([zlib.h fcntl.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h sys/prctl.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([bzero memset socket strerror])
AC_CONFIG_FILES([Makefile
lib/Makefile
test-server/Makefile])
AC_OUTPUT
lib_LTLIBRARIES=libwebsockets.la
include_HEADERS=libwebsockets.h
dist_libwebsockets_la_SOURCES=libwebsockets.c \
handshake.c \
parsers.c \
libwebsockets.h \
base64-decode.c \
client-handshake.c \
extension.c \
extension-deflate-stream.c \
private-libwebsockets.h
if EXT_GOOGLE_MUX
dist_libwebsockets_la_SOURCES += extension-x-google-mux.c
endif
if LIBCRYPTO
else
dist_libwebsockets_la_SOURCES += md5.c sha-1.c
endif
libwebsockets_la_CFLAGS:=-Wall -std=gnu99 -pedantic
libwebsockets_la_LDFLAGS:=
if MINGW
libwebsockets_la_CFLAGS+= -w -I../win32port/win32helpers -I ../win32port/zlib/
libwebsockets_la_LDFLAGS+= -lm -luser32 -ladvapi32 -lkernel32 -lgcc
else
libwebsockets_la_CFLAGS+= -rdynamic -fPIC -Werror
libwebsockets_la_LDFLAGS+= -version-info 0:3
endif
libwebsockets_la_CFLAGS+= -c \
-DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
libwebsockets_la_LDFLAGS+= -lz
all-local:
../scripts/kernel-doc -html \
libwebsockets.c \
parsers.c \
client-handshake.c \
libwebsockets.h \
> ../libwebsockets-api-doc.html
差异被折叠。
/*
* This code originally came from here
*
* http://base64.sourceforge.net/b64.c
*
* with the following license:
*
* LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall
* be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* VERSION HISTORY:
* Bob Trower 08/04/01 -- Create Version 0.00.00B
*
* I cleaned it up quite a bit to match the (linux kernel) style of the rest
* of libwebsockets; this version is under LGPL2 like the rest of libwebsockets
* since he explictly allows sublicensing, but I give the URL above so you can
* get the original with Bob's super-liberal terms directly if you prefer.
*/
#include <stdio.h>
#include <string.h>
static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789+/";
static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
"$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
int
lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
{
unsigned char triple[3];
int i;
int len;
int line = 0;
int done = 0;
while (in_len) {
len = 0;
for (i = 0; i < 3; i++) {
if (in_len) {
triple[i] = *in++;
len++;
in_len--;
} else
triple[i] = 0;
}
if (!len)
continue;
if (done + 4 >= out_size)
return -1;
*out++ = encode[triple[0] >> 2];
*out++ = encode[((triple[0] & 0x03) << 4) |
((triple[1] & 0xf0) >> 4)];
*out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
((triple[2] & 0xc0) >> 6)] : '=');
*out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
done += 4;
line += 4;
}
if (done + 1 >= out_size)
return -1;
*out++ = '\0';
return done;
}
/*
* returns length of decoded string in out, or -1 if out was too small
* according to out_size
*/
int
lws_b64_decode_string(const char *in, char *out, int out_size)
{
int len;
int i;
int done = 0;
unsigned char v;
unsigned char quad[4];
while (*in) {
len = 0;
for (i = 0; i < 4 && *in; i++) {
v = 0;
while (*in && !v) {
v = *in++;
v = (v < 43 || v > 122) ? 0 : decode[v - 43];
if (v)
v = (v == '$') ? 0 : v - 61;
if (*in) {
len++;
if (v)
quad[i] = v - 1;
} else
quad[i] = 0;
}
}
if (!len)
continue;
if (out_size < (done + len - 1))
/* out buffer is too small */
return -1;
if (len >= 2)
*out++ = quad[0] << 2 | quad[1] >> 4;
if (len >= 3)
*out++ = quad[1] << 4 | quad[2] >> 2;
if (len >= 4)
*out++ = ((quad[2] << 6) & 0xc0) | quad[3];
done += len - 1;
}
if (done + 1 >= out_size)
return -1;
*out++ = '\0';
return done;
}
int
lws_b64_selftest(void)
{
char buf[64];
int n;
int test;
static const char * const plaintext[] = {
"sanity check base 64"
};
static const char * const coded[] = {
"c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
};
for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
buf[sizeof(buf) - 1] = '\0';
n = lws_b64_encode_string(plaintext[test],
strlen(plaintext[test]), buf, sizeof buf);
if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
fprintf(stderr, "Failed lws_b64 encode selftest "
"%d result '%s' %d\n", test, buf, n);
return -1;
}
buf[sizeof(buf) - 1] = '\0';
n = lws_b64_decode_string(coded[test], buf, sizeof buf);
if (n != strlen(plaintext[test]) ||
strcmp(buf, plaintext[test])) {
fprintf(stderr, "Failed lws_b64 decode selftest "
"%d result '%s' %d\n", test, buf, n);
return -1;
}
}
return 0;
}
#include "private-libwebsockets.h"
struct libwebsocket *__libwebsocket_client_connect_2(
struct libwebsocket_context *context,
struct libwebsocket *wsi
) {
struct pollfd pfd;
struct timeval tv;
struct hostent *server_hostent;
struct sockaddr_in server_addr;
int n;
int plen = 0;
char pkt[512];
int opt = 1;
#if defined(__APPLE__)
struct protoent *tcp_proto;
#endif
debug("__libwebsocket_client_connect_2\n");
wsi->candidate_children_list = NULL;
/*
* proxy?
*/
if (context->http_proxy_port) {
plen = sprintf(pkt, "CONNECT %s:%u HTTP/1.0\x0d\x0a"
"User-agent: libwebsockets\x0d\x0a"
/*Proxy-authorization: basic aGVsbG86d29ybGQ= */
"\x0d\x0a", wsi->c_address, wsi->c_port);
/* OK from now on we talk via the proxy */
free(wsi->c_address);
wsi->c_address = strdup(context->http_proxy_address);
wsi->c_port = context->http_proxy_port;
}
/*
* prepare the actual connection (to the proxy, if any)
*/
debug("__libwebsocket_client_connect_2: address %s", wsi->c_address);
server_hostent = gethostbyname(wsi->c_address);
if (server_hostent == NULL) {
fprintf(stderr, "Unable to get host name from %s\n",
wsi->c_address);
goto oom4;
}
wsi->sock = socket(AF_INET, SOCK_STREAM, 0);
if (wsi->sock < 0) {
fprintf(stderr, "Unable to open socket\n");
goto oom4;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(wsi->c_port);
server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
bzero(&server_addr.sin_zero, 8);
/* Disable Nagle */
#if !defined(__APPLE__)
setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY,
(const void *)&opt, sizeof(opt));
#else
tcp_proto = getprotobyname("TCP");
setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY,
&opt, sizeof(opt));
#endif
/* Set receiving timeout */
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000;
setsockopt(wsi->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
if (connect(wsi->sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) {
fprintf(stderr, "Connect failed\n");
#ifdef WIN32
closesocket(wsi->sock);
#else
close(wsi->sock);
#endif
goto oom4;
}
debug("connected\n");
/* into fd -> wsi hashtable */
insert_wsi(context, wsi);
/* into internal poll list */
context->fds[context->fds_count].fd = wsi->sock;
context->fds[context->fds_count].revents = 0;
context->fds[context->fds_count++].events = POLLIN;
/* external POLL support via protocol 0 */
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_ADD_POLL_FD,
(void *)(long)wsi->sock, NULL, POLLIN);
/* we are connected to server, or proxy */
if (context->http_proxy_port) {
n = send(wsi->sock, pkt, plen, 0);
if (n < 0) {
#ifdef WIN32
closesocket(wsi->sock);
#else
close(wsi->sock);
#endif
fprintf(stderr, "ERROR writing to proxy socket\n");
goto bail1;
}
libwebsocket_set_timeout(wsi,
PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, 5);
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY;
return wsi;
}
/*
* provoke service to issue the handshake directly
* we need to do it this way because in the proxy case, this is the
* next state and executed only if and when we get a good proxy
* response inside the state machine
*/
wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
pfd.fd = wsi->sock;
pfd.revents = POLLIN;
if (libwebsocket_service_fd(context, &pfd) < 0)
goto oom4;
return wsi;
oom4:
if (wsi->c_protocol)
free(wsi->c_protocol);
if (wsi->c_origin)
free(wsi->c_origin);
free(wsi->c_host);
free(wsi->c_path);
bail1:
free(wsi);
return NULL;
}
/**
* libwebsocket_client_connect() - Connect to another websocket server
* @context: Websocket context
* @address: Remote server address, eg, "myserver.com"
* @port: Port to connect to on the remote server, eg, 80
* @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
* signed certs
* @path: Websocket path on server
* @host: Hostname on server
* @origin: Socket origin name
* @protocol: Comma-separated list of protocols being asked for from
* the server, or just one. The server will pick the one it
* likes best.
* @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
* protocol supported, or the specific protocol ordinal
*
* This function creates a connection to a remote server
*/
struct libwebsocket *
libwebsocket_client_connect(struct libwebsocket_context *context,
const char *address,
int port,
int ssl_connection,
const char *path,
const char *host,
const char *origin,
const char *protocol,
int ietf_version_or_minus_one)
{
struct libwebsocket *wsi;
int n;
int m;
struct libwebsocket_extension *ext;
int handled;
#ifndef LWS_OPENSSL_SUPPORT
if (ssl_connection) {
fprintf(stderr, "libwebsockets not configured for ssl\n");
return NULL;
}
#endif
wsi = malloc(sizeof(struct libwebsocket));
if (wsi == NULL)
goto bail1;
memset(wsi, 0, sizeof *wsi);
/* -1 means just use latest supported */
if (ietf_version_or_minus_one == -1)
ietf_version_or_minus_one = SPEC_LATEST_SUPPORTED;
wsi->ietf_spec_revision = ietf_version_or_minus_one;
wsi->name_buffer_pos = 0;
wsi->user_space = NULL;
wsi->state = WSI_STATE_CLIENT_UNCONNECTED;
wsi->pings_vs_pongs = 0;
wsi->protocol = NULL;
wsi->pending_timeout = NO_PENDING_TIMEOUT;
wsi->count_active_extensions = 0;
#ifdef LWS_OPENSSL_SUPPORT
wsi->use_ssl = ssl_connection;
#endif
wsi->c_port = port;
wsi->c_address = strdup(address);
/* copy parameters over so state machine has access */
wsi->c_path = malloc(strlen(path) + 1);
if (wsi->c_path == NULL)
goto bail1;
strcpy(wsi->c_path, path);
wsi->c_host = malloc(strlen(host) + 1);
if (wsi->c_host == NULL)
goto oom1;
strcpy(wsi->c_host, host);
if (origin) {
wsi->c_origin = malloc(strlen(origin) + 1);
strcpy(wsi->c_origin, origin);
if (wsi->c_origin == NULL)
goto oom2;
} else
wsi->c_origin = NULL;
wsi->c_callback = NULL;
if (protocol) {
const char *pc;
struct libwebsocket_protocols *pp;
wsi->c_protocol = malloc(strlen(protocol) + 1);
if (wsi->c_protocol == NULL)
goto oom3;
strcpy(wsi->c_protocol, protocol);
pc = protocol;
while (*pc && *pc != ',')
pc++;
n = pc - protocol;
pp = context->protocols;
while (pp->name && !wsi->c_callback) {
if (!strncmp(protocol, pp->name, n))
wsi->c_callback = pp->callback;
pp++;
}
} else
wsi->c_protocol = NULL;
if (!wsi->c_callback)
wsi->c_callback = context->protocols[0].callback;
/* set up appropriate masking */
wsi->xor_mask = xor_no_mask;
switch (wsi->ietf_spec_revision) {
case 0:
break;
case 4:
wsi->xor_mask = xor_mask_04;
break;
case 5:
case 6:
case 7:
case 8:
case 13:
wsi->xor_mask = xor_mask_05;
break;
default:
fprintf(stderr,
"Client ietf version %d not supported\n",
wsi->ietf_spec_revision);
goto oom4;
}
/* force no mask if he asks for that though */
if (context->options & LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK)
wsi->xor_mask = xor_no_mask;
for (n = 0; n < WSI_TOKEN_COUNT; n++) {
wsi->utf8_token[n].token = NULL;
wsi->utf8_token[n].token_len = 0;
}
/*
* Check with each extension if it is able to route and proxy this
* connection for us. For example, an extension like x-google-mux
* can handle this and then we don't need an actual socket for this
* connection.
*/
handled = 0;
ext = context->extensions;
n = 0;
while (ext && ext->callback && !handled) {
m = ext->callback(context, ext, wsi,
LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
(void *)(long)n, (void *)address, port);
if (m)
handled = 1;
ext++;
n++;
}
if (handled) {
debug("libwebsocket_client_connect: ext handling conn\n");
libwebsocket_set_timeout(wsi,
PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, 5);
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT;
return wsi;
}
debug("libwebsocket_client_connect: direct conn\n");
return __libwebsocket_client_connect_2(context, wsi);
oom4:
if (wsi->c_protocol)
free(wsi->c_protocol);
oom3:
if (wsi->c_origin)
free(wsi->c_origin);
oom2:
free(wsi->c_host);
oom1:
free(wsi->c_path);
bail1:
free(wsi);
return NULL;
}
/**
* libwebsocket_client_connect_extended() - Connect to another websocket server
* @context: Websocket context
* @address: Remote server address, eg, "myserver.com"
* @port: Port to connect to on the remote server, eg, 80
* @ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
* signed certs
* @path: Websocket path on server
* @host: Hostname on server
* @origin: Socket origin name
* @protocol: Comma-separated list of protocols being asked for from
* the server, or just one. The server will pick the one it
* likes best.
* @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
* protocol supported, or the specific protocol ordinal
* @userdata: Pre-allocated user data
*
* This function creates a connection to a remote server
*/
struct libwebsocket *
libwebsocket_client_connect_extended(struct libwebsocket_context *context,
const char *address,
int port,
int ssl_connection,
const char *path,
const char *host,
const char *origin,
const char *protocol,
int ietf_version_or_minus_one,
void *userdata)
{
struct libwebsocket *ws =
libwebsocket_client_connect(context, address, port, ssl_connection, path, host, origin, protocol, ietf_version_or_minus_one) ;
if (ws && !ws->user_space && userdata)
ws->user_space = userdata ;
return ws ;
}
#include "private-libwebsockets.h"
#include "extension-deflate-stream.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define LWS_ZLIB_WINDOW_BITS 15
#define LWS_ZLIB_MEMLEVEL 8
int lws_extension_callback_deflate_stream(
struct libwebsocket_context *context,
struct libwebsocket_extension *ext,
struct libwebsocket *wsi,
enum libwebsocket_extension_callback_reasons reason,
void *user, void *in, size_t len)
{
struct lws_ext_deflate_stream_conn *conn =
(struct lws_ext_deflate_stream_conn *)user;
int n;
struct lws_tokens *eff_buf = (struct lws_tokens *)in;
switch (reason) {
/*
* for deflate-stream, both client and server sides act the same
*/
case LWS_EXT_CALLBACK_CLIENT_CONSTRUCT:
case LWS_EXT_CALLBACK_CONSTRUCT:
conn->zs_in.zalloc = conn->zs_out.zalloc = Z_NULL;
conn->zs_in.zfree = conn->zs_out.zfree = Z_NULL;
conn->zs_in.opaque = conn->zs_out.opaque = Z_NULL;
n = inflateInit2(&conn->zs_in, -LWS_ZLIB_WINDOW_BITS);
if (n != Z_OK) {
fprintf(stderr, "deflateInit returned %d\n", n);
return 1;
}
n = deflateInit2(&conn->zs_out,
DEFLATE_STREAM_COMPRESSION_LEVEL, Z_DEFLATED,
-LWS_ZLIB_WINDOW_BITS, LWS_ZLIB_MEMLEVEL,
Z_DEFAULT_STRATEGY);
if (n != Z_OK) {
fprintf(stderr, "deflateInit returned %d\n", n);
return 1;
}
debug("zlibs constructed\n");
break;
case LWS_EXT_CALLBACK_DESTROY:
(void)inflateEnd(&conn->zs_in);
(void)deflateEnd(&conn->zs_out);
debug("zlibs destructed\n");
break;
case LWS_EXT_CALLBACK_PACKET_RX_PREPARSE:
/*
* inflate the incoming compressed data
* Notice, length may be 0 and pointer NULL
* in the case we are flushing with nothing new coming in
*/
conn->zs_in.next_in = (unsigned char *)eff_buf->token;
conn->zs_in.avail_in = eff_buf->token_len;
conn->zs_in.next_out = conn->buf;
conn->zs_in.avail_out = sizeof(conn->buf);
n = inflate(&conn->zs_in, Z_SYNC_FLUSH);
switch (n) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
/*
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error inflate %d\n", n);
return -1;
}
/* rewrite the buffer pointers and length */
eff_buf->token = (char *)conn->buf;
eff_buf->token_len = sizeof(conn->buf) - conn->zs_in.avail_out;
/*
* if we filled the output buffer, signal that we likely have
* more and need to be called again
*/
if (eff_buf->token_len == sizeof(conn->buf))
return 1;
/* we don't need calling again until new input data comes */
return 0;
case LWS_EXT_CALLBACK_FLUSH_PENDING_TX:
case LWS_EXT_CALLBACK_PACKET_TX_PRESEND:
/*
* deflate the outgoing compressed data
*/
conn->zs_out.next_in = (unsigned char *)eff_buf->token;
conn->zs_out.avail_in = eff_buf->token_len;
conn->zs_out.next_out = conn->buf;
conn->zs_out.avail_out = sizeof(conn->buf);
n = Z_PARTIAL_FLUSH;
if (reason == LWS_EXT_CALLBACK_FLUSH_PENDING_TX)
n = Z_FULL_FLUSH;
n = deflate(&conn->zs_out, n);
if (n == Z_STREAM_ERROR) {
/*
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
fprintf(stderr, "zlib error deflate\n");
return -1;
}
/* rewrite the buffer pointers and length */
eff_buf->token = (char *)conn->buf;
eff_buf->token_len = sizeof(conn->buf) - conn->zs_out.avail_out;
/*
* if we filled the output buffer, signal that we likely have
* more and need to be called again... even in deflate case
* we might sometimes need to spill more than came in
*/
if (eff_buf->token_len == sizeof(conn->buf))
return 1;
/* we don't need calling again until new input data comes */
return 0;
default:
break;
}
return 0;
}
#include <zlib.h>
#define DEFLATE_STREAM_CHUNK 128
#define DEFLATE_STREAM_COMPRESSION_LEVEL 1
struct lws_ext_deflate_stream_conn {
z_stream zs_in;
z_stream zs_out;
unsigned char buf[2000];
};
extern int lws_extension_callback_deflate_stream(
struct libwebsocket_context *context,
struct libwebsocket_extension *ext,
struct libwebsocket *wsi,
enum libwebsocket_extension_callback_reasons reason,
void *user, void *in, size_t len);
#if 0
#ifdef WIN32
static
#else
static inline
#endif
void muxdebug(const char *format, ...)
{
va_list ap;
va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
}
#else
#ifdef WIN32
static
#else
static inline
#endif
void muxdebug(const char *format, ...)
{
}
#endif
#define MAX_XGM_SUBCHANNELS 8192
enum lws_ext_x_google_mux__parser_states {
LWS_EXT_XGM_STATE__MUX_BLOCK_1,
LWS_EXT_XGM_STATE__MUX_BLOCK_2,
LWS_EXT_XGM_STATE__MUX_BLOCK_3,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_1,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN16_2,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_1,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_2,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_3,
LWS_EXT_XGM_STATE__ADDCHANNEL_LEN32_4,
LWS_EXT_XGM_STATE__ADDCHANNEL_HEADERS,
LWS_EXT_XGM_STATE__FLOWCONTROL_1,
LWS_EXT_XGM_STATE__FLOWCONTROL_2,
LWS_EXT_XGM_STATE__FLOWCONTROL_3,
LWS_EXT_XGM_STATE__FLOWCONTROL_4,
LWS_EXT_XGM_STATE__DATA,
};
enum lws_ext_x_goole_mux__mux_opcodes {
LWS_EXT_XGM_OPC__DATA,
LWS_EXT_XGM_OPC__ADDCHANNEL,
LWS_EXT_XGM_OPC__DROPCHANNEL,
LWS_EXT_XGM_OPC__FLOWCONTROL,
LWS_EXT_XGM_OPC__RESERVED_4,
LWS_EXT_XGM_OPC__RESERVED_5,
LWS_EXT_XGM_OPC__RESERVED_6,
LWS_EXT_XGM_OPC__RESERVED_7,
};
/* one of these per context (server or client) */
struct lws_ext_x_google_mux_context {
/*
* these are listing physical connections, not children sharing a
* parent mux physical connection
*/
struct libwebsocket *wsi_muxconns[MAX_CLIENTS];
/*
* when this is < 2, we do not do any mux blocks
* just pure websockets
*/
int active_conns;
};
/* one of these per connection (server or client) */
struct lws_ext_x_google_mux_conn {
enum lws_ext_x_goole_mux__mux_opcodes block_subopcode;
int block_subchannel;
unsigned int length;
enum lws_ext_x_google_mux__parser_states state;
/* child points to the mux wsi using this */
struct libwebsocket *wsi_parent;
int subchannel;
struct libwebsocket *wsi_children[MAX_CLIENTS];
int highest_child_subchannel;
char awaiting_POLLOUT;
int count_children_needing_POLLOUT;
int sticky_mux_used;
int defeat_mux_opcode_wrapping;
int original_ch1_closed;
int ignore_cmd;
};
extern int
lws_extension_callback_x_google_mux(struct libwebsocket_context *context,
struct libwebsocket_extension *ext,
struct libwebsocket *wsi,
enum libwebsocket_extension_callback_reasons reason,
void *user, void *in, size_t len);
#include "private-libwebsockets.h"
#include "extension-deflate-stream.h"
#include "extension-x-google-mux.h"
struct libwebsocket_extension libwebsocket_internal_extensions[] = {
#ifdef LWS_EXT_GOOGLE_MUX
{
"x-google-mux",
lws_extension_callback_x_google_mux,
sizeof (struct lws_ext_x_google_mux_conn)
},
#endif
{
"deflate-stream",
lws_extension_callback_deflate_stream,
sizeof (struct lws_ext_deflate_stream_conn)
},
{ /* terminator */
NULL, NULL, 0
}
};
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
Name: libwebsockets
Version: 0.1
Release: 45.gmaster_f1d2113d%{?dist}
Summary: Websocket Server Library
Group: System
License: GPL
URL: http://warmcat.com
Source0: %{name}-%{version}.tar.gz
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildRequires: openssl-devel
Requires: openssl-devel
%description
Webserver server library
%package devel
Summary: Development files for libwebsockets
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}
Requires: openssl-devel
%description devel
Development files for libwebsockets
%prep
%setup -q
%build
./configure --prefix=/usr --libdir=%{_libdir} --enable-openssl
make
%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
%attr(755,root,root) /usr/bin/libwebsockets-test-server
%attr(755,root,root)
/%{_libdir}/libwebsockets.so.0.0.3
/%{_libdir}/libwebsockets.so.0
/%{_libdir}/libwebsockets.so
/%{_libdir}/libwebsockets.la
%attr(755,root,root) /usr/share/libwebsockets-test-server
%attr(755,root,root) /usr/share/libwebsockets-test-server-extpoll
%attr(755,root,root) /usr/share/libwebsockets-test-client
%attr(755,root,root) /usr/share/libwebsockets-test-ping
%doc
%files devel
%defattr(-,root,root,-)
/usr/include/*
%attr(755,root,root)
/%{_libdir}/libwebsockets.a
%changelog
差异被折叠。
bin_PROGRAMS=libwebsockets-test-server libwebsockets-test-client libwebsockets-test-server-extpoll libwebsockets-test-fraggle
libwebsockets_test_server_SOURCES=test-server.c
libwebsockets_test_server_LDADD=-L../lib -lwebsockets -lz
libwebsockets_test_client_SOURCES=test-client.c
libwebsockets_test_client_LDADD=-L../lib -lwebsockets -lz
libwebsockets_test_server_extpoll_SOURCES=test-server-extpoll.c
libwebsockets_test_server_extpoll_LDADD=-L../lib -lwebsockets -lz
libwebsockets_test_fraggle_SOURCES=test-fraggle.c
libwebsockets_test_fraggle_LDADD=-L../lib -lwebsockets -lz
if MINGW
libwebsockets_test_server_CFLAGS:= -w -I../win32port/win32helpers
libwebsockets_test_client_CFLAGS:= -w -I../win32port/win32helpers
libwebsockets_test_server_extpoll_CFLAGS:= -w -I../win32port/win32helpers
libwebsockets_test_fraggle_CFLAGS:= -w -I../win32port/win32helpers
libwebsockets_test_server_LDADD+= -lm -luser32 -ladvapi32 -lkernel32 -lgcc -lws2_32 -lz
libwebsockets_test_client_LDADD+= -lm -luser32 -ladvapi32 -lkernel32 -lgcc -lws2_32 -lz
libwebsockets_test_server_extpoll_LDADD+= -lm -luser32 -ladvapi32 -lkernel32 -lgcc -lws2_32 -lz
libwebsockets_test_fraggle_LDADD+= -lm -luser32 -ladvapi32 -lkernel32 -lgcc -lws2_32 -lz
else
libwebsockets_test_server_CFLAGS:= -Werror
libwebsockets_test_client_CFLAGS:= -Werror
libwebsockets_test_server_extpoll_CFLAGS:= -Werror
libwebsockets_test_fraggle_CFLAGS:= -Werror
endif
libwebsockets_test_server_CFLAGS+= -Wall -std=gnu99 -pedantic -DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
libwebsockets_test_client_CFLAGS+= -Wall -std=gnu99 -pedantic -DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
libwebsockets_test_server_extpoll_CFLAGS+= -Wall -std=gnu99 -pedantic -DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
libwebsockets_test_fraggle_CFLAGS+= -Wall -std=gnu99 -pedantic -DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
if NOPING
else
bin_PROGRAMS+=libwebsockets-test-ping
libwebsockets_test_ping_SOURCES=test-ping.c
libwebsockets_test_ping_LDADD=-L../lib -lwebsockets
libwebsockets_test_ping_CFLAGS:= -Wall -Werror -std=gnu99 -pedantic -DINSTALL_DATADIR=\"@datadir@\" -DLWS_OPENSSL_CLIENT_CERTS=\"@clientcertdir@\"
endif
#
# cook a random test cert and key
# notice your real cert and key will want to be 0600 permissions
libwebsockets-test-server.pem libwebsockets-test-server.key.pem:
printf "GB\nErewhon\nAll around\nlibwebsockets-test\n\nlocalhost\nnone@invalid.org\n" | \
openssl req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout \
./libwebsockets-test-server.key.pem -out ./libwebsockets-test-server.pem >/dev/null 2>&1 && \
chmod 644 ./libwebsockets-test-server.key.pem \
./libwebsockets-test-server.pem
clean-local:
rm -f ./libwebsockets-test-server.key.pem ./libwebsockets-test-server.pem
install-data-local:libwebsockets-test-server.key.pem libwebsockets-test-server.pem
mkdir -p $(DESTDIR)$(datadir)/libwebsockets-test-server
cp -a test.html favicon.ico libwebsockets-test-server.key.pem libwebsockets-test-server.pem \
$(DESTDIR)$(datadir)/libwebsockets-test-server
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
// Left blank for win32
\ No newline at end of file
// left blank
\ No newline at end of file
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
/* inffast.h -- header to use inffast.c
* Copyright (C) 1995-2003, 2010 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论