转载自:http://www.cnblogs.com/uvsjoh/archive/2012/12/31/2840883.html

我们平常所用到的网络编程都是在应用层收发数据,每个程序只能收到发给自己的数据,即每个程序只能收到来自该程序绑定的端口的数据。收到的数据往往只包括应用层数据。某些情况下我们需要执行更底层的操作,比如监听所有本机收发的数据、修改报头等。

通过原始套接字,我们可以抓取所有发送到本机的IP包(包括IP头和TCP/UDP/ICMP包头),也可以抓取所有本机收到的帧(包括数据链路层 协议头)。普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以。利用原始套接字,我们可以自己构造IP头。

有两种原始套接字
一种是处理IP层即其上的数据,通过指定socket第一个参数为AF_INET来创建这种套接字。
另一种是处理数据链路层即其上的数据,通过指定socket第一个参数为AF_PACKET来创建这种套接字。

AF_INET表示获取从网络层开始的数据
socket(AF_INET, SOCK_RAW, …)
当接收包时,表示用户获得完整的包含IP报头的数据包,即数据从IP报头开始算起。
当发送包时,用户只能发送包含TCP报头或UDP报头或包含其他传输协议的报文,IP报头以及以太网帧头则由内核自动加封。除非是设置了IP_HDRINCL的socket选项。
如果第二个参数为SOCK_STREAM, SOCK_DGRAM,表示接收的数据直接为应用层数据。

PF_PACKET,表示获取的数据是从数据链路层开始的数据
socket(PF_PACKET,SOCK_RAW,htos(ETH_P_IP)):表示获得IPV4的数据链路层帧,即数据包含以太网帧头。14+20+(8:udp 或 20:tcp)
ETH_P_IP: 在<linux/if_ether.h>中定义,可以查看该文件了解支持的其它协议。
SOCK_RAW, SOCK_DGRAM两个参数都可以使用,区别在于使用SOCK_DGRAM收到的数据不包括数据链路层协议头。

总结起来就是:
socket(AF_INET, SOCK_RAW, IPPROTO_TCP|IPPROTO_UDP|IPPROTO_ICMP)发送接收ip数据包
能:该套接字可以接收协议类型为(tcp udp icmp等)发往本机的ip数据包
不能:收到非发往本地ip的数据包(ip软过滤会丢弃这些不是发往本机ip的数据包)
不能:收到从本机发送出去的数据包
发送的话需要自己组织tcp udp icmp等头部.可以setsockopt来自己包装ip头部
这种套接字用来写个ping程序比较适合

socket(PF_PACKET, SOCK_RAW|SOCK_DGRAM, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ALL))发送接收以太网数据帧
这种套接字比较强大,可以监听网卡上的所有数据帧

能: 接收发往本地mac的数据帧
能: 接收从本机发送出去的数据帧(第3个参数需要设置为ETH_P_ALL)
能: 接收非发往本地mac的数据帧(网卡需要设置为promisc混杂模式)
协议类型一共有四个
ETH_P_IP 0x800 只接收发往本机mac的ip类型的数据帧
ETH_P_ARP 0x806 只接受发往本机mac的arp类型的数据帧
ETH_P_RARP 0x8035 只接受发往本机mac的rarp类型的数据帧
ETH_P_ALL 0x3 接收发往本机mac的所有类型ip arp rarp的数据帧, 接收从本机发出的所有类型的数据帧.(混杂模式打开的情况下,会接收到非发往本地mac的数据帧)

应用例子:抓取所有IP包

#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> 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);
void analyseTCP(TCP_HEADER *tcp);
void analyseUDP(UDP_HEADER *udp);
void analyseICMP(ICMP_HEADER *icmp); int main(void)
{
int sockfd;
IP_HEADER *ip;
char buf[];
ssize_t n;
/* capture ip datagram without ethernet header */
if ((sockfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)))== -)
{
printf("socket error!\n");
return ;
}
while ()
{
n = recv(sockfd, buf, sizeof(buf), );
if (n == -)
{
printf("recv error!\n");
break;
}
else if (n==)
continue;
//接收数据不包括数据链路帧头
ip = ( IP_HEADER *)(buf);
analyseIP(ip);
size_t iplen = (ip->h_verlen&0x0f)*;
TCP_HEADER *tcp = (TCP_HEADER *)(buf +iplen);
if (ip->proto == IPPROTO_TCP)
{
TCP_HEADER *tcp = (TCP_HEADER *)(buf +iplen);
analyseTCP(tcp);
}
else if (ip->proto == IPPROTO_UDP)
{
UDP_HEADER *udp = (UDP_HEADER *)(buf + iplen);
analyseUDP(udp);
}
else if (ip->proto == IPPROTO_ICMP)
{
ICMP_HEADER *icmp = (ICMP_HEADER *)(buf + iplen);
analyseICMP(icmp);
}
else if (ip->proto == IPPROTO_IGMP)
{
printf("IGMP----\n");
}
else
{
printf("other protocol!\n");
}
printf("\n\n");
}
close(sockfd);
return ;
} void analyseIP(IP_HEADER *ip)
{
unsigned char* p = (unsigned char*)&ip->sourceIP;
printf("Source IP\t: %u.%u.%u.%u\n",p[],p[],p[],p[]);
p = (unsigned char*)&ip->destIP;
printf("Destination IP\t: %u.%u.%u.%u\n",p[],p[],p[],p[]); } 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);
}

Linux raw socket的更多相关文章

  1. raw, SOCK_RAW - Linux IPv4 raw socket.

    总 览 #include <sys/socket.h> #include <netinet/in.h> raw_socket = socket(PF_INET, SOCK_RA ...

  2. raw socket遇上windows

    最近很长一段时间内又捡起了大学时丢下的网络协议,开始回顾网络协议编程,于是linux系统成了首选,它让我感到了无比的自由,可以很通透的游走于协议的各层. 最初写了个ARP欺骗程序,很成功的欺骗了win ...

  3. linux Packet socket (1)简单介绍

    本文主要来自于linux自带的man packet手冊: http://man7.org/linux/man-pages/man7/packet.7.html 平时常常使用的INET套接字提供的是7层 ...

  4. linux c socket programming

    原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...

  5. Raw Socket vs Stream Socket vs datagram socket,原始套接字与流式套接字与数据报套接字

    https://opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/ In this tutorial, let’s take a look ...

  6. linux系统socket通信编程1

    Linux下的Socket编程大体上包括Tcp Socket.Udp Socket即Raw Socket这三种,其中TCP和UDP方式的Socket编程用于编写应用层的socket程序,是我们用得比较 ...

  7. Linux C Socket 编程

    1 Socket 是什么 Socket(套接字),就是对 网络上进程通信 的 端点 的 抽象.一个 Socket 就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制. 从所处的位置来 ...

  8. linux 下socket编程

    原理 类unix系统中, 一切皆文件, 诸如磁盘文件, 显卡, 内核驱动, 网络协议栈等 socket就是linux中提供的用于网络通信的文件接口, 两台机器之间可以读写消息 在使用socket真正的 ...

  9. linux中socket的理解

    对linux中socket的理解 一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打开open –> 读写write/read –> ...

随机推荐

  1. WndProc和hook区别

    1)WndProc函数作用:主要在程序中拦截并处理系统消息和自定义消息 比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息.这个函数就是默认的消息处理函数.你可以重载这个函数 ...

  2. C#去边框以及无边框窗体移动

    1.去边框  : 1) 选中要去除边框的窗体,按F4调出窗体属性. 2)在属性框中找到FormBorderStyle选择none. 2.去掉边框后实现对窗口程序的拖动 1)双击窗体,进入程序设计界面 ...

  3. 机器学习基石笔记:11 Linear Models for Classification、LC vs LinReg vs LogReg、OVA、OVO

    原文地址:https://www.jianshu.com/p/6f86290e70f9 一.二元分类的线性模型 线性回归后的参数值常用于PLA/PA/Logistic Regression的参数初始化 ...

  4. AOP切点相关

    1.切点定义 切点定义包含两个部分 一个切入点表达式 一个包含名字和任意参数的方法签名 package com.sysker.aspect; import org.aspectj.lang.annot ...

  5. AngularJS(二)——常见指令以及下拉框实现

    前言 学完AngularJS,总体上感觉没什么新鲜的东西,但是又感觉每一步都很新鲜,因为没有见过,又因为学到的语法函数和JavaScript差不多,本篇主要介绍一些AngularJS的指令,常见指令和 ...

  6. 【bzoj3122】: [Sdoi2013]随机数生成器 数论-BSGS

    [bzoj3122]: [Sdoi2013]随机数生成器 当a>=2 化简得 然后 BSGS 求解 其他的特判 : 当 x=t  n=1 当 a=1  当 a=0 判断b==t /* http: ...

  7. Eclipse中使用Spring IOC容器的具体方法

    1.通过IOC容器创建对象,并为属性赋值 在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建 <bean id=&quo ...

  8. Django 自定义模板标签 报错django.template.exceptions.TemplateSyntaxError: '####' is not a registered tag library. Must be one of:

    我写代码遇到这个错误,但是发现程序没有写错,好像是程序有缓存,重新运行几次就好了. 自定义模板标签,可以不用写views,url直接通过自定义函数把变量传给模板. 具体实现: 1.在app下新建Pyt ...

  9. HDU6301 Distinct Values (多校第一场1004) (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. (16)break和continue

    #(1)这里顺带说下pass # pass 过 的意思,防止代码报错,就是个占位的, if True: # 不允许代码块里面的内容为空,用pass占位 pass #(2)break (只能用在循环当中 ...