From patchwork Thu Feb 15 21:23:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874135 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nY8t2vLL"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8Rm0JCVz9t20 for ; Fri, 16 Feb 2018 08:24:36 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755949AbeBOVYe (ORCPT ); Thu, 15 Feb 2018 16:24:34 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:39115 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162074AbeBOVX5 (ORCPT ); Thu, 15 Feb 2018 16:23:57 -0500 Received: by mail-lf0-f68.google.com with SMTP id h78so1471873lfg.6 for ; Thu, 15 Feb 2018 13:23:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+d56mWjErbFYFMlvjKL/wtvLXefcFlS8rm5nthd7avo=; b=nY8t2vLL2VekMRceq4uFwTxSC96X/gQeRwlTKZolTiLdnw/crt5fwRgwZ/8T6czW/E 41DSPMs5vxTW9H2/+oV5+HAbgq+6pYn/RxIxL9Rx1JqYqFyMLDW73HDxO/No6Y/wGQDg eRjVZ1OnIA9n8Mo5fEdXJmcnPb4lLwSOys8GokWOGawwNMJ1bwqoTg5RC7/ZWhTe3bip imBMk18lxy1BN+7FlAxfDdx4CbyryOFC7rRmivydTnD0awVlqoIOo9IWUQvq1Mh25PIh PPxq75geJqt1AJMypm9lC9BqnkBB5yb4f03mPDO1nqt5/+Eo/Gdisd2el62LnmZz5VCM KhSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+d56mWjErbFYFMlvjKL/wtvLXefcFlS8rm5nthd7avo=; b=hqtuG5lhST6D2Ytff4N93e4mViZ9+fDfGvlyeS+8LLCZQEkdpwJX3djkXgQ2y85bh0 sGyCF5KHJ97+qIGId7NmK7uNi62L5u7T0juU5LCzssHbibmap1p7lVvoiaZFgFQ70Agq lHB1Kgh/DkjWFQh+K4NXpKNZfZxwBt0N//w0wRRBhUYmbe0zJUafBmvxfmJeNz1J8unD VyWixjD9KTHQjjdrZaP3W30Z9EPcCRg3ZVT6QJmdJqS+7Kx9P0L9eyluqEJY437FSLgi frVASk4t2QiU6d0m0es/wHBhbdzvnDss1lqTiNRiu5aCS1XzXx2X6ejJ6/hv6P+0xUwe f6Fg== X-Gm-Message-State: APf1xPB1QUHgwXee4a7IwLPEsdAyX1815nCrVbMSrlqfM/usPhuGM10T CVW7KbOFPstfsCmw29IRRvvCyw== X-Google-Smtp-Source: AH8x22463Q2UTkw9H6x8LwCUSxuoa7Pj3PYR3GoRviLCKOkg60+FTP5E5UYPy5P7YyYWd0qrhnzYcA== X-Received: by 10.25.150.78 with SMTP id y75mr2813138lfd.81.1518729835830; Thu, 15 Feb 2018 13:23:55 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:54 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 3/9] utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n() Date: Thu, 15 Feb 2018 23:23:18 +0200 Message-Id: <1518729804-19873-4-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Now all users of ll_idx_n2a() replaced with ll_index_to_name() we can move it's functionality to ll_index_to_name() and implement index to name conversion using snprintf() and "if%u". Use %u specifier in "if%..." template consistently: network device indexes are always greather than zero. Also introduce ll_idx_n2a() conterpart: ll_idx_a2n() that is used to translate name of the "if%u" form to index using sscanf(). Signed-off-by: Serhey Popovych --- include/ll_map.h | 4 +++- lib/ll_map.c | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/ll_map.h b/include/ll_map.h index c8474e6..8546ff9 100644 --- a/include/ll_map.h +++ b/include/ll_map.h @@ -8,9 +8,11 @@ int ll_remember_index(const struct sockaddr_nl *who, void ll_init_map(struct rtnl_handle *rth); unsigned ll_name_to_index(const char *name); const char *ll_index_to_name(unsigned idx); -const char *ll_idx_n2a(unsigned idx, char *buf); int ll_index_to_type(unsigned idx); int ll_index_to_flags(unsigned idx); unsigned namehash(const char *str); +const char *ll_idx_n2a(unsigned int idx); +unsigned int ll_idx_a2n(const char *name); + #endif /* __LL_MAP_H__ */ diff --git a/lib/ll_map.c b/lib/ll_map.c index f65614f..0afe689 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -136,8 +136,26 @@ int ll_remember_index(const struct sockaddr_nl *who, return 0; } -const char *ll_idx_n2a(unsigned idx, char *buf) +const char *ll_idx_n2a(unsigned int idx) { + static char buf[IFNAMSIZ]; + + snprintf(buf, sizeof(buf), "if%u", idx); + return buf; +} + +unsigned int ll_idx_a2n(const char *name) +{ + unsigned int idx; + + if (sscanf(name, "if%u", &idx) != 1) + return 0; + return idx; +} + +const char *ll_index_to_name(unsigned int idx) +{ + static char buf[IFNAMSIZ]; const struct ll_cache *im; if (idx == 0) @@ -148,18 +166,11 @@ const char *ll_idx_n2a(unsigned idx, char *buf) return im->name; if (if_indextoname(idx, buf) == NULL) - snprintf(buf, IFNAMSIZ, "if%d", idx); + snprintf(buf, IFNAMSIZ, "if%u", idx); return buf; } -const char *ll_index_to_name(unsigned idx) -{ - static char nbuf[IFNAMSIZ]; - - return ll_idx_n2a(idx, nbuf); -} - int ll_index_to_type(unsigned idx) { const struct ll_cache *im; @@ -196,7 +207,7 @@ unsigned ll_name_to_index(const char *name) idx = if_nametoindex(name); if (idx == 0) - sscanf(name, "if%u", &idx); + idx = ll_idx_a2n(name); return idx; }