From nobody Thu May 15 07:28:18 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 151721500490418.46356481815826; Mon, 29 Jan 2018 00:36:44 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E21887E42A; Mon, 29 Jan 2018 08:36:42 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2513B6EE70; Mon, 29 Jan 2018 08:36:42 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 92CAB4ED37; Mon, 29 Jan 2018 08:36:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w0T8Ztcn012973 for ; Mon, 29 Jan 2018 03:35:55 -0500 Received: by smtp.corp.redhat.com (Postfix) id 2603560C9C; Mon, 29 Jan 2018 08:35:55 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1E14760C9F for ; Mon, 29 Jan 2018 08:35:51 +0000 (UTC) Received: from m15-114.126.com (m15-114.126.com [220.181.15.114]) by mx1.redhat.com (Postfix) with ESMTP id 8BA59C04BD57 for ; Mon, 29 Jan 2018 08:35:47 +0000 (UTC) Received: from localhost.localdomain (unknown [58.213.111.46]) by smtp7 (Coremail) with SMTP id DsmowAB3v8rY3G5aWHXEBQ--.13603S3; Mon, 29 Jan 2018 16:35:46 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=SveMHpvVJ72pKr1b60 SepCxDfwN+AHR6RKuNjcHw6a8=; b=WXdnF9jhgOsJIjfjMfecMph6L9dBYLHDnq xkjrGOgjU8sM+S3R2QBwL9PQDNf6VCM+i1RlEAOFqsxgYr2gs8Q6nZj23Eedu4ys B1RetWmGOglXgyuWifRli8d/yc5FViuSIVgRG0xPAW1tw6qf0wPL6aovDYJrL/kh g7cO6st6Y= From: Chen Hanxiao To: libvir-list@redhat.com Date: Mon, 29 Jan 2018 16:35:33 +0800 Message-Id: <20180129083536.24554-2-chen_han_xiao@126.com> In-Reply-To: <20180129083536.24554-1-chen_han_xiao@126.com> References: <20180129083536.24554-1-chen_han_xiao@126.com> X-CM-TRANSID: DsmowAB3v8rY3G5aWHXEBQ--.13603S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3AFWkJr15uF4xuF4rArW3GFg_yoWxJFyxpF y5G3ZIgw4rJ3WxGr93XF1FyF1rArs7Wry3Wwn3ur1qqw18JF17Jrn7KFy7KFyxX3y3CF48 uFs7KFyxKFWkJaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j8DG5UUUUU= X-Originating-IP: [58.213.111.46] X-CM-SenderInfo: xfkh0spkdqs5xldrqiyswou0bp/1tbiaQLcrlpD4FZE+QAAsL X-Greylist: Sender passed SPF test, ACL 227 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 29 Jan 2018 08:35:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 29 Jan 2018 08:35:49 +0000 (UTC) for IP:'220.181.15.114' DOMAIN:'m15-114.126.com' HELO:'m15-114.126.com' FROM:'chen_han_xiao@126.com' RCPT:'' X-RedHat-Spam-Score: 1.99 * (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_PSBL, SPF_PASS, T_RP_MATCHES_RCVD) 220.181.15.114 m15-114.126.com 220.181.15.114 m15-114.126.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Chen Hanxiao Subject: [libvirt] [PATCH v3 1/4] util: introduce helper to parse /proc/net/arp X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 29 Jan 2018 08:36:44 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Chen Hanxiao introduce helper to parse /proc/net/arp and store it in struct virArpTable. Signed-off-by: Chen Hanxiao --- v3: s/virGetArpTable/virArpTableGet alloc virArpTable in virArpTableGet return ENOSUPP on none-Linux platform move helpers to virarptable.[ch] po/POTFILES.in | 1 + src/Makefile.am | 1 + src/libvirt_private.syms | 5 +++ src/util/virarptable.c | 114 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virarptable.h | 48 ++++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 src/util/virarptable.c create mode 100644 src/util/virarptable.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 8382ee633..91a4c5730 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -193,6 +193,7 @@ src/uml/uml_conf.c src/uml/uml_driver.c src/util/iohelper.c src/util/viralloc.c +src/util/virarptable.c src/util/viraudit.c src/util/virauth.c src/util/virauthconfig.c diff --git a/src/Makefile.am b/src/Makefile.am index 289b03747..923eea6fe 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -98,6 +98,7 @@ augeastest_DATA =3D UTIL_SOURCES =3D \ util/viralloc.c util/viralloc.h \ util/virarch.h util/virarch.c \ + util/virarptable.h util/virarptable.c \ util/viratomic.h util/viratomic.c \ util/viraudit.c util/viraudit.h \ util/virauth.c util/virauth.h \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f2a2c8650..08229797a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1332,6 +1332,11 @@ virArchGetWordSize; virArchToString; =20 =20 +# util/virarptable.h +virArpTableFree; +virArpTableGet; + + # util/viraudit.h virAuditClose; virAuditEncode; diff --git a/src/util/virarptable.c b/src/util/virarptable.c new file mode 100644 index 000000000..48a38352f --- /dev/null +++ b/src/util/virarptable.c @@ -0,0 +1,114 @@ +/* + * virarptable.c Linux ARP table handling + * + * Copyright (C) 2018 Chen Hanxiao + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Authors: + * Chen Hanxiao + */ + +#include + +#include +#include + +#include "viralloc.h" +#include "virarptable.h" +#include "virfile.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +#ifdef __linux__ + +virArpTablePtr virArpTableGet(void) +{ + FILE *fp =3D NULL; + char line[1024]; + int num =3D 0; + virArpTablePtr table =3D NULL; + + if (VIR_ALLOC(table) < 0) + return NULL; + + if (!(fp =3D fopen("/proc/net/arp", "r"))) + goto cleanup; + + while (fgets(line, sizeof(line), fp)) { + char ip[32], mac[32], dev_name[32], hwtype[32], + flags[32], mask[32], nouse[32]; + + if (STRPREFIX(line, "IP address")) + continue; + + if (VIR_REALLOC_N(table->t, num + 1) < 0) + goto cleanup; + + table->n =3D num + 1; + + /* /proc/net/arp looks like: + * 172.16.17.254 0x1 0x2 e4:68:a3:8d:ed:d3 * enp3s0 + */ + sscanf(line, "%[0-9.]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ \= t\n]", + ip, nouse, + hwtype, nouse, + flags, nouse, + mac, nouse, + mask, nouse, + dev_name); + + if (VIR_STRDUP(table->t[num].ipaddr, ip) < 0) + goto cleanup; + + if (VIR_STRDUP(table->t[num].mac, mac) < 0) + goto cleanup; + + if (VIR_STRDUP(table->t[num].dev_name, dev_name) < 0) + goto cleanup; + + num++; + } + + return table; + + cleanup: + VIR_FORCE_FCLOSE(fp); + return NULL; +} + +#else + +virArpTablePtr virArpTableGet(void) +{ + virReportError(VIR_ERR_NO_SUPPORT, "%s", + _("get arp table not implemented on this platform")); + return NULL; +} + +#endif /* __linux__ */ + +void +virArpTableFree(virArpTablePtr table) +{ + size_t i; + for (i =3D 0; i < table->n; i++) { + VIR_FREE(table->t[i].ipaddr); + VIR_FREE(table->t[i].mac); + VIR_FREE(table->t[i].dev_name); + } + VIR_FREE(table); +} diff --git a/src/util/virarptable.h b/src/util/virarptable.h new file mode 100644 index 000000000..77db4e9f4 --- /dev/null +++ b/src/util/virarptable.h @@ -0,0 +1,48 @@ +/* + * virarptable.h Linux ARP table handling + * + * Copyright (C) 2018 Chen Hanxiao + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Authors: + * Chen Hanxiao + */ + +#ifndef __VIR_ARPTABLE_H__ +# define __VIR_ARPTABLE_H__ + +# include "internal.h" + +typedef struct _virArpTableEntry virArpTableEntry; +typedef virArpTableEntry *virArpTableEntryPtr; +typedef struct _virArpTable virArpTable; +typedef virArpTable *virArpTablePtr; + +struct _virArpTableEntry{ + char *ipaddr; + char *mac; + char *dev_name; +}; + +struct _virArpTable { + int n; + virArpTableEntryPtr t; +}; + +virArpTablePtr virArpTableGet(void); +void virArpTableFree(virArpTablePtr table); + +#endif /* __VIR_ARPTABLE_H__ */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list