mirror of
https://github.com/payden/libwsclient
synced 2024-10-27 17:54:01 +00:00
Fix handshake, add support for SSL (wss)
This commit is contained in:
parent
550ce57eab
commit
5b6c5e24fe
@ -1,6 +1,6 @@
|
||||
lib_LTLIBRARIES=libwsclient.la
|
||||
libwsclient_la_SOURCES = wsclient.c base64.c sha1.c
|
||||
library_includedir=$(includedir)/wsclient
|
||||
library_include_HEADERS = wsclient.h
|
||||
library_include_HEADERS = wsclient.h config.h
|
||||
AM_LDFLAGS = -lpthread
|
||||
ACLOCAL_AMFLAGS = -I m4
|
||||
|
12
configure.ac
12
configure.ac
@ -10,22 +10,30 @@ AC_CONFIG_HEADERS([config.h])
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
# Checks for programs.
|
||||
AC_PROG_CC
|
||||
AC_PROG_CXX
|
||||
|
||||
|
||||
|
||||
# Checks for libraries.
|
||||
|
||||
# Checks for header files.
|
||||
AC_CHECK_HEADERS([netdb.h stdlib.h string.h sys/socket.h unistd.h])
|
||||
AC_CHECK_HEADERS([netdb.h stdlib.h string.h sys/socket.h unistd.h netinet/in.h stddef.h sys/time.h])
|
||||
|
||||
AC_C_INLINE
|
||||
# Checks for typedefs, structures, and compiler characteristics.
|
||||
AC_TYPE_SIZE_T
|
||||
AC_TYPE_SSIZE_T
|
||||
|
||||
# Checks for library functions.
|
||||
AC_FUNC_MALLOC
|
||||
AC_FUNC_REALLOC
|
||||
AC_CHECK_FUNCS([memset socket strstr strchr])
|
||||
AC_CHECK_FUNCS([memset socket strstr strchr gettimeofday])
|
||||
AC_CHECK_LIB(pthread, pthread_create, [], [
|
||||
echo "This library requires pthread"
|
||||
exit -1
|
||||
])
|
||||
AC_CHECK_LIB(ssl, SSL_CTX_new, [], [
|
||||
echo "OpenSSL not found, building without SSL support"
|
||||
])
|
||||
AC_CONFIG_FILES([Makefile])
|
||||
AC_OUTPUT
|
||||
|
109
wsclient.c
109
wsclient.c
@ -13,8 +13,10 @@
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wsclient.h"
|
||||
#include "config.h"
|
||||
#include "sha1.h"
|
||||
|
||||
|
||||
void libwsclient_run(wsclient *c) {
|
||||
if(c->flags & CLIENT_CONNECTING) {
|
||||
pthread_join(c->handshake_thread, NULL);
|
||||
@ -37,7 +39,7 @@ void *libwsclient_run_thread(void *ptr) {
|
||||
int n, i;
|
||||
do {
|
||||
memset(buf, 0, 1024);
|
||||
n = recv(c->sockfd, buf, 1023, 0);
|
||||
n = _libwsclient_read(c, buf, 1023);
|
||||
for(i = 0; i < n; i++)
|
||||
libwsclient_in_data(c, buf[i]);
|
||||
|
||||
@ -63,7 +65,9 @@ void *libwsclient_run_thread(void *ptr) {
|
||||
}
|
||||
|
||||
void libwsclient_finish(wsclient *client) {
|
||||
pthread_join(client->run_thread, NULL);
|
||||
if(client->run_thread) {
|
||||
pthread_join(client->run_thread, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void libwsclient_onclose(wsclient *client, int (*cb)(wsclient *c)) {
|
||||
@ -103,7 +107,7 @@ void libwsclient_close(wsclient *client) {
|
||||
data[1] = 0x80;
|
||||
pthread_mutex_lock(&client->send_lock);
|
||||
do {
|
||||
n = send(client->sockfd, data, 6, 0);
|
||||
n = _libwsclient_write(client, data, 6);
|
||||
i += n;
|
||||
} while(i < 6 && n > 0);
|
||||
pthread_mutex_unlock(&client->send_lock);
|
||||
@ -145,7 +149,7 @@ void libwsclient_handle_control_frame(wsclient *c, wsclient_frame *ctl_frame) {
|
||||
i = 0;
|
||||
pthread_mutex_lock(&c->send_lock);
|
||||
while(i < ctl_frame->payload_offset + ctl_frame->payload_len && n >= 0) {
|
||||
n = send(c->sockfd, ctl_frame->rawdata + i, ctl_frame->payload_offset + ctl_frame->payload_len - i, 0);
|
||||
n = _libwsclient_write(c, ctl_frame->rawdata + i, ctl_frame->payload_offset + ctl_frame->payload_len - i);
|
||||
i += n;
|
||||
}
|
||||
pthread_mutex_unlock(&c->send_lock);
|
||||
@ -519,36 +523,37 @@ void *libwsclient_handshake_thread(void *ptr) {
|
||||
fprintf(stderr, "Invalid scheme for URI: %s\n", scheme);
|
||||
exit(WS_EXIT_BAD_SCHEME);
|
||||
}
|
||||
if(strcmp(scheme, "ws") == 0) {
|
||||
strncpy(port, "80", 9);
|
||||
} else {
|
||||
strncpy(port, "443", 9);
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->flags |= CLIENT_IS_SSL;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
}
|
||||
for(i=p-URI_copy+3,z=0;*(URI_copy+i) != '/' && *(URI_copy+i) != ':' && *(URI_copy+i) != '\0';i++,z++) {
|
||||
host[z] = *(URI_copy+i);
|
||||
}
|
||||
host[z] = '\0';
|
||||
if(*(URI_copy+i) == ':') {
|
||||
i++;
|
||||
p = strchr(URI_copy+i, '/');
|
||||
if(!p)
|
||||
p = strchr(URI_copy+i, '\0');
|
||||
strncpy(port, URI_copy+i, (p - (URI_copy+i)));
|
||||
port[p-(URI_copy+i)] = '\0';
|
||||
i += p-(URI_copy+i);
|
||||
}
|
||||
if(*(URI_copy+i) == '\0') {
|
||||
//end of URI request path will be /
|
||||
strncpy(path, "/", 1);
|
||||
} else {
|
||||
if(*(URI_copy+i) != ':') {
|
||||
if(strcmp(scheme, "ws") == 0) {
|
||||
strncpy(port, "80", 9);
|
||||
} else {
|
||||
strncpy(port, "443", 9);
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->flags |= CLIENT_IS_SSL;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
}
|
||||
} else {
|
||||
i++;
|
||||
p = strchr(URI_copy+i, '/');
|
||||
if(!p)
|
||||
p = strchr(URI_copy+i, '\0');
|
||||
strncpy(port, URI_copy+i, (p - (URI_copy+i)));
|
||||
port[p-(URI_copy+i)] = '\0';
|
||||
i += p-(URI_copy+i);
|
||||
}
|
||||
strncpy(path, URI_copy+i, 254);
|
||||
}
|
||||
strncpy(path, URI_copy+i, 254);
|
||||
free(URI_copy);
|
||||
sockfd = libwsclient_open_connection(host, port);
|
||||
|
||||
|
||||
if(sockfd < 0) {
|
||||
if(client->onerror) {
|
||||
err = libwsclient_new_error(sockfd);
|
||||
@ -558,6 +563,21 @@ void *libwsclient_handshake_thread(void *ptr) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBSSL
|
||||
if(client->flags & CLIENT_IS_SSL) {
|
||||
if((libwsclient_flags & WS_FLAGS_SSL_INIT) == 0) {
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
libwsclient_flags |= WS_FLAGS_SSL_INIT;
|
||||
}
|
||||
client->ssl_ctx = SSL_CTX_new(SSLv23_method());
|
||||
client->ssl = SSL_new(client->ssl_ctx);
|
||||
SSL_set_fd(client->ssl, sockfd);
|
||||
SSL_connect(client->ssl);
|
||||
}
|
||||
#endif
|
||||
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->sockfd = sockfd;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
@ -576,13 +596,13 @@ void *libwsclient_handshake_thread(void *ptr) {
|
||||
snprintf(request_host, 255, "%s", host);
|
||||
}
|
||||
snprintf(request_headers, 1024, "GET %s HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nHost: %s\r\nSec-WebSocket-Key: %s\r\nSec-WebSocket-Version: 13\r\n\r\n", path, request_host, websocket_key);
|
||||
n = send(client->sockfd, request_headers, strlen(request_headers), 0);
|
||||
n = _libwsclient_write(client, request_headers, strlen(request_headers));
|
||||
z = 0;
|
||||
memset(recv_buf, 0, 1024);
|
||||
//TODO: actually handle data after \r\n\r\n in case server
|
||||
// sends post-handshake data that gets coalesced in this recv
|
||||
do {
|
||||
n = recv(client->sockfd, recv_buf + z, 1023 - z, 0);
|
||||
n = _libwsclient_read(client, recv_buf + z, 1023 - z);
|
||||
z += n;
|
||||
} while((z < 4 || strstr(recv_buf, "\r\n\r\n") == NULL) && n > 0);
|
||||
|
||||
@ -886,10 +906,10 @@ int libwsclient_send_fragment(wsclient *client, char *strdata, int len, int flag
|
||||
sent = 0;
|
||||
i = 1;
|
||||
|
||||
//we don't need the send lock here. It *should* have already been aquired before sending fragmented message
|
||||
//we don't need the send lock here. It *should* have already been acquired before sending fragmented message
|
||||
//and will be released after last fragment sent.
|
||||
while(sent < frame_size && i > 0) {
|
||||
i = send(sockfd, data+sent, frame_size - sent, 0);
|
||||
i = _libwsclient_write(client, data+sent, frame_size - sent);
|
||||
sent += i;
|
||||
}
|
||||
|
||||
@ -943,7 +963,6 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
sockfd = client->sockfd;
|
||||
if(strdata == NULL) {
|
||||
if(client->onerror) {
|
||||
err = libwsclient_new_error(WS_SEND_NULL_DATA_ERR);
|
||||
@ -962,17 +981,14 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
finNopcode = 0x81; //FIN and text opcode.
|
||||
if(payload_len <= 125) {
|
||||
frame_size = 6 + payload_len;
|
||||
data = (void *)malloc(frame_size);
|
||||
payload_len_small = payload_len;
|
||||
|
||||
} else if(payload_len > 125 && payload_len <= 0xffff) {
|
||||
frame_size = 8 + payload_len;
|
||||
data = (void *)malloc(frame_size);
|
||||
payload_len_small = 126;
|
||||
payload_offset += 2;
|
||||
} else if(payload_len > 0xffff && payload_len <= 0xffffffffffffffffLL) {
|
||||
frame_size = 14 + payload_len;
|
||||
data = (void *)malloc(frame_size);
|
||||
payload_len_small = 127;
|
||||
payload_offset += 8;
|
||||
} else {
|
||||
@ -984,6 +1000,7 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
data = (char *)malloc(frame_size);
|
||||
memset(data, 0, frame_size);
|
||||
*data = finNopcode;
|
||||
*(data+1) = payload_len_small | 0x80; //payload length with mask bit on
|
||||
@ -991,14 +1008,14 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
payload_len &= 0xffff;
|
||||
len_size = 2;
|
||||
for(i = 0; i < len_size; i++) {
|
||||
memcpy(data+2+i, (void *)&payload_len+(len_size-i-1), 1);
|
||||
*(data+2+i) = *((char *)&payload_len+(len_size-i-1));
|
||||
}
|
||||
}
|
||||
if(payload_len_small == 127) {
|
||||
payload_len &= 0xffffffffffffffffLL;
|
||||
len_size = 8;
|
||||
for(i = 0; i < len_size; i++) {
|
||||
memcpy(data+2+i, (void *)&payload_len+(len_size-i-1), 1);
|
||||
*(data+2+i) = *((char *)&payload_len+(len_size-i-1));
|
||||
}
|
||||
}
|
||||
for(i=0;i<4;i++)
|
||||
@ -1012,7 +1029,7 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
|
||||
pthread_mutex_lock(&client->send_lock);
|
||||
while(sent < frame_size && i >= 0) {
|
||||
i = send(sockfd, data+sent, frame_size - sent, 0);
|
||||
i = _libwsclient_write(client, data+sent, frame_size - sent);
|
||||
sent += i;
|
||||
}
|
||||
pthread_mutex_unlock(&client->send_lock);
|
||||
@ -1030,4 +1047,28 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
return sent;
|
||||
}
|
||||
|
||||
ssize_t _libwsclient_read(wsclient *c, void *buf, size_t length) {
|
||||
#ifdef HAVE_LIBSSL
|
||||
if(c->flags & CLIENT_IS_SSL) {
|
||||
return (ssize_t)SSL_read(c->ssl, buf, length);
|
||||
} else {
|
||||
#endif
|
||||
return recv(c->sockfd, buf, length, 0);
|
||||
#ifdef HAVE_LIBSSL
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t _libwsclient_write(wsclient *c, const void *buf, size_t length) {
|
||||
#ifdef HAVE_LIBSSL
|
||||
if(c->flags & CLIENT_IS_SSL) {
|
||||
return (ssize_t)SSL_write(c->ssl, buf, length);
|
||||
} else {
|
||||
#endif
|
||||
return send(c->sockfd, buf, length, 0);
|
||||
#ifdef HAVE_LIBSSL
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
27
wsclient.h
27
wsclient.h
@ -1,11 +1,23 @@
|
||||
#ifndef WSCLIENT_H_
|
||||
#define WSCLIENT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
|
||||
#ifdef HAVE_LIBSSL
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/crypto.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef WSCLIENT_H_
|
||||
#define WSCLIENT_H_
|
||||
|
||||
#define FRAME_CHUNK_LENGTH 1024
|
||||
#define HELPER_RECV_BUF_SIZE 1024
|
||||
@ -24,6 +36,8 @@
|
||||
#define WS_FRAGMENT_START (1 << 0)
|
||||
#define WS_FRAGMENT_FIN (1 << 7)
|
||||
|
||||
#define WS_FLAGS_SSL_INIT (1 << 0)
|
||||
|
||||
#define WS_EXIT_MALLOC -1
|
||||
#define WS_EXIT_PTHREAD_MUTEX_INIT -2
|
||||
#define WS_EXIT_PTHREAD_CREATE -3
|
||||
@ -95,7 +109,10 @@ typedef struct _wsclient {
|
||||
wsclient_frame *current_frame;
|
||||
struct sockaddr_un helper_sa;
|
||||
int helper_sock;
|
||||
|
||||
#ifdef HAVE_LIBSSL
|
||||
SSL_CTX *ssl_ctx;
|
||||
SSL *ssl;
|
||||
#endif
|
||||
} wsclient;
|
||||
|
||||
|
||||
@ -103,6 +120,8 @@ typedef struct _wsclient {
|
||||
|
||||
wsclient *libwsclient_new(const char *URI);
|
||||
wsclient_error *libwsclient_new_error(int errcode);
|
||||
ssize_t _libwsclient_read(wsclient *c, void *buf, size_t length);
|
||||
ssize_t _libwsclient_write(wsclient *c, const void *buf, size_t length);
|
||||
int libwsclient_open_connection(const char *host, const char *port);
|
||||
int stricmp(const char *s1, const char *s2);
|
||||
int libwsclient_complete_frame(wsclient *c, wsclient_frame *frame);
|
||||
@ -142,5 +161,7 @@ char *errors[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
int libwsclient_flags; //global flags variable
|
||||
|
||||
|
||||
#endif /* WSCLIENT_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user