From patchwork Thu Jul 12 11:44:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hamid jafarian X-Patchwork-Id: 170642 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 870392C020E for ; Thu, 12 Jul 2012 21:55:02 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933681Ab2GLLy7 (ORCPT ); Thu, 12 Jul 2012 07:54:59 -0400 Received: from static.32.95.40.188.clients.your-server.de ([188.40.95.32]:60628 "EHLO host4drupal.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752359Ab2GLLy6 (ORCPT ); Thu, 12 Jul 2012 07:54:58 -0400 X-Greylist: delayed 596 seconds by postgrey-1.27 at vger.kernel.org; Thu, 12 Jul 2012 07:54:58 EDT X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network Received: from [192.168.0.211] (unknown [85.185.161.201]) by host4drupal.com (Postfix) with ESMTPSA id C8549580502; Thu, 12 Jul 2012 13:44:59 +0200 (CEST) Subject: [PATCH iproute2] Ability to compile iproute2 as shared library From: hamid jafarian To: netdev@vger.kernel.org Cc: shemminger@osdl.org Date: Thu, 12 Jul 2012 16:14:54 +0430 X-Mailer: Evolution 3.0.3 (3.0.3-1.fc15) Message-ID: <1342093501.19963.11.camel@gol> Mime-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Hi, This is a try with minimum changes to compile iproute2 as shared library. Some functions would be used when we compile iproute2 as shared library has been defined in "ip.c". Also NICs caching strategy has been changed because, when we use "libiproute2.so", system NIC list may change, so we should re-cache all NICs at each call to NIC manipulation functions. Also some call of "exit(*)" changed to "return *". HOWTO Make: # export LIBIPROUTE2_SO=y; make in attached files there is a simple wrapper to work with libiproute2.so ... --- ip/Makefile | 13 ++++++++++++- ip/ip.c | 40 ++++++++++++++++++++++++++++++++++++++++ ip/iproute.c | 2 +- lib/Makefile | 3 +++ lib/ll_map.c | 25 ++++++++++++++++++++++++- lib/utils.c | 7 ++++--- 6 files changed, 84 insertions(+), 6 deletions(-) @@ -346,9 +347,9 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family) slash = strchr(arg, '/'); if (slash) - *slash = 0; + addr = strndup(arg, slash - arg); - err = get_addr_1(dst, arg, family); + err = get_addr_1(dst, addr, family); if (err == 0) { switch(dst->family) { case AF_INET6: @@ -373,7 +374,7 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family) } done: if (slash) - *slash = '/'; + free(addr); return err; } From 623097d32e26f51aca72bcd75979c187f656b4ff Mon Sep 17 00:00:00 2001 From: "Hamid Jafarian (hm.t.)" Date: Thu, 12 Jul 2012 15:26:20 +0430 Subject: [PATCH] Ability to compile iproute2 as shared library This is a try with minimum changes to compile iproute2 as shared library. Some functions would be used when we compile iproute2 as shared library has been defined in "ip.c". Also NICs caching strategy has been changed because, when we use "libiproute2.so", system NIC list may change, so we should re-cache all NICs at each call to NIC manipulation functions. Also some call of "exit(*)" changed to "return *". HOWTO Make: # export LIBIPROUTE2_SO=y; make --- ip/Makefile | 13 ++++++++++++- ip/ip.c | 40 ++++++++++++++++++++++++++++++++++++++++ ip/iproute.c | 2 +- lib/Makefile | 3 +++ lib/ll_map.c | 25 ++++++++++++++++++++++++- lib/utils.c | 7 ++++--- 6 files changed, 84 insertions(+), 6 deletions(-) diff --git a/ip/Makefile b/ip/Makefile index e029ea1..23fc22f 100644 --- a/ip/Makefile +++ b/ip/Makefile @@ -13,14 +13,25 @@ ifeq ($(IP_CONFIG_SETNS),y) CFLAGS += -DHAVE_SETNS endif +ifeq ($(LIBIPROUTE2_SO), y)) + CFLAGS += -fPIC -DLIBIPROUTE2_SO +endif + ALLOBJ=$(IPOBJ) $(RTMONOBJ) SCRIPTS=ifcfg rtpr routel routef -TARGETS=ip rtmon + +ifeq ($(LIBIPROUTE2_SO), y) + TARGETS=libiproute2.so rtmon +else + TARGETS=ip rtmon +endif all: $(TARGETS) $(SCRIPTS) ip: $(IPOBJ) $(LIBNETLINK) +libiproute2.so: $(IPOBJ) $(LIBNETLINK) + $(CC) $(CFLAGS) -shared $(IPOBJ) $(LIBNETLINK) -o $(@) rtmon: $(RTMONOBJ) diff --git a/ip/ip.c b/ip/ip.c index 20dc3b5..d5c7b2f 100644 --- a/ip/ip.c +++ b/ip/ip.c @@ -87,6 +87,44 @@ static const struct cmd { { 0 } }; +#ifdef LIBIPROUTE2_SO +int do_ipinit() +{ + + if (rtnl_open(&rth, 0) < 0) + return 1; + _SL_="\n"; + return 0; +} + +int do_ipfini() +{ + rtnl_close(&rth); + return 0; +} + +void do_ipflushloop(int loop) +{ + max_flush_loops = loop; +} + +int do_ipfamily(char *family) +{ + if (strcmp(family, "inet") == 0) + preferred_family = AF_INET; + else if (strcmp(family, "inet6") == 0) + preferred_family = AF_INET6; + else if (strcmp(family, "dnet") == 0) + preferred_family = AF_DECnet; + else if (strcmp(family, "link") == 0) + preferred_family = AF_PACKET; + else if (strcmp(family, "ipx") == 0) + preferred_family = AF_IPX; + else return -1; + return 0; +} +#endif //#ifdef LIBIPROUTE2_SO + static int do_cmd(const char *argv0, int argc, char **argv) { const struct cmd *c; @@ -101,6 +139,7 @@ static int do_cmd(const char *argv0, int argc, char **argv) return EXIT_FAILURE; } +#ifndef LIBIPROUTE2_SO static int batch(const char *name) { char *line = NULL; @@ -264,3 +303,4 @@ int main(int argc, char **argv) rtnl_close(&rth); usage(); } +#endif //#ifndef LIBIPROUTE2_SO diff --git a/ip/iproute.c b/ip/iproute.c index 5cd313e..8ae253b 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -1012,7 +1012,7 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv) req.r.rtm_family = AF_INET; if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) - exit(2); + return 2; return 0; } diff --git a/lib/Makefile b/lib/Makefile index da2f0fc..aa9a10f 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,4 +1,7 @@ CFLAGS += -fPIC +ifeq ($(LIBIPROUTE2_SO), y)) + CFLAGS += -DLIBIPROUTE2_SO +endif UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o diff --git a/lib/ll_map.c b/lib/ll_map.c index 1ca781e..6923511 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -172,15 +172,18 @@ unsigned ll_name_to_index(const char *name) if (name == NULL) return 0; - +#ifndef LIBIPROUTE2_SO if (icache && strcmp(name, ncache) == 0) return icache; +#endif for (i=0; iidx_next) { if (strcmp(im->name, name) == 0) { +#ifndef LIBIPROUTE2_SO icache = im->index; strcpy(ncache, name); +#endif return im->index; } } @@ -192,12 +195,32 @@ unsigned ll_name_to_index(const char *name) return idx; } +#ifdef LIBIPROUTE2_SO +int ll_free_map() +{ + int i; + struct ll_cache *im, *imt; + for (i=0; iidx_next; + free(im); + } + idx_head[i] = NULL; + } + return 0; +} +#endif + int ll_init_map(struct rtnl_handle *rth) { static int initialized; +#ifdef LIBIPROUTE2_SO + ll_free_map(); +#else if (initialized) return 0; +#endif if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { perror("Cannot send dump request"); diff --git a/lib/utils.c b/lib/utils.c index d80f79b..6b7cdee 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -330,6 +330,7 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family) int err; unsigned plen; char *slash; + char *addr = arg; memset(dst, 0, sizeof(*dst)); @@ -346,9 +347,9 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family) slash = strchr(arg, '/'); if (slash) - *slash = 0; + addr = strndup(arg, slash - arg); - err = get_addr_1(dst, arg, family); + err = get_addr_1(dst, addr, family); if (err == 0) { switch(dst->family) { case AF_INET6: @@ -373,7 +374,7 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family) } done: if (slash) - *slash = '/'; + free(addr); return err; } -- 1.7.6.4