Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose tcp_read/ssl_read errors #713

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/colony/lua_cares.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define INET6_ADDRSTRLEN 46

uint8_t ipaddr[4] = { 0 };
uint32_t ip_dns = 0;

static void
state_cb(void *data, int s, int read, int write)
Expand Down Expand Up @@ -103,8 +104,12 @@ uint32_t tm__sync_gethostbyname (const char *domain)

struct in_addr ns1;

// get the dns server
uint32_t ip_dns = tm_net_dnsserver();
// check cache of old dns server
// if it isn't there, get the new dns server
if (ip_dns == 0) {
ip_dns = tm_net_dnsserver();
}

if (ip_dns == 0) {
// error not connected
return 1;
Expand Down
30 changes: 26 additions & 4 deletions src/colony/lua_tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,20 @@ static int l_tm_tcp_read (lua_State* L)
uint8_t buf[512];
size_t buf_len = sizeof(buf);
int err = tm_tcp_read(socket, buf, &buf_len);
(void) err;

if (err != 0) {
char strbuffer[100];
ssize_t len = (ssize_t) snprintf(strbuffer, sizeof(strbuffer), "TCP read error: %d", err);
if (len > 0) {
lua_pushlstring(L, strbuffer, len);
} else {
lua_pushstring(L, "TCP read error: [unavailable]");
}
lua_error(L);
} else {
colony_pushbuffer(L, buf, buf_len);
}

colony_pushbuffer(L, buf, buf_len);
return 1;
}

Expand Down Expand Up @@ -427,9 +438,20 @@ static int l_tm_ssl_read (lua_State* L)
uint8_t buf[20000];
size_t buf_len = sizeof(buf);
int err = tm_ssl_read(session, buf, &buf_len);
(void) err;

if (err != 0) {
char strbuffer[100];
ssize_t len = (ssize_t) snprintf(strbuffer, sizeof(strbuffer), "SSL read error: %d", err);
if (len > 0) {
lua_pushlstring(L, strbuffer, len);
} else {
lua_pushstring(L, "SSL read error: [unavailable]");
}
lua_error(L);
} else {
colony_pushbuffer(L, buf, buf_len);
}

colony_pushbuffer(L, buf, buf_len);
return 1;
}

Expand Down
48 changes: 33 additions & 15 deletions src/colony/modules/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,24 @@ TCPSocket.prototype.connect = function (/*options | [port], [host], [cb]*/) {
self.emit('error', new Error("ENETUNREACH: Wifi is not connected"));
// force the cleanup
self.destroy();
return self.__close(); // need to call close otherwise we keep listening for the tcp-close event
return self.__close(true); // need to call close otherwise we keep listening for the tcp-close event
}

if (ret < 0) {
var closeRet = tm.tcp_close(self.socket); // returns -57 if socket is already closed
if (closeRet < 0 && closeRet != -tm.ENOTCONN){
// couldn't close socket, throw an error
// failed to connect, stay silent
self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+closeRet));
self.destroy();
return self.__close(false);
return self.__close(true);
}

if (retries > 3) {
self.emit('error', new Error('ENOENT Cannot connect to ' + ip + ' Got: err'+ret));
// force the cleanup
self.destroy();
return self.__close();
return self.__close(true);
} else {
retries++;
setTimeout(function(){
Expand All @@ -221,7 +222,7 @@ TCPSocket.prototype.connect = function (/*options | [port], [host], [cb]*/) {

// force the close
self.destroy();
return self.__close();
return self.__close(true);
} else {
doConnect();
}
Expand Down Expand Up @@ -254,13 +255,13 @@ TCPSocket.prototype.connect = function (/*options | [port], [host], [cb]*/) {

tm.tcp_readable(self.socket);
self.destroy();
self.__close();
self.__close(true);
return;
} else {
// close socket
self.emit('error', new Error('Could not validate SSL request (error ' + ret + ')'));
self.destroy();
self.__close();
self.__close(true);
return;
}
}
Expand Down Expand Up @@ -372,7 +373,7 @@ TCPSocket.prototype.__send = function (cb) {
if (this._queueEnd) {
// close actual socket
this._queueEnd = false;
this.__close();
this.__close(true);
}
return cb ? cb() : false;
}
Expand Down Expand Up @@ -425,9 +426,17 @@ TCPSocket.prototype.__readSocket = function(restartTimeout) {
var arr = [], flag = 0;
while (self.socket != null && (flag = tm.tcp_readable(self.socket)) > 0) {
if (self._ssl) {
var data = tm.ssl_read(self._ssl);
try {
var data = tm.ssl_read(self._ssl);
} catch(e){
self.emit("error", typeof e == 'string' ? new Error(e) : e);
}
} else {
var data = tm.tcp_read(self.socket);
try {
var data = tm.tcp_read(self.socket);
} catch(e){
self.emit("error", typeof e == 'string' ? new Error(e) : e);
}
}
if (!data || data.length == 0) {
break;
Expand Down Expand Up @@ -463,11 +472,10 @@ TCPSocket.prototype.__close = function (tryToClose) {
function closeSocket(){
if (self.socket === null) return;
var ret = tm.tcp_close(self.socket);

if (ret < 0 && ret != -tm.ENOTCONN) { // -57 is inactive, socket has already been closed
if (retries > 3) {
// tried 3 times and couldn't close, error out
self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+ret));
// tried 3 times and couldn't close
// nothing for the user to do if this occurs, close the socket and user should re-request connection
self.emit('close');
} else {
retries++;
Expand All @@ -490,6 +498,10 @@ TCPSocket.prototype.destroy = TCPSocket.prototype.close = function () {
if (this._destroy) return;

this._destroy = true;
if (this._secure) {
// free ssl context
tm.ssl_context_free(this._ssl_ctx);
}

var self = this;
setImmediate(function () {
Expand All @@ -503,7 +515,7 @@ TCPSocket.prototype.destroy = TCPSocket.prototype.close = function () {
if (self._outgoing.length || self._sending) {
self._queueEnd = true;
} else {
self.__close();
self.__close(true);
}
}
self.removeAllListeners();
Expand Down Expand Up @@ -615,9 +627,15 @@ TCPServer.prototype.listen = function (port, host, backlog, cb) {
clientsocket.remotePort = port;
clientsocket.__listen();
self.emit('connection', clientsocket);
}

setTimeout(poll, 10);
// do not poll if we're not connected
// this also gives time for the 'disconnect' event to fire from the wifi-cc3000 lib
// user should listen for require('wifi-cc3000').on('disconnect') event and
// reissue the request
setTimeout(poll, 10);
} else {
self.emit('error', new Error("Cannot listen on a bad socket %d", client));
}
}
return this;
};
Expand Down
1 change: 1 addition & 0 deletions src/tm_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int tm_ssl_read (tm_ssl_session_t ssl, uint8_t *buf, size_t *buf_len)
uint8_t *read_buf;
int res = ssl_read(ssl, &read_buf);
if (res < 0) {
TM_DEBUG("ssl read res %d", res);
*buf_len = 0;
return res;
} else {
Expand Down