aboutsummaryrefslogtreecommitdiffstats
path: root/tools/gtp-link.c
blob: 97dfa45743d1b6181f1d6e359c0af56e0f505c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Command line utility to create GTP link */

/* (C) 2014 by sysmocom - s.f.m.c. GmbH
 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>

#include <linux/gtp.h>
#include <linux/if_link.h>

#include <libgtpnl/gtpnl.h>

int main(int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	int ret;

	if (argc != 3) {
		printf("Usage: %s <add|del> <device>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	if (!strcmp(argv[1], "del")) {
		printf("destroying gtp interface...\n");
		if (gtp_dev_destroy(argv[2]) < 0)
			perror("gtp_dev_destroy");

		return 0;
	}

	int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
	int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
	struct sockaddr_in sockaddr_fd1 = {
		.sin_family	= AF_INET,
		.sin_port	= htons(3386),
		.sin_addr	= {
			.s_addr 	= INADDR_ANY,
		},
	};
	struct sockaddr_in sockaddr_fd2 = {
		.sin_family	= AF_INET,
		.sin_port	= htons(2152),
		.sin_addr	= {
			.s_addr 	= INADDR_ANY,
		},
	};

	if (bind(fd1, (struct sockaddr *) &sockaddr_fd1,
		 sizeof(sockaddr_fd1)) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}
	if (bind(fd2, (struct sockaddr *) &sockaddr_fd2,
		 sizeof(sockaddr_fd2)) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}

	ret = gtp_dev_create(-1, argv[2], fd1, fd2);
	if (ret < 0) {
		perror("cannot create GTP device\n");
		exit(EXIT_FAILURE);
	}

	fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep "
			"this process running for testing purposes.\n");

	while (1) {
		struct sockaddr_in addr;
		socklen_t len = sizeof(addr);

		ret = recvfrom(fd1, buf, sizeof(buf), 0,
			       (struct sockaddr *)&addr, &len);
		printf("received %d bytes via UDP socket\n", ret);
	}

	return 0;
}