Skip to content

cmd/tailscale: implement getInterfaces + SDK 30 - #21

Merged
DentonGentry merged 2 commits into
mainfrom
getinterfaces
Oct 8, 2021
Merged

DentonGentry merged 2 commits into
mainfrom
getinterfaces

Conversation

@DentonGentry

@DentonGentry DentonGentry commented Oct 4, 2021 •

Copy link
Copy Markdown
Contributor

SDK 30 prohibits syscall.NetlinkRIB(syscall.RTM_GETADDR, ...) which Go's net.Interfaces uses. Implement an Android specific version of net.Interfaces to use instead.

Passing primitive types across JNI is relatively straightforward, passing a single object of a complex class is annoying but still possible, but passing lists and other more complex data structures is way harder. As such, this commit added a Java routine to render the interface information to a string and pass that across JNI as a primitive type for Go code to parse.

Fixes tailscale/tailscale#2293

Comment thread cmd/tailscale/main.go Outdated
rcfg *router.Config
dcfg *dns.OSConfig
}
interfaces.RegisterInterfaceGetter(a.getInterfaces)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try doing this line earlier in func main, before the goroutine to start runBackend?

I think it's fine here, but I can't convince myself there's not a race from something else.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@DentonGentry
DentonGentry force-pushed the getinterfaces branch 3 times, most recently from c7efa63 to 5286bed Compare October 7, 2021 04:22
@DentonGentry DentonGentry changed the title WIP: cmd/tailscale: implement getInterfaces. cmd/tailscale: implement getInterfaces + SDK 30 Oct 7, 2021
Signed-off-by: Denton Gentry <[email protected]>
@DentonGentry

Copy link
Copy Markdown
Contributor Author

Tested on Android 6.0.1 and Android 12.

// v4-rmnet_data2 68 1472 true true false true true | 192.0.0.4/32
//
// Where the fields are:
// name ifindex mtu isUp hasBroadcast isLoopback isPointToPoint hasMulticast | ip1/N,ip2/N,ip3/N;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray semicolon at the end?

Oh! that's the separator, not a line delimiter.

How about remove the semicolon and just make every line end with a \n ?

Include the newline in the String iface and the join on the empty string instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, used newline as line separator.

return "";
}

List<String> ifs = new ArrayList<String>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A java.lang.StringBuilder would be a bit less alloc heavy than all the String splitting and joining and formatting below.

But perhaps it doesn't matter at this point. We shouldn't call this super often? On Android it's currently every 10 minutes, or on link change notifications from the OS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used StringBuilder.

Comment thread cmd/tailscale/main.go Outdated
}
}

ifaces = append(ifaces, *newIf)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop the * from this line and the & from line 1295?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread cmd/tailscale/main.go
}

for _, iface := range strings.Split(ifaceString, ";") {
// Example of the strings we're processing:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go with newline-separated lines:

if strings.TrimSpace(line) == "" {
    continue
}

So you don't need to worry about the final empty element and still use Split here.

(strings.Split is generally not my friend, since it allocates, but this is no worse than the Java side's allocs, so it's fine for now.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with TrimSpace().

Java now uses a StringBuilder, we can revisit the Go code later.

@crawshaw crawshaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings are the secret to ffi

Comment thread go.mod
golang.zx2c4.com/wireguard v0.0.0-20210905140043-2ef39d47540c
inet.af/netaddr v0.0.0-20210721214506-ce7a8ad02cc1
tailscale.com v1.1.1-0.20211005220235-67e5fabdbdba
tailscale.com v1.1.1-0.20211008004653-1f506d23514a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.6.0 now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used a release-branch for 1.14 in tailscale-android and it worked well.
Will update to 1.16.0 when release-branch/1.16 is created.

@DentonGentry
DentonGentry force-pushed the getinterfaces branch 2 times, most recently from 92c0865 to 9ec3d84 Compare October 8, 2021 23:08
SDK 30 prohibits syscall.NetlinkRIB(syscall.RTM_GETADDR, ...)
which Go's net.Interfaces uses. Implement an Android
specific version of net.Interfaces to use instead.

Passing primitive types across JNI is relatively straightforward,
passing a single object of a complex class is annoying but still
possible, but passing lists and other more complex data structures is
way harder. As such, this commit added a Java routine to render the
interface information to a string and pass that across JNI as a
primitive type for Go code to parse.

Fixes tailscale/tailscale#2293
@DentonGentry

Copy link
Copy Markdown
Contributor Author

I think this will work for 1.16.0, and lets us move to SDK 30.

@DentonGentry
DentonGentry merged commit f0dcec6 into main Oct 8, 2021
@DentonGentry
DentonGentry deleted the getinterfaces branch October 8, 2021 23:19
nadeemakhter0602 pushed a commit to nadeemakhter0602/TailMon that referenced this pull request Mar 22, 2026
cmd/tailscale: implement getInterfaces + SDK 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

android: move to targetSdkVersion 30

3 participants