-
Notifications
You must be signed in to change notification settings - Fork 14
/
taptun_darwin.c
49 lines (42 loc) · 1.07 KB
/
taptun_darwin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#import <stdio.h>
#import <unistd.h>
#import <sys/socket.h>
#import <sys/sys_domain.h>
#import <net/if_utun.h>
#import <sys/kern_control.h>
#import <sys/ioctl.h>
#import <string.h>
#import <sys/errno.h>
void osxtun_open(int *fd, int *unit, char **err)
{
// open socket
*fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
struct ctl_info ci;
snprintf(ci.ctl_name, sizeof(ci.ctl_name), UTUN_CONTROL_NAME);
if (ioctl(*fd, CTLIOCGINFO, &ci) == -1) {
*err = strerror(errno);
close(*fd);
*fd = -1;
}
struct sockaddr_ctl sc;
sc.sc_id = ci.ctl_id;
sc.sc_len = sizeof(sc);
sc.sc_family = AF_SYSTEM;
sc.ss_sysaddr = AF_SYS_CONTROL;
int unit_nr = 0;
do {
sc.sc_unit = unit_nr + 1;
if (!connect(*fd, (struct sockaddr * )&sc, sizeof(sc))) {
break;
}
unit_nr++;
} while (sc.sc_unit < 255);
if (unit_nr > 254) {
*err = strerror(errno);
close(*fd);
*fd = -1;
return;
}
*unit = unit_nr;
}