...
 
Commits (3)
......@@ -75,7 +75,7 @@ BOOL DLLCALL js_argc(JSContext *cx, uintN argc, uintN min)
static JSBool js_system_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
jsval idval;
char err[128];
char err[256];
jsint tiny;
JSString* js_str;
......@@ -3821,6 +3821,46 @@ js_socket_select(JSContext *cx, uintN argc, jsval *arglist)
}
}
static JSBool
js_socket_strerror(JSContext *cx, uintN argc, jsval *arglist)
{
if(!js_argc(cx, argc, 1))
return JS_FALSE;
jsval *argv = JS_ARGV(cx, arglist);
int32 err = 0;
if(!JS_ValueToInt32(cx, argv[0], &err))
return JS_FALSE;
char str[256];
JSString* js_str;
if((js_str = JS_NewStringCopyZ(cx, socket_strerror(err, str, sizeof(str)))) == NULL)
return JS_FALSE;
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
return JS_TRUE;
}
static JSBool
js_strerror(JSContext *cx, uintN argc, jsval *arglist)
{
if(!js_argc(cx, argc, 1))
return JS_FALSE;
jsval *argv = JS_ARGV(cx, arglist);
int32 err = 0;
if(!JS_ValueToInt32(cx, argv[0], &err))
return JS_FALSE;
char str[256];
JSString* js_str;
if((js_str = JS_NewStringCopyZ(cx, safe_strerror(err, str, sizeof(str)))) == NULL)
return JS_FALSE;
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
return JS_TRUE;
}
static JSBool
js_mkdir(JSContext *cx, uintN argc, jsval *arglist)
{
......@@ -4702,11 +4742,19 @@ static jsSyncMethodSpec js_global_functions[] = {
,JSDOCSTR("checks an array of socket objects or descriptors for read or write ability (default is <i>read</i>), "
"default timeout value is 0.0 seconds (immediate timeout), "
"returns an array of 0-based index values into the socket array, representing the sockets that were ready for reading or writing, or <i>null</i> on error. "
"If multiple arrays of sockets are passed, they are presumet to be in the order of read, write, and except. In this case, the write parameter is ignored "
"If multiple arrays of sockets are passed, they are presumed to be in the order of read, write, and except. In this case, the write parameter is ignored "
"and an object is returned instead with up to three properties \"read\", \"write\", and \"except\", corresponding to the passed arrays. Empty passed "
"arrays will not have a corresponding property in the returned object.")
,311
},
{"socket_strerror", js_socket_strerror, 1, JSTYPE_STRING, JSDOCSTR("error")
,JSDOCSTR("get the description(string representation) of a numeric socket error value (e.g. <tt>socket_errno</tt>)")
,31802
},
{"strerror", js_strerror, 1, JSTYPE_STRING, JSDOCSTR("error")
,JSDOCSTR("get the description(string representation) of a numeric system error value (e.g. <tt>errno</tt>)")
,31802
},
{"mkdir", js_mkdir, 1, JSTYPE_BOOLEAN, JSDOCSTR("path/directory")
,JSDOCSTR("make a directory")
,310
......
......@@ -270,7 +270,7 @@ static int close_socket(SOCKET sock)
if(startup!=NULL && startup->socket_open!=NULL)
startup->socket_open(startup->cbdata,FALSE);
if(result!=0)
lprintf(LOG_WARNING,"%04d !ERROR %d (%s) closing socket",sock, ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)));
lprintf(LOG_WARNING,"%04d !ERROR %d closing socket: %s",sock, ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)));
return(result);
}
......@@ -1657,7 +1657,7 @@ static void cleanup(int code)
if(WSAInitialized) {
char err[128];
if(WSACleanup()!=0)
lprintf(LOG_ERR,"0000 !WSACleanup ERROR %d (%s)",ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)));
lprintf(LOG_ERR,"0000 !WSACleanup ERROR %d: %s",ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)));
WSAInitialized = FALSE;
}
#endif
......@@ -1704,15 +1704,15 @@ void service_udp_sock_cb(SOCKET sock, void *cbdata)
/* We need to set the REUSE ADDRESS socket option */
optval=TRUE;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&optval,sizeof(optval))!=0) {
lprintf(LOG_ERR,"%04d !ERROR %d (%s) setting %s socket option"
,sock, ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)), serv->protocol);
lprintf(LOG_ERR,"%04d !ERROR %d setting %s socket option: %s"
,sock, ERROR_VALUE, serv->protocol, socket_strerror(socket_errno, err, sizeof(err)));
close_socket(sock);
return;
}
#ifdef BSD
if(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char*)&optval,sizeof(optval))!=0) {
lprintf(LOG_ERR,"%04d !ERROR %d (%s) setting %s socket option"
,sock, ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)), serv->protocol);
lprintf(LOG_ERR,"%04d !ERROR %d setting %s socket option: %s"
,sock, ERROR_VALUE, serv->protocol, socket_strerror(socket_errno, err, sizeof(err)));
close_socket(sock);
return;
}
......@@ -2010,7 +2010,8 @@ void DLLCALL services_thread(void* arg)
else if(ERROR_VALUE == ENOTSOCK)
lprintf(LOG_NOTICE,"0000 Services sockets closed");
else
lprintf(LOG_WARNING,"0000 !ERROR %d (%s) selecting sockets",ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
lprintf(LOG_WARNING,"0000 !ERROR %d selecting sockets: %s"
, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
continue;
}
......@@ -2041,7 +2042,7 @@ void DLLCALL services_thread(void* arg)
,&client_addr.addr, &client_addr_len);
if(udp_len<1) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_WARNING,"%04d %s !ERROR %d (%s) recvfrom failed"
lprintf(LOG_WARNING,"%04d %s !ERROR %d recvfrom failed: %s"
,service[i].set->socks[j].sock, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
continue;
}
......@@ -2049,7 +2050,7 @@ void DLLCALL services_thread(void* arg)
if((client_socket = open_socket(service[i].set->socks[j].domain, SOCK_DGRAM, &service[i]))
==INVALID_SOCKET) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_ERR,"%04d %s !ERROR %d (%s) opening socket"
lprintf(LOG_ERR,"%04d %s !ERROR %d opening socket: %s"
,service[i].set->socks[j].sock, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
continue;
}
......@@ -2062,7 +2063,7 @@ void DLLCALL services_thread(void* arg)
if(setsockopt(client_socket,SOL_SOCKET,SO_REUSEADDR
,(char*)&optval,sizeof(optval))!=0) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_ERR,"%04d %s !ERROR %d (%s) setting socket option"
lprintf(LOG_ERR,"%04d %s !ERROR %d setting socket option: %s"
,client_socket, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
close_socket(client_socket);
continue;
......@@ -2071,7 +2072,7 @@ void DLLCALL services_thread(void* arg)
if(setsockopt(client_socket,SOL_SOCKET,SO_REUSEPORT
,(char*)&optval,sizeof(optval))!=0) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_ERR,"%04d %s !ERROR %d (%s) setting socket option"
lprintf(LOG_ERR,"%04d %s !ERROR %d setting socket option: %s"
,client_socket, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
close_socket(client_socket);
continue;
......@@ -2083,16 +2084,16 @@ void DLLCALL services_thread(void* arg)
result=bind(client_socket, &addr.addr, addr_len);
if(result==SOCKET_ERROR) {
/* Failed to re-bind to same port number, use user port */
lprintf(LOG_NOTICE,"%04d %s ERROR %d (%s) re-binding socket to port %u failed, "
"using user port"
,client_socket, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)), service[i].port);
lprintf(LOG_NOTICE,"%04d %s ERROR %d re-binding socket to port %u failed, "
"using user port: %s"
,client_socket, service[i].protocol, ERROR_VALUE, service[i].port, socket_strerror(socket_errno,error,sizeof(error)));
inet_setaddrport(&addr, 0);
result=bind(client_socket, (struct sockaddr *) &addr, addr_len);
}
if(result!=0) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_ERR,"%04d %s !ERROR %d (%s) re-binding socket to port %u"
,client_socket, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)), service[i].port);
lprintf(LOG_ERR,"%04d %s !ERROR %d re-binding socket to port %u: %s"
,client_socket, service[i].protocol, ERROR_VALUE, service[i].port, socket_strerror(socket_errno,error,sizeof(error)));
close_socket(client_socket);
continue;
}
......@@ -2101,7 +2102,7 @@ void DLLCALL services_thread(void* arg)
if(connect(client_socket
,(struct sockaddr *)&client_addr, client_addr_len)!=0) {
FREE_AND_NULL(udp_buf);
lprintf(LOG_ERR,"%04d %s !ERROR %d (%s) connect failed"
lprintf(LOG_ERR,"%04d %s !ERROR %d connect failed: %s"
,client_socket, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
close_socket(client_socket);
continue;
......@@ -2115,7 +2116,7 @@ void DLLCALL services_thread(void* arg)
lprintf(LOG_NOTICE,"%04d %s socket closed while listening"
,service[i].set->socks[j].sock, service[i].protocol);
else
lprintf(LOG_WARNING,"%04d %s !ERROR %d (%s) accepting connection"
lprintf(LOG_WARNING,"%04d %s !ERROR %d accepting connection: %s"
,service[i].set->socks[j].sock, service[i].protocol, ERROR_VALUE, socket_strerror(socket_errno,error,sizeof(error)));
#ifdef _WIN32
if(WSAGetLastError()==WSAENOBUFS) /* recycle (re-init WinSock) on this error */
......
......@@ -153,9 +153,9 @@ BOOL DLLCALL xpms_add(struct xpms_set *xpms_set, int domain, int type,
if(type != SOCK_DGRAM) {
if(listen(xpms_set->socks[xpms_set->sock_count].sock, SOMAXCONN)==-1) {
if(xpms_set->lprintf)
xpms_set->lprintf(LOG_WARNING, "%04d !%s ERROR %d (%s) listening on port %d"
xpms_set->lprintf(LOG_WARNING, "%04d !%s ERROR %d listening on port %d: %s"
,xpms_set->socks[xpms_set->sock_count].sock, prot, ERROR_VALUE
,socket_strerror(socket_errno,err,sizeof(err)), port);
,port, socket_strerror(socket_errno,err,sizeof(err)));
closesocket(xpms_set->socks[xpms_set->sock_count].sock);
FREE_AND_NULL(xpms_set->socks[xpms_set->sock_count].address);
FREE_AND_NULL(xpms_set->socks[xpms_set->sock_count].prot);
......
......@@ -352,7 +352,7 @@ int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen
,int (*lprintf)(int level, const char *fmt, ...))
{
char port_str[128];
char err[128];
char err[256];
int result=-1;
uint i;
......@@ -365,7 +365,7 @@ int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen
break;
if(lprintf!=NULL)
lprintf(i<retries ? LOG_WARNING:LOG_CRIT
,"%04d !ERROR %d (%s) binding %s socket%s", s, ERROR_VALUE, socket_strerror(socket_errno, err, sizeof(err)), prot, port_str);
,"%04d !ERROR %d binding %s socket%s: %s", s, ERROR_VALUE, prot, port_str, socket_strerror(socket_errno, err, sizeof(err)));
if(i<retries) {
if(lprintf!=NULL)
lprintf(LOG_WARNING,"%04d Will retry in %u seconds (%u of %u)"
......@@ -509,13 +509,14 @@ DLLEXPORT char* socket_strerror(int error_number, char* buf, size_t buflen)
buf[buflen - 1] = 0;
if(error_number > 0 && error_number < WSABASEERR)
error_number += WSABASEERR;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, // dwFlags
if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, // dwFlags
NULL, // lpSource
error_number, // dwMessageId
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // dwLanguageId
buf,
buflen,
NULL);
NULL))
safe_snprintf(buf, buflen, "Error %d getting error description", GetLastError());
truncsp(buf);
return buf;
#else
......