summaryrefslogtreecommitdiffstats
path: root/libnetutils
diff options
context:
space:
mode:
authorRobert Greenwalt <rgreenwalt@google.com>2011-05-19 07:21:22 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2011-05-19 07:21:22 -0700
commitab86504f5820945f6ac9fcccdbda06efa73d15ca (patch)
treef6484ec1acce05437f8b0973950e1ed583f74fb8 /libnetutils
parentaef403e917ddbf0576d69c1c0b6bb2fb75161afe (diff)
parent61e33b338cbb90c34cb3e07fe55edcc08c6ce3b8 (diff)
downloadcore-ab86504f5820945f6ac9fcccdbda06efa73d15ca.tar.gz
core-ab86504f5820945f6ac9fcccdbda06efa73d15ca.tar.bz2
core-ab86504f5820945f6ac9fcccdbda06efa73d15ca.zip
am 61e33b33: am aded02cb: Merge "Add v4/v6 route add/removal support" into honeycomb-LTE
* commit '61e33b338cbb90c34cb3e07fe55edcc08c6ce3b8': Add v4/v6 route add/removal support
Diffstat (limited to 'libnetutils')
-rw-r--r--libnetutils/ifc_utils.c112
1 files changed, 29 insertions, 83 deletions
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index 92b96816f..2e3df0003 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -279,7 +279,7 @@ int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned
return 0;
}
-int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
+int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, int prefix_length,
struct in_addr gw)
{
struct rtentry rt;
@@ -311,7 +311,7 @@ int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length
return -errno;
}
- result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt);
+ result = ioctl(ifc_ctl_sock, action, &rt);
if (result < 0) {
if (errno == EEXIST) {
result = 0;
@@ -330,17 +330,7 @@ int ifc_create_default_route(const char *name, in_addr_t gw)
in_dst.s_addr = 0;
in_gw.s_addr = gw;
- return ifc_add_ipv4_route(name, in_dst, 0, in_gw);
-}
-
-int ifc_add_host_route(const char *name, in_addr_t dst)
-{
- struct in_addr in_dst, in_gw;
-
- in_dst.s_addr = dst;
- in_gw.s_addr = 0;
-
- return ifc_add_ipv4_route(name, in_dst, 32, in_gw);
+ return ifc_act_on_route(SIOCADDRT, name, in_dst, 0, in_gw);
}
int ifc_enable(const char *ifname)
@@ -459,67 +449,6 @@ int ifc_remove_host_routes(const char *name)
}
/*
- * Return the address of the default gateway
- *
- * TODO: factor out common code from this and remove_host_routes()
- * so that we only scan /proc/net/route in one place.
- */
-int ifc_get_default_route(const char *ifname)
-{
- char name[64];
- in_addr_t dest, gway, mask;
- int flags, refcnt, use, metric, mtu, win, irtt;
- int result;
- FILE *fp;
-
- fp = fopen("/proc/net/route", "r");
- if (fp == NULL)
- return 0;
- /* Skip the header line */
- if (fscanf(fp, "%*[^\n]\n") < 0) {
- fclose(fp);
- return 0;
- }
- ifc_init();
- result = 0;
- for (;;) {
- int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
- name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
- &mtu, &win, &irtt);
- if (nread != 11) {
- break;
- }
- if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
- && dest == 0
- && strcmp(ifname, name) == 0) {
- result = gway;
- break;
- }
- }
- fclose(fp);
- ifc_close();
- return result;
-}
-
-/*
- * Sets the specified gateway as the default route for the named interface.
- */
-int ifc_set_default_route(const char *ifname, in_addr_t gateway)
-{
- struct in_addr addr;
- int result;
-
- ifc_init();
- addr.s_addr = gateway;
- if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
- LOGD("failed to add %s as default route for %s: %s",
- inet_ntoa(addr), ifname, strerror(errno));
- }
- ifc_close();
- return result;
-}
-
-/*
* Removes the default route for the named interface.
*/
int ifc_remove_default_route(const char *ifname)
@@ -582,7 +511,7 @@ ifc_configure(const char *ifname,
return 0;
}
-int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
+int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
struct in6_addr gw)
{
struct in6_rtmsg rtmsg;
@@ -617,7 +546,7 @@ int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_lengt
return -errno;
}
- result = ioctl(ifc_ctl_sock6, SIOCADDRT, &rtmsg);
+ result = ioctl(ifc_ctl_sock6, action, &rtmsg);
if (result < 0) {
if (errno == EEXIST) {
result = 0;
@@ -629,8 +558,8 @@ int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_lengt
return result;
}
-int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
- const char *gw)
+int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
+ const char *gw)
{
int ret = 0;
struct sockaddr_in ipv4_dst, ipv4_gw;
@@ -648,7 +577,7 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
return -EINVAL;
}
- if (gw == NULL) {
+ if (gw == NULL || (strlen(gw) == 0)) {
if (addr_ai->ai_family == AF_INET6) {
gw = "::";
} else if (addr_ai->ai_family == AF_INET) {
@@ -656,6 +585,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
}
}
+ if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
+ ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
+ printerr("ifc_add_route: invalid prefix length");
+ freeaddrinfo(addr_ai);
+ return -EINVAL;
+ }
+
ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
if (ret != 0) {
printerr("getaddrinfo failed: invalid gateway %s\n", gw);
@@ -673,13 +609,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
if (addr_ai->ai_family == AF_INET6) {
memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
- ret = ifc_add_ipv6_route(ifname, ipv6_dst.sin6_addr, prefix_length,
- ipv6_gw.sin6_addr);
+ ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
+ prefix_length, ipv6_gw.sin6_addr);
} else if (addr_ai->ai_family == AF_INET) {
memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
- ret = ifc_add_ipv4_route(ifname, ipv4_dst.sin_addr, prefix_length,
- ipv4_gw.sin_addr);
+ ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
+ prefix_length, ipv4_gw.sin_addr);
} else {
printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
addr_ai->ai_family);
@@ -690,3 +626,13 @@ int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
freeaddrinfo(gw_ai);
return ret;
}
+
+int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
+{
+ return ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
+}
+
+int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
+{
+ return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
+}