android - 在没有root的linux/android上为UDP数据包分配传出接口(interface)

标签 android linux sockets routes

我正在尝试为没有 root 的 UDP 数据包分配传出接口(interface)。

目前我已经找到了

  1. IP_PKTINFO 将修改源地址,但接口(interface)选择仍将基于路由表

  2. SO_BINDTODEVICE 可以工作但需要 root

  3. 修改路由表需要我没有的权限。

我在 Solaris 上找到了 IP_NEXTHOP,但显然它不在 Linux 中。

有什么想法吗?

最佳答案

所以我在 API 级别 22 中找到了这个功能

bindSocket

void bindSocket(DatagramSocket 套接字)

Binds the specified DatagramSocket to this Network. All data traffic on the socket will be sent on this Network, irrespective of any process-wide network binding set by bindProcessToNetwork(Network). The socket must not be connected.

您使用此请求网络

requestNetwork

void requestNetwork (NetworkRequest 请求, PendingIntent 操作)

Request a network to satisfy a set of NetworkCapabilities. This function behaves identically to the version that takes a NetworkCallback, but instead of ConnectivityManager.NetworkCallback a PendingIntent is used. This means the request may outlive the calling application and get called back when a suitable network is found.

The operation is an Intent broadcast that goes to a broadcast receiver that you registered with registerReceiver(BroadcastReceiver, IntentFilter) or through the tag in an AndroidManifest.xml file

The operation Intent is delivered with two extras, a Network typed extra called EXTRA_NETWORK and a NetworkRequest typed extra called EXTRA_NETWORK_REQUEST containing the original requests parameters. It is important to create a new, ConnectivityManager.NetworkCallback based request before completing the processing of the Intent to reserve the network or it will be released shortly after the Intent is processed.

If there is already a request for this Intent registered (with the equality of two Intents defined by filterEquals(Intent)), then it will be removed and replaced by this one, effectively releasing the previous NetworkRequest.

The request may be released normally by calling releaseNetworkRequest(android.app.PendingIntent).

It is presently unsupported to request a network with either NET_CAPABILITY_VALIDATED or NET_CAPABILITY_CAPTIVE_PORTAL as these NetworkCapabilities represent states that a particular network may never attain, and whether a network will attain these states is unknown prior to bringing up the network so the framework does not know how to go about satisfing a request with these capabilities.

This method requires the caller to hold either the CHANGE_NETWORK_STATE permission or the ability to modify system settings as determined by canWrite(Context).

因此,如果您请求当前可用的网络,您可以将套接字绑定(bind)到它。

关于android - 在没有root的linux/android上为UDP数据包分配传出接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185520/

相关文章:

android - 如何在android xml中绘制三角形

java - Camera.Parameters 设备特定崩溃(Samsung S3 Mini)

python - 探测正在运行的 python 进程以获取信息

node.js - Node 上有 2 个不同的端口,带有 azure

c++ - DLib - 为什么 start_async() 不接受后台线程中的连接?

java - ListView 未导航到其他 Activity

安卓布局问题

linux - 分支与 merge 策略 - Linux 内核

php - 为什么 PHP 卡在 mysqli_connect 而没有错误代码?

python - 退出/崩溃时关闭服务器套接字