-
Notifications
You must be signed in to change notification settings - Fork 13
/
tuntap_osx_app.m
247 lines (196 loc) · 6.66 KB
/
tuntap_osx_app.m
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* (C) 2007-09 - Luca Deri <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
*/
#include "n2n.h"
#ifdef _DARWIN_
void tun_close(tuntap_dev *device);
#include <sys/socket.h>
#include <sys/un.h>
#define QLEN 10
#define ADDRESS "/tmp/n2n"
/* size of control buffer to send/recv one file descriptor */
#define CONTROLLEN CMSG_LEN(sizeof(int))
static struct cmsghdr *cmptr = NULL; /* malloc'ed first time */
/*
* Receive a file descriptor from a server process. Also, any data
* received is passed to (*userfunc)(STDERR_FILENO, buf, nbytes).
* We have a 2-byte protocol for receiving the fd from send_fd().
*/
int
recv_fd(int fd)
{
int newfd, nr, status;
char *ptr;
char buf[256];
struct iovec iov[1];
struct msghdr msg;
status = -1;
for ( ; ; ) {
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL)
return(-1);
msg.msg_control = cmptr;
msg.msg_controllen = CONTROLLEN;
if ((nr = recvmsg(fd, &msg, 0)) < 0) {
perror("recvmsg error");
} else if (nr == 0) {
perror("connection closed by server");
return(-1);
}
/*
* See if this is the final data with null & status. Null
* is next to last byte of buffer; status byte is last byte.
* Zero status means there is a file descriptor to receive.
*/
for (ptr = buf; ptr < &buf[nr]; ) {
if (*ptr++ == 0) {
if (ptr != &buf[nr-1])
perror("message format error");
status = *ptr & 0xFF; /* prevent sign extension */
if (status == 0) {
if (msg.msg_controllen != CONTROLLEN)
perror("status = 0 but no fd");
newfd = *(int *)CMSG_DATA(cmptr);
} else {
newfd = -status;
}
nr -= 2;
}
}
if (status >= 0) /* final data has arrived */
return(newfd); /* descriptor, or -status */
}
}
int sock_client()
{
int s,len;
int fd;
int err;
struct sockaddr_un saun;
if((s = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0){
perror("client: socket");
}
saun.sun_family = PF_LOCAL;
strcpy(saun.sun_path, ADDRESS);
do {
usleep(1000);
err = connect(s, (const struct sockaddr *)&saun, sizeof(saun));
} while (err < 0);
fd = recv_fd(s);
return fd;
}
/* ********************************** */
#define N2N_OSX_TAPDEVICE_SIZE 32
int tuntap_open(tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
int i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
NSString *filename = @"~/Library/Application Support/ganesh/n2n.app/Contents/Resources/TunHelper";
NSArray *arguments = [NSArray arrayWithObjects: @"-s", [NSString stringWithCString:device_ip encoding:NSUTF8StringEncoding], nil];
NSTask *helper = [[NSTask alloc] init];
[helper setLaunchPath:filename];
[helper setArguments:arguments];
[helper launch];
device->fd = sock_client();
[helper waitUntilExit];
i = [helper terminationStatus];
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);
if(device->fd > 0) {
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
}
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open tap device");
return(-1);
} else {
char buf[256];
FILE *fd;
device->ip_addr = inet_addr(device_ip);
if ( device_mac && device_mac[0] != '\0' )
{
/* FIXME - This is not tested. Might be wrong syntax for OS X */
/* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s",
i, device_mac);
system(buf);
}
#if 0
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up",
i, device_ip, device_mask, mtu);
system(buf);
#endif
helper = [[NSTask alloc] init];
arguments = [NSArray arrayWithObjects: @"-d", [NSString stringWithFormat:@"%d", i], nil];
[helper setLaunchPath:filename];
[helper setArguments:arguments];
[helper launch];
[helper waitUntilExit];
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)",
i, device_ip, device_mask);
/* Read MAC address */
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
/* traceEvent(TRACE_INFO, "%s", buf); */
fd = popen(buf, "r");
if(fd < 0) {
tun_close(device);
return(-1);
} else {
int a, b, c, d, e, f;
buf[0] = 0;
fgets(buf, sizeof(buf), fd);
pclose(fd);
if(buf[0] == '\0') {
traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address");
exit(0);
}
traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, buf);
if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) {
device->mac_addr[0] = a, device->mac_addr[1] = b;
device->mac_addr[2] = c, device->mac_addr[3] = d;
device->mac_addr[4] = e, device->mac_addr[5] = f;
}
}
}
/* read_mac(dev, device->mac_addr); */
return(device->fd);
}
/* ********************************** */
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len));
}
/* ********************************** */
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len));
}
/* ********************************** */
void tuntap_close(struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
}
#endif /* _DARWIN_ */