Skip to content

Commit

Permalink
added ability to ipconfig set DHCP in tunhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Jelveh committed Aug 17, 2010
1 parent 6951e7f commit 6cfabf1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
53 changes: 38 additions & 15 deletions osx/TunHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,56 @@ int sock_server(int fd_to_send){
assumes(close(s) == 0);
}

#define MAX_CMDLINE_BUFFER 256

int main (int argc, const char * argv[]) {
int fd, i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
char ip_address[255];
char ip_address[MAX_CMDLINE_BUFFER];
char buf[MAX_CMDLINE_BUFFER];
char cmd_switch[3];
char tap_num[4];
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

if(argc<2){
NSLog(@"TunHelper, ip address is missing");
if(argc<3){
NSLog(@"TunHelper parameters incorrect");
return -1;
}
strncpy(ip_address, argv[1], sizeof(ip_address));

NSLog(@"TunHelper started!, ip: %s, %d", ip_address, argc);
for (i = 0; i < 255; i++) {
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);

fd = open(tap_device, O_RDWR);
if(fd > 0) {
NSLog(@"Succesfully opened %s, fd: %d", tap_device, fd);

setup_ipv4(i,ip_address, "255.255.255.0", 1400);
sock_server(fd);
break;
// some really simple input formatting checks
strncpy(cmd_switch, argv[1], sizeof(cmd_switch));
if(cmd_switch[0] == '-'){
if(cmd_switch[1] == 's'){
strncpy(ip_address, argv[2], sizeof(ip_address));
NSLog(@"TunHelper started!, ip: %s, %d", ip_address, argc);
for (i = 0; i < 255; i++) {
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);

fd = open(tap_device, O_RDWR);
if(fd > 0) {
NSLog(@"Succesfully opened %s, fd: %d", tap_device, fd);

setup_ipv4(i,ip_address, "255.255.255.0", 1400);
sock_server(fd);
break;
}
}
}
else if(cmd_switch[1] == 'd'){
// 255 = maximum 4 characters 255\0
strncpy(tap_num, argv[2], sizeof(tap_num));
tap_num[3] = '\0';
i = atoi(tap_num);
if(i < 256){
setuid(0);
snprintf(buf, sizeof(buf), "ipconfig set tap%d DHCP", i);
NSLog(@"Executing buf %s", buf);
system(buf);
}
}
}


[pool drain];
return i;
}
13 changes: 10 additions & 3 deletions tuntap_osx_app.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
int i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
NSString *filename = @"~/Library/Application Support/ganesh/n2n.app/Contents/Resources/TunHelper";
NSArray *arguments = [NSArray arrayWithObjects:[NSString stringWithCString:device_ip encoding:NSUTF8StringEncoding], nil];
NSArray *arguments = [NSArray arrayWithObjects: @"-s", [NSString stringWithCString:device_ip encoding:NSUTF8StringEncoding], nil];
NSTask *helper = [[NSTask alloc] init];

[helper setLaunchPath:filename];
Expand Down Expand Up @@ -172,8 +172,15 @@ int tuntap_open(tuntap_dev *device /* ignored */,
system(buf);
#endif

snprintf(buf, sizeof(buf), "sudo ipconfig set tap%d DHCP", i);
system(buf);
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);
Expand Down

0 comments on commit 6cfabf1

Please sign in to comment.