Skip to content

Commit

Permalink
-s PROXY_POSIX_SOCKETS option: Allows POSIX sockets support via proxy…
Browse files Browse the repository at this point in the history
…ing the calls out to a sockets proxy bridge via WebSockets
  • Loading branch information
juj committed Feb 5, 2019
1 parent 053198d commit 52ce4d5
Show file tree
Hide file tree
Showing 31 changed files with 5,039 additions and 739 deletions.
24 changes: 20 additions & 4 deletions embuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
build libc
libc-mt
libc-extras
libc-sockets
struct_info
emmalloc
emmalloc_debug
Expand All @@ -47,6 +48,7 @@
dlmalloc_threadsafe_noerrno
dlmalloc_threadsafe_debug_noerrno
pthreads
posix_proxy
libc++
libc++_noexcept
libc++abi
Expand Down Expand Up @@ -110,11 +112,11 @@

SYSTEM_TASKS = [
'al', 'compiler-rt', 'gl', 'gl-mt', 'libc', 'libc-mt', 'libc-extras',
'emmalloc', 'emmalloc_debug', 'dlmalloc', 'dlmalloc_threadsafe', 'pthreads',
'dlmalloc_debug', 'dlmalloc_threadsafe_debug', 'libc++', 'libc++_noexcept',
'libc-sockets', 'emmalloc', 'emmalloc_debug', 'dlmalloc',
'dlmalloc_threadsafe', 'pthreads', 'posix_proxy', 'dlmalloc_debug',
'dlmalloc_threadsafe_debug', 'libc++', 'libc++_noexcept',
'dlmalloc_debug_noerrno', 'dlmalloc_threadsafe_debug_noerrno',
'dlmalloc_noerrno', 'dlmalloc_threadsafe_noerrno',
'libc++abi', 'html5'
'dlmalloc_noerrno', 'dlmalloc_threadsafe_noerrno', 'libc++abi', 'html5'
]
USER_TASKS = [
'binaryen', 'bullet', 'freetype', 'icu', 'libpng', 'ogg', 'sdl2',
Expand Down Expand Up @@ -303,6 +305,20 @@ def main():
build_port('cocos2d', None, ['-s', 'USE_COCOS2D=3', '-s', 'USE_ZLIB=1', '-s', 'USE_LIBPNG=1', '-s', 'ERROR_ON_UNDEFINED_SYMBOLS=0'])
elif what == 'regal':
build_port('regal', 'libregal.bc', ['-s', 'USE_REGAL=1'])
elif what == 'libc-sockets':
build('''
#include <sys/socket.h>
int main() {
return socket(0,0,0);
}
''', ['libc-sockets.bc'])
elif what == 'posix_proxy':
build('''
#include <sys/socket.h>
int main() {
return socket(0,0,0);
}
''', ['libposix-sockets-proxy.bc'], ['-s', 'PROXY_POSIX_SOCKETS=1', '-s', 'USE_PTHREADS=1', '-s', 'PROXY_TO_PTHREAD=1'])
else:
logger.error('unfamiliar build target: ' + what)
sys.exit(1)
Expand Down
1 change: 1 addition & 0 deletions site/source/docs/porting/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The topics in this section cover the main integration points that you need to co
Audio
Debugging
pthreads
networking
simd
asyncify
emterpreter
Expand Down
38 changes: 38 additions & 0 deletions site/source/docs/porting/networking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.. Networking:
==============================
Networking
==============================

Emscripten compiled applications have a number of ways to connect with online servers. Check the subtopics here to learn about the different strategies that are available.

If you are familiar with networking concepts provided by different web APIs, such as XmlHttpRequest, Fetch, WebSockets and WebRTC, you can quickly get started by leveraging what you already know: by calling out from C/C++ code to JavaScript (see the "Connecting C++ and JavaScript" section), you can establish networked connections by writing regular JavaScript. For C/C++ developers, Emscripten provides a few approaches, described here.

Emscripten WebSockets API
=========================

WebSockets API provides connection-oriented message-framed bidirectional asynchronous networking communication to the browser. It is the closest to TCP on the web that web sites can access, direct access to TCP sockets is not possible from web browsers.

Emscripten provides a passthrough API for accessing the WebSockets API from C/C++ code. This is useful for developers who would prefer not to write any JavaScript code, or deal with the C/C++ and JavaScript language interop. See the system include file <emscripten/websocket.h> for details. One benefit that the Emscripten WebSockets API provides over manual WebSockets access in JavaScript is the ability to share access to a WebSocket handle across multiple threads, something that can be time consuming to develop from scratch.

Emulated POSIX TCP Sockets over WebSockets
==========================================

If you have existing TCP networking code written in C/C++ that utilizes the Posix Sockets API, by default Emscripten attempts to emulate such connections to take place over the WebSocket protocol instead. For this to work, you will need to use something like WebSockify on the server side to enable the TCP server stack to receive incoming WebSocket connections. This emulation is not very complete at the moment, it is likely that you will run into problems out of the box and need to adapt the code to work within the limitations that this emulation provides.

Full POSIX Sockets over WebSocket Proxy Server
==============================================

Emscripten provides a native POSIX Sockets proxy server program, located in directory tools/websocket_to_posix_proxy/, that allows full POSIX Sockets API access from a web browser. This support works by proxying all POSIX Sockets API calls from the browser to the Emscripten POSIX Sockets proxy server (via transparent use of WebSockets API), and the proxy server then performs the native TCP/UDP calls on behalf of the page. This allows a web browser page to run full TCP & UDP connections, act as a server to accept incoming connections, and perform host name lookups and reverse lookups. Because all API calls are individually proxied, this support can be slow. This support is mostly useful for developing testing infrastructure and debugging.

For an example of how the POSIX Sockets proxy server works in an Emscripten client program, see the file tests/websocket/tcp_echo_client.cpp.

XmlHttpRequests and Fetch API
=============================

For HTTP transfers, one can use the browser built-in XmlHttpRequest (XHR) API and the newer Fetch API. These can be accessed directly from JavaScript. Emscripten also provides passthrough APIs to perform HTTP requests. For more information, see the emscripten_async_wget*() C API and the Emscripten Fetch API.

WebRTC and UDP
==============

Direct UDP communication is not available in browsers, but as a close alternative, the WebRTC specification provides a mechanism to perform UDP-like communication with WebRTC Data Channels. Currently Emscripten does not provide a C/C++ API for interacting with WebRTC.
3 changes: 3 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,7 @@ LibraryManager.library = {
// arpa/inet.h
// ==========================================================================

#if PROXY_POSIX_SOCKETS == 0
// old ipv4 only functions
inet_addr__deps: ['_inet_pton4_raw'],
inet_addr: function(ptr) {
Expand Down Expand Up @@ -4201,6 +4202,8 @@ LibraryManager.library = {
0x0b00000a, 0x0c00000a, 0x0d00000a, 0x0e00000a] /* 0x0100000a is reserved */
},

#endif // PROXY_POSIX_SOCKETS == 0

// pwd.h

getpwnam: function() { throw 'getpwnam: TODO' },
Expand Down
2 changes: 2 additions & 0 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ var SyscallsLibrary = {
__syscall97: function(which, varargs) { // setpriority
return -ERRNO_CODES.EPERM;
},
#if PROXY_POSIX_SOCKETS == 0
__syscall102__deps: ['$SOCKFS', '$DNS', '_read_sockaddr', '_write_sockaddr'],
__syscall102: function(which, varargs) { // socketcall
var call = SYSCALLS.get(), socketvararg = SYSCALLS.get();
Expand Down Expand Up @@ -654,6 +655,7 @@ var SyscallsLibrary = {
default: abort('unsupported socketcall syscall ' + call);
}
},
#endif // ~PROXY_POSIX_SOCKETS==0
__syscall104: function(which, varargs) { // setitimer
return -ERRNO_CODES.ENOSYS; // unsupported feature
},
Expand Down
1 change: 1 addition & 0 deletions src/library_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
HEAPU32[urlLength>>2] = lengthBytesUTF8(socket.url);
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand Down
3 changes: 3 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ var SOCKET_WEBRTC = 0;
// where addr and port are derived from the socket connect/bind/accept calls.
var WEBSOCKET_URL = 'ws://';

// If 1, the POSIX sockets API uses a proxy bridge to proxy sockets calls
var PROXY_POSIX_SOCKETS = 0;

// A string containing a comma separated list of WebSocket subprotocols
// as would be present in the Sec-WebSocket-Protocol header.
var WEBSOCKET_SUBPROTOCOL = 'binary';
Expand Down
13 changes: 13 additions & 0 deletions system/include/emscripten/posix_socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "websocket.h"

#ifdef __cplusplus
extern "C" {
#endif

extern EMSCRIPTEN_RESULT emscripten_init_websocket_to_posix_socket_bridge(const char *bridgeUrl);

#ifdef __cplusplus
}
#endif
23 changes: 23 additions & 0 deletions system/lib/libc-sockets.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
U __errno_location
U __syscall102
U __syscall221
U __syscall_ret
-------- T accept
-------- T bind
-------- T connect
U free
-------- T freeaddrinfo
-------- T getpeername
-------- T getsockname
-------- T getsockopt
-------- T listen
-------- T recv
-------- T recvfrom
-------- T recvmsg
-------- T send
-------- T sendmsg
-------- T sendto
-------- T setsockopt
-------- T shutdown
-------- T socket
-------- T socketpair
Loading

0 comments on commit 52ce4d5

Please sign in to comment.