Permalink
Browse files

Added linux support for configuring TUNConfigurator (set tun ipv6 add…

…ress)
  • Loading branch information...
1 parent a1b0aee commit 066eb13feda235fbd9142e88bf32d4a469f5b88f kris kechagia committed Mar 13, 2012
Showing with 64 additions and 12 deletions.
  1. +1 −1 cjdroute.c
  2. +62 −10 interface/TUNConfigurator.c
  3. +1 −1 interface/TUNConfigurator.h
View
@@ -571,7 +571,7 @@ static void registerRouter(Dict* config, struct Address *addr, struct Context* c
String* tunPath = Dict_getString(iface, BSTR("tunDevice"));
context->routerIf = TUNInterface_new(tunPath, context->base, context->allocator);
- if (TUNConfigurator_configure(context->routerIf, addr->ip6.bytes, 8) < 0) {
+ if (TUNConfigurator_configure(tunPath->bytes, context->routerIf, addr->ip6.bytes, 8) < 0) {
fprintf(stderr, "Couldn't configure TUN interface\n");
}
}
@@ -11,29 +11,40 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+
+#include <event2/event.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <netinet/in.h>
+
#ifdef __APPLE__
- #include <sys/types.h>
- #include <sys/socket.h>
#include <netdb.h>
- #include <net/if.h>
#include <net/if_var.h>
- #include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet6/nd6.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <stdio.h>
#define UTUN_OPT_IFNAME 2
+#else
+ #define INET6_ETHERTYPE ETH_P_IPV6
+
+ #include <linux/if.h>
+ #include <linux/if_tun.h>
+ #include <linux/if_ether.h>
#endif
+
#include "interface/Interface.h"
#include "interface/TUNConfigurator.h"
#include "interface/TUNInterface_struct.h"
-int TUNConfigurator_configure(struct Interface* interface, uint8_t *address/*16*/, int prefixLen) {
-#ifdef __APPLE__
- struct TUNInterface_struct* tun = (struct TUNInterface_struct*) interface->senderContext;
+int TUNConfigurator_configure(char* dev, struct Interface* interface, uint8_t *address/*16*/, int prefixLen) {
/* stringify our IP address */
char myIp[40];
@@ -55,6 +66,9 @@ int TUNConfigurator_configure(struct Interface* interface, uint8_t *address/*16*
address[14],
address[15]);
+#ifdef __APPLE__
+ struct TUNInterface_struct* tun = (struct TUNInterface_struct*) interface->senderContext;
+
/* set up the interface ip assignment request */
struct in6_aliasreq in6_addreq;
memset(&in6_addreq, 0, sizeof(in6_addreq));
@@ -126,7 +140,45 @@ int TUNConfigurator_configure(struct Interface* interface, uint8_t *address/*16*
close(s);
return 0;
+
#else
+
+ int s;
+ struct ifreq ifRequest;
+ struct in6_ifreq {
+ struct in6_addr ifr6_addr;
+ __u32 ifr6_prefixlen;
+ unsigned int ifr6_ifindex;
+ } ifr6;
+
+ if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
+ perror("socket");
+ return -1;
+ }
+
+ strncpy(ifRequest.ifr_name, dev, IFNAMSIZ);
+
+ if (ioctl(s, SIOGIFINDEX, &ifRequest) < 0)
+ {
+ perror("SIOGIFINDEX");
+ return -1;
+ }
+
+ ifr6.ifr6_ifindex = ifRequest.ifr_ifindex;
+ ifr6.ifr6_prefixlen = 128;
+ inet_pton(AF_INET6, myIp, &ifr6.ifr6_addr);
+
+ if (ioctl(s, SIOCSIFADDR, &ifr6) < 0) {
+ perror("SIOCSIFADDR");
+ return -1;
+ }
+
+ ifRequest.ifr_flags |= IFF_UP | IFF_RUNNING;
+ if (ioctl(s, SIOCSIFFLAGS, &ifRequest) < 0) {
+ perror("SIOCSIFFLAGS");
+ return -1;
+ }
+
return 0;
#endif
}
@@ -16,6 +16,6 @@
#include "interface/Interface.h"
-int TUNConfigurator_configure(struct Interface* interface, uint8_t *address/*16*/, int prefixLen);
+int TUNConfigurator_configure(char* dev, struct Interface* interface, uint8_t *address/*16*/, int prefixLen);
#endif

0 comments on commit 066eb13

Please sign in to comment.