您当前的位置:首页 > 计算机 > 系统应用 > Linux

Linux下完美驱动HTC Android手机的Internet传输(通过USB共享电脑网络)

时间:03-07来源:作者:点击数:

Htc android手机的Internet传输(Internet pass-through)是手机使用USB连接线通过PC上网,共享电脑网络。

但是,这么好的功能需要在windows系统下安装最新版的HTC Sync才能启动。

linux下怎么才能使用Internet传输功能呢?

------------------------------------------------------------

前期做的研究:Android手机通过USB数据线共享Linux电脑网络(internet传输)

能勉强使用,但是很麻烦,不完善,缺陷有:

1.需要手机有root权限。

2.需要在手机终端上输入大量命令。

3.实际联上网,手机却一直显示脱机状态。

------------------------------------------------------------

Internet传输这么好的功能,难道在Linux下就只能这么将就的用着?对Linuxer来说,没有什么是不可能的!不需root,不需敲命令,手机完美进入internet传输状态,这都可以实现。

项目地址:https://github.com/qileilu/htcnetup

一、win下htc sync激活internet传输过程——抓包

要想像htc sync那样完美的激活internet传输,我们先小小的hacker一下这个htc sync。

这里我使用tcpdump来抓包。

1.首先把tcpdump文件放到手机的/data/local/tmp/目录下。(tcpdump下载)

adb push tcpdump  /data/local/tmp/
adb shell  chmod  777  /data/locla/tmp/tcpdump

2.win下打开终端,转到adb.exe目录,插入手机后,运行命令:

adb shell  su  -c  "./data/local/tmp/tcpdump -nnX -i usb0"

说明:做这一步是需要手机root权限,毕竟是抓包啊;命令运行要快,时机很重要,太早adb连接还没建立,太晚很多包又抓不上;网络设备是usb0。

3.抓包结果如下:

tcpdump: verbose output suppressed, use - v  or -vv  for  full protocol decode 
listening on usb0, link- type  EN10MB (Ethernet), capture size 96 bytes 
15:28:09.408922 IP 192.168.99.89.53370 > 192.168.99.214.6000: S 4103028695:41030 28695(0) win 8192          
         0x0000:  4500 0034 000f 4000 8006 b234 c0a8 6359  E..4..@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fd7 0000 0000  括c中z.p???...          
         0x0020:  8002 2000 ea3d 0000 0204 05b4 0103 0308  ....?.....?...          
         0x0030:  0101 0402                                .... 
15:28:09.409135 IP 192.168.99.214.6000 > 192.168.99.89.53370: S 793036861:793036 861(0) ack 4103028696 win 65535           
         0x0000:  4500 0034 0000 4000 4006 f243 c0a8 63d6  E..4..@.@.駽括c?          
         0x0010:  c0a8 6359 1770 d07a 2f44 c83d f48f 3fd8  括cY.p衵 /D ????          
         0x0020:  8012 ffff 48a7 0000 0204 05b4 0101 0402  ..H?....?...          
         0x0030:  0103 0301                                .... 
15:28:09.409929 IP 192.168.99.89.53370 > 192.168.99.214.6000: . ack 1 win 256          
         0x0000:  4500 0028 0010 4000 8006 b23f c0a8 6359  E..(..@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fd8 2f44 c83e  括c中z.p???D?          
         0x0020:  5010 0100 527e 0000                      P...R~.. 
15:28:09.410600 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 1:5(4) ack 1 win   256          
         0x0000:  4500 002c 0011 4000 8006 b23a c0a8 6359  E..,..@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fd8 2f44 c83e  括c中z.p???D?          
         0x0020:  5018 0100 5270 0000 0002 0000            P...Rp...... 
15:28:09.410753 IP 192.168.99.214.6000 > 192.168.99.89.53370: . ack 5 win 32766          
         0x0000:  4500 0028 ef33 4000 4006 031c c0a8 63d6  E..(?@.@...括c?          
         0x0010:  c0a8 6359 1770 d07a 2f44 c83e f48f 3fdc  括cY.p衵 /D ????          
         0x0020:  5010 7ffe 489b 0000                      P..﨟... 
15:28:09.419328 IP 192.168.99.214.6000 > 192.168.99.89.53370: P 1:5(4) ack 5 win   32766          
         0x0000:  4500 002c ef34 4000 4006 0317 c0a8 63d6  E..,?@.@...括c?          
         0x0010:  c0a8 6359 1770 d07a 2f44 c83e f48f 3fdc  括cY.p衵 /D ????          
         0x0020:  5018 7ffe 536d 0000 8002 0000            P..m...... 
15:28:09.636522 IP 192.168.99.89.53370 > 192.168.99.214.6000: . ack 5 win 256          
         0x0000:  4500 0028 0013 4000 8006 b23c c0a8 6359  E..(..@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fdc 2f44 c842  括c中z.p???D菳          
         0x0020:  5010 0100 5276 0000                      P...Rv.. 
15:28:12.636705 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 5:9(4) ack 5 win   256          
         0x0000:  4500 002c 0025 4000 8006 b226 c0a8 6359  E..,.%@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fdc 2f44 c842  括c中z.p???D菳          
         0x0020:  5018 0100 5267 0000 0003 0000            P...Rg...... 
15:28:12.638841 IP 192.168.99.214.6000 > 192.168.99.89.53370: P 5:9(4) ack 9 win   32764          
         0x0000:  4500 002c ef35 4000 4006 0316 c0a8 63d6  E..,?@.@...括c?          
         0x0010:  c0a8 6359 1770 d07a 2f44 c842 f48f 3fe0  括cY.p衵 /D 菳???          
         0x0020:  5018 7ffc 5366 0000 8003 0000            P..黃f...... 
15:28:12.639848 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 9:13(4) ack 9 wi n 256          
         0x0000:  4500 002c 0026 4000 8006 b225 c0a8 6359  E..,.&@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fe0 2f44 c846  括c中z.p???D菷          
         0x0020:  5018 0100 525d 0000 0005 0000            P...R]...... 
15:28:12.640062 IP 192.168.99.89.53370 > 192.168.99.214.6000: F 13:13(0) ack 9 w in  256          
         0x0000:  4500 0028 0027 4000 8006 b228 c0a8 6359  E..(.'@...?括cY          
         0x0010:  c0a8 63d6 d07a 1770 f48f 3fe4 2f44 c846  括c中z.p???D菷          
         0x0020:  5011 0100 5269 0000                      P...Ri.. 
15:28:12.675279 IP 192.168.99.214.6000 > 192.168.99.89.53370: . ack 14 win 32762              
         0x0000:  4500 0028 ef36 4000 4006 0319 c0a8 63d6  E..(?@.@...括c?          
         0x0010:  c0a8 6359 1770 d07a 2f44 c846 f48f 3fe5  括cY.p衵 /D 菷???          
         0x0020:  5010 7ffa 489b 0000                      P..鶫...

看不懂?没关系,我看懂了:

a.手机在电脑生成的网口地址:192.168.99.89 端口53370;手机网口地址:192.168.99.214 端口 6000;

b.电脑向手机发送的第一次数据:4字节,内容:0002 0000; 手机向电脑应答:4字节 80002 0000;

c.电脑向手机发送的第二次数据:4字节,内容:0003 0000; 手机向电脑应答:4字节 80003 0000;

d.电脑向手机发送的第三次数据:4字节,内容:0005 0000; 手机无应答,internet传输模式建立。

好了,有这些就够了,接下来我们在linux下实现这个过程。

二、Linux下激活internet传输——编程

有了上述htc sync激活internet传输过程的抓包,我们要做的就是在linux下模仿这个激活过程。

这里我用c来写。

1.程序流程

1)主程序:

a.接收输入的手机IP地址

b.建立socket

c.建立connect

d.调用激活子程序

e.关闭socket退出

2)激活子程序:

a.向手机IP的6000端口,发送0x00020000,

b.收到手机发来的0x8002000后,向手机IP的6000端口发送0x00030000

c.收到手机发来的0x8003000后,向手机IP的6000端口发送0x00050000

2.程序代码

/* htcnetup.c
  * LuoXiaoqiu <qilvilu@gmail.com>
  * An activater for HTC android phone Internet pass-through.
  */
   
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
    
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>//close()
#include<netinet/in.h>//struct sockaddr_in
#include<arpa/inet.h>//inet_ntoa
   
#define DEST_PORT 6000
   
   
/* activater process*/
void  process_info( int  s)
{
         int  send_num;
         int  recv_num;      
         char  recv_buf[4];
   
         /*the first handshaking*/
         send_num = send(s, "\x00\x02\x00\x00" ,4,0);
         if  (send_num < 0){
             perror ( "send" );
                 exit (1);
         }  else  {
         printf ( "send 0x00020000 sucess!\n" );
         }
   
         recv_num = recv(s,recv_buf, sizeof (recv_buf),0);
         if (recv_num < 0){
                 perror ( "recv" );
                 exit (1);
         }  else  {
                 recv_buf[recv_num]= '\0' ;
         if  ( strcmp (recv_buf,  "\x80\x02\x00\x00" ) != 0){
             perror ( "0x80020000 handshaking failure" );
                         exit (1);
         }  else  {
             printf ( "recv 0x80020000 sucess!\n" );
   
             /*the second handshaking*/
             send_num = send(s, "\x00\x03\x00\x00" ,4,0);
                     if  (send_num < 0){
                             perror ( "send" );
                             exit (1);
                     }  else  {
                             printf ( "send 0x00030000 sucess!\n" );
                     }
         }
     }
   
   
         recv_num = recv(s,recv_buf, sizeof (recv_buf),0);
     if (recv_num < 0){
                 perror ( "recv" );
                 exit (1);
         }  else  {
                 recv_buf[recv_num]= '\0' ;
         if  ( strcmp (recv_buf,  "\x80\x03\x00\x00" ) != 0){
             perror ( "0x80030000 handshaking failure" );
                         exit (1);
         }  else  {
             printf ( "recv 0x80030000 sucess!\n" );
   
             /*the third handshaking*/
             send_num = send(s, "\x00\x05\x00\x00" ,4,0);
                     if  (send_num < 0){
                             perror ( "send" );
                             exit (1);
                     }  else  {
                 printf ( "send 0x00050000 sucess!\n" );
                     }
            
         }
     }
     printf ( "the activater process success!\n" );
}
   
   
   
int  main( int  argc, char  *argv[])
{
         int  sock_fd;
         struct  sockaddr_in addr_serv;
   
     /* check args */
     if (argc != 2)
     {
     printf ( "usage: htcnetup <IPaddress>\n" );
     exit (1);
     }
   
   
         sock_fd = socket(AF_INET,SOCK_STREAM,0);
         if (sock_fd < 0){
                perror ( "sock" );
                 exit (1);
         }  else  {
                 printf ( "sock sucessful\n" );
         }
         memset (&addr_serv,0, sizeof (addr_serv));
         addr_serv.sin_family = AF_INET;
         addr_serv.sin_port =  htons(DEST_PORT);
         addr_serv.sin_addr.s_addr = inet_addr(argv[1]);
        if ( connect(sock_fd,( struct  sockaddr *)&addr_serv, sizeof ( struct  sockaddr)) < 0){
                 perror ( "connect" );
                 printf ( "connect (%d)\n" , errno );
                 exit (1);
        }  else  {
                 printf ( "connect sucessful\n" );
        }
         process_info(sock_fd);
         close(sock_fd);
}

3.程序编译执行

gcc编译:

gcc  -o htcnetup htcnetup.c

执行:

. /htcnetup  192.168.99.214

这里的命令参数192.168.99.214是手机的IP地址。手机ip地址可以用下列命令获取:

grep  -e  "usb0"  /proc/net/arp  |  grep  -oP  "\d+\.\d+\.\d+\.\d+"

执行完程序后,你会发现手机状态栏久违的internet传输图标出现了!手机的internet传输功能在linux下完美激活!

看图左上角:

三、Linux下的包转发和网关服务

手机的internet传输激活之后(或者说最好是之前),还需要设置一下Linux系统的包转发。

网关服务在 Linux 系统中非常容易实现的,两条命令即可完成:

sudo  sysctl net.ipv4.ip_forward=1
sudo  iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  # eth0 是 PC 端 Internet 出口网络接口名称

DNS 服务在 Linux 系统中也有很多实现,不过如果是访问 Internet 的话,还是使用 Iptables 转到 8.8.8.8 解决更简便:

sudo  iptables -t nat -A PREROUTING -i usb0 -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.8.8:53

(此处参考:Linux 系统使用 HTC Internet pass-through)

四、shell脚本编制

总结前面步骤,首先需设置linux系统网关服务,然后在运行internet传输激活程序前,还需知道手机端的ip地址,把这些内容在一个shell脚本中实现如下:

#!/bin/bash
   
#htcnet.sh
#LuoXiaoqiu <qilvilu@gmail.com>
#An activater for HTC android phone Internet pass-through.
   
#PC's internet interface
pc_internet_device= "eth0"
#phone's connection  interface in pc
phone_usb_device= "usb0"
   
sudo  sysctl -w net.ipv4.ip_forward=1
sudo  iptables -t nat -A POSTROUTING -o $pc_internet_device -j MASQUERADE
sudo  iptables -t nat -A PREROUTING -i $phone_usb_device -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.8.8:53
   
get_ip ()
{
     arp -n |  grep  $phone_usb_device |  awk  '{print $1}'
}
   
#TODO: This needs a timeout and loop needs cleaning up, but works fine and borrowed from another post.
echo  "waiting for IP on computer usb"
while  [[ `get_ip` < 192 ]]; do  sleep  2;  done
phoneip=`get_ip`
echo  "IP adress is $phoneip "
   
#run activater process
. /htcnetup  $phoneip

最后,手机插入电脑usb,运行htcnet.sh脚本,完美实现htc android手机internet传输,共享电脑网络吧!!

$ . /htcnet .sh
waiting  for  IP on computer usb
IP adress is 192.168.99.241
sock sucessful
connect sucessful
send 0x00020000 sucess!
recv 0x80020000 sucess!
send 0x00030000 sucess!
recv 0x80030000 sucess!
send 0x00050000 sucess!
the activater process success!

五、加入udev规则

插入手机后还得手动运行一个命令激活,能否更方便呢?可以的,我们加入udev规则即可。

1.把htcnetup和htcnet.sh文件复制到命令搜索目录:

sudo  cp  htcnetup  /usr/local/bin
sudo  cp  htcnet.sh  /usr/local/bin
sudo  chmod  a+x  /usr/local/bin/htcnetup
sudo  chmod  a+x  /usr/local/bin/htcnet .sh

2.建立规则文件49-htcpnet.rules,内容如下:

SUBSYSTEM== "usb" , ATTR{idVendor}== "0bb4" , ATTR{idProduct}== "0fb4" ,ACTION== "add" , RUN+= "/usr/bin/sudo -b /usr/local/bin/htcnet.sh"

把它放到/etc/udev/rules.d/中去:

sudo  cp  49-htcpnet.rules  /etc/udev/rules .d/

这样插入手机,就会自动激活internet 传输了!

六、从git安装

这里简单的说一下怎么从git安装。

git clone https: //github .com /qileilu/htcnetup .git
cd  htcnetup/
make
sudo  make  install
卸载的话:
sudo  make  uninstall
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门