add struct gtp_tunnel and adapt functions to use it
This patch adds a gtp_tunnel structure to avoid having to deal with
functions with lots of parameters. This should also help to extend
the interfaces and the gtp_tunnel object without breaking the binary
interface (which will be good by when ipv6 support will be added).
diff --git a/src/Makefile.am b/src/Makefile.am
index a9b072f..f4d8d6c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,4 +2,8 @@
lib_LTLIBRARIES = libgtpnl.la
libgtpnl_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libgtpnl.map -version-info $(LIBVERSION)
-libgtpnl_la_SOURCES = genl.c gtp-genl.c gtp-rtnl.c libgtpnl.map
+libgtpnl_la_SOURCES = genl.c \
+ gtp-genl.c \
+ gtp-rtnl.c \
+ gtp.c \
+ libgtpnl.map
diff --git a/src/gtp-genl.c b/src/gtp-genl.c
index 0dd5473..4891b08 100644
--- a/src/gtp-genl.c
+++ b/src/gtp-genl.c
@@ -11,6 +11,7 @@
#include <libmnl/libmnl.h>
#include <linux/genetlink.h>
+#include <libgtpnl/gtp.h>
#include <libgtpnl/gtpnl.h>
#include <net/if.h>
@@ -29,42 +30,22 @@
mnl_attr_put_u64(nlh, GTPA_TID, tid);
}
-int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, const char *ifname,
- const char *ms_addr, const char *sgsn_addr, uint64_t tid,
- int gtp_version)
+int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
{
- uint32_t gtp_ifidx;
- struct in_addr ms, sgsn;
struct nlmsghdr *nlh;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL);
- gtp_ifidx = if_nametoindex(ifname);
- if (gtp_ifidx == 0){
- fprintf(stderr, "wrong GTP interface %s\n", ifname);
- return -1;
- }
-
- if (inet_aton(ms_addr, &ms) < 0) {
- perror("bad address for ms");
- return -1;
- }
-
- if (inet_aton(sgsn_addr, &sgsn) < 0) {
- perror("bad address for sgsn");
- return -1;
- }
-
- if (gtp_version > GTP_V1) {
+ if (t->gtp_version > GTP_V1) {
fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
- gtp_version);
+ t->gtp_version);
return -1;
}
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
GTP_CMD_TUNNEL_NEW);
- gtp_build_payload(nlh, tid, gtp_ifidx, sgsn.s_addr,
- ms.s_addr, gtp_version);
+ gtp_build_payload(nlh, t->tid, t->ifidx, t->sgsn_addr.s_addr,
+ t->ms_addr.s_addr, t->gtp_version);
if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
perror("genl_socket_talk");
@@ -73,19 +54,15 @@
}
EXPORT_SYMBOL(gtp_add_tunnel);
-int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, const char *ifname,
- uint64_t tid, uint32_t gtp_version)
+int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
{
- uint32_t gtp_ifidx;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t seq = time(NULL);
- gtp_ifidx = if_nametoindex(ifname);
-
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
GTP_CMD_TUNNEL_DELETE);
- gtp_build_payload(nlh, tid, gtp_ifidx, 0, 0, gtp_version);
+ gtp_build_payload(nlh, t->tid, t->ifidx, 0, 0, t->gtp_version);
if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
perror("genl_socket_talk");
diff --git a/src/gtp.c b/src/gtp.c
new file mode 100644
index 0000000..9ec523f
--- /dev/null
+++ b/src/gtp.c
@@ -0,0 +1,78 @@
+#include <stdlib.h>
+#include <netinet/in.h>
+
+#include <libgtpnl/gtp.h>
+
+#include "internal.h"
+
+struct gtp_tunnel *gtp_tunnel_alloc(void)
+{
+ return calloc(1, sizeof(struct gtp_tunnel));
+}
+EXPORT_SYMBOL(gtp_tunnel_alloc);
+
+void gtp_tunnel_free(struct gtp_tunnel *t)
+{
+ free(t);
+}
+EXPORT_SYMBOL(gtp_tunnel_free);
+
+void gtp_tunnel_set_ifidx(struct gtp_tunnel *t, uint32_t ifidx)
+{
+ t->ifidx = ifidx;
+}
+EXPORT_SYMBOL(gtp_tunnel_set_ifidx);
+
+void gtp_tunnel_set_ms_ip4(struct gtp_tunnel *t, struct in_addr *ms_addr)
+{
+ t->ms_addr = *ms_addr;
+}
+EXPORT_SYMBOL(gtp_tunnel_set_ms_ip4);
+
+void gtp_tunnel_set_sgsn_ip4(struct gtp_tunnel *t, struct in_addr *sgsn_addr)
+{
+ t->sgsn_addr = *sgsn_addr;
+}
+EXPORT_SYMBOL(gtp_tunnel_set_sgsn_ip4);
+
+void gtp_tunnel_set_version(struct gtp_tunnel *t, uint32_t version)
+{
+ t->gtp_version = version;
+}
+EXPORT_SYMBOL(gtp_tunnel_set_version);
+
+void gtp_tunnel_set_tid(struct gtp_tunnel *t, uint64_t tid)
+{
+ t->tid = tid;
+}
+EXPORT_SYMBOL(gtp_tunnel_set_tid);
+
+const uint32_t gtp_tunnel_get_ifidx(struct gtp_tunnel *t)
+{
+ return t->ifidx;
+}
+EXPORT_SYMBOL(gtp_tunnel_get_ifidx);
+
+const struct in_addr *gtp_tunnel_get_ms_ip4(struct gtp_tunnel *t)
+{
+ return &t->ms_addr;
+}
+EXPORT_SYMBOL(gtp_tunnel_get_ms_ip4);
+
+const struct in_addr *gtp_tunnel_get_sgsn_ip4(struct gtp_tunnel *t)
+{
+ return &t->sgsn_addr;
+}
+EXPORT_SYMBOL(gtp_tunnel_get_sgsn_ip4);
+
+int gtp_tunnel_get_version(struct gtp_tunnel *t)
+{
+ return t->gtp_version;
+}
+EXPORT_SYMBOL(gtp_tunnel_get_version);
+
+uint64_t gtp_tunnel_get_tid(struct gtp_tunnel *t)
+{
+ return t->tid;
+}
+EXPORT_SYMBOL(gtp_tunnel_get_tid);
diff --git a/src/internal.h b/src/internal.h
index 3a88d1a..cd2093a 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -9,4 +9,15 @@
# define EXPORT_SYMBOL
#endif
+#include <stdint.h>
+#include <netinet/in.h>
+
+struct gtp_tunnel {
+ uint32_t ifidx;
+ struct in_addr ms_addr;
+ struct in_addr sgsn_addr;
+ uint64_t tid;
+ int gtp_version;
+};
+
#endif
diff --git a/src/libgtpnl.map b/src/libgtpnl.map
index ead02e0..b38785c 100644
--- a/src/libgtpnl.map
+++ b/src/libgtpnl.map
@@ -9,5 +9,18 @@
gtp_del_tunnel;
gtp_list_tunnel;
+ gtp_tunnel_alloc;
+ gtp_tunnel_free;
+ gtp_tunnel_set_ifidx;
+ gtp_tunnel_set_ms_ip4;
+ gtp_tunnel_set_sgsn_ip4;
+ gtp_tunnel_set_version;
+ gtp_tunnel_set_tid;
+ gtp_tunnel_get_ifidx;
+ gtp_tunnel_get_ms_ip4;
+ gtp_tunnel_get_sgsn_ip4;
+ gtp_tunnel_get_version;
+ gtp_tunnel_get_tid;
+
local: *;
};