* [PATCH] Remove Scope IDs from IPv6 addresses.
@ 2013-09-10 15:17 Hleb Valoshka
2013-09-10 18:35 ` Eric Wong
0 siblings, 1 reply; 4+ messages in thread
From: Hleb Valoshka @ 2013-09-10 15:17 UTC (permalink / raw)
To: kgio
Scoped ipv6 addresses are defined in rfc4007.
Ruby doesn't support them yet and it's unknown whether it will
(see http://bugs.ruby-lang.org/issues/8464).
So we just remove scope ids.
---
ext/raindrops/linux_inet_diag.c | 49 ++++++++++++++++++++++++++++++++++++---
1 file changed, 46 insertions(+), 3 deletions(-)
diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c
index cd4a876..3948c33 100644
--- a/ext/raindrops/linux_inet_diag.c
+++ b/ext/raindrops/linux_inet_diag.c
@@ -134,17 +134,54 @@ static int st_free_data(st_data_t key, st_data_t value, st_data_t ignored)
return ST_DELETE;
}
+/*
+ * call-seq:
+ * remove_scope_id(ipv4_or_ipv6_address)
+ *
+ * Returns copy of IP address with Scope ID removed,
+ * if address has it (only IPv6 actually may have it).
+ *
+ * NOTE: free() must be called on its result.
+ */
+static char* remove_scope_id(const char * addr)
+{
+ char *newaddr, *t;
+
+ newaddr = strdup(addr);
+ if (newaddr == NULL)
+ perror("strdup");
+
+ t = strchr(newaddr, '%');
+
+ if (t != NULL) {
+ *t = '\0';
+ t = strchr(t+1, ']');
+ if (t == NULL)
+ fprintf(stderr, "Bad IPv6 address: %s!\n", addr);
+ else
+ strcat(newaddr, t);
+ }
+
+ return newaddr;
+}
+
static int st_to_hash(st_data_t key, st_data_t value, VALUE hash)
{
struct listen_stats *stats = (struct listen_stats *)value;
if (stats->listener_p) {
- VALUE k = rb_str_new2((const char *)key);
- VALUE v = rb_listen_stats(stats);
+ VALUE k, v;
+ char *xkey = remove_scope_id((const char *)key);
+ if (xkey == NULL)
+ goto get_out;
+ k = rb_str_new2(xkey);
+ v = rb_listen_stats(stats);
+ free(xkey);
OBJ_FREEZE(k);
rb_hash_aset(hash, k, v);
}
+get_out:
return st_free_data(key, value, 0);
}
@@ -153,7 +190,12 @@ static int st_AND_hash(st_data_t key, st_data_t value, VALUE hash)
struct listen_stats *stats = (struct listen_stats *)value;
if (stats->listener_p) {
- VALUE k = rb_str_new2((const char *)key);
+ VALUE k;
+ char *xkey = remove_scope_id((const char *)key);
+ if (xkey == NULL)
+ goto get_out;
+ k = rb_str_new2(xkey);
+ free(xkey);
if (rb_hash_lookup(hash, k) == Qtrue) {
VALUE v = rb_listen_stats(stats);
@@ -161,6 +203,7 @@ static int st_AND_hash(st_data_t key, st_data_t value, VALUE hash)
rb_hash_aset(hash, k, v);
}
}
+get_out:
return st_free_data(key, value, 0);
}
--
1.7.10.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] Remove Scope IDs from IPv6 addresses.
2013-09-10 15:17 Hleb Valoshka
@ 2013-09-10 18:35 ` Eric Wong
2013-09-10 18:37 ` Eric Wong
0 siblings, 1 reply; 4+ messages in thread
From: Eric Wong @ 2013-09-10 18:35 UTC (permalink / raw)
To: kgio
Hleb Valoshka <375gnu@gmail.com> wrote:
> Scoped ipv6 addresses are defined in rfc4007.
> Ruby doesn't support them yet and it's unknown whether it will
> (see http://bugs.ruby-lang.org/issues/8464).
> So we just remove scope ids.
Thanks. Comments inline.
> --- a/ext/raindrops/linux_inet_diag.c
> +++ b/ext/raindrops/linux_inet_diag.c
> @@ -134,17 +134,54 @@ static int st_free_data(st_data_t key, st_data_t value, st_data_t ignored)
> +static char* remove_scope_id(const char * addr)
> +{
> + char *newaddr, *t;
> +
> + newaddr = strdup(addr);
> + if (newaddr == NULL)
> + perror("strdup");
We'll segfault on strchr below if we perrored above. If we continue
using strdup, I would just use ruby_strdup() (works on both MRI/rbx).
But I'd rather avoid *strdup entirely. This is unlikely to
remove, right?
> + t = strchr(newaddr, '%');
> +
> + if (t != NULL) {
> + *t = '\0';
> + t = strchr(t+1, ']');
> + if (t == NULL)
> + fprintf(stderr, "Bad IPv6 address: %s!\n", addr);
Better to use rb_warn/rb_warning.
These are preferable since they allow assigning $stderr to StringIO and
such. But if we don't use strdup, maybe we could raise instead.
> + else
> + strcat(newaddr, t);
> + }
> +
> + return newaddr;
> +}
Perhaps something like this (totally untested, likely off-by-one errors)
static VALUE remove_scope_id(const char *addr)
{
VALUE rv = rb_str_new2(addr);
long len = RSTRING_LEN(rv);
char *ptr = RSTRING_PTR(rv);
char *pct = memchr(ptr, '%', len);
/*
* remove scoped portion
* Ruby equivalent: rv.sub!(/%([^\]]*)\]/, "]")
*/
if (pct) {
long newlen = pct - ptr;
char *rbracket = memchr(pct, ']', len - newlen);
if (rbracket) {
size_t move = len - (rbracket - ptr);
memmove(pct, rbracket, move);
newlen += move;
rb_str_set_len(rv, newlen);
} else {
/* perhaps raise here? */
rb_warn("Bad IPv6 address: %s", addr);
}
}
return rv;
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Remove Scope IDs from IPv6 addresses.
2013-09-10 18:35 ` Eric Wong
@ 2013-09-10 18:37 ` Eric Wong
0 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2013-09-10 18:37 UTC (permalink / raw)
To: kgio
I just noticed this is the wrong list, please redirect all comments
to raindrops@librelist.org Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] Remove Scope IDs from IPv6 addresses.
[not found] <CAAB-Kcmzm4Gr58k2YexCD5+4QYzZJf3HydDRHcSAsoEEut_OFw@mail.gmail.com>
@ 2013-09-12 13:31 ` Hleb Valoshka
0 siblings, 0 replies; 4+ messages in thread
From: Hleb Valoshka @ 2013-09-12 13:31 UTC (permalink / raw)
To: kgio
Scoped ipv6 addresses are defined in rfc4007.
Ruby doesn't support them yet and it's unknown whether it will
(see http://bugs.ruby-lang.org/issues/8464).
So we just remove scope ids.
Tested with MRI and Rubinius.
---
ext/raindrops/linux_inet_diag.c | 41 +++++++++++++++++++++++++++++++++++++--
1 file changed, 39 insertions(+), 2 deletions(-)
diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c
index cd4a876..7b5bae1 100644
--- a/ext/raindrops/linux_inet_diag.c
+++ b/ext/raindrops/linux_inet_diag.c
@@ -134,12 +134,49 @@ static int st_free_data(st_data_t key, st_data_t value, st_data_t ignored)
return ST_DELETE;
}
+/*
+ * call-seq:
+ * remove_scope_id(ip_address)
+ *
+ * Returns copy of IP address with Scope ID removed,
+ * if address has it (only IPv6 actually may have it).
+ */
+static VALUE remove_scope_id(const char *addr)
+{
+ VALUE rv = rb_str_new2(addr);
+ long len = RSTRING_LEN(rv);
+ char *ptr = RSTRING_PTR(rv);
+ char *pct = memchr(ptr, '%', len);
+
+ /*
+ * remove scoped portion
+ * Ruby equivalent: rv.sub!(/%([^\]]*)\]/, "]")
+ */
+ if (pct) {
+ size_t newlen = pct - ptr;
+ char *rbracket = memchr(pct, ']', len - newlen);
+
+ if (rbracket) {
+ size_t move = len - (rbracket - ptr);
+
+ memmove(pct, rbracket, move);
+ newlen += move;
+
+ rb_str_set_len(rv, newlen);
+ } else {
+ rb_raise(rb_eArgError,
+ "']' not found in IPv6 addr=%s", ptr);
+ }
+ }
+ return rv;
+}
+
static int st_to_hash(st_data_t key, st_data_t value, VALUE hash)
{
struct listen_stats *stats = (struct listen_stats *)value;
if (stats->listener_p) {
- VALUE k = rb_str_new2((const char *)key);
+ VALUE k = remove_scope_id((const char *)key);
VALUE v = rb_listen_stats(stats);
OBJ_FREEZE(k);
@@ -153,7 +190,7 @@ static int st_AND_hash(st_data_t key, st_data_t value, VALUE hash)
struct listen_stats *stats = (struct listen_stats *)value;
if (stats->listener_p) {
- VALUE k = rb_str_new2((const char *)key);
+ VALUE k = remove_scope_id((const char *)key);
if (rb_hash_lookup(hash, k) == Qtrue) {
VALUE v = rb_listen_stats(stats);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-09-12 13:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CAAB-Kcmzm4Gr58k2YexCD5+4QYzZJf3HydDRHcSAsoEEut_OFw@mail.gmail.com>
2013-09-12 13:31 ` [PATCH] Remove Scope IDs from IPv6 addresses Hleb Valoshka
2013-09-10 15:17 Hleb Valoshka
2013-09-10 18:35 ` Eric Wong
2013-09-10 18:37 ` Eric Wong
Code repositories for project(s) associated with this public inbox
https://yhbt.net/kgio.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).