Created
January 28, 2012 06:42
-
-
Save bringhurst/1693075 to your computer and use it in GitHub Desktop.
How to create a tun in osx without installing 3rd party crap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <netinet/in.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/kern_control.h> | |
#include <sys/ioctl.h> | |
#include <sys/sys_domain.h> | |
#include <sys/kern_event.h> | |
#include <sys/errno.h> | |
#define UTUN_CONTROL_NAME "com.apple.net.utun_control" | |
int get_utun_socket() { | |
int sock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL); | |
struct sockaddr_ctl addr; | |
/* get/set the id */ | |
struct ctl_info info; | |
memset(&info, 0, sizeof(info)) ; | |
strncpy(info.ctl_name, UTUN_CONTROL_NAME, strlen(UTUN_CONTROL_NAME)); | |
ioctl(sock, CTLIOCGINFO, &info) ; | |
addr.sc_id = info.ctl_id; | |
addr.sc_len = sizeof(addr) ; | |
addr.sc_family = AF_SYSTEM ; | |
addr.ss_sysaddr = AF_SYS_CONTROL ; | |
addr.sc_unit = 0 ; /* allocate dynamically */ | |
connect(sock, (struct sockaddr*)&addr, sizeof(addr)) ; | |
sleep(500); | |
return sock; | |
} | |
int main(void) { | |
get_utun_socket(); | |
} | |
/* EOf */ |
It may also be useful to review this:
https://github.com/cjdelisle/cjdns/blob/master/interface/tuntap/TUNInterface_darwin.c
This gist is an early experiment that eventually went into that code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may also need this to determine tun number: