概述
Linux raw socket demo
抓包:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <stdint.h>
/**
* IPv4 结构
*/
typedef struct {
#define IPH_GET_VER(v) (((v) >> 4) & 0x0F)
#define IPH_GET_LEN(v) (((v) & 0x0F) << 2)
uint8_t version_len;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
#define IP_OFFMASK 0x1fff
uint16_t frag_off;
uint8_t ttl;
#define IP_PROTO_UDP 17 /* UDP protocol */
#define IP_PROTO_TCP 6 /* TCP protocol */
#define IP_PROTO_ICMP 1 /* ICMP protocol */
#define IP_PROTO_IGMP 2 /* IGMP protocol */
uint8_t protocol;
uint16_t check_sum;
uint32_t saddr;
uint32_t daddr;
/* The options start here. */
} IPHDR;
/**
* ICMP 头结构
*/
typedef struct {
IPHDR ip_hdr;
uint8_t type;
uint8_t code;
uint16_t check_sum;
/* data start here. */
}ICMPHDR;
#define BUFFER_MAX 2048
int main(int argc, char *argv[])
{
int rawsock;
char buffer[BUFFER_MAX];
if ((rawsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0){
perror("rawsock create error");
exit(1);
}
while (1)
{
int readnum = read(rawsock, buffer, sizeof(buffer));
uint8_t *p_data = &buffer[14];
IPHDR *piphdr = (IPHDR *)p_data;
if(IP_PROTO_ICMP == piphdr->protocol)
{
char sipaddr[30] = {0};
char dipaddr[30] = {0};
struct in_addr s;
struct in_addr d;
s.s_addr = piphdr->saddr;
d.s_addr = piphdr->daddr;
inet_ntop(AF_INET,&s.s_addr,sipaddr,sizeof(sipaddr));
inet_ntop(AF_INET,&d.s_addr,dipaddr,sizeof(dipaddr));
ICMPHDR *picmp = (ICMPHDR *)p_data;
if(picmp->type == 8){
printf("ICMP ping request ");
}else if(picmp->type == 0){
printf("ICMP ping reply ");
}else {
printf("ICMP unknown");
}
printf("src ip %s,dst ip %sn",sipaddr,dipaddr);
}
else {
continue;
}
}
close(rawsock);
return 0;
}
运行:
发包:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <netpacket/packet.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
int rawsock;
if ((rawsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0){
perror("rawsock create error");
exit(1);
}
struct sockaddr_ll sll;
struct ifreq ifstruct;
memset (&sll, 0, sizeof (sll));
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons (ETH_P_ALL);
strcpy (ifstruct.ifr_name, "ens33");
ioctl (rawsock, SIOCGIFINDEX, &ifstruct);
sll.sll_ifindex = ifstruct.ifr_ifindex;
strcpy (ifstruct.ifr_name, "ens33");
ioctl (rawsock, SIOCGIFHWADDR, &ifstruct);
memcpy (sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, ETH_ALEN);
sll.sll_halen = ETH_ALEN;
if (bind (rawsock, (struct sockaddr *) &sll, sizeof (sll)) == -1)
{
perror("bind ");
}
while (1)
{
// ICMP ping
uint8_t pkt[]={
0x00,0x50,0x56,0xc0,0x00,0x08,0x00,0x0c,0x29,0xb3,0x6e,0x26,0x08,0x00,0x45,0x00,
0x00,0x3c,0xd8,0xcc,0x00,0x00,0x40,0x01,0x20,0x1f,0xc0,0xa8,0x80,0x83,0xc0,0xa8,
0x80,0x01,0x00,0x00,0x53,0x7f,0x00,0x01,0x01,0xdc,0x61,0x62,0x63,0x64,0x65,0x66,
0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,
0x77,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69
};
int txlen = write(rawsock, pkt, sizeof(pkt));
if(txlen > 0){
sleep(2);
}
else {
break;
}
}
close(rawsock);
return 0;
}
运行:
最后
以上就是激情犀牛为你收集整理的socket 编程(四) Linux raw socket的全部内容,希望文章能够帮你解决socket 编程(四) Linux raw socket所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复