Skip to content

Instantly share code, notes, and snippets.

@sanae10001
Forked from bringhurst/gist:1693075
Created October 25, 2017 10:48
Show Gist options
  • Save sanae10001/fa908fda6420b6c90567e202ae19cc42 to your computer and use it in GitHub Desktop.
Save sanae10001/fa908fda6420b6c90567e202ae19cc42 to your computer and use it in GitHub Desktop.
How to create a tun in osx without installing 3rd party crap
#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 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment