Skip to content

Commit

Permalink
shorten variable name to prevent shadowing of a global, minor compile…
Browse files Browse the repository at this point in the history
… fixes
  • Loading branch information
mbalmer committed Jan 17, 2016
1 parent 59523ea commit 8990abb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 79 deletions.
155 changes: 77 additions & 78 deletions luawebsocket.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2015 by Micro Systems Marc Balmer, CH-5073 Gipf-Oberfrick
* Copyright (c) 2014 - 2016 by Micro Systems Marc Balmer, CH-5073 Gipf-Oberfrick
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -51,13 +51,13 @@
static int
websocket_accept(lua_State *L)
{
WEBSOCKET *websocket;
WEBSOCKET *websock;
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
int socket, ret;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
socket = accept(websocket->socket, (struct sockaddr *)&addr, &len);
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
socket = accept(websock->socket, (struct sockaddr *)&addr, &len);

if (socket == -1) {
return luaL_error(L, "error accepting connection");
Expand All @@ -71,8 +71,8 @@ websocket_accept(lua_State *L)

acc->socket = socket;

if (websocket->ctx != NULL) {
if ((acc->ssl = SSL_new(websocket->ctx)) == NULL)
if (websock->ctx != NULL) {
if ((acc->ssl = SSL_new(websock->ctx)) == NULL)
return luaL_error(L, "error creating SSL "
"context");

Expand All @@ -94,7 +94,7 @@ websocket_bind(lua_State *L)
int fd, error, optval;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
const char *port, *host, *cert;
WEBSOCKET *websocket;
WEBSOCKET *websock;

cert = NULL;

Expand Down Expand Up @@ -139,21 +139,20 @@ websocket_bind(lua_State *L)
return luaL_error(L, "listen error");

/* XXX seed_prng(); */
websocket = lua_newuserdata(L, sizeof(WEBSOCKET));
memset(websocket, 0, sizeof(WEBSOCKET));
websock = lua_newuserdata(L, sizeof(WEBSOCKET));
memset(websock, 0, sizeof(WEBSOCKET));

websocket->socket = fd;
websock->socket = fd;

if (cert != NULL) {
SSL_library_init();
SSL_load_error_strings();
if ((websocket->ctx = SSL_CTX_new(SSLv23_method())) == NULL)
if ((websock->ctx = SSL_CTX_new(SSLv23_method())) == NULL)
return luaL_error(L, "error creating new SSL context");

if (SSL_CTX_use_certificate_chain_file(websocket->ctx, cert)
!= 1)
if (SSL_CTX_use_certificate_chain_file(websock->ctx, cert) != 1)
return luaL_error(L, "error loading certificate");
if (SSL_CTX_use_PrivateKey_file(websocket->ctx, cert,
if (SSL_CTX_use_PrivateKey_file(websock->ctx, cert,
SSL_FILETYPE_PEM) != 1)
return luaL_error(L, "error loading private key");
}
Expand All @@ -167,36 +166,36 @@ websocket_handshake(lua_State *L)
{
struct handshake hs;
size_t nread;
WEBSOCKET *websocket;
WEBSOCKET *websock;
char *buf;

nullHandshake(&hs);
websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);

buf = malloc(BUFSIZE);
if (websocket->ssl)
nread = SSL_read(websocket->ssl, buf, BUFSIZE);
if (websock->ssl)
nread = SSL_read(websock->ssl, buf, BUFSIZE);
else
nread = recv(websocket->socket, buf, BUFSIZE, 0);
nread = recv(websock->socket, buf, BUFSIZE, 0);
buf[nread] = '\0';

if (wsParseHandshake((unsigned char *)buf, nread, &hs) ==
WS_OPENING_FRAME) {
if (!strcmp(hs.resource, luaL_checkstring(L, 2))) {
wsGetHandshakeAnswer(&hs, (unsigned char *)buf, &nread);
freeHandshake(&hs);
if (websocket->ssl)
SSL_write(websocket->ssl, buf, nread);
if (websock->ssl)
SSL_write(websock->ssl, buf, nread);
else
send(websocket->socket, buf, nread, 0);
send(websock->socket, buf, nread, 0);
buf[nread] = '\0';
lua_pushboolean(L, 1);
} else {
nread = sprintf(buf, "HTTP/1.1 404 Not Found\r\n\r\n");
if (websocket->ssl)
SSL_write(websocket->ssl, buf, nread);
if (websock->ssl)
SSL_write(websock->ssl, buf, nread);
else
send(websocket->socket, buf, nread, 0);
send(websock->socket, buf, nread, 0);
lua_pushnil(L);
}
} else {
Expand All @@ -205,10 +204,10 @@ websocket_handshake(lua_State *L)
"%s%s\r\n\r\n",
versionField,
version);
if (websocket->ssl)
SSL_write(websocket->ssl, buf, nread);
if (websock->ssl)
SSL_write(websock->ssl, buf, nread);
else
send(websocket->socket, buf, nread, 0);
send(websock->socket, buf, nread, 0);
lua_pushnil(L);
}
free(buf);
Expand All @@ -218,24 +217,24 @@ websocket_handshake(lua_State *L)
static int
websocket_recv(lua_State *L)
{
WEBSOCKET *websocket;
WEBSOCKET *websock;
char *buf;
unsigned char *data;
size_t nread, len, datasize;
int type;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);

buf = malloc(BUFSIZE);

nread = len = 0;
type = WS_INCOMPLETE_FRAME;
do {
if (websocket->ssl)
nread = SSL_read(websocket->ssl, buf + len,
if (websock->ssl)
nread = SSL_read(websock->ssl, buf + len,
BUFSIZE - len);
else
nread = recv(websocket->socket, buf + len,
nread = recv(websock->socket, buf + len,
BUFSIZE - len, 0);
if (nread <= 0) {
lua_pushnil(L);
Expand All @@ -249,25 +248,25 @@ websocket_recv(lua_State *L)
case WS_CLOSING_FRAME:
wsMakeFrame(NULL, 0, (unsigned char *)buf, &datasize,
WS_CLOSING_FRAME);
if (websocket->ssl) {
SSL_write(websocket->ssl, buf, datasize);
SSL_shutdown(websocket->ssl);
SSL_free(websocket->ssl);
websocket->ssl = NULL;
if (websock->ssl) {
SSL_write(websock->ssl, buf, datasize);
SSL_shutdown(websock->ssl);
SSL_free(websock->ssl);
websock->ssl = NULL;
} else {
send(websocket->socket, buf, datasize, 0);
close(websocket->socket);
websocket->socket = -1;
send(websock->socket, buf, datasize, 0);
close(websock->socket);
websock->socket = -1;
}
lua_pushnil(L);
break;
case WS_PING_FRAME:
wsMakeFrame(NULL, 0, (unsigned char *)buf, &datasize,
WS_PONG_FRAME);
if (websocket->ssl)
SSL_write(websocket->ssl, buf, datasize);
if (websock->ssl)
SSL_write(websock->ssl, buf, datasize);
else
send(websocket->socket, buf, datasize, 0);
send(websock->socket, buf, datasize, 0);
len = 0;
type = WS_INCOMPLETE_FRAME;
break;
Expand All @@ -290,71 +289,71 @@ websocket_send(lua_State *L)
char *buf;
const char *data;
size_t datasize, framesize;
WEBSOCKET *websocket;
WEBSOCKET *websock;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
buf = malloc(BUFSIZE);

data = luaL_checklstring(L, 2, &datasize);
wsMakeFrame((unsigned char *)data, datasize, (unsigned char *)buf,
wsMakeFrame((const uint8_t *)data, datasize, (unsigned char *)buf,
&framesize, WS_TEXT_FRAME);
if (websocket->ssl)
SSL_write(websocket->ssl, buf, framesize);
if (websock->ssl)
SSL_write(websock->ssl, buf, framesize);
else
send(websocket->socket, buf, framesize, 0);
send(websock->socket, buf, framesize, 0);
free(buf);
return 0;
}

static int
websocket_socket(lua_State *L)
{
WEBSOCKET *websocket;
WEBSOCKET *websock;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
lua_pushinteger(L, websocket->socket);
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
lua_pushinteger(L, websock->socket);
return 1;
}

static int
websocket_close(lua_State *L)
{
WEBSOCKET *websocket;
WEBSOCKET *websock;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
if (websocket->ssl != NULL) {
SSL_set_shutdown(websocket->ssl,
websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
if (websock->ssl != NULL) {
SSL_set_shutdown(websock->ssl,
SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
SSL_free(websocket->ssl);
websocket->ssl = NULL;
} else if (websocket->socket != -1) {
close(websocket->socket);
websocket->socket = -1;
SSL_free(websock->ssl);
websock->ssl = NULL;
} else if (websock->socket != -1) {
close(websock->socket);
websock->socket = -1;
}
if (websocket->ctx != NULL) {
SSL_CTX_free(websocket->ctx);
websocket->ctx = NULL;
if (websock->ctx != NULL) {
SSL_CTX_free(websock->ctx);
websock->ctx = NULL;
}
return 0;
}

static int
websocket_shutdown(lua_State *L)
{
WEBSOCKET *websocket;

websocket = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
if (websocket->ssl != NULL) {
SSL_shutdown(websocket->ssl);
SSL_free(websocket->ssl);
websocket->ssl = NULL;
} else if (websocket->socket != -1) {
close(websocket->socket);
websocket->socket = -1;
WEBSOCKET *websock;

websock = luaL_checkudata(L, 1, WEBSOCKET_METATABLE);
if (websock->ssl != NULL) {
SSL_shutdown(websock->ssl);
SSL_free(websock->ssl);
websock->ssl = NULL;
} else if (websock->socket != -1) {
close(websock->socket);
websock->socket = -1;
}
if (websocket->ctx != NULL) {
SSL_CTX_free(websocket->ctx);
websocket->ctx = NULL;
if (websock->ctx != NULL) {
SSL_CTX_free(websock->ctx);
websock->ctx = NULL;
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ wsGetHandshakeAnswer(const struct handshake *hs, uint8_t *outFrame,
b64 = base64(mdbuf, mdlen);
BIO_free(bio);

int written = sprintf((char *)outFrame,
size_t written = sprintf((char *)outFrame,
"HTTP/1.1 101 Switching Protocols\r\n"
"%s%s\r\n"
"%s%s\r\n"
Expand Down

0 comments on commit 8990abb

Please sign in to comment.