android接入traceroute
时间: 2023-11-06 13:06:27 浏览: 215
原始套接字 原始套接字的创建
4星 · 用户满意度95%
在 Android 上实现 Traceroute 通常需要使用 Native Code(C/C++)实现。下面提供一种基于使用 Java Native Interface(JNI)调用系统命令的方法。
1. 在 Android Studio 中创建一个新项目。
2. 在项目中创建一个名为“jni”的文件夹,然后创建一个名为“traceroute.c”的 C 文件。
3. 在 traceroute.c 文件中,添加以下代码:
```
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
JNIEXPORT void JNICALL
Java_com_example_traceroute_TracerouteActivity_traceroute(JNIEnv *env,
jobject instance, jstring host) {
const char *hostname = (*env)->GetStringUTFChars(env, host, NULL);
struct hostent *hostent = gethostbyname(hostname);
if (hostent == NULL) {
(*env)->ReleaseStringUTFChars(env, host, hostname);
return;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *(in_addr_t *) hostent->h_addr_list[0];
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
(*env)->ReleaseStringUTFChars(env, host, hostname);
return;
}
int ttl = 1;
int max_ttl = 30;
for (; ttl <= max_ttl; ttl++) {
setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(int));
int seq = rand() % 65535 + 1;
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
char packet[1024];
struct iphdr *ip_hdr = (struct iphdr *) packet;
ip_hdr->version = 4;
ip_hdr->ihl = 5;
ip_hdr->tos = 0;
ip_hdr->tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr));
ip_hdr->id = htons(getpid() & 0xFFFF);
ip_hdr->frag_off = 0;
ip_hdr->ttl = ttl;
ip_hdr->protocol = IPPROTO_ICMP;
ip_hdr->check = 0;
ip_hdr->saddr = 0;
ip_hdr->daddr = addr.sin_addr.s_addr;
struct icmphdr *icmp_hdr = (struct icmphdr *) (packet + sizeof(struct iphdr));
icmp_hdr->type = ICMP_ECHO;
icmp_hdr->code = 0;
icmp_hdr->un.echo.id = getpid() & 0xFFFF;
icmp_hdr->un.echo.sequence = seq;
icmp_hdr->checksum = 0;
icmp_hdr->checksum = htons(~(ICMP_ECHO << 8));
socklen_t fromlen = sizeof(struct sockaddr_in);
if (sendto(sock, packet, sizeof(packet), 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
continue;
}
char recv_packet[1024];
struct sockaddr_in recv_addr;
memset(&recv_addr, 0, sizeof(recv_addr));
int recv_len = recvfrom(sock, recv_packet, sizeof(recv_packet), 0, (struct sockaddr *) &recv_addr, &fromlen);
if (recv_len < sizeof(struct iphdr) + sizeof(struct icmphdr)) {
continue;
}
struct iphdr *recv_ip_hdr = (struct iphdr *) recv_packet;
if (recv_ip_hdr->daddr != addr.sin_addr.s_addr) {
continue;
}
int ip_hdr_len = recv_ip_hdr->ihl * 4;
struct icmphdr *recv_icmp_hdr = (struct icmphdr *) (recv_packet + ip_hdr_len);
if (recv_icmp_hdr->type != ICMP_ECHOREPLY || recv_icmp_hdr->un.echo.id != (getpid() & 0xFFFF) ||
recv_icmp_hdr->un.echo.sequence != seq) {
continue;
}
gettimeofday(&end_time, NULL);
double rtt = (end_time.tv_sec - start_time.tv_sec) * 1000.0 + (end_time.tv_usec - start_time.tv_usec) / 1000.0;
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &recv_addr.sin_addr, ip_str, INET_ADDRSTRLEN);
printf("%d\t%s\t%gms\n", ttl, ip_str, rtt);
if (recv_icmp_hdr->type == ICMP_ECHOREPLY) {
break;
}
}
close(sock);
(*env)->ReleaseStringUTFChars(env, host, hostname);
}
```
4. 在 MainActivity.java 文件中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("traceroute");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
traceroute("www.baidu.com");
}
});
}
public native void traceroute(String host);
}
```
5. 运行应用程序并单击按钮,将在控制台中看到类似以下内容的输出:
```
1 192.168.1.1 1.996ms
2 10.0.0.1 4.994ms
3 172.25.0.1 5.994ms
4 202.96.12.109 11.998ms
5 202.96.12.41 10.994ms
6 202.96.12.13 11.995ms
7 202.97.53.249 13.992ms
8 202.97.35.173 13.994ms
9 202.97.56.234 12.994ms
10 202.97.60.85 28.992ms
11 220.181.176.245 15.994ms
12 220.181.70.147 15.992ms
13 220.181.70.150 17.992ms
14 220.181.70.149 14.994ms
15 220.181.16.82 18.993ms
16 220.181.57.217 19.994ms
17 180.149.132.98 20.994ms
18 61.135.113.154 23.993ms
19 61.135.113.153 23.993ms
20 180.149.128.74 23.994ms
21 180.149.128.42 24.991ms
22 220.181.170.26 21.994ms
23 220.181.170.29 22.994ms
24 220.181.170.30 22.995ms
25 220.181.17.114 27.994ms
26 180.149.132.230 27.992ms
27 180.149.132.229 27.994ms
28 180.149.132.234 28.994ms
29 180.149.132.233 25.994ms
30 180.149.134.203 29.994ms
```
阅读全文