diff options
author | Martin Xu <martin.xu@intel.com> | 2009-12-30 16:24:11 +0800 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2009-12-30 00:54:04 -0800 |
commit | 87edbec1ae4b6971640d77d50ecfdc67693d5bcd (patch) | |
tree | ef02e8e5de665e6f226860f46e0e4823254ea358 | |
parent | fc17fb73a580904fcce7c37b5a9ec2364715f9e7 (diff) | |
download | connman-87edbec1ae4b6971640d77d50ecfdc67693d5bcd.tar.gz connman-87edbec1ae4b6971640d77d50ecfdc67693d5bcd.tar.bz2 connman-87edbec1ae4b6971640d77d50ecfdc67693d5bcd.zip |
Add INET functions for setting gateway address and gateway interface
-rw-r--r-- | include/inet.h | 3 | ||||
-rw-r--r-- | src/inet.c | 54 |
2 files changed, 53 insertions, 4 deletions
diff --git a/include/inet.h b/include/inet.h index d8be5b79..412cf116 100644 --- a/include/inet.h +++ b/include/inet.h @@ -44,7 +44,8 @@ connman_bool_t connman_inet_is_mac80211(int index); int connman_inet_set_address(int index, struct connman_ipaddress *ipaddress); int connman_inet_clear_address(int index); -int connman_inet_set_gateway(int index, struct in_addr gateway); +int connman_inet_set_gateway_interface(int index); +int connman_inet_set_gateway_address(int index, const char *gateway); #ifdef __cplusplus } @@ -620,7 +620,55 @@ int connman_inet_clear_address(int index) return 0; } -int connman_inet_set_gateway(int index, struct in_addr gateway) +int connman_inet_set_gateway_interface(int index) +{ + struct ifreq ifr; + struct rtentry rt; + struct sockaddr_in addr; + int sk, err; + + DBG(""); + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -1; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = index; + + if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) { + close(sk); + return -1; + } + + DBG("ifname %s", ifr.ifr_name); + + memset(&rt, 0, sizeof(rt)); + + rt.rt_dev = ifr.ifr_name; + + rt.rt_flags = RTF_UP; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + + memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask)); + + memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst)); + + memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway)); + + err = ioctl(sk, SIOCADDRT, &rt); + if (err < 0) + connman_error("Setting interface as default route failed (%s)", + strerror(errno)); + close(sk); + + return err; +} + +int connman_inet_set_gateway_address(int index, const char *gateway) { struct ifreq ifr; struct rtentry rt; @@ -646,7 +694,7 @@ int connman_inet_set_gateway(int index, struct in_addr gateway) memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr = gateway; + addr.sin_addr.s_addr = inet_addr(gateway); memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst)); memset(&addr, 0, sizeof(addr)); @@ -676,7 +724,7 @@ int connman_inet_set_gateway(int index, struct in_addr gateway) memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr = gateway; + addr.sin_addr.s_addr = inet_addr(gateway); memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway)); memset(&addr, 0, sizeof(addr)); |