Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add setBroadcase support to node 0.5.10 and libuv
From 8ebf3363017dc223da153e69b372749ace222244 Mon Sep 17 00:00:00 2001
From: Dan VerWeire <dverweire@gmail.com>
Date: Thu, 27 Oct 2011 10:48:10 -0400
Subject: [PATCH 1/2] Add support for uv_udp_set_broadcast
---
deps/uv/include/uv.h | 12 ++++++++++++
deps/uv/src/unix/udp.c | 16 ++++++++++++++++
2 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/deps/uv/include/uv.h b/deps/uv/include/uv.h
index 7bee299..069a2cd 100644
--- a/deps/uv/include/uv.h
+++ b/deps/uv/include/uv.h
@@ -582,6 +582,18 @@ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
const char* interface_addr, uv_membership membership);
/*
+ * Set broadcast on or off
+ *
+ * Arguments:
+ * handle UDP handle. Should have been initialized with `uv_udp_init`.
+ * on 1 for on, 0 for off
+ *
+ * Returns:
+ * 0 on success, -1 on error.
+ */
+int uv_udp_set_broadcast(uv_udp_t* handle, int on);
+
+/*
* Send data. If the socket has not previously been bound with `uv_udp_bind`
* or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
* and a random port number.
diff --git a/deps/uv/src/unix/udp.c b/deps/uv/src/unix/udp.c
index 15cd609..bb4df9a 100644
--- a/deps/uv/src/unix/udp.c
+++ b/deps/uv/src/unix/udp.c
@@ -495,6 +495,22 @@ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
}
+int uv_udp_set_broadcast(uv_udp_t* handle, int on) {
+ /*if (setsockopt(handle->fd, IPPROTO_IP, 15, &on, sizeof on) == -1) {*/
+ if (setsockopt(handle->fd, IPPROTO_IP, SO_REUSEADDR, &on, sizeof on) == -1) {
+ uv__set_sys_error(handle->loop, errno);
+ return -1;
+ }
+
+ if (setsockopt(handle->fd, IPPROTO_IP, SO_BROADCAST, &on, sizeof on) == -1) {
+ uv__set_sys_error(handle->loop, errno);
+ return -1;
+ }
+
+ return 0;
+}
+
+
int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
int* namelen) {
socklen_t socklen;
--
1.7.5.4
From 12fce12514253bf5ae11ed3e3fcdd108e195b95c Mon Sep 17 00:00:00 2001
From: Dan VerWeire <dverweire@gmail.com>
Date: Thu, 27 Oct 2011 10:48:38 -0400
Subject: [PATCH 2/2] Add support for setBroadcast
---
lib/dgram.js | 6 +++++-
src/udp_wrap.cc | 17 +++++++++++++++++
2 files changed, 22 insertions(+), 1 deletions(-)
diff --git a/lib/dgram.js b/lib/dgram.js
index 4356447..c413a41 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -223,7 +223,11 @@ Socket.prototype.address = function() {
Socket.prototype.setBroadcast = function(arg) {
- throw new Error('not yet implemented');
+ console.log("hello");
+ console.log(this._handle.setBroadcast((arg) ? 1 : 0));
+ //return this._handle.setBroadcast((arg) ? 1 : 0);
+
+ //throw new Error('not yet implemented');
};
diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc
index eb9a484..dfa8a05 100644
--- a/src/udp_wrap.cc
+++ b/src/udp_wrap.cc
@@ -60,6 +60,7 @@ public:
static Handle<Value> RecvStart(const Arguments& args);
static Handle<Value> RecvStop(const Arguments& args);
static Handle<Value> GetSockName(const Arguments& args);
+ static Handle<Value> SetBroadcast(const Arguments& args);
private:
UDPWrap(Handle<Object> object);
@@ -113,6 +114,7 @@ void UDPWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
+ NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
target->Set(String::NewSymbol("UDP"),
Persistent<FunctionTemplate>::New(t)->GetFunction());
@@ -170,6 +172,21 @@ Handle<Value> UDPWrap::Bind6(const Arguments& args) {
return DoBind(args, AF_INET6);
}
+Handle<Value> UDPWrap::SetBroadcast(const Arguments& args) {
+ HandleScope scope;
+ UNWRAP
+ printf("hello1");
+ assert(args.Length() == 1);
+ printf("hello2");
+ int on = args[0]->Uint32Value();
+ printf("hello3");
+ int r = uv_udp_set_broadcast(&wrap->handle_, on);
+ printf("hello4");
+ if (r)
+ SetErrno(uv_last_error(uv_default_loop()));
+ printf("hello5");
+ return scope.Close(Integer::New(r));
+}
Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
HandleScope scope;
--
1.7.5.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.