Import luanet library
[project/luci.git] / libs / luanet / src / route.c
1 /*
2  *  Licensed under the Apache License, Version 2.0 (the "License");
3  *  you may not use this file except in compliance with the License.
4  *  You may obtain a copy of the License at
5  *
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  *
8  *  Unless required by applicable law or agreed to in writing, software
9  *  distributed under the License is distributed on an "AS IS" BASIS,
10  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  *  See the License for the specific language governing permissions and
12  *  limitations under the License.
13  *
14  *   Copyright (C) 2008 John Crispin <blogic@openwrt.org> 
15  *   Copyright (C) 2008 Steven Barth <steven@midlink.org>
16  */
17
18 #include <linux/sockios.h>
19 #include <net/route.h>
20 #include <sys/ioctl.h>
21
22 #include "helper.h"
23
24 extern int sock_ifconfig;
25
26 int route_add(char *dev, int flag_gateway, int flag_host, char *dst, char *gateway, char *mask)
27 {
28         struct rtentry r;
29         char ip[4];
30         r.rt_flags = RTF_UP;
31         if(flag_gateway)
32                 r.rt_flags |= RTF_GATEWAY;
33         if(flag_host)
34                 r.rt_flags |= RTF_HOST;
35         r.rt_dst.sa_family = AF_INET;
36         r.rt_gateway.sa_family = AF_INET;
37         r.rt_genmask.sa_family = AF_INET;
38         char2ipv4(dst, ip);
39         ((struct sockaddr_in *) &r.rt_dst)->sin_addr.s_addr = (unsigned int)ip;
40         char2ipv4(gateway, ip);
41         ((struct sockaddr_in *) &r.rt_gateway)->sin_addr.s_addr = (unsigned int)ip;
42         char2ipv4(mask, ip);
43         ((struct sockaddr_in *) &r.rt_genmask)->sin_addr.s_addr = (unsigned int)ip;
44         return ioctl(sock_ifconfig, SIOCADDRT, (void *) &r);
45 }