apps/netutils/netlib: Add default route in netlib_set_dripv4addr()
#include <netinet/in.h>
#include <net/if.h>
+#ifdef CONFIG_NET_ROUTE
+# include <net/route.h>
+#endif
+
#include "netutils/netlib.h"
/****************************************************************************
@@ -76,6 +80,21 @@ int netlib_set_dripv4addr(FAR const char *ifname,
{
int ret = ERROR;
+#ifdef CONFIG_NET_ROUTE
+ struct sockaddr_in target;
+ struct sockaddr_in netmask;
+ struct sockaddr_in router;
+
+ memset(&target, 0, sizeof(target));
+ target.sin_family = AF_INET;
+
+ memset(&netmask, 0, sizeof(netmask));
+ netmask.sin_family = AF_INET;
+
+ router.sin_addr = *addr;
+ router.sin_family = AF_INET;
+#endif
+
if (ifname && addr)
{
int sockfd = socket(PF_INET, NETLIB_SOCK_TYPE, 0);
@@ -96,6 +115,26 @@ int netlib_set_dripv4addr(FAR const char *ifname,
memcpy(&inaddr->sin_addr, addr, sizeof(struct in_addr));
ret = ioctl(sockfd, SIOCSIFDSTADDR, (unsigned long)&req);
+
+#ifdef CONFIG_NET_ROUTE
+ if (OK == ret)
+ {
+ /* Delete the default route first */
+ /* This call fails if no default route exists, but it's OK */
+
+ (void)delroute(sockfd,
+ (FAR struct sockaddr_storage *)&target,
+ (FAR struct sockaddr_storage *)&netmask);
+
+ /* Then add the new default route */
+
+ ret = addroute(sockfd,
+ (FAR struct sockaddr_storage *)&target,
+ (FAR struct sockaddr_storage *)&netmask,
+ (FAR struct sockaddr_storage *)&router);
+ }
+#endif
+
close(sockfd);
}
}
