kgio RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Hleb Valoshka <375gnu@gmail.com>
To: kgio@librelist.org
Subject: [PATCH] Remove Scope IDs from IPv6 addresses.
Date: Thu, 12 Sep 2013 16:31:13 +0300	[thread overview]
Message-ID: <1378992673-1667-1-git-send-email-375gnu@gmail.com> (raw)
In-Reply-To: CAAB-Kcmzm4Gr58k2YexCD5+4QYzZJf3HydDRHcSAsoEEut_OFw@mail.gmail.com

	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



       reply	other threads:[~2013-09-12 13:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAAB-Kcmzm4Gr58k2YexCD5+4QYzZJf3HydDRHcSAsoEEut_OFw@mail.gmail.com>
2013-09-12 13:31 ` Hleb Valoshka [this message]
2013-09-10 15:17 [PATCH] Remove Scope IDs from IPv6 addresses Hleb Valoshka
2013-09-10 18:35 ` Eric Wong
2013-09-10 18:37   ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/kgio/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1378992673-1667-1-git-send-email-375gnu@gmail.com \
    --to=375gnu@gmail.com \
    --cc=kgio@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).