summaryrefslogtreecommitdiffstats
path: root/LogReceiver/routemanager.cpp
blob: 3063b21078ed95f55cae8f60e4dddd4516720505 (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * routemanager.cpp
 *
 *  Created on: Sep 5, 2011
 *      Author: niklas
 */

#include "routemanager.h"

routemanager::routemanager() {
	// TODO Auto-generated constructor stub

}

routemanager::~routemanager() {
	// TODO Auto-generated destructor stub
}

int routemanager::addRoute(QString ifname, QString destination,
		QString netmask, QString gateway, int metric) {
	//struct in_addr destination, netmask, gateway;
	//add_route();
	return doRoute(destination, gateway, AF_INET, 0);
}

int routemanager::addRoute6(QString ifname, QString destination,
		QString netmask, QString gateway, int metric) {
	//struct in_addr destination, netmask, gateway;
	//add_route();
	return doRoute(destination, gateway, AF_INET6, 0);
}


int routemanager::delRoute(QString ifname, QString destination,
		QString netmask, QString gateway, int metric) {
	/*struct in_addr ds, nm, gw;
	 ba = ifname.toAscii();
	 const char *in = ba.constData();
	 inet_aton("0.0.0.0", &ds);
	 inet_aton("0.0.0.0", &nm);
	 ba = gateway.toAscii();
	 char * gwaddr = ba.data();
	 inet_aton(gwaddr,&gw);
	 del_route(in, ds, nm, gw, metric);
	 */
	return doRoute(destination, gateway, AF_INET, 1);
}

int routemanager::delRoute6(QString ifname, QString destination,
		QString netmask, QString gateway, int metric) {
	return doRoute(destination, gateway, AF_INET6, 1);
}

/**
 * This method adds or deletes a route.
 * This method adds or deletes a route. According to the action,
 * you can delete a route or add a route from/to the
 * main routing table. In most cases, this method will be used for deleting
 * or adding a default rout. To keep it modular, it is possible
 * to specify an ip address family.
 *
 * @param destination
 *  the destination address (e.g: 0.0.0.0)
 *
 * @param gateway
 *  the gateway address (e.g: 192.168.0.254)
 *
 * @param af
 *  specify the family type of the ip address.
 *  possible values are: AF_INET for an IPv4 address
 *                       AF_INET6 for an IPv6 address
 *
 * @param action
 *  possible values are: 0: perform add route
 *                       1: perform delete route
 *
 * @return
 *  return 1 if an error happened.
 *  return 0 if everything was ok.
 */
int routemanager::doRoute(QString destination, QString gateway, int af, int action) {
	struct nl_handle* rtsock;
	struct nl_addr * dst;
	struct nl_addr * gw;
	struct rtnl_route * route;
	int retval;

	ba = destination.toAscii();
	char *dstaddr = ba.data();

	ba = gateway.toAscii();
	char * gwaddr = ba.data();

	if (!(dst = nl_addr_parse(dstaddr, af))) {
		qDebug() << "Invalid network address given:" << dstaddr;
		return -1;
	}
	if (!(gw = nl_addr_parse(gwaddr, af))) {
		qDebug() << "Invalid router address given: " << gwaddr;
		nl_addr_put(dst);
		return -1;
	}

	route = rtnl_route_alloc();
	rtnl_route_set_family(route, af);
	rtnl_route_set_scope(route, RT_SCOPE_UNIVERSE);
	rtnl_route_set_dst(route, dst);
	rtnl_route_set_gateway(route, gw);

	rtsock = nl_handle_alloc();
	nl_connect(rtsock, NETLINK_ROUTE);

	if (action == 0) {
		rtnl_route_add(rtsock, route, 0);
	}
	else if (action == 1) {
		rtnl_route_del(rtsock, route, 0);
	}
	else {
		qDebug() << "unknown action:" << action;
	}


	rtnl_route_put(route);
	nl_addr_put(dst);
	nl_addr_put(gw);
	nl_handle_destroy(rtsock);

	return 0;
}