clatd: no need for argument parsing via getopt

as we always pass in all of them in a well defined order

Test: TreeHugger, manually
Flag: EXEMPT mainline
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I93c4a9883983645cbe7d11101aa749dc2faf9d6d
This commit is contained in:
Maciej Żenczykowski 2025-08-05 10:43:04 -07:00
commit 1d2360adec
2 changed files with 27 additions and 69 deletions

View file

@ -44,20 +44,6 @@
*/
static void handle_sigterm(__attribute__((unused)) int unused) { sigterm = 1; };
/* function: print_help
* in case the user is running this on the command line
*/
void print_help() {
printf("android-clat arguments:\n");
printf("-i [uplink interface]\n");
printf("-p [plat prefix]\n");
printf("-4 [IPv4 address]\n");
printf("-6 [IPv6 address]\n");
printf("-t [tun file descriptor number]\n");
printf("-r [read socket descriptor number]\n");
printf("-w [write socket descriptor number]\n");
}
// Load the architecture identifier (AUDIT_ARCH_* constant)
#define BPF_SECCOMP_LOAD_AUDIT_ARCH \
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch))
@ -190,13 +176,6 @@ void enable_seccomp(void) {
* allocate and setup the tun device, then run the event loop
*/
int main(int argc, char **argv) {
struct tun_data tunnel;
int opt;
char *uplink_interface = NULL, *plat_prefix = NULL;
char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL,
*write_sock_str = NULL;
unsigned len;
// Clatd binary is setuid/gid CLAT, thus when we reach here we have:
// $ adb shell ps | grep clat
// [pid] [ppid]
@ -239,44 +218,23 @@ int main(int argc, char **argv) {
// Gid: 1029 1029 1029 1029
// Groups: 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1018 1021 1023 1024 1032 1065 3001 3002 3003 3005 3006 3007 3009 3010 3011 3012
while ((opt = getopt(argc, argv, "i:p:4:6:t:r:w:h")) != -1) {
switch (opt) {
case 'i':
uplink_interface = optarg;
break;
case 'p':
plat_prefix = optarg;
break;
case '4':
v4_addr = optarg;
break;
case '6':
v6_addr = optarg;
break;
case 't':
tunfd_str = optarg;
break;
case 'r':
read_sock_str = optarg;
break;
case 'w':
write_sock_str = optarg;
break;
case 'h':
print_help();
exit(0);
default:
logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt);
exit(1);
}
}
if (uplink_interface == NULL) {
logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
if (argc != 8) {
logmsg(ANDROID_LOG_FATAL, "incorrect arguments, expect: "
"uplink_interface plat_prefix IPv4_addr IPv6_addr tun_fd read_sock_fd write_sock_fd");
exit(1);
}
if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) {
const char* const uplink_interface = argv[1];
const char* const plat_prefix = argv[2];
const char* const v4_addr = argv[3];
const char* const v6_addr = argv[4];
const char* const tunfd_str = argv[5];
const char* const read_sock_str = argv[6];
const char* const write_sock_str = argv[7];
struct tun_data tunnel;
if (!parse_int(tunfd_str, &tunnel.fd4)) {
logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str);
exit(1);
}
@ -285,7 +243,7 @@ int main(int argc, char **argv) {
exit(1);
}
if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) {
if (!parse_int(read_sock_str, &tunnel.read_fd6)) {
logmsg(ANDROID_LOG_FATAL, "invalid read socket %s", read_sock_str);
exit(1);
}
@ -294,7 +252,7 @@ int main(int argc, char **argv) {
exit(1);
}
if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) {
if (!parse_int(write_sock_str, &tunnel.write_fd6)) {
logmsg(ANDROID_LOG_FATAL, "invalid write socket %s", write_sock_str);
exit(1);
}
@ -303,24 +261,24 @@ int main(int argc, char **argv) {
exit(1);
}
len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
unsigned len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
if (len >= sizeof(tunnel.device4)) {
logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
exit(1);
}
Global_Clatd_Config.native_ipv6_interface = uplink_interface;
if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
if (inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
exit(1);
}
if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) {
if (!inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) {
logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr);
exit(1);
}
if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
if (!inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
exit(1);
}

View file

@ -397,13 +397,13 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
// clang-format off
const char* args[] = {progname.c_str(),
"-i", ifaceStr.c_str(),
"-p", pfx96Str.c_str(),
"-4", v4Str.c_str(),
"-6", v6Str.c_str(),
"-t", tunFdStr,
"-r", sockReadStr,
"-w", sockWriteStr,
ifaceStr.c_str(),
pfx96Str.c_str(),
v4Str.c_str(),
v6Str.c_str(),
tunFdStr,
sockReadStr,
sockWriteStr,
nullptr};
// clang-format on