如何在macOS中创建虚拟接口?

时间:2019-05-06 10:43:19

标签: macos vpn

我想为我的设备编写一个无法设置VPN的简单VPN。在Linux中,我可以创建一个tun接口来处理此问题。但是在macOS中,我在/dev/中找不到任何tun接口。

我找到一些实现此功能的软件来创建utun2接口,但我不知道该怎么做。有没有很好的例子?

1 个答案:

答案 0 :(得分:0)

关于这个问题,我找到两个很好的链接。

OpenVPNA node packe

因此,这是在macOS中创建uTun界面的方法:

#include <uv.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/kern_event.h>
#include <sys/socket.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <sys/kern_control.h>
#include <ctype.h>
#include <fcntl.h>


#define UTUN_CONTROL_NAME "com.apple.net.utun_control"
#define UTUN_OPT_IFNAME 2

int open_tun_socket () {
  struct sockaddr_ctl addr;
  struct ctl_info info;
  char ifname[20];
  socklen_t ifname_len = sizeof(ifname);
  int fd = -1;
  int err = 0;

  fd = socket (PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
  if (fd < 0) return fd;

  bzero(&info, sizeof (info));
  strncpy(info.ctl_name, UTUN_CONTROL_NAME, MAX_KCTL_NAME);

  err = ioctl(fd, CTLIOCGINFO, &info);
  if (err != 0) goto on_error;

  addr.sc_len = sizeof(addr);
  addr.sc_family = AF_SYSTEM;
  addr.ss_sysaddr = AF_SYS_CONTROL;
  addr.sc_id = info.ctl_id;
  addr.sc_unit = 0;

  err = connect(fd, (struct sockaddr *)&addr, sizeof (addr));
  if (err != 0) goto on_error;

  // TODO: forward ifname (we just expect it to be utun0 for now...)
  err = getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, ifname, &ifname_len);
  if (err != 0) goto on_error;

  printf("%s",ifname);

  // There is to close the socket,But in this case I don't need it.
  //err = fcntl(fd, F_SETFL, O_NONBLOCK);
  //if (err != 0) goto on_error;

  //fcntl(fd, F_SETFD, FD_CLOEXEC);
  //if (err != 0) goto on_error;

on_error:
  if (err != 0) {
    close(fd);
    return err;
  }

  return fd;
}

int main(){
    int result = open_tun_socket();
    while( true ){
    }
    printf("%d",result);
}