Skip to content
Snippets Groups Projects
Commit 9d3fb64b authored by rswindell's avatar rswindell
Browse files

Added support for an IPv6 bool argument to the Socket() constructor:

new Socket(true) // creates an IPv6 TCP socket
new Socket(SOCK_STREAM, true) // creates an IPv6 TCP socket
new Socket(SOCK_DGRAM, true) // creates an IPv6 UDP socket
new Socket("myprot", true) // creates an IPv6 TCP socket named "myprot"
parent c6822e8e
No related branches found
No related tags found
No related merge requests found
......@@ -2271,40 +2271,49 @@ JSObject* DLLCALL js_CreateSocketObjectWithoutParent(JSContext* cx, SOCKET sock,
static JSBool
js_socket_constructor(JSContext *cx, uintN argc, jsval *arglist)
{
SOCKET sock;
JSObject *obj;
jsval *argv=JS_ARGV(cx, arglist);
int32 type=SOCK_STREAM; /* default = TCP */
int domain = AF_INET; /* default = IPv4 */
uintN i;
js_socket_private_t* p;
char* protocol=NULL;
BOOL from_descriptor=FALSE;
for(i=0;i<argc;i++) {
if(JSVAL_IS_NUMBER(argv[i])) {
if (from_descriptor) {
#ifdef WIN32
JS_ValueToECMAUint32(cx,argv[i],&sock);
#else
JS_ValueToInt32(cx,argv[i],&sock);
#endif
obj = js_CreateSocketObjectWithoutParent(cx, sock, -1);
if (obj == NULL) {
JS_ReportError(cx, "Failed to create external socket object");
return JS_FALSE;
}
JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(obj));
FREE_AND_NULL(protocol);
return JS_TRUE;
i = 0;
if(JSVAL_IS_BOOLEAN(argv[i]) && argc > 1) {
from_descriptor = JSVAL_TO_BOOLEAN(argv[i]);
i++;
if (from_descriptor) {
uint32 sock;
if(!JS_ValueToECMAUint32(cx,argv[i],&sock)) {
JS_ReportError(cx, "Failed to convert socket descriptor to uint32");
return JS_FALSE;
}
else
JS_ValueToInt32(cx,argv[i],&type);
obj = js_CreateSocketObjectWithoutParent(cx, sock, -1);
if (obj == NULL) {
JS_ReportError(cx, "Failed to create external socket object");
return JS_FALSE;
}
JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(obj));
FREE_AND_NULL(protocol);
return JS_TRUE;
}
else if(JSVAL_IS_BOOLEAN(argv[i]))
from_descriptor = TRUE;
else if(protocol==NULL) {
JSVALUE_TO_MSTRING(cx, argv[i], protocol, NULL);
HANDLE_PENDING(cx, protocol);
}
for(;i<argc;i++) {
if(JSVAL_IS_NUMBER(argv[i])) {
JS_ValueToInt32(cx,argv[i],&type);
}
else if(JSVAL_IS_BOOLEAN(argv[i])) {
if(argv[i] == JSVAL_TRUE)
domain = AF_INET6;
}
else if(JSVAL_IS_STRING(argv[i])) {
if(protocol == NULL) {
JSVALUE_TO_MSTRING(cx, argv[i], protocol, NULL);
HANDLE_PENDING(cx, protocol);
}
}
}
......@@ -2319,7 +2328,7 @@ js_socket_constructor(JSContext *cx, uintN argc, jsval *arglist)
}
memset(p,0,sizeof(js_socket_private_t));
if((p->sock=open_socket(PF_INET,type,protocol))==INVALID_SOCKET) {
if((p->sock=open_socket(domain,type,protocol))==INVALID_SOCKET) {
JS_ReportError(cx,"open_socket failed with error %d",ERROR_VALUE);
if(protocol)
free(protocol);
......@@ -2345,10 +2354,10 @@ js_socket_constructor(JSContext *cx, uintN argc, jsval *arglist)
#ifdef BUILD_JSDOCS
js_DescribeSyncObject(cx,obj,"Class used for TCP/IP socket communications",310);
js_DescribeSyncConstructor(cx,obj,"To create a new Socket object: "
"<tt>load('sockdefs.js'); var s = new Socket(<i>type</i>, <i>protocol</i>)</tt><br>"
"<tt>load('sockdefs.js'); var s = new Socket(<i>type</i>, <i>protocol</i> ,<i>ipv6</i>=false)</tt><br>"
"where <i>type</i> = <tt>SOCK_STREAM</tt> for TCP (default) or <tt>SOCK_DGRAM</tt> for UDP<br>"
"and <i>protocol</i> (optional) = the name of the protocol or service the socket is to be used for<br>"
"To create a socket from a socket descriptor: "
"To create a socket from an existing socket descriptor: "
"<tt>var s = new Socket(true, <i>descriptor</i>)</tt><br>"
"where <i>descriptor</i> is the numerical value of an existing valid socket descriptor"
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment