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).
This commit is contained in:
Pablo Neira Ayuso 2014-02-22 22:09:59 +01:00
parent 1b00243775
commit 2cf5c87b96
8 changed files with 143 additions and 38 deletions

View File

@ -1 +1 @@
pkginclude_HEADERS = gtpnl.h
pkginclude_HEADERS = gtp.h gtpnl.h

View File

@ -0,0 +1,23 @@
#ifndef _LIBGTP_H_
#define _LIBGTP_H_
#include <stdint.h>
struct gtp_tunnel;
struct gtp_tunnel *gtp_tunnel_alloc(void);
void gtp_tunnel_free(struct gtp_tunnel *t);
void gtp_tunnel_set_ifidx(struct gtp_tunnel *t, uint32_t ifidx);
void gtp_tunnel_set_ms_ip4(struct gtp_tunnel *t, struct in_addr *ms_addr);
void gtp_tunnel_set_sgsn_ip4(struct gtp_tunnel *t, struct in_addr *sgsn_addr);
void gtp_tunnel_set_version(struct gtp_tunnel *t, uint32_t version);
void gtp_tunnel_set_tid(struct gtp_tunnel *t, uint64_t tid);
const uint32_t gtp_tunnel_get_ifidx(struct gtp_tunnel *t);
const struct in_addr *gtp_tunnel_get_ms_ip4(struct gtp_tunnel *t);
const struct in_addr *gtp_tunnel_get_sgsn_ip4(struct gtp_tunnel *t);
int gtp_tunnel_get_version(struct gtp_tunnel *t);
uint64_t gtp_tunnel_get_tid(struct gtp_tunnel *t);
#endif

View File

@ -16,11 +16,10 @@ int genl_lookup_family(struct mnl_socket *nl, const char *family);
int gtp_dev_create(const char *ifname);
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_del_tunnel(int genl_id, struct mnl_socket *nl, const char *ifname,
uint64_t tid, uint32_t gtp_version);
struct gtp_tunnel;
int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t);
int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t);
int gtp_list_tunnel(int genl_id, struct mnl_socket *nl);
#endif

View File

@ -2,4 +2,8 @@ include $(top_srcdir)/Make_global.am
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

View File

@ -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 @@ static void gtp_build_payload(struct nlmsghdr *nlh, uint64_t tid,
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 @@ int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, const char *ifname,
}
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");

78
libgtnl/src/gtp.c Normal file
View File

@ -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);

View File

@ -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

View File

@ -9,5 +9,18 @@ global:
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: *;
};