Skip to content

Commit 98b4bc8

Browse files
committed
reorganized project to make room for server code. updated build process to include server
1 parent e00bcd5 commit 98b4bc8

14 files changed

+362
-105
lines changed

.cproject

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
</toolChain>
5151
</folderInfo>
5252
<sourceEntries>
53-
<entry excluding="websocket|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
54-
<entry excluding="websocket" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
55-
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src/websocket"/>
53+
<entry excluding="cwebsocket|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
54+
<entry excluding="cwebsocket" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
55+
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src/cwebsocket"/>
5656
</sourceEntries>
5757
</configuration>
5858
</storageModule>
@@ -99,9 +99,9 @@
9999
</toolChain>
100100
</folderInfo>
101101
<sourceEntries>
102-
<entry excluding="websocket|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
103-
<entry excluding="websocket" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
104-
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src/websocket"/>
102+
<entry excluding="cwebsocket|src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
103+
<entry excluding="cwebsocket" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
104+
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src/cwebsocket"/>
105105
</sourceEntries>
106106
</configuration>
107107
</storageModule>

Makefile.am

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
AUTOMAKE_OPTIONS = foreign
22

3-
bin_PROGRAMS = websocket-client
4-
websocket_client_SOURCES = src/websocket/utf8.h src/websocket/utf8.c src/websocket/cwebsocket_client.h src/websocket/cwebsocket_client.c src/client.c
3+
bin_PROGRAMS = websocket-client websocket-server
4+
websocket_client_SOURCES = src/cwebsocket/utf8.h src/cwebsocket/common.h src/cwebsocket/common.c src/cwebsocket/utf8.c src/cwebsocket/client.h src/cwebsocket/client.c src/websocket-client.c
5+
websocket_server_SOURCES = src/cwebsocket/utf8.h src/cwebsocket/common.h src/cwebsocket/common.c src/cwebsocket/utf8.c src/cwebsocket/server.h src/cwebsocket/server.c src/websocket-server.c
56

67
#LDFLAGS = -L/usr/local/lib -lcrypto
78

@@ -22,4 +23,4 @@ if USESSL
2223
endif
2324

2425
clean:
25-
rm -rf autoscan.log config.h.in config.h config.cache configure install-sh aclocal.m4 autom4te.cache/ config.log config.status Debug/ depcomp .deps/ m4/ Makefile Makefile.in missing stamp-h1 *.o src/*.o *~ client server
26+
rm -rf autoscan.log config.h.in config.h config.cache configure install-sh aclocal.m4 autom4te.cache/ config.log config.status Debug/ depcomp .deps/ m4/ Makefile Makefile.in missing stamp-h1 *.o src/*.o *~ websocket-client websocket-server

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
###### Fast, lightweight websocket server/client.
66

7-
The goal of cwebsocket is to provide a simple, lightweight, high performance websocket solution that's
8-
efficient enough to run well on low power embedded systems.
7+
The goal of cwebsocket is to provide a simple, lightweight, high performance websocket client/server optimized for low power embedded systems.
98

10-
cwebsocket is currently in a development state. You may encounter bugs. Report them for a timely fix.
9+
cwebsocket is currently in a development state. You may encounter bugs. Report them in the issues section for a timely fix.
1110

1211
Successful tests have been conducted on the following architectures:
1312

@@ -43,7 +42,7 @@ Without SSL:
4342
The websocket client is able to connect and exchange data with any RFC 6455 compliant server.
4443

4544
```C
46-
#include "cwebsocket.h"
45+
#include "cwebsocket/client.h"
4746

4847
cwebsocket_client websocket;
4948

@@ -54,7 +53,6 @@ void onopen(cwebsocket_client *websocket) {
5453
void onmessage(cwebsocket_client *websocket, cwebsocket_message *message) {
5554
syslog(LOG_DEBUG, "onmessage: socket_fd=%i, opcode=%#04x, payload_len=%zu, payload=%s\n",
5655
websocket->socket, message->opcode, message->payload_len, message->payload);
57-
5856
}
5957

6058
void onclose(cwebsocket_client *websocket, const char *message) {
@@ -104,3 +102,10 @@ int main(int argc, char **argv) {
104102
./websocket-client ws://echo.websocket.org
105103
./websocket-client wss://echo.websocket.org
106104
105+
### TODO
106+
107+
1. More testing on various embedded devices
108+
2. Implement pluggable sub-protocols (socketio, WAMP, custom)
109+
3. Implement pluggable extensions on the client per RFC (section 9)
110+
4. Get a basic websocket server developed
111+

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
AC_PREREQ([2.69])
55
AC_INIT([cwebsocket], [0.01], [root@localhost])
66
AM_INIT_AUTOMAKE([1.9 foreign])
7-
AC_CONFIG_SRCDIR([src/client.c])
7+
AC_CONFIG_SRCDIR([src/websocket-client.c])
88
AC_CONFIG_HEADERS([config.h])
99

1010
if test -z $CFLAGS; then

src/websocket/cwebsocket_client.c renamed to src/cwebsocket/client.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
#include "cwebsocket_client.h"
25+
#include "client.h"
2626

2727
void cwebsocket_init() {
2828
const rlim_t kStackSize = STACK_SIZE_MIN * 1024 * 1024;
@@ -43,27 +43,6 @@ void cwebsocket_init() {
4343
syslog(LOG_DEBUG, "stack limit min=%ld, max=%ld\n", rl.rlim_cur, rl.rlim_max);
4444
}
4545

46-
char* cwebsocket_base64_encode(const unsigned char *input, int length) {
47-
48-
BIO *bmem, *b64;
49-
BUF_MEM *bptr;
50-
51-
b64 = BIO_new(BIO_f_base64());
52-
bmem = BIO_new(BIO_s_mem());
53-
b64 = BIO_push(b64, bmem);
54-
BIO_write(b64, input, length);
55-
BIO_flush(b64);
56-
BIO_get_mem_ptr(b64, &bptr);
57-
58-
char *buff = (char *)malloc(bptr->length);
59-
memcpy(buff, bptr->data, bptr->length-1);
60-
buff[bptr->length-1] = '\0';
61-
62-
BIO_free_all(b64);
63-
64-
return buff;
65-
}
66-
6746
void cwebsocket_parse_uri(cwebsocket_client *websocket, const char *uri,
6847
char *hostname, char *port, char *resource, char *querystring) {
6948

@@ -130,11 +109,6 @@ void cwebsocket_parse_uri(cwebsocket_client *websocket, const char *uri,
130109
}
131110
}
132111

133-
void cwebsocket_print_frame(cwebsocket_frame *frame) {
134-
syslog(LOG_DEBUG, "cwebsocket_print_frame: fin=%i, rsv1=%i, rsv2=%i, rsv3=%i, opcode=%#04x, mask=%i, payload_len=%i\n",
135-
frame->fin, frame->rsv1, frame->rsv2, frame->rsv3, frame->opcode, frame->mask, frame->payload_len);
136-
}
137-
138112
int cwebsocket_connect(cwebsocket_client *websocket) {
139113

140114
if(websocket->state & WEBSOCKET_STATE_CONNECTED) {

src/websocket/cwebsocket_client.h renamed to src/cwebsocket/client.h

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,14 @@
2525
#ifndef WEBSOCKET_CLIENT_H
2626
#define WEBSOCKET_CLIENT_H
2727

28-
#include <stdlib.h>
29-
#include <stdio.h>
30-
#include <stdint.h>
3128
#include <time.h>
32-
#include <string.h>
33-
#include <syslog.h>
34-
#include <errno.h>
3529
#include <fcntl.h>
36-
#include <unistd.h>
3730
#include <ctype.h>
3831
#include <netdb.h>
3932
#include <netinet/in.h>
40-
#include <arpa/inet.h>
41-
#include <sys/types.h>
42-
#include <sys/socket.h>
4333
#include <sys/time.h>
44-
#include <openssl/sha.h>
45-
#include <openssl/hmac.h>
46-
#include <openssl/evp.h>
47-
#include <openssl/bio.h>
48-
#include <openssl/buffer.h>
4934
#include <sys/resource.h>
50-
#include "utf8.h"
35+
#include "common.h"
5136

5237
#ifdef USESSL
5338
#include <openssl/rand.h>
@@ -59,18 +44,6 @@
5944
#include <pthread.h>
6045
#endif
6146

62-
#ifndef HANDSHAKE_BUFFER_MAX
63-
#define HANDSHAKE_BUFFER_MAX 256
64-
#endif
65-
66-
#ifndef DATA_BUFFER_MAX
67-
#define DATA_BUFFER_MAX 65536
68-
#endif
69-
70-
#ifndef STACK_SIZE_MIN
71-
#define STACK_SIZE_MIN 2
72-
#endif
73-
7447
#define WEBSOCKET_STATE_CONNECTING (1 << 0)
7548
#define WEBSOCKET_STATE_CONNECTED (1 << 1)
7649
#define WEBSOCKET_STATE_OPEN (1 << 2)
@@ -80,37 +53,6 @@
8053
#define WEBSOCKET_FLAG_SSL (1 << 0)
8154
#define WEBSOCKET_FLAG_AUTORECONNECT (1 << 1)
8255

83-
typedef enum {
84-
TRUE,
85-
FALSE
86-
} bool;
87-
88-
typedef enum {
89-
CONTINUATION = 0x00,
90-
TEXT_FRAME = 0x01,
91-
BINARY_FRAME = 0x02,
92-
CLOSE = 0x08,
93-
PING = 0x09,
94-
PONG = 0x0A,
95-
} opcode;
96-
97-
typedef struct {
98-
uint32_t opcode;
99-
uint64_t payload_len;
100-
char *payload;
101-
} cwebsocket_message;
102-
103-
typedef struct {
104-
bool fin;
105-
bool rsv1;
106-
bool rsv2;
107-
bool rsv3;
108-
opcode opcode;
109-
bool mask;
110-
int payload_len;
111-
uint32_t masking_key[4];
112-
} cwebsocket_frame;
113-
11456
typedef struct _cwebsocket {
11557
int socket;
11658
int retry;
@@ -145,10 +87,8 @@ ssize_t cwebsocket_write_data(cwebsocket_client *websocket, const char *data, in
14587
void cwebsocket_run(cwebsocket_client *websocket);
14688
void cwebsocket_close(cwebsocket_client *websocket, const char *message);
14789
void cwebsocket_listen(cwebsocket_client *websocket);
148-
void cwebsocket_print_frame(cwebsocket_frame *frame);
14990

15091
// "private"
151-
char* cwebsocket_base64_encode(const unsigned char *input, int length);
15292
void cwebsocket_parse_uri(cwebsocket_client *websocket, const char *uri, char *hostname, char *port, char *resource, char *querystring);
15393
int cwebsocket_handshake_handler(cwebsocket_client *websocket, const char *handshake_response, char *seckey);
15494
int cwebsocket_read_handshake(cwebsocket_client *websocket, char *seckey);

src/cwebsocket/common.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 Jeremy Hahn
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "common.h"
26+
27+
char* cwebsocket_base64_encode(const unsigned char *input, int length) {
28+
29+
BIO *bmem, *b64;
30+
BUF_MEM *bptr;
31+
32+
b64 = BIO_new(BIO_f_base64());
33+
bmem = BIO_new(BIO_s_mem());
34+
b64 = BIO_push(b64, bmem);
35+
BIO_write(b64, input, length);
36+
BIO_flush(b64);
37+
BIO_get_mem_ptr(b64, &bptr);
38+
39+
char *buff = (char *)malloc(bptr->length);
40+
memcpy(buff, bptr->data, bptr->length-1);
41+
buff[bptr->length-1] = '\0';
42+
43+
BIO_free_all(b64);
44+
45+
return buff;
46+
}
47+
48+
void cwebsocket_print_frame(cwebsocket_frame *frame) {
49+
syslog(LOG_DEBUG, "cwebsocket_print_frame: fin=%i, rsv1=%i, rsv2=%i, rsv3=%i, opcode=%#04x, mask=%i, payload_len=%i\n",
50+
frame->fin, frame->rsv1, frame->rsv2, frame->rsv3, frame->opcode, frame->mask, frame->payload_len);
51+
}

src/cwebsocket/common.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 Jeremy Hahn
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef CWEBSOCKET_H_
26+
#define CWEBSOCKET_H_
27+
28+
#include <stdlib.h>
29+
#include <stdio.h>
30+
#include <stdint.h>
31+
#include <syslog.h>
32+
#include <string.h>
33+
#include <errno.h>
34+
#include <unistd.h>
35+
#include <sys/types.h>
36+
#include <sys/socket.h>
37+
#include <arpa/inet.h>
38+
#include <openssl/sha.h>
39+
#include <openssl/hmac.h>
40+
#include <openssl/evp.h>
41+
#include <openssl/bio.h>
42+
#include <openssl/buffer.h>
43+
#include "utf8.h"
44+
45+
#ifndef HANDSHAKE_BUFFER_MAX
46+
#define HANDSHAKE_BUFFER_MAX 256 // bytes
47+
#endif
48+
49+
#ifndef DATA_BUFFER_MAX
50+
#define DATA_BUFFER_MAX 65536 // bytes
51+
#endif
52+
53+
#ifndef STACK_SIZE_MIN
54+
#define STACK_SIZE_MIN 2 // MB
55+
#endif
56+
57+
typedef enum {
58+
TRUE,
59+
FALSE
60+
} bool;
61+
62+
typedef enum {
63+
CONTINUATION = 0x00,
64+
TEXT_FRAME = 0x01,
65+
BINARY_FRAME = 0x02,
66+
CLOSE = 0x08,
67+
PING = 0x09,
68+
PONG = 0x0A,
69+
} opcode;
70+
71+
typedef struct {
72+
uint32_t opcode;
73+
uint64_t payload_len;
74+
char *payload;
75+
} cwebsocket_message;
76+
77+
typedef struct {
78+
bool fin;
79+
bool rsv1;
80+
bool rsv2;
81+
bool rsv3;
82+
opcode opcode;
83+
bool mask;
84+
int payload_len;
85+
uint32_t masking_key[4];
86+
} cwebsocket_frame;
87+
88+
char* cwebsocket_base64_encode(const unsigned char *input, int length);
89+
void cwebsocket_print_frame(cwebsocket_frame *frame);
90+
91+
#endif

0 commit comments

Comments
 (0)