Android 原始套接字

Android 原始套接字

网上查到,Android下的原始套接字 需要机子root之后才能使用(机子root之后到底能不能使用,我没测试过[20151103])。

 

PS:我看了一下,java实现的 RockSaw 的与那吗里面也是使用的jni方式。看了 jpcap-0.6.rar 的源码,貌似也是jni。

 

使用下面这个 Qt代码,在创建 套接字的部分 会返回错误代码(我遇到的是 errno 返回 1 或者 93):

(工程的 AndroidManifest.xml 文件里面已经加入

<uses-permission android:name=”android.permission.INTERNET” />

<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”></uses-permission>

”)

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QScrollBar>
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //ui->textEdit->append();
    //ui->plainTextEdit->appendPlainText();
}

MainWindow::~MainWindow()
{
    delete ui;
}

int g_i01 = 0;
int g_i02 = 0;

void MainWindow::on_pbtnTextEdit_clicked()
{
    //ui->textEdit->setReadOnly(true);
    ui->textEdit->append(QString::number(g_i01));
    g_i01 ++;



// http://www.w-cun.com/post/diaozheng_1976.htm

// http://stackoverflow.com/questions/7411761/how-to-increase-qtablewidget-vertical-scrollbar-width
    int iWidth = ui->plainTextEdit->verticalScrollBar()->width();
    QMessageBox::about(this, "title", QString::number(iWidth));
    ui->plainTextEdit->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: "+QString::number(iWidth * 2)+"px; }");
}

void MainWindow::on_pbtnPlainTextEdit_clicked()
{/*
    ui->plainTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    QScrollBar* pScrollBar = ui->plainTextEdit->verticalScrollBar();
    pScrollBar->resize(pScrollBar->width() *2, pScrollBar->height());
*/
    ui->lineEdit->setReadOnly(true);
    ui->plainTextEdit->setReadOnly(true);
    //ui->textEdit->sel
    ui->plainTextEdit->appendPlainText(QString::number(g_i02));
    g_i02 ++;
    qDebug() << "on_pbtnPlainTextEdit_clicked";


/*
    QTextCharFormat fmt;
    fmt.setBackground(Qt::yellow);

    QTextCursor cursor(ui->plainTextEdit->document());
    cursor.setPosition(begin, QTextCursor::MoveAnchor);
    cursor.setPosition(end, QTextCursor::KeepAnchor);
    cursor.setCharFormat(fmt);
    */
}

int main_z();
void* thread_z(void* arg);

void MainWindow::on_pushButton_clicked()
{
    pthread_t tid1 = 0;
    int iRtn = pthread_create(&tid1, NULL, thread_z, &tid1);
    if(iRtn != 0)
    {
        QString str1 = __func__;
        QString str2 = strerror(iRtn);
        qDebug() << str1 + str2;
    }
    //main_z();
    QMessageBox::about(this, "on_pushButton_clicked", "after main_z");
}


void* thread_z(void* arg)
{
    main_z();
}

// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/if_packet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>

#include <unistd.h> // 函数close()
#include <errno.h>

typedef struct _iphdr //定义IP首部
{
    unsigned char h_verlen; //4位首部长度+4位IP版本号
    unsigned char tos; //8位服务类型TOS
    unsigned short total_len; //16位总长度(字节)
    unsigned short ident; //16位标识
    unsigned short frag_and_flags; //3位标志位
    unsigned char ttl; //8位生存时间 TTL
    unsigned char proto; //8位协议 (TCP, UDP 或其他)
    unsigned short checksum; //16位IP首部校验和
    unsigned int sourceIP; //32位源IP地址
    unsigned int destIP; //32位目的IP地址
}IP_HEADER;

typedef struct _udphdr //定义UDP首部
{
    unsigned short uh_sport;    //16位源端口
    unsigned short uh_dport;    //16位目的端口
    unsigned int uh_len;//16位UDP包长度
    unsigned int uh_sum;//16位校验和
}UDP_HEADER;

typedef struct _tcphdr //定义TCP首部
{
    unsigned short th_sport; //16位源端口
    unsigned short th_dport; //16位目的端口
    unsigned int th_seq; //32位序列号
    unsigned int th_ack; //32位确认号
    unsigned char th_lenres;//4位首部长度/6位保留字
    unsigned char th_flag; //6位标志位
    unsigned short th_win; //16位窗口大小
    unsigned short th_sum; //16位校验和
    unsigned short th_urp; //16位紧急数据偏移量
}TCP_HEADER;

typedef struct _icmphdr {
    unsigned char  icmp_type;
    unsigned char icmp_code; /* type sub code */
    unsigned short icmp_cksum;
    unsigned short icmp_id;
    unsigned short icmp_seq;
    /* This is not the std header, but we reserve space for time */
    unsigned short icmp_timestamp;
}ICMP_HEADER;

void analyseIP(IP_HEADER *ip)
{
    unsigned char* p = (unsigned char*)&ip->sourceIP;
    printf("Source IP\t: %u.%u.%u.%u\n",p[0],p[1],p[2],p[3]);
    p = (unsigned char*)&ip->destIP;
    printf("Destination IP\t: %u.%u.%u.%u\n",p[0],p[1],p[2],p[3]);

}

void analyseTCP(TCP_HEADER *tcp)
{
    printf("TCP -----\n");
    printf("Source port: %u\n", ntohs(tcp->th_sport));
    printf("Dest port: %u\n", ntohs(tcp->th_dport));
}

void analyseUDP(UDP_HEADER *udp)
{
    printf("UDP -----\n");
    printf("Source port: %u\n", ntohs(udp->uh_sport));
    printf("Dest port: %u\n", ntohs(udp->uh_dport));
}

void analyseICMP(ICMP_HEADER *icmp)
{
    printf("ICMP -----\n");
    printf("type: %u\n", icmp->icmp_type);
    printf("sub code: %u\n", icmp->icmp_code);
}

int main_z(void)
{
    int sockfd;
     IP_HEADER *ip;
    char buf[10240];
    ssize_t n;

    qDebug() << "main_z in";
    /* capture ip datagram without ethernet header */
    //if ((sockfd = socket(PF_PACKET,  SOCK_DGRAM, htons(ETH_P_IP)))== -1)
    if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP|IPPROTO_UDP|IPPROTO_ICMP))== -1)
    //if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_IP))== -1)
    //int skt = socket(AF_INET, SOCK_STREAM, 0);
    {
        qDebug() << "socket error : "+QString::number(errno);
        printf("socket error!\n");
        return 1;
    }
    return 0;
    while (1)
    {
        n = recv(sockfd, buf, sizeof(buf), 0);
        if (n == -1)
        {
            printf("recv error!\n");
            break;
        }
        else if (n==0)
            continue;
        //接收数据不包括数据链路帧头
        ip = ( IP_HEADER *)(buf);
        analyseIP(ip);
        size_t iplen =  (ip->h_verlen&0x0f)*4;
        TCP_HEADER *tcp = (TCP_HEADER *)(buf +iplen);
        if (ip->proto == IPPROTO_TCP)
        {
            TCP_HEADER *tcp = (TCP_HEADER *)(buf +iplen);
            analyseTCP(tcp);
            qDebug() << "TCP";
        }
        else if (ip->proto == IPPROTO_UDP)
        {
            UDP_HEADER *udp = (UDP_HEADER *)(buf + iplen);
            analyseUDP(udp);
            qDebug() << "UDP";
        }
        else if (ip->proto == IPPROTO_ICMP)
        {
            ICMP_HEADER *icmp = (ICMP_HEADER *)(buf + iplen);
            analyseICMP(icmp);
            qDebug() << "ICMP";
        }
        else if (ip->proto == IPPROTO_IGMP)
        {
            printf("IGMP----\n");
            qDebug() << "IGMP";
        }
        else
        {
            printf("other protocol!\n");
            qDebug() << "other protocol !";
        }
        printf("\n\n");

        break;
    }
    close(sockfd);
    return 0;
}

  

转载于:https://www.cnblogs.com/codeskilla/p/4933613.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109263.html原文链接:https://javaforall.net

(5)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • qmake的使用

    qmake的使用前言在linux环境下进行程序开发时,经常需要使用makefile管理编译代码,特别是一些大型工程,而makefile工具语法晦涩深入研究较为困难,好在有很多工具可以自动生成makefile,qmake就是其中的一种。qmake特点为不同的平台的开发项目创建makefile。可以供给任何一个软件项目使用,而不用管它是不是用Qt写的,尽管它包含了为支持Qt开发所拥有的额外的特征。…

    2022年5月19日
    165
  • 大数据分析及工具应用总结「建议收藏」

    大数据分析及工具应用总结「建议收藏」概述数据分析即从数据、信息到知识的过程,数据分析需要数学理论、行业经验以及计算机工具三者结合数据分析工具:各种厂商开发了数据分析的工具、模块,将分析模型封装,使不了解技术的人也能够快捷的实现数学建模,快速响应分析需求传统分析:在数据量较少时,传统的数据分析已能够发现数据中包含的知识,包括结构分析、杜邦分析等模型,方法成熟,应用广泛。数据挖掘:就是充分利用了统计学和人工智能技术的应用程序,并把这些高深复杂的技术封装起来,使人们不用自己掌握这些技术也能完成同样的功能,并且…

    2022年5月3日
    55
  • dedecms如何去掉底部power by dedecms 链接[通俗易懂]

    dedecms如何去掉底部power by dedecms 链接[通俗易懂]dedecms在底部有个cfg_powerby 标签,在后台的系统-》系统基本参数那里面可以编辑cfg_powerby 这个标签,可是新版的更新后还会加一个powerbydedecms链接在后台设置是不起效的打开网站的/inclue/dedesql.class.php查看第588行如下$arrs1=array(0x63,0x66,0x67,0x5f,

    2022年7月13日
    15
  • Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结

    Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结1.增加一个字段 代码如下 复制代码 //增加一个字段,默认为空altertableuseraddCOLUMNnew1VARCHAR(20)DEFAULTNULL; //增加一个字段,默认不能为空altertableuseraddCOLUMNnew2VARCHAR(20)NOTNULL; 2….

    2022年6月1日
    54
  • MYSQL截取字符串前后数据

    MYSQL截取字符串前后数据select LEFT(‘一把刀把|YBDB’,locate(‘|’,’一把刀把|YBDB’)-1)’前面’, substr(‘一把刀把|YBDB’,locate(‘|’,’一把刀把|YBDB’)+1,octet_length(‘一把刀把|YBDB’))’后面’substr(‘’,‘’,‘’)从竖线取值到结尾locate(‘|’,‘一把刀把|YBDB’)竖线位置octet_length(‘一把刀把|YBDB’)字符长度…

    2022年5月26日
    40
  • 整除的尾数_整除数

    整除的尾数_整除数整除的尾数时间限制:1000 ms | 内存限制:65535 KB难度:0描述一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢?输入输入数据有若干组,每组数据包含二个整数a,b(0输出对应每组数据,将满足条件的所有尾数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。样例输入

    2025年5月24日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号