Compare commits
	
		
			13 Commits
		
	
	
		
			v11.2.8
			...
			feature/wi
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					a9ffa7da11 | ||
| 
						 | 
					44e5ab7f7d | ||
| 
						 | 
					280d1d7366 | ||
| 
						 | 
					37e899e872 | ||
| 
						 | 
					644d988c29 | ||
| 
						 | 
					b68a4c26f2 | ||
| 
						 | 
					c3bf260330 | ||
| 
						 | 
					88c2e1f6de | ||
| 
						 | 
					1dc9b559e9 | ||
| 
						 | 
					d31ecfc64e | ||
| 
						 | 
					4813a40f2a | ||
| 
						 | 
					ea81470f4a | ||
| 
						 | 
					2a6b1d5f15 | 
							
								
								
									
										46
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					#
 | 
				
			||||||
 | 
					# cmd_websocket_chat.cpp
 | 
				
			||||||
 | 
					# Author: Benjamin Sergeant
 | 
				
			||||||
 | 
					# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cmake_minimum_required(VERSION 3.4.1)
 | 
				
			||||||
 | 
					project(ixwebsocket C CXX)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_CXX_STANDARD 11)
 | 
				
			||||||
 | 
					set (CXX_STANDARD_REQUIRED ON)
 | 
				
			||||||
 | 
					set (CMAKE_CXX_EXTENSIONS OFF)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set( IXWEBSOCKET_SOURCES
 | 
				
			||||||
 | 
					    ixwebsocket/IXSocket.cpp
 | 
				
			||||||
 | 
					    ixwebsocket/IXWebSocket.cpp
 | 
				
			||||||
 | 
					    ixwebsocket/IXWebSocketTransport.cpp
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set( IXWEBSOCKET_HEADERS
 | 
				
			||||||
 | 
					    ixwebsocket/IXSocket.h
 | 
				
			||||||
 | 
					    ixwebsocket/IXWebSocket.h
 | 
				
			||||||
 | 
					    ixwebsocket/IXWebSocketTransport.h
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (USE_TLS)
 | 
				
			||||||
 | 
					    add_definitions(-DIXWEBSOCKET_USE_TLS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (APPLE)
 | 
				
			||||||
 | 
					        list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h)
 | 
				
			||||||
 | 
					        list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp)
 | 
				
			||||||
 | 
					    else()
 | 
				
			||||||
 | 
					        list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h)
 | 
				
			||||||
 | 
					        list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp)
 | 
				
			||||||
 | 
					    endif()
 | 
				
			||||||
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_library( ixwebsocket STATIC
 | 
				
			||||||
 | 
					    ${IXWEBSOCKET_SOURCES}
 | 
				
			||||||
 | 
					    ${IXWEBSOCKET_HEADERS}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set( IXWEBSOCKET_INCLUDE_DIRS
 | 
				
			||||||
 | 
					    .
 | 
				
			||||||
 | 
					    ../../shared/OpenSSL/include)
 | 
				
			||||||
 | 
					target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} )
 | 
				
			||||||
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								README.md
									
									
									
									
									
								
							@@ -36,14 +36,18 @@ webSocket.send("hello world");
 | 
				
			|||||||
// ... finally ...
 | 
					// ... finally ...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Stop the connection
 | 
					// Stop the connection
 | 
				
			||||||
webSocket:stop()
 | 
					webSocket.stop()
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Build
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Implementation details
 | 
					## Implementation details
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### TLS/SSL
 | 
					### TLS/SSL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and OpenSSL is used on Android.
 | 
					Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and macOS, and OpenSSL is used on Android and Linux.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Polling and background thread work
 | 
					### Polling and background thread work
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -91,7 +95,7 @@ Here's a simplistic diagram which explains how the code is structured in term of
 | 
				
			|||||||
+-----------------------+
 | 
					+-----------------------+
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Advanced usage
 | 
					## API
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Sending messages
 | 
					### Sending messages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								examples/chat/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								examples/chat/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					#
 | 
				
			||||||
 | 
					# cmd_websocket_chat.cpp
 | 
				
			||||||
 | 
					# Author: Benjamin Sergeant
 | 
				
			||||||
 | 
					# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cmake_minimum_required (VERSION 3.4.1)
 | 
				
			||||||
 | 
					project (cmd_websocket_chat)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set (CMAKE_CXX_STANDARD 11)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					option(USE_TLS "Add TLS support" ON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_executable(cmd_websocket_chat cmd_websocket_chat.cpp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (APPLE AND USE_TLS)
 | 
				
			||||||
 | 
					    target_link_libraries(cmd_websocket_chat "-framework foundation" "-framework security")
 | 
				
			||||||
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					target_link_libraries(cmd_websocket_chat ixwebsocket)
 | 
				
			||||||
 | 
					install(TARGETS cmd_websocket_chat DESTINATION bin)
 | 
				
			||||||
							
								
								
									
										39
									
								
								examples/chat/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								examples/chat/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					# Building
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. cmake -G .
 | 
				
			||||||
 | 
					2. make
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Disable TLS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					chat$ cmake -DUSE_TLS=OFF .
 | 
				
			||||||
 | 
					-- Configuring done
 | 
				
			||||||
 | 
					-- Generating done
 | 
				
			||||||
 | 
					-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat
 | 
				
			||||||
 | 
					chat$ make
 | 
				
			||||||
 | 
					Scanning dependencies of target ixwebsocket
 | 
				
			||||||
 | 
					[ 16%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o
 | 
				
			||||||
 | 
					[ 33%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o
 | 
				
			||||||
 | 
					[ 50%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o
 | 
				
			||||||
 | 
					[ 66%] Linking CXX static library libixwebsocket.a
 | 
				
			||||||
 | 
					[ 66%] Built target ixwebsocket
 | 
				
			||||||
 | 
					[ 83%] Linking CXX executable cmd_websocket_chat
 | 
				
			||||||
 | 
					[100%] Built target cmd_websocket_chat
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Enable TLS (default)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					chat$ cmake -DUSE_TLS=ON .
 | 
				
			||||||
 | 
					-- Configuring done
 | 
				
			||||||
 | 
					-- Generating done
 | 
				
			||||||
 | 
					-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat
 | 
				
			||||||
 | 
					(venv) chat$ make
 | 
				
			||||||
 | 
					Scanning dependencies of target ixwebsocket
 | 
				
			||||||
 | 
					[ 14%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o
 | 
				
			||||||
 | 
					[ 28%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o
 | 
				
			||||||
 | 
					[ 42%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o
 | 
				
			||||||
 | 
					[ 57%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocketAppleSSL.cpp.o
 | 
				
			||||||
 | 
					[ 71%] Linking CXX static library libixwebsocket.a
 | 
				
			||||||
 | 
					[ 71%] Built target ixwebsocket
 | 
				
			||||||
 | 
					[ 85%] Linking CXX executable cmd_websocket_chat
 | 
				
			||||||
 | 
					[100%] Built target cmd_websocket_chat
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
@@ -4,11 +4,13 @@
 | 
				
			|||||||
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
 | 
					# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 'manual' way of building. You can also use cmake.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
clang++ --std=c++11 --stdlib=libc++ \
 | 
					clang++ --std=c++11 --stdlib=libc++ \
 | 
				
			||||||
    ../ixwebsocket/IXSocket.cpp	\
 | 
					    ../../ixwebsocket/IXSocket.cpp	\
 | 
				
			||||||
    ../ixwebsocket/IXWebSocketTransport.cpp \
 | 
					    ../../ixwebsocket/IXWebSocketTransport.cpp \
 | 
				
			||||||
    ../ixwebsocket/IXSocketAppleSSL.cpp	\
 | 
					    ../../ixwebsocket/IXSocketAppleSSL.cpp	\
 | 
				
			||||||
    ../ixwebsocket/IXWebSocket.cpp \
 | 
					    ../../ixwebsocket/IXWebSocket.cpp \
 | 
				
			||||||
    cmd_websocket_chat.cpp \
 | 
					    cmd_websocket_chat.cpp \
 | 
				
			||||||
    -o cmd_websocket_chat \
 | 
					    -o cmd_websocket_chat \
 | 
				
			||||||
    -framework Security \
 | 
					    -framework Security \
 | 
				
			||||||
@@ -12,7 +12,8 @@
 | 
				
			|||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <sstream>
 | 
					#include <sstream>
 | 
				
			||||||
#include <queue>
 | 
					#include <queue>
 | 
				
			||||||
#include "../ixwebsocket/IXWebSocket.h"
 | 
					#include <ixwebsocket/IXWebSocket.h>
 | 
				
			||||||
 | 
					#include <ixwebsocket/IXSocket.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "nlohmann/json.hpp"
 | 
					#include "nlohmann/json.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -160,11 +161,9 @@ namespace
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    void interactiveMain()
 | 
					    void interactiveMain()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        std::string user(getenv("USER"));
 | 
					        std::string user("foo");
 | 
				
			||||||
 | 
					 | 
				
			||||||
        WebSocketChat webSocketChat(user);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
 | 
					        std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
 | 
				
			||||||
 | 
					        WebSocketChat webSocketChat(user);
 | 
				
			||||||
        webSocketChat.start();
 | 
					        webSocketChat.start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        while (true)
 | 
					        while (true)
 | 
				
			||||||
@@ -188,6 +187,9 @@ namespace
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int main()
 | 
					int main()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    std::cout << "main starting" << std::endl;
 | 
				
			||||||
 | 
					    Socket::init();
 | 
				
			||||||
 | 
					    std::cout << "socket initialized" << std::endl;
 | 
				
			||||||
    interactiveMain();
 | 
					    interactiveMain();
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -6,22 +6,34 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "IXSocket.h"
 | 
					#include "IXSocket.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					# include <basetsd.h>
 | 
				
			||||||
 | 
					# include <WinSock2.h>
 | 
				
			||||||
 | 
					# include <ws2def.h>
 | 
				
			||||||
 | 
					# include <WS2tcpip.h>
 | 
				
			||||||
 | 
					# include <io.h>
 | 
				
			||||||
 | 
					#pragma comment(lib, "ws2_32")
 | 
				
			||||||
 | 
					# include <io.h>
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					# include <unistd.h>
 | 
				
			||||||
 | 
					# include <errno.h>
 | 
				
			||||||
# include <netdb.h>
 | 
					# include <netdb.h>
 | 
				
			||||||
# include <netinet/tcp.h>
 | 
					# include <netinet/tcp.h>
 | 
				
			||||||
 | 
					# include <sys/socket.h>
 | 
				
			||||||
 | 
					# include <sys/time.h>
 | 
				
			||||||
 | 
					# include <sys/select.h>
 | 
				
			||||||
 | 
					# include <sys/stat.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <sys/socket.h>
 | 
					 | 
				
			||||||
#include <sys/time.h>
 | 
					 | 
				
			||||||
#include <sys/types.h>
 | 
					 | 
				
			||||||
#include <unistd.h>
 | 
					 | 
				
			||||||
#include <stdint.h>
 | 
					 | 
				
			||||||
#include <sys/select.h>
 | 
					 | 
				
			||||||
#include <errno.h>
 | 
					 | 
				
			||||||
#include <sys/types.h>
 | 
					 | 
				
			||||||
#include <sys/stat.h>
 | 
					 | 
				
			||||||
#include <fcntl.h>
 | 
					 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					#include <fcntl.h>
 | 
				
			||||||
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <algorithm>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// Linux/Android has a special type of virtual files. select(2) will react
 | 
					// Linux/Android has a special type of virtual files. select(2) will react
 | 
				
			||||||
@@ -35,7 +47,7 @@
 | 
				
			|||||||
// cf Android/Kernel table here 
 | 
					// cf Android/Kernel table here 
 | 
				
			||||||
// https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel
 | 
					// https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
#ifndef __APPLE__
 | 
					#ifdef __linux__
 | 
				
			||||||
# include <sys/eventfd.h>
 | 
					# include <sys/eventfd.h>
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -51,7 +63,7 @@ namespace ix
 | 
				
			|||||||
        _sockfd(-1),
 | 
					        _sockfd(-1),
 | 
				
			||||||
        _eventfd(-1)
 | 
					        _eventfd(-1)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
#ifndef __APPLE__
 | 
					#ifdef __linux__
 | 
				
			||||||
        _eventfd = eventfd(0, 0);
 | 
					        _eventfd = eventfd(0, 0);
 | 
				
			||||||
        assert(_eventfd != -1 && "Panic - eventfd not functioning on this platform");
 | 
					        assert(_eventfd != -1 && "Panic - eventfd not functioning on this platform");
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -61,12 +73,12 @@ namespace ix
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        close();
 | 
					        close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifndef __APPLE__
 | 
					#ifdef __linux__
 | 
				
			||||||
        ::close(_eventfd);
 | 
					        ::close(_eventfd);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool connectToAddress(const struct addrinfo *address, 
 | 
					    bool Socket::connectToAddress(const struct addrinfo *address, 
 | 
				
			||||||
                                  int& sockfd,
 | 
					                                  int& sockfd,
 | 
				
			||||||
                                  std::string& errMsg)
 | 
					                                  std::string& errMsg)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -84,7 +96,7 @@ namespace ix
 | 
				
			|||||||
        int maxRetries = 3;
 | 
					        int maxRetries = 3;
 | 
				
			||||||
        for (int i = 0; i < maxRetries; ++i)
 | 
					        for (int i = 0; i < maxRetries; ++i)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (connect(fd, address->ai_addr, address->ai_addrlen) != -1)
 | 
					            if (::connect(fd, address->ai_addr, address->ai_addrlen) != -1)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                sockfd = fd;
 | 
					                sockfd = fd;
 | 
				
			||||||
                return true;
 | 
					                return true;
 | 
				
			||||||
@@ -94,7 +106,7 @@ namespace ix
 | 
				
			|||||||
            if (errno != EINTR) break;
 | 
					            if (errno != EINTR) break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ::close(fd);
 | 
					        closeSocket(fd);
 | 
				
			||||||
        sockfd = -1;
 | 
					        sockfd = -1;
 | 
				
			||||||
        errMsg = strerror(errno);
 | 
					        errMsg = strerror(errno);
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
@@ -142,7 +154,13 @@ namespace ix
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        int flag = 1;
 | 
					        int flag = 1;
 | 
				
			||||||
        setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm
 | 
					        setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					        unsigned long nonblocking = 1;
 | 
				
			||||||
 | 
					        ioctlsocket(_sockfd, FIONBIO, &nonblocking);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
        fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking
 | 
					        fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef SO_NOSIGPIPE
 | 
					#ifdef SO_NOSIGPIPE
 | 
				
			||||||
        int value = 1;
 | 
					        int value = 1;
 | 
				
			||||||
@@ -163,12 +181,12 @@ namespace ix
 | 
				
			|||||||
        FD_ZERO(&rfds);
 | 
					        FD_ZERO(&rfds);
 | 
				
			||||||
        FD_SET(_sockfd, &rfds);
 | 
					        FD_SET(_sockfd, &rfds);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifndef __APPLE__
 | 
					#ifdef __linux__
 | 
				
			||||||
        FD_SET(_eventfd, &rfds);
 | 
					        FD_SET(_eventfd, &rfds);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        int sockfd = _sockfd;
 | 
					        int sockfd = _sockfd;
 | 
				
			||||||
        int nfds = std::max(sockfd, _eventfd);
 | 
					        int nfds = (std::max)(sockfd, _eventfd);
 | 
				
			||||||
        select(nfds + 1, &rfds, nullptr, nullptr, nullptr);
 | 
					        select(nfds + 1, &rfds, nullptr, nullptr, nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onPollCallback();
 | 
					        onPollCallback();
 | 
				
			||||||
@@ -191,7 +209,7 @@ namespace ix
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
#ifdef __APPLE__
 | 
					#ifdef __APPLE__
 | 
				
			||||||
        wakeUpFromPollApple();
 | 
					        wakeUpFromPollApple();
 | 
				
			||||||
#else
 | 
					#elif defined(__linux__)
 | 
				
			||||||
        wakeUpFromPollLinux();
 | 
					        wakeUpFromPollLinux();
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -202,7 +220,7 @@ namespace ix
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        std::lock_guard<std::mutex> lock(_socketMutex);
 | 
					        std::lock_guard<std::mutex> lock(_socketMutex);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifndef __APPLE__
 | 
					#ifdef __linux__
 | 
				
			||||||
        if (_eventfd == -1)
 | 
					        if (_eventfd == -1)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return false; // impossible to use this socket if eventfd is broken
 | 
					            return false; // impossible to use this socket if eventfd is broken
 | 
				
			||||||
@@ -219,7 +237,7 @@ namespace ix
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if (_sockfd == -1) return;
 | 
					        if (_sockfd == -1) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ::close(_sockfd);
 | 
					        closeSocket(_sockfd);
 | 
				
			||||||
        _sockfd = -1;
 | 
					        _sockfd = -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -245,7 +263,36 @@ namespace ix
 | 
				
			|||||||
        flags = MSG_NOSIGNAL;
 | 
					        flags = MSG_NOSIGNAL;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return (int) ::recv(_sockfd, buffer, length, flags);
 | 
					        return (int) ::recv(_sockfd, (char*) buffer, length, flags);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int Socket::getErrno() const
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					        return WSAGetLastError();
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					        return errno;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void Socket::closeSocket(int fd)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
						closesocket(fd);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					        ::close(fd);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // FIXME: need finalize
 | 
				
			||||||
 | 
					    bool Socket::init()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					        INT rc;
 | 
				
			||||||
 | 
					        WSADATA wsaData;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
 | 
				
			||||||
 | 
					        return rc != 0;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,6 +11,8 @@
 | 
				
			|||||||
#include <mutex>
 | 
					#include <mutex>
 | 
				
			||||||
#include <atomic>
 | 
					#include <atomic>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct addrinfo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace ix 
 | 
					namespace ix 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    class Socket {
 | 
					    class Socket {
 | 
				
			||||||
@@ -20,7 +22,9 @@ namespace ix
 | 
				
			|||||||
        Socket();
 | 
					        Socket();
 | 
				
			||||||
        virtual ~Socket();
 | 
					        virtual ~Socket();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        static int hostname_connect(const std::string& hostname,
 | 
						static bool init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int hostname_connect(const std::string& hostname,
 | 
				
			||||||
                             int port,
 | 
					                             int port,
 | 
				
			||||||
                             std::string& errMsg);
 | 
					                             std::string& errMsg);
 | 
				
			||||||
        void configure();
 | 
					        void configure();
 | 
				
			||||||
@@ -38,13 +42,22 @@ namespace ix
 | 
				
			|||||||
        virtual int send(const std::string& buffer);
 | 
					        virtual int send(const std::string& buffer);
 | 
				
			||||||
        virtual int recv(void* buffer, size_t length);
 | 
					        virtual int recv(void* buffer, size_t length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int getErrno() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected:
 | 
					    protected:
 | 
				
			||||||
        void wakeUpFromPollApple();
 | 
					        void wakeUpFromPollApple();
 | 
				
			||||||
        void wakeUpFromPollLinux();
 | 
					        void wakeUpFromPollLinux();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void closeSocket(int fd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::atomic<int> _sockfd;
 | 
					        std::atomic<int> _sockfd;
 | 
				
			||||||
        int _eventfd;
 | 
					        int _eventfd;
 | 
				
			||||||
        std::mutex _socketMutex;
 | 
					        std::mutex _socketMutex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private:
 | 
				
			||||||
 | 
					        bool connectToAddress(const struct addrinfo *address, 
 | 
				
			||||||
 | 
					                              int& sockfd,
 | 
				
			||||||
 | 
					                              std::string& errMsg);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <openssl/x509v3.h>
 | 
					#include <openssl/x509v3.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <fnmatch.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
#define socketerrno errno
 | 
					#define socketerrno errno
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -158,51 +159,10 @@ namespace ix
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Check whether a hostname matches a pattern
 | 
					     * Check whether a hostname matches a pattern
 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * The pattern MUST contain at most a single, leading asterisk. This means that
 | 
					 | 
				
			||||||
     * this function cannot serve as a generic validation function, as that would
 | 
					 | 
				
			||||||
     * allow for partial wildcards, too. Also, this does not check whether the
 | 
					 | 
				
			||||||
     * wildcard covers multiple levels of labels. For RTM, this suffices, as we
 | 
					 | 
				
			||||||
     * are only interested in the main domain name.
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param[in] hostname The hostname of the server
 | 
					 | 
				
			||||||
     * @param[in] pattern The hostname pattern from a SSL certificate
 | 
					 | 
				
			||||||
     * @return TRUE if the pattern matches, FALSE otherwise
 | 
					 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    bool SocketOpenSSL::checkHost(const std::string& host, const char *pattern)
 | 
					    bool SocketOpenSSL::checkHost(const std::string& host, const char *pattern)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        const char* hostname = host.c_str();
 | 
					        return fnmatch(pattern, host.c_str(), 0) != FNM_NOMATCH;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        while (*pattern && *hostname)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            if (*pattern == '*')
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                while (*hostname != '.' && *hostname) hostname++;
 | 
					 | 
				
			||||||
                if (*(++pattern) != '.')
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    return false;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                char p = *pattern;
 | 
					 | 
				
			||||||
                char h = *hostname;
 | 
					 | 
				
			||||||
                if ((p & ~32) >= 'A' && (p & ~32) <= 'Z')
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    p &= ~32;
 | 
					 | 
				
			||||||
                    h &= ~32;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (*pattern != *hostname)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    return false;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            pattern++;
 | 
					 | 
				
			||||||
            hostname++;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        bool success = !(*hostname || *pattern);
 | 
					 | 
				
			||||||
        return success;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool SocketOpenSSL::openSSLCheckServerCert(SSL *ssl,
 | 
					    bool SocketOpenSSL::openSSLCheckServerCert(SSL *ssl,
 | 
				
			||||||
@@ -354,6 +314,15 @@ namespace ix
 | 
				
			|||||||
            // SNI support
 | 
					            // SNI support
 | 
				
			||||||
            SSL_set_tlsext_host_name(_ssl_connection, host.c_str());
 | 
					            SSL_set_tlsext_host_name(_ssl_connection, host.c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if OPENSSL_VERSION_NUMBER >= 0x10002000L
 | 
				
			||||||
 | 
					            // Support for server name verification
 | 
				
			||||||
 | 
					            // (The docs say that this should work from 1.0.2, and is the default from
 | 
				
			||||||
 | 
					            // 1.1.0, but it does not. To be on the safe side, the manual test below is
 | 
				
			||||||
 | 
					            // enabled for all versions prior to 1.1.0.)
 | 
				
			||||||
 | 
					            X509_VERIFY_PARAM *param = SSL_get0_param(_ssl_connection);
 | 
				
			||||||
 | 
					            X509_VERIFY_PARAM_set1_host(param, host.c_str(), 0);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            handshakeSuccessful = openSSLHandshake(host, errMsg);
 | 
					            handshakeSuccessful = openSSLHandshake(host, errMsg);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -90,7 +90,5 @@ namespace ix
 | 
				
			|||||||
        std::atomic<bool> _automaticReconnection;
 | 
					        std::atomic<bool> _automaticReconnection;
 | 
				
			||||||
        std::thread _thread;
 | 
					        std::thread _thread;
 | 
				
			||||||
        std::mutex _writeMutex;
 | 
					        std::mutex _writeMutex;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        static int kHeartBeatPeriod;
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,15 +11,17 @@
 | 
				
			|||||||
#include "IXWebSocketTransport.h"
 | 
					#include "IXWebSocketTransport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "IXSocket.h"
 | 
					#include "IXSocket.h"
 | 
				
			||||||
 | 
					#ifdef IXWEBSOCKET_USE_TLS
 | 
				
			||||||
# ifdef __APPLE__
 | 
					# ifdef __APPLE__
 | 
				
			||||||
#  include "IXSocketAppleSSL.h"
 | 
					#  include "IXSocketAppleSSL.h"
 | 
				
			||||||
# else
 | 
					# else
 | 
				
			||||||
#  include "IXSocketOpenSSL.h"
 | 
					#  include "IXSocketOpenSSL.h"
 | 
				
			||||||
# endif
 | 
					# endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <unistd.h>
 | 
					// #include <unistd.h>
 | 
				
			||||||
#include <errno.h>
 | 
					 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <cstdlib>
 | 
					#include <cstdlib>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
@@ -140,10 +142,14 @@ namespace ix {
 | 
				
			|||||||
        if (protocol == "wss")
 | 
					        if (protocol == "wss")
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _socket.reset();
 | 
					            _socket.reset();
 | 
				
			||||||
 | 
					#ifdef IXWEBSOCKET_USE_TLS
 | 
				
			||||||
# ifdef __APPLE__
 | 
					# ifdef __APPLE__
 | 
				
			||||||
             _socket = std::make_shared<SocketAppleSSL>();
 | 
					             _socket = std::make_shared<SocketAppleSSL>();
 | 
				
			||||||
# else
 | 
					# else
 | 
				
			||||||
             _socket = std::make_shared<SocketOpenSSL>();
 | 
					             _socket = std::make_shared<SocketOpenSSL>();
 | 
				
			||||||
 | 
					# endif
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					            return WebSocketInitResult(false, 0, "TLS is not supported.");
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
@@ -267,12 +273,12 @@ namespace ix {
 | 
				
			|||||||
                {
 | 
					                {
 | 
				
			||||||
                    int N = (int) _rxbuf.size();
 | 
					                    int N = (int) _rxbuf.size();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    ssize_t ret;
 | 
					                    int ret;
 | 
				
			||||||
                    _rxbuf.resize(N + 1500);
 | 
					                    _rxbuf.resize(N + 1500);
 | 
				
			||||||
                    ret = _socket->recv((char*)&_rxbuf[0] + N, 1500);
 | 
					                    ret = _socket->recv((char*)&_rxbuf[0] + N, 1500);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (ret < 0 && (errno == EWOULDBLOCK || 
 | 
					                    if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || 
 | 
				
			||||||
                                    errno == EAGAIN)) {
 | 
					                                    _socket->getErrno() == EAGAIN)) {
 | 
				
			||||||
                        _rxbuf.resize(N);
 | 
					                        _rxbuf.resize(N);
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
@@ -569,8 +575,8 @@ namespace ix {
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            int ret = _socket->send((char*)&_txbuf[0], _txbuf.size());
 | 
					            int ret = _socket->send((char*)&_txbuf[0], _txbuf.size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (ret < 0 && (errno == EWOULDBLOCK || 
 | 
					            if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || 
 | 
				
			||||||
                            errno == EAGAIN))
 | 
					                            _socket->getErrno() == EAGAIN))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user