diff --git a/CommandListener.cpp b/CommandListener.cpp index 782cf1b597770c35f719e4a891779280948bcc64..b04ec81d1b3c1d6745c412072394d08ff8dd6cac 100644 --- a/CommandListener.cpp +++ b/CommandListener.cpp @@ -324,17 +324,24 @@ int CommandListener::TetherCmd::runCommand(SocketClient *cli, } if (!strcmp(argv[1], "start")) { - struct in_addr s, e; - - if (!inet_aton(argv[2], &s)) { - cli->sendMsg(ResponseCode::CommandParameterError, "Invalid start address", false); + if (argc % 2 == 1) { + cli->sendMsg(ResponseCode::CommandSyntaxError, "Bad number of arguments", false); return 0; } - if (!inet_aton(argv[3], &e)) { - cli->sendMsg(ResponseCode::CommandParameterError, "Invalid end address", false); - return 0; + + int num_addrs = argc - 2; + int arg_index = 2; + int array_index = 0; + in_addr *addrs = (in_addr *)malloc(sizeof(in_addr) * num_addrs); + while (array_index < num_addrs) { + if (!inet_aton(argv[arg_index++], &(addrs[array_index++]))) { + cli->sendMsg(ResponseCode::CommandParameterError, "Invalid address", false); + free(addrs); + return 0; + } } - rc = sTetherCtrl->startTethering(s, e); + rc = sTetherCtrl->startTethering(num_addrs, addrs); + free(addrs); } else if (!strcmp(argv[1], "interface")) { if (!strcmp(argv[2], "add")) { rc = sTetherCtrl->tetherInterface(argv[3]); diff --git a/TetherController.cpp b/TetherController.cpp index 08f6bb130961d827476ff9880a8ee03d3262c21f..30ff165fbbe33d35a41cc932d580dd8b4d0cca16 100644 --- a/TetherController.cpp +++ b/TetherController.cpp @@ -86,8 +86,7 @@ bool TetherController::getIpFwdEnabled() { return (enabled == '1' ? true : false); } -int TetherController::startTethering(struct in_addr dhcpStart, struct in_addr dhcpEnd) { - +int TetherController::startTethering(int num_addrs, struct in_addr* addrs) { if (mDaemonPid != 0) { LOGE("Tethering already started"); errno = EBUSY; @@ -124,17 +123,27 @@ int TetherController::startTethering(struct in_addr dhcpStart, struct in_addr dh } close(pipefd[0]); } - char *start = strdup(inet_ntoa(dhcpStart)); - char *end = strdup(inet_ntoa(dhcpEnd)); - char *range; - asprintf(&range, "--dhcp-range=%s,%s,1h", start, end); + int num_processed_args = 4 + (num_addrs/2) + 1; // 1 null for termination + char **args = (char **)malloc(sizeof(char *) * num_processed_args); + args[num_processed_args - 1] = NULL; + args[0] = (char *)"/system/bin/dnsmasq"; + args[1] = (char *)"--no-daemon"; + args[2] = (char *)"--no-resolv"; + args[3] = (char *)"--no-poll"; + + int nextArg = 4; + for (int addrIndex=0; addrIndex < num_addrs;) { + char *start = strdup(inet_ntoa(addrs[addrIndex++])); + char *end = strdup(inet_ntoa(addrs[addrIndex++])); + asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end); + } - if (execl("/system/bin/dnsmasq", "/system/bin/dnsmasq", "--no-daemon", "--no-resolv", - "--no-poll", "--no-hosts", range, (char *) NULL)) { + if (execv(args[0], args)) { LOGE("execl failed (%s)", strerror(errno)); } LOGE("Should never get here!"); + free(args); return 0; } else { close(pipefd[0]); @@ -237,4 +246,3 @@ int TetherController::untetherInterface(const char *interface) { InterfaceCollection *TetherController::getTetheredInterfaceList() { return mInterfaces; } - diff --git a/TetherController.h b/TetherController.h index 208780a1c48505480bf9eeaaf1048dae5cfdedb3..1fd4f958eaade7e4917a4f998adc513cbcd08385 100644 --- a/TetherController.h +++ b/TetherController.h @@ -37,7 +37,8 @@ public: int setIpFwdEnabled(bool enable); bool getIpFwdEnabled(); - int startTethering(struct in_addr dhcpStart, struct in_addr dhcpEnd); + int startTethering(int num_addrs, struct in_addr* addrs); + int stopTethering(); bool isTetheringStarted();