From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS47066 71.19.144.0/20 X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00, MSGID_FROM_MTA_HEADER shortcircuit=no autolearn=unavailable version=3.3.2 Path: news.gmane.org!not-for-mail From: Eric Wong Newsgroups: gmane.comp.lang.ruby.kgio.general Subject: Re: [PATCH] Remove Scope IDs from IPv6 addresses. Date: Tue, 10 Sep 2013 18:35:04 +0000 Message-ID: <20130910183504.GA502@dcvr.yhbt.net> References: <1378826261-10771-1-git-send-email-375gnu@gmail.com> <1378826261-10771-1-git-send-email-375gnu@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1378838120 19865 80.91.229.3 (10 Sep 2013 18:35:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 10 Sep 2013 18:35:20 +0000 (UTC) To: kgio@librelist.org Original-X-From: kgio@librelist.org Tue Sep 10 20:35:23 2013 Return-path: Envelope-to: gclrkg-kgio@m.gmane.org List-Archive: List-Help: List-Id: List-Post: List-Subscribe: List-Unsubscribe: Precedence: list Original-Sender: kgio@librelist.org Xref: news.gmane.org gmane.comp.lang.ruby.kgio.general:224 Archived-At: Received: from zedshaw2.xen.prgmr.com ([71.19.156.177]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VJSmS-00079b-Rp for gclrkg-kgio@m.gmane.org; Tue, 10 Sep 2013 20:35:17 +0200 Received: from zedshaw2.xen.prgmr.com (unknown [IPv6:::1]) by zedshaw2.xen.prgmr.com (Postfix) with ESMTP id C40DC7506D for ; Tue, 10 Sep 2013 18:44:42 +0000 (UTC) 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; }