Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
4ca4c862
Commit
4ca4c862
authored
Oct 09, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated net_skeleton, added extra_headers param to mg_send_file()
parent
0918df15
Changes
14
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
225 additions
and
163 deletions
+225
-163
Makefile
examples/Makefile
+1
-1
Makefile
examples/file_upload/Makefile
+2
-1
Makefile
examples/hello_world/Makefile
+2
-0
Makefile
examples/send_file/Makefile
+2
-0
send_file.c
examples/send_file/send_file.c
+1
-1
websocket_chat.c
examples/websocket_chat/websocket_chat.c
+1
-1
websocket_echo_server.c
examples/websocket_echo_server/websocket_echo_server.c
+1
-1
net_skeleton.c
examples/websocket_ssl_proxy/net_skeleton.c
+53
-40
net_skeleton.h
examples/websocket_ssl_proxy/net_skeleton.h
+40
-26
ssl_wrapper.c
examples/websocket_ssl_proxy/ssl_wrapper.c
+9
-9
ssl_wrapper.h
examples/websocket_ssl_proxy/ssl_wrapper.h
+1
-1
ws_ssl.c
examples/websocket_ssl_proxy/ws_ssl.c
+1
-1
mongoose.c
mongoose.c
+110
-80
mongoose.h
mongoose.h
+1
-1
No files found.
examples/Makefile
View file @
4ca4c862
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
CFLAGS
=
-W
-Wall
-I
..
-pthread
-g
-pipe
$(CFLAGS_EXTRA)
CFLAGS
=
-W
-Wall
-I
..
-pthread
-g
-pipe
$(CFLAGS_EXTRA)
RM
=
rm
-rf
RM
=
rm
-rf
OUT
=
-o
$@
OUT
=
-o
$@
ALL_PROGS
=
hello websocket server post multi_threaded upload auth pubsub
ALL_PROGS
=
server post auth
NS
=
../../net_skeleton
NS
=
../../net_skeleton
SW
=
../../ssl_wrapper
SW
=
../../ssl_wrapper
...
...
examples/file_upload/Makefile
View file @
4ca4c862
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
SOURCES
=
$(PROG)
.c ../../mongoose.c
SOURCES
=
$(PROG)
.c ../../mongoose.c
all
:
$(PROG)
all
:
$(PROG)
run
:
$(PROG)
./
$(PROG)
./
$(PROG)
$(PROG)
:
$(SOURCES) Makefile
$(PROG)
:
$(SOURCES) Makefile
...
@@ -13,7 +15,6 @@ $(PROG): $(SOURCES) Makefile
...
@@ -13,7 +15,6 @@ $(PROG): $(SOURCES) Makefile
win
:
win
:
wine cl
$(SOURCES)
/MD /nologo /DNDEBUG /O1 /I../.. /Fe
$(PROG)
.exe
wine cl
$(SOURCES)
/MD /nologo /DNDEBUG /O1 /I../.. /Fe
$(PROG)
.exe
wine
$(PROG)
.exe
clean
:
clean
:
rm
-rf
$(PROG)
*
.exe
*
.dSYM
*
.obj
*
.exp .
*
o
*
.lib
*
.gc
*
rm
-rf
$(PROG)
*
.exe
*
.dSYM
*
.obj
*
.exp .
*
o
*
.lib
*
.gc
*
examples/hello_world/Makefile
View file @
4ca4c862
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
SOURCES
=
$(PROG)
.c ../../mongoose.c
SOURCES
=
$(PROG)
.c ../../mongoose.c
all
:
$(PROG)
all
:
$(PROG)
run
:
$(PROG)
./
$(PROG)
./
$(PROG)
$(PROG)
:
$(SOURCES) Makefile
$(PROG)
:
$(SOURCES) Makefile
...
...
examples/send_file/Makefile
View file @
4ca4c862
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
...
@@ -6,6 +6,8 @@ CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
SOURCES
=
$(PROG)
.c ../../mongoose.c
SOURCES
=
$(PROG)
.c ../../mongoose.c
all
:
$(PROG)
all
:
$(PROG)
run
:
$(PROG)
./
$(PROG)
./
$(PROG)
$(PROG)
:
$(SOURCES) Makefile
$(PROG)
:
$(SOURCES) Makefile
...
...
examples/send_file/send_file.c
View file @
4ca4c862
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
switch
(
ev
)
{
switch
(
ev
)
{
case
MG_REQUEST
:
case
MG_REQUEST
:
mg_send_file
(
conn
,
"send_file.c"
);
// Also could be a directory, or CGI file
mg_send_file
(
conn
,
"send_file.c"
,
NULL
);
// Also could be a dir, or CGI
return
MG_MORE
;
// It is important to return MG_MORE after mg_send_file!
return
MG_MORE
;
// It is important to return MG_MORE after mg_send_file!
case
MG_AUTH
:
return
MG_TRUE
;
case
MG_AUTH
:
return
MG_TRUE
;
default:
return
MG_FALSE
;
default:
return
MG_FALSE
;
...
...
examples/websocket_chat/websocket_chat.c
View file @
4ca4c862
...
@@ -49,7 +49,7 @@ static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
...
@@ -49,7 +49,7 @@ static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
handle_websocket_message
(
conn
);
handle_websocket_message
(
conn
);
return
MG_TRUE
;
return
MG_TRUE
;
}
else
{
}
else
{
mg_send_file
(
conn
,
"index.html"
);
// Return MG_MORE after mg_send_file()
mg_send_file
(
conn
,
"index.html"
,
NULL
);
// Return MG_MORE after!
return
MG_MORE
;
return
MG_MORE
;
}
}
case
MG_WS_CONNECT
:
case
MG_WS_CONNECT
:
...
...
examples/websocket_echo_server/websocket_echo_server.c
View file @
4ca4c862
...
@@ -27,7 +27,7 @@ static int send_reply(struct mg_connection *conn) {
...
@@ -27,7 +27,7 @@ static int send_reply(struct mg_connection *conn) {
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
)
?
return
conn
->
content_len
==
4
&&
!
memcmp
(
conn
->
content
,
"exit"
,
4
)
?
MG_FALSE
:
MG_TRUE
;
MG_FALSE
:
MG_TRUE
;
}
else
{
}
else
{
mg_send_file
(
conn
,
"index.html"
);
mg_send_file
(
conn
,
"index.html"
,
NULL
);
return
MG_MORE
;
return
MG_MORE
;
}
}
}
}
...
...
examples/websocket_ssl_proxy/net_skeleton.c
View file @
4ca4c862
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
// Alternatively, you can license this software under a commercial
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/>.
// license, as set out in <http://cesanta.com/>.
//
//
// $Date: 2014-09-
09 17:09:33
UTC $
// $Date: 2014-09-
28 05:04:41
UTC $
#include "net_skeleton.h"
#include "net_skeleton.h"
...
@@ -38,13 +38,19 @@ struct ctl_msg {
...
@@ -38,13 +38,19 @@ struct ctl_msg {
char
message
[
1024
*
8
];
char
message
[
1024
*
8
];
};
};
void
iobuf_init
(
struct
iobuf
*
iobuf
,
size_t
size
)
{
void
iobuf_resize
(
struct
iobuf
*
io
,
size_t
new_size
)
{
char
*
p
;
if
((
new_size
>
io
->
size
||
(
new_size
<
io
->
size
&&
new_size
>=
io
->
len
))
&&
(
p
=
(
char
*
)
NS_REALLOC
(
io
->
buf
,
new_size
))
!=
NULL
)
{
io
->
size
=
new_size
;
io
->
buf
=
p
;
}
}
void
iobuf_init
(
struct
iobuf
*
iobuf
,
size_t
initial_size
)
{
iobuf
->
len
=
iobuf
->
size
=
0
;
iobuf
->
len
=
iobuf
->
size
=
0
;
iobuf
->
buf
=
NULL
;
iobuf
->
buf
=
NULL
;
iobuf_resize
(
iobuf
,
initial_size
);
if
(
size
>
0
&&
(
iobuf
->
buf
=
(
char
*
)
NS_MALLOC
(
size
))
!=
NULL
)
{
iobuf
->
size
=
size
;
}
}
}
void
iobuf_free
(
struct
iobuf
*
iobuf
)
{
void
iobuf_free
(
struct
iobuf
*
iobuf
)
{
...
@@ -85,8 +91,8 @@ void iobuf_remove(struct iobuf *io, size_t n) {
...
@@ -85,8 +91,8 @@ void iobuf_remove(struct iobuf *io, size_t n) {
static
size_t
ns_out
(
struct
ns_connection
*
nc
,
const
void
*
buf
,
size_t
len
)
{
static
size_t
ns_out
(
struct
ns_connection
*
nc
,
const
void
*
buf
,
size_t
len
)
{
if
(
nc
->
flags
&
NSF_UDP
)
{
if
(
nc
->
flags
&
NSF_UDP
)
{
long
n
=
send
(
nc
->
sock
,
buf
,
len
,
0
);
long
n
=
send
to
(
nc
->
sock
,
buf
,
len
,
0
,
&
nc
->
sa
.
sa
,
sizeof
(
nc
->
sa
.
sin
)
);
DBG
((
"%p %d send %ld (%d
)"
,
nc
,
nc
->
sock
,
n
,
errno
));
DBG
((
"%p %d send %ld (%d
%s)"
,
nc
,
nc
->
sock
,
n
,
errno
,
strerror
(
errno
)
));
return
n
<
0
?
0
:
n
;
return
n
<
0
?
0
:
n
;
}
else
{
}
else
{
return
iobuf_append
(
&
nc
->
send_iobuf
,
buf
,
len
);
return
iobuf_append
(
&
nc
->
send_iobuf
,
buf
,
len
);
...
@@ -191,7 +197,7 @@ int ns_printf(struct ns_connection *conn, const char *fmt, ...) {
...
@@ -191,7 +197,7 @@ int ns_printf(struct ns_connection *conn, const char *fmt, ...) {
}
}
static
void
hexdump
(
struct
ns_connection
*
nc
,
const
char
*
path
,
static
void
hexdump
(
struct
ns_connection
*
nc
,
const
char
*
path
,
int
num_bytes
,
enum
ns_eve
nt
ev
)
{
int
num_bytes
,
i
nt
ev
)
{
const
struct
iobuf
*
io
=
ev
==
NS_SEND
?
&
nc
->
send_iobuf
:
&
nc
->
recv_iobuf
;
const
struct
iobuf
*
io
=
ev
==
NS_SEND
?
&
nc
->
send_iobuf
:
&
nc
->
recv_iobuf
;
FILE
*
fp
;
FILE
*
fp
;
char
*
buf
,
src
[
60
],
dst
[
60
];
char
*
buf
,
src
[
60
],
dst
[
60
];
...
@@ -201,7 +207,7 @@ static void hexdump(struct ns_connection *nc, const char *path,
...
@@ -201,7 +207,7 @@ static void hexdump(struct ns_connection *nc, const char *path,
ns_sock_to_str
(
nc
->
sock
,
src
,
sizeof
(
src
),
3
);
ns_sock_to_str
(
nc
->
sock
,
src
,
sizeof
(
src
),
3
);
ns_sock_to_str
(
nc
->
sock
,
dst
,
sizeof
(
dst
),
7
);
ns_sock_to_str
(
nc
->
sock
,
dst
,
sizeof
(
dst
),
7
);
fprintf
(
fp
,
"%lu %p %s %s %s %d
\n
"
,
(
unsigned
long
)
time
(
NULL
),
fprintf
(
fp
,
"%lu %p %s %s %s %d
\n
"
,
(
unsigned
long
)
time
(
NULL
),
nc
->
connection
_data
,
src
,
nc
->
user
_data
,
src
,
ev
==
NS_RECV
?
"<-"
:
ev
==
NS_SEND
?
"->"
:
ev
==
NS_RECV
?
"<-"
:
ev
==
NS_SEND
?
"->"
:
ev
==
NS_ACCEPT
?
"<A"
:
ev
==
NS_CONNECT
?
"C>"
:
"XX"
,
ev
==
NS_ACCEPT
?
"<A"
:
ev
==
NS_CONNECT
?
"C>"
:
"XX"
,
dst
,
num_bytes
);
dst
,
num_bytes
);
...
@@ -215,12 +221,13 @@ static void hexdump(struct ns_connection *nc, const char *path,
...
@@ -215,12 +221,13 @@ static void hexdump(struct ns_connection *nc, const char *path,
}
}
}
}
static
void
ns_call
(
struct
ns_connection
*
conn
,
enum
ns_eve
nt
ev
,
void
*
p
)
{
static
void
ns_call
(
struct
ns_connection
*
nc
,
i
nt
ev
,
void
*
p
)
{
if
(
conn
->
mgr
->
hexdump_file
!=
NULL
&&
ev
!=
NS_POLL
)
{
if
(
nc
->
mgr
->
hexdump_file
!=
NULL
&&
ev
!=
NS_POLL
)
{
int
len
=
(
ev
==
NS_RECV
||
ev
==
NS_SEND
)
?
*
(
int
*
)
p
:
0
;
int
len
=
(
ev
==
NS_RECV
||
ev
==
NS_SEND
)
?
*
(
int
*
)
p
:
0
;
hexdump
(
conn
,
conn
->
mgr
->
hexdump_file
,
len
,
ev
);
hexdump
(
nc
,
nc
->
mgr
->
hexdump_file
,
len
,
ev
);
}
}
if
(
conn
->
mgr
->
callback
)
conn
->
mgr
->
callback
(
conn
,
ev
,
p
);
nc
->
callback
(
nc
,
ev
,
p
);
}
}
static
void
ns_destroy_conn
(
struct
ns_connection
*
conn
)
{
static
void
ns_destroy_conn
(
struct
ns_connection
*
conn
)
{
...
@@ -443,7 +450,8 @@ static int ns_use_cert(SSL_CTX *ctx, const char *pem_file) {
...
@@ -443,7 +450,8 @@ static int ns_use_cert(SSL_CTX *ctx, const char *pem_file) {
}
}
#endif // NS_ENABLE_SSL
#endif // NS_ENABLE_SSL
struct
ns_connection
*
ns_bind
(
struct
ns_mgr
*
srv
,
const
char
*
str
,
void
*
data
)
{
struct
ns_connection
*
ns_bind
(
struct
ns_mgr
*
srv
,
const
char
*
str
,
ns_callback_t
callback
,
void
*
user_data
)
{
union
socket_address
sa
;
union
socket_address
sa
;
struct
ns_connection
*
nc
=
NULL
;
struct
ns_connection
*
nc
=
NULL
;
int
use_ssl
,
proto
;
int
use_ssl
,
proto
;
...
@@ -454,12 +462,13 @@ struct ns_connection *ns_bind(struct ns_mgr *srv, const char *str, void *data) {
...
@@ -454,12 +462,13 @@ struct ns_connection *ns_bind(struct ns_mgr *srv, const char *str, void *data) {
if
(
use_ssl
&&
cert
[
0
]
==
'\0'
)
return
NULL
;
if
(
use_ssl
&&
cert
[
0
]
==
'\0'
)
return
NULL
;
if
((
sock
=
ns_open_listening_socket
(
&
sa
,
proto
))
==
INVALID_SOCKET
)
{
if
((
sock
=
ns_open_listening_socket
(
&
sa
,
proto
))
==
INVALID_SOCKET
)
{
}
else
if
((
nc
=
ns_add_sock
(
srv
,
sock
,
NULL
))
==
NULL
)
{
}
else
if
((
nc
=
ns_add_sock
(
srv
,
sock
,
callback
,
NULL
))
==
NULL
)
{
closesocket
(
sock
);
closesocket
(
sock
);
}
else
{
}
else
{
nc
->
sa
=
sa
;
nc
->
sa
=
sa
;
nc
->
flags
|=
NSF_LISTENING
;
nc
->
flags
|=
NSF_LISTENING
;
nc
->
connection_data
=
data
;
nc
->
user_data
=
user_data
;
nc
->
callback
=
callback
;
if
(
proto
==
SOCK_DGRAM
)
{
if
(
proto
==
SOCK_DGRAM
)
{
nc
->
flags
|=
NSF_UDP
;
nc
->
flags
|=
NSF_UDP
;
...
@@ -490,7 +499,8 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
...
@@ -490,7 +499,8 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
// NOTE(lsm): on Windows, sock is always > FD_SETSIZE
// NOTE(lsm): on Windows, sock is always > FD_SETSIZE
if
((
sock
=
accept
(
ls
->
sock
,
&
sa
.
sa
,
&
len
))
==
INVALID_SOCKET
)
{
if
((
sock
=
accept
(
ls
->
sock
,
&
sa
.
sa
,
&
len
))
==
INVALID_SOCKET
)
{
}
else
if
((
c
=
ns_add_sock
(
ls
->
mgr
,
sock
,
NULL
))
==
NULL
)
{
}
else
if
((
c
=
ns_add_sock
(
ls
->
mgr
,
sock
,
ls
->
callback
,
ls
->
user_data
))
==
NULL
)
{
closesocket
(
sock
);
closesocket
(
sock
);
#ifdef NS_ENABLE_SSL
#ifdef NS_ENABLE_SSL
}
else
if
(
ls
->
ssl_ctx
!=
NULL
&&
}
else
if
(
ls
->
ssl_ctx
!=
NULL
&&
...
@@ -502,6 +512,7 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
...
@@ -502,6 +512,7 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
#endif
#endif
}
else
{
}
else
{
c
->
listener
=
ls
;
c
->
listener
=
ls
;
c
->
proto_data
=
ls
->
proto_data
;
ns_call
(
c
,
NS_ACCEPT
,
&
sa
);
ns_call
(
c
,
NS_ACCEPT
,
&
sa
);
DBG
((
"%p %d %p %p"
,
c
,
c
->
sock
,
c
->
ssl_ctx
,
c
->
ssl
));
DBG
((
"%p %d %p %p"
,
c
,
c
->
sock
,
c
->
ssl_ctx
,
c
->
ssl
));
}
}
...
@@ -698,10 +709,16 @@ static void ns_handle_udp(struct ns_connection *ls) {
...
@@ -698,10 +709,16 @@ static void ns_handle_udp(struct ns_connection *ls) {
if
(
n
<=
0
)
{
if
(
n
<=
0
)
{
DBG
((
"%p recvfrom: %s"
,
ls
,
strerror
(
errno
)));
DBG
((
"%p recvfrom: %s"
,
ls
,
strerror
(
errno
)));
}
else
{
}
else
{
nc
.
mgr
=
ls
->
mgr
;
nc
.
recv_iobuf
.
buf
=
buf
;
nc
.
recv_iobuf
.
buf
=
buf
;
nc
.
recv_iobuf
.
len
=
nc
.
recv_iobuf
.
size
=
n
;
nc
.
recv_iobuf
.
len
=
nc
.
recv_iobuf
.
size
=
n
;
nc
.
sock
=
ls
->
sock
;
nc
.
sock
=
ls
->
sock
;
nc
.
callback
=
ls
->
callback
;
nc
.
user_data
=
ls
->
user_data
;
nc
.
proto_data
=
ls
->
proto_data
;
nc
.
mgr
=
ls
->
mgr
;
nc
.
mgr
=
ls
->
mgr
;
nc
.
listener
=
ls
;
nc
.
flags
=
NSF_UDP
;
DBG
((
"%p %d bytes received"
,
ls
,
n
));
DBG
((
"%p %d bytes received"
,
ls
,
n
));
ns_call
(
&
nc
,
NS_RECV
,
&
n
);
ns_call
(
&
nc
,
NS_RECV
,
&
n
);
}
}
...
@@ -716,11 +733,10 @@ static void ns_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) {
...
@@ -716,11 +733,10 @@ static void ns_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) {
}
}
}
}
in
t
ns_mgr_poll
(
struct
ns_mgr
*
mgr
,
int
milli
)
{
time_
t
ns_mgr_poll
(
struct
ns_mgr
*
mgr
,
int
milli
)
{
struct
ns_connection
*
conn
,
*
tmp_conn
;
struct
ns_connection
*
conn
,
*
tmp_conn
;
struct
timeval
tv
;
struct
timeval
tv
;
fd_set
read_set
,
write_set
;
fd_set
read_set
,
write_set
;
int
num_active_connections
=
0
;
sock_t
max_fd
=
INVALID_SOCKET
;
sock_t
max_fd
=
INVALID_SOCKET
;
time_t
current_time
=
time
(
NULL
);
time_t
current_time
=
time
(
NULL
);
...
@@ -730,7 +746,9 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
...
@@ -730,7 +746,9 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
for
(
conn
=
mgr
->
active_connections
;
conn
!=
NULL
;
conn
=
tmp_conn
)
{
for
(
conn
=
mgr
->
active_connections
;
conn
!=
NULL
;
conn
=
tmp_conn
)
{
tmp_conn
=
conn
->
next
;
tmp_conn
=
conn
->
next
;
if
(
!
(
conn
->
flags
&
(
NSF_LISTENING
|
NSF_CONNECTING
)))
{
ns_call
(
conn
,
NS_POLL
,
&
current_time
);
ns_call
(
conn
,
NS_POLL
,
&
current_time
);
}
if
(
!
(
conn
->
flags
&
NSF_WANT_WRITE
))
{
if
(
!
(
conn
->
flags
&
NSF_WANT_WRITE
))
{
//DBG(("%p read_set", conn));
//DBG(("%p read_set", conn));
ns_add_to_set
(
conn
->
sock
,
&
read_set
,
&
max_fd
);
ns_add_to_set
(
conn
->
sock
,
&
read_set
,
&
max_fd
);
...
@@ -799,47 +817,41 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
...
@@ -799,47 +817,41 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
for
(
conn
=
mgr
->
active_connections
;
conn
!=
NULL
;
conn
=
tmp_conn
)
{
for
(
conn
=
mgr
->
active_connections
;
conn
!=
NULL
;
conn
=
tmp_conn
)
{
tmp_conn
=
conn
->
next
;
tmp_conn
=
conn
->
next
;
num_active_connections
++
;
if
((
conn
->
flags
&
NSF_CLOSE_IMMEDIATELY
)
||
if
((
conn
->
flags
&
NSF_CLOSE_IMMEDIATELY
)
||
(
conn
->
send_iobuf
.
len
==
0
&&
(
conn
->
send_iobuf
.
len
==
0
&&
(
conn
->
flags
&
NSF_FINISHED_SENDING_DATA
)))
{
(
conn
->
flags
&
NSF_FINISHED_SENDING_DATA
)))
{
ns_close_conn
(
conn
);
ns_close_conn
(
conn
);
}
}
}
}
//DBG(("%d active connections", num_active_connections));
return
num_active_connections
;
return
current_time
;
}
}
struct
ns_connection
*
ns_connect
(
struct
ns_mgr
*
mgr
,
struct
ns_connection
*
ns_connect
(
struct
ns_mgr
*
mgr
,
const
char
*
address
,
const
char
*
address
,
void
*
param
)
{
ns_callback_t
callback
,
void
*
user_data
)
{
sock_t
sock
=
INVALID_SOCKET
;
sock_t
sock
=
INVALID_SOCKET
;
struct
ns_connection
*
nc
=
NULL
;
struct
ns_connection
*
nc
=
NULL
;
union
socket_address
sa
;
union
socket_address
sa
;
char
cert
[
100
],
ca_cert
[
100
];
char
cert
[
100
],
ca_cert
[
100
];
int
connect_ret_val
,
use_ssl
,
proto
;
int
rc
,
use_ssl
,
proto
;
ns_parse_address
(
address
,
&
sa
,
&
proto
,
&
use_ssl
,
cert
,
ca_cert
);
ns_parse_address
(
address
,
&
sa
,
&
proto
,
&
use_ssl
,
cert
,
ca_cert
);
if
((
sock
=
socket
(
AF_INET
,
proto
,
0
))
==
INVALID_SOCKET
)
{
if
((
sock
=
socket
(
AF_INET
,
proto
,
0
))
==
INVALID_SOCKET
)
{
return
NULL
;
return
NULL
;
}
}
ns_set_non_blocking_mode
(
sock
);
ns_set_non_blocking_mode
(
sock
);
connect_ret_val
=
connect
(
sock
,
&
sa
.
sa
,
sizeof
(
sa
.
sin
));
rc
=
(
proto
==
SOCK_DGRAM
)
?
0
:
connect
(
sock
,
&
sa
.
sa
,
sizeof
(
sa
.
sin
));
if
(
connect_ret_val
!=
0
&&
ns_is_error
(
connect_ret_val
))
{
if
(
rc
!=
0
&&
ns_is_error
(
rc
))
{
closesocket
(
sock
);
closesocket
(
sock
);
return
NULL
;
return
NULL
;
}
else
if
((
nc
=
ns_add_sock
(
mgr
,
sock
,
param
))
==
NULL
)
{
}
else
if
((
nc
=
ns_add_sock
(
mgr
,
sock
,
callback
,
user_data
))
==
NULL
)
{
closesocket
(
sock
);
closesocket
(
sock
);
return
NULL
;
return
NULL
;
}
}
nc
->
sa
=
sa
;
// Essential, cause UDP conns will use sendto()
nc
->
sa
=
sa
;
// Important, cause UDP conns will use sendto()
if
(
proto
==
SOCK_DGRAM
)
{
nc
->
flags
=
(
proto
==
SOCK_DGRAM
)
?
NSF_UDP
:
NSF_CONNECTING
;
nc
->
flags
=
NSF_UDP
;
}
else
{
nc
->
flags
=
NSF_CONNECTING
;
}
#ifdef NS_ENABLE_SSL
#ifdef NS_ENABLE_SSL
if
(
use_ssl
)
{
if
(
use_ssl
)
{
...
@@ -858,14 +870,16 @@ struct ns_connection *ns_connect(struct ns_mgr *mgr,
...
@@ -858,14 +870,16 @@ struct ns_connection *ns_connect(struct ns_mgr *mgr,
return
nc
;
return
nc
;
}
}
struct
ns_connection
*
ns_add_sock
(
struct
ns_mgr
*
s
,
sock_t
sock
,
void
*
p
)
{
struct
ns_connection
*
ns_add_sock
(
struct
ns_mgr
*
s
,
sock_t
sock
,
ns_callback_t
callback
,
void
*
user_data
)
{
struct
ns_connection
*
conn
;
struct
ns_connection
*
conn
;
if
((
conn
=
(
struct
ns_connection
*
)
NS_MALLOC
(
sizeof
(
*
conn
)))
!=
NULL
)
{
if
((
conn
=
(
struct
ns_connection
*
)
NS_MALLOC
(
sizeof
(
*
conn
)))
!=
NULL
)
{
memset
(
conn
,
0
,
sizeof
(
*
conn
));
memset
(
conn
,
0
,
sizeof
(
*
conn
));
ns_set_non_blocking_mode
(
sock
);
ns_set_non_blocking_mode
(
sock
);
ns_set_close_on_exec
(
sock
);
ns_set_close_on_exec
(
sock
);
conn
->
sock
=
sock
;
conn
->
sock
=
sock
;
conn
->
connection_data
=
p
;
conn
->
user_data
=
user_data
;
conn
->
callback
=
callback
;
conn
->
mgr
=
s
;
conn
->
mgr
=
s
;
conn
->
last_io_time
=
time
(
NULL
);
conn
->
last_io_time
=
time
(
NULL
);
ns_add_conn
(
s
,
conn
);
ns_add_conn
(
s
,
conn
);
...
@@ -890,11 +904,10 @@ void ns_broadcast(struct ns_mgr *mgr, ns_callback_t cb,void *data, size_t len) {
...
@@ -890,11 +904,10 @@ void ns_broadcast(struct ns_mgr *mgr, ns_callback_t cb,void *data, size_t len) {
}
}
}
}
void
ns_mgr_init
(
struct
ns_mgr
*
s
,
void
*
user_data
,
ns_callback_t
cb
)
{
void
ns_mgr_init
(
struct
ns_mgr
*
s
,
void
*
user_data
)
{
memset
(
s
,
0
,
sizeof
(
*
s
));
memset
(
s
,
0
,
sizeof
(
*
s
));
s
->
ctl
[
0
]
=
s
->
ctl
[
1
]
=
INVALID_SOCKET
;
s
->
ctl
[
0
]
=
s
->
ctl
[
1
]
=
INVALID_SOCKET
;
s
->
user_data
=
user_data
;
s
->
user_data
=
user_data
;
s
->
callback
=
cb
;
#ifdef _WIN32
#ifdef _WIN32
{
WSADATA
data
;
WSAStartup
(
MAKEWORD
(
2
,
2
),
&
data
);
}
{
WSADATA
data
;
WSAStartup
(
MAKEWORD
(
2
,
2
),
&
data
);
}
...
...
examples/websocket_ssl_proxy/net_skeleton.h
View file @
4ca4c862
...
@@ -14,12 +14,12 @@
...
@@ -14,12 +14,12 @@
// Alternatively, you can license this software under a commercial
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/>.
// license, as set out in <http://cesanta.com/>.
//
//
// $Date: 2014-09-
09 17:09:33
UTC $
// $Date: 2014-09-
28 05:04:41
UTC $
#ifndef NS_SKELETON_HEADER_INCLUDED
#ifndef NS_SKELETON_HEADER_INCLUDED
#define NS_SKELETON_HEADER_INCLUDED
#define NS_SKELETON_HEADER_INCLUDED
#define NS_SKELETON_VERSION "2.
0
.0"
#define NS_SKELETON_VERSION "2.
1
.0"
#undef UNICODE // Use ANSI WinAPI functions
#undef UNICODE // Use ANSI WinAPI functions
#undef _UNICODE // Use multibyte encoding on Windows
#undef _UNICODE // Use multibyte encoding on Windows
...
@@ -43,6 +43,7 @@
...
@@ -43,6 +43,7 @@
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <assert.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdarg.h>
...
@@ -84,6 +85,7 @@ typedef unsigned short uint16_t;
...
@@ -84,6 +85,7 @@ typedef unsigned short uint16_t;
typedef
unsigned
__int64
uint64_t
;
typedef
unsigned
__int64
uint64_t
;
typedef
__int64
int64_t
;
typedef
__int64
int64_t
;
typedef
SOCKET
sock_t
;
typedef
SOCKET
sock_t
;
typedef
struct
_stati64
ns_stat_t
;
#ifndef S_ISDIR
#ifndef S_ISDIR
#define S_ISDIR(x) ((x) & _S_IFDIR)
#define S_ISDIR(x) ((x) & _S_IFDIR)
#endif
#endif
...
@@ -103,6 +105,7 @@ typedef SOCKET sock_t;
...
@@ -103,6 +105,7 @@ typedef SOCKET sock_t;
#define INVALID_SOCKET (-1)
#define INVALID_SOCKET (-1)
#define to64(x) strtoll(x, NULL, 10)
#define to64(x) strtoll(x, NULL, 10)
typedef
int
sock_t
;
typedef
int
sock_t
;
typedef
struct
stat
ns_stat_t
;
#endif
#endif
#ifdef NS_ENABLE_DEBUG
#ifdef NS_ENABLE_DEBUG
...
@@ -140,6 +143,12 @@ union socket_address {
...
@@ -140,6 +143,12 @@ union socket_address {
#endif
#endif
};
};
// Describes chunk of memory
struct
ns_str
{
const
char
*
p
;
size_t
len
;
};
// IO buffers interface
// IO buffers interface
struct
iobuf
{
struct
iobuf
{
char
*
buf
;
char
*
buf
;
...
@@ -151,43 +160,45 @@ void iobuf_init(struct iobuf *, size_t initial_size);
...
@@ -151,43 +160,45 @@ void iobuf_init(struct iobuf *, size_t initial_size);
void
iobuf_free
(
struct
iobuf
*
);
void
iobuf_free
(
struct
iobuf
*
);
size_t
iobuf_append
(
struct
iobuf
*
,
const
void
*
data
,
size_t
data_size
);
size_t
iobuf_append
(
struct
iobuf
*
,
const
void
*
data
,
size_t
data_size
);
void
iobuf_remove
(
struct
iobuf
*
,
size_t
data_size
);
void
iobuf_remove
(
struct
iobuf
*
,
size_t
data_size
);
void
iobuf_resize
(
struct
iobuf
*
,
size_t
new_size
);
// Net skeleton interface
// Events. Meaning of event parameter (evp) is given in the comment.
enum
ns_event
{
NS_POLL
,
// Sent to each connection on each call to ns_mgr_poll()
NS_ACCEPT
,
// New connection accept()-ed. union socket_address *remote_addr
NS_CONNECT
,
// connect() succeeded or failed. int *success_status
NS_RECV
,
// Data has benn received. int *num_bytes
NS_SEND
,
// Data has been written to a socket. int *num_bytes
NS_CLOSE
// Connection is closed. NULL
};
// Callback function (event handler) prototype, must be defined by user.
// Callback function (event handler) prototype, must be defined by user.
// Net skeleton will call event handler, passing events defined above.
// Net skeleton will call event handler, passing events defined above.
struct
ns_connection
;
struct
ns_connection
;
typedef
void
(
*
ns_callback_t
)(
struct
ns_connection
*
,
enum
ns_event
,
void
*
evp
);
typedef
void
(
*
ns_callback_t
)(
struct
ns_connection
*
,
int
event_num
,
void
*
evp
);
// Events. Meaning of event parameter (evp) is given in the comment.
#define NS_POLL 0 // Sent to each connection on each call to ns_mgr_poll()
#define NS_ACCEPT 1 // New connection accept()-ed. union socket_address *addr
#define NS_CONNECT 2 // connect() succeeded or failed. int *success_status
#define NS_RECV 3 // Data has benn received. int *num_bytes
#define NS_SEND 4 // Data has been written to a socket. int *num_bytes
#define NS_CLOSE 5 // Connection is closed. NULL
struct
ns_mgr
{
struct
ns_mgr
{
struct
ns_connection
*
active_connections
;
struct
ns_connection
*
active_connections
;
ns_callback_t
callback
;
// Event handler function
const
char
*
hexdump_file
;
// Debug hexdump file path
const
char
*
hexdump_file
;
// Debug hexdump file path
sock_t
ctl
[
2
];
// Socketpair for mg_wakeup()
sock_t
ctl
[
2
];
// Socketpair for mg_wakeup()
void
*
user_data
;
// User data
void
*
user_data
;
// User data
};
};
struct
ns_connection
{
struct
ns_connection
{
struct
ns_connection
*
next
,
*
prev
;
// ns_mgr::active_connections linkage
struct
ns_connection
*
next
,
*
prev
;
// ns_mgr::active_connections linkage
struct
ns_connection
*
listener
;
// Set only for accept()-ed connections
struct
ns_connection
*
listener
;
// Set only for accept()-ed connections
struct
ns_mgr
*
mgr
;
struct
ns_mgr
*
mgr
;
sock_t
sock
;
union
socket_address
sa
;
sock_t
sock
;
// Socket
struct
iobuf
recv_iobuf
;
union
socket_address
sa
;
// Peer address
struct
iobuf
send_iobuf
;
struct
iobuf
recv_iobuf
;
// Received data
struct
iobuf
send_iobuf
;
// Data scheduled for sending
SSL
*
ssl
;
SSL
*
ssl
;
SSL_CTX
*
ssl_ctx
;
SSL_CTX
*
ssl_ctx
;
void
*
connection_data
;
void
*
user_data
;
// User-specific data
time_t
last_io_time
;
void
*
proto_data
;
// Application protocol-specific data
time_t
last_io_time
;
// Timestamp of the last socket IO
ns_callback_t
callback
;
// Event handler function
unsigned
int
flags
;
unsigned
int
flags
;
#define NSF_FINISHED_SENDING_DATA (1 << 0)
#define NSF_FINISHED_SENDING_DATA (1 << 0)
...
@@ -208,15 +219,18 @@ struct ns_connection {
...
@@ -208,15 +219,18 @@ struct ns_connection {
#define NSF_USER_6 (1 << 25)
#define NSF_USER_6 (1 << 25)
};
};
void
ns_mgr_init
(
struct
ns_mgr
*
,
void
*
data
,
ns_callback_t
);
void
ns_mgr_init
(
struct
ns_mgr
*
,
void
*
user_data
);
void
ns_mgr_free
(
struct
ns_mgr
*
);
void
ns_mgr_free
(
struct
ns_mgr
*
);
in
t
ns_mgr_poll
(
struct
ns_mgr
*
,
int
milli
);
time_
t
ns_mgr_poll
(
struct
ns_mgr
*
,
int
milli
);
void
ns_broadcast
(
struct
ns_mgr
*
,
ns_callback_t
,
void
*
,
size_t
);
void
ns_broadcast
(
struct
ns_mgr
*
,
ns_callback_t
,
void
*
,
size_t
);
struct
ns_connection
*
ns_next
(
struct
ns_mgr
*
,
struct
ns_connection
*
);
struct
ns_connection
*
ns_next
(
struct
ns_mgr
*
,
struct
ns_connection
*
);
struct
ns_connection
*
ns_add_sock
(
struct
ns_mgr
*
,
sock_t
sock
,
void
*
p
);
struct
ns_connection
*
ns_add_sock
(
struct
ns_mgr
*
,
sock_t
,
struct
ns_connection
*
ns_bind
(
struct
ns_mgr
*
,
const
char
*
addr
,
void
*
p
);
ns_callback_t
,
void
*
);
struct
ns_connection
*
ns_connect
(
struct
ns_mgr
*
,
const
char
*
addr
,
void
*
p
);
struct
ns_connection
*
ns_bind
(
struct
ns_mgr
*
,
const
char
*
,
ns_callback_t
,
void
*
);
struct
ns_connection
*
ns_connect
(
struct
ns_mgr
*
,
const
char
*
,
ns_callback_t
,
void
*
);
int
ns_send
(
struct
ns_connection
*
,
const
void
*
buf
,
int
len
);
int
ns_send
(
struct
ns_connection
*
,
const
void
*
buf
,
int
len
);
int
ns_printf
(
struct
ns_connection
*
,
const
char
*
fmt
,
...);
int
ns_printf
(
struct
ns_connection
*
,
const
char
*
fmt
,
...);
...
...
examples/websocket_ssl_proxy/ssl_wrapper.c
View file @
4ca4c862
...
@@ -14,22 +14,22 @@
...
@@ -14,22 +14,22 @@
// Alternatively, you can license this software under a commercial
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/products.html>.
// license, as set out in <http://cesanta.com/products.html>.
//
//
// $Date
: 2014-09-09 17:09:33 UTC
$
// $Date$
#include "net_skeleton.h"
#include "net_skeleton.h"
#include "ssl_wrapper.h"
#include "ssl_wrapper.h"
static
void
ev_handler
(
struct
ns_connection
*
nc
,
enum
ns_eve
nt
ev
,
void
*
p
)
{
static
void
ev_handler
(
struct
ns_connection
*
nc
,
i
nt
ev
,
void
*
p
)
{
const
char
*
target_addr
=
(
const
char
*
)
nc
->
mgr
->
user_data
;
const
char
*
target_addr
=
(
const
char
*
)
nc
->
mgr
->
user_data
;
struct
ns_connection
*
pc
=
(
struct
ns_connection
*
)
nc
->
connection
_data
;
struct
ns_connection
*
pc
=
(
struct
ns_connection
*
)
nc
->
user
_data
;
struct
iobuf
*
io
=
&
nc
->
recv_iobuf
;
struct
iobuf
*
io
=
&
nc
->
recv_iobuf
;
(
void
)
p
;
(
void
)
p
;
switch
(
ev
)
{
switch
(
ev
)
{
case
NS_ACCEPT
:
case
NS_ACCEPT
:
// Create a connection to the target, and interlink both connections
// Create a connection to the target, and interlink both connections
nc
->
connection_data
=
ns_connect
(
nc
->
mgr
,
target_add
r
,
nc
);
nc
->
user_data
=
ns_connect
(
nc
->
mgr
,
target_addr
,
ev_handle
r
,
nc
);
if
(
nc
->
connection
_data
==
NULL
)
{
if
(
nc
->
user
_data
==
NULL
)
{
nc
->
flags
|=
NSF_CLOSE_IMMEDIATELY
;
nc
->
flags
|=
NSF_CLOSE_IMMEDIATELY
;
}
}
break
;
break
;
...
@@ -38,9 +38,9 @@ static void ev_handler(struct ns_connection *nc, enum ns_event ev, void *p) {
...
@@ -38,9 +38,9 @@ static void ev_handler(struct ns_connection *nc, enum ns_event ev, void *p) {
// If either connection closes, unlink them and shedule closing
// If either connection closes, unlink them and shedule closing
if
(
pc
!=
NULL
)
{
if
(
pc
!=
NULL
)
{
pc
->
flags
|=
NSF_FINISHED_SENDING_DATA
;
pc
->
flags
|=
NSF_FINISHED_SENDING_DATA
;
pc
->
connection
_data
=
NULL
;
pc
->
user
_data
=
NULL
;
}
}
nc
->
connection
_data
=
NULL
;
nc
->
user
_data
=
NULL
;
break
;
break
;
case
NS_RECV
:
case
NS_RECV
:
...
@@ -64,8 +64,8 @@ void *ssl_wrapper_init(const char *local_addr, const char *target_addr,
...
@@ -64,8 +64,8 @@ void *ssl_wrapper_init(const char *local_addr, const char *target_addr,
if
(
mgr
==
NULL
)
{
if
(
mgr
==
NULL
)
{
*
err_msg
=
"malloc failed"
;
*
err_msg
=
"malloc failed"
;
}
else
{
}
else
{
ns_mgr_init
(
mgr
,
(
void
*
)
target_addr
,
ev_handler
);
ns_mgr_init
(
mgr
,
(
void
*
)
target_addr
);
if
(
ns_bind
(
mgr
,
local_addr
,
NULL
)
==
NULL
)
{
if
(
ns_bind
(
mgr
,
local_addr
,
ev_handler
,
NULL
)
==
NULL
)
{
*
err_msg
=
"ns_bind() failed: bad listening_port"
;
*
err_msg
=
"ns_bind() failed: bad listening_port"
;
ns_mgr_free
(
mgr
);
ns_mgr_free
(
mgr
);
free
(
mgr
);
free
(
mgr
);
...
...
examples/websocket_ssl_proxy/ssl_wrapper.h
View file @
4ca4c862
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
// Alternatively, you can license this software under a commercial
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/products.html>.
// license, as set out in <http://cesanta.com/products.html>.
//
//
// $Date
: 2014-09-09 17:09:33 UTC
$
// $Date$
#ifndef SSL_WRAPPER_HEADER_INCLUDED
#ifndef SSL_WRAPPER_HEADER_INCLUDED
#define SSL_WRAPPER_HEADER_INCLUDED
#define SSL_WRAPPER_HEADER_INCLUDED
...
...
examples/websocket_ssl_proxy/ws_ssl.c
View file @
4ca4c862
...
@@ -88,7 +88,7 @@ static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
...
@@ -88,7 +88,7 @@ static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
}
}
// Not a CONNECT request, serve HTML file.
// Not a CONNECT request, serve HTML file.
mg_send_file
(
conn
,
"ws_ssl.html"
);
mg_send_file
(
conn
,
"ws_ssl.html"
,
NULL
);
return
MG_MORE
;
return
MG_MORE
;
default:
default:
...
...
mongoose.c
View file @
4ca4c862
This diff is collapsed.
Click to expand it.
mongoose.h
View file @
4ca4c862
...
@@ -113,7 +113,7 @@ size_t mg_websocket_write(struct mg_connection *, int opcode,
...
@@ -113,7 +113,7 @@ size_t mg_websocket_write(struct mg_connection *, int opcode,
size_t
mg_websocket_printf
(
struct
mg_connection
*
conn
,
int
opcode
,
size_t
mg_websocket_printf
(
struct
mg_connection
*
conn
,
int
opcode
,
const
char
*
fmt
,
...);
const
char
*
fmt
,
...);
void
mg_send_file
(
struct
mg_connection
*
,
const
char
*
path
);
void
mg_send_file
(
struct
mg_connection
*
,
const
char
*
path
,
const
char
*
);
void
mg_send_file_data
(
struct
mg_connection
*
,
int
fd
);
void
mg_send_file_data
(
struct
mg_connection
*
,
int
fd
);
const
char
*
mg_get_header
(
const
struct
mg_connection
*
,
const
char
*
name
);
const
char
*
mg_get_header
(
const
struct
mg_connection
*
,
const
char
*
name
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment