原始套接字-自定义IP首部和TCP首部

时间:2023-03-09 04:19:54
原始套接字-自定义IP首部和TCP首部
 /* =====================================================================================
*
* Filename: raw.c
* Description: 使用原始套接字发送TCP协议,并外带自己的数据。
*
* ====================================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define DATA "hello"
#define PACKET_SIZE sizeof(struct iphdr) + sizeof(struct tcphdr) + sizeof(DATA) /*---------------------------------------------------------
Function Name : check_sum()
Descrypthon : 校验和计算,摘自UNP源码
------------------------------------------------------------*/
unsigned short check_sum(unsigned short *addr, int len)
{
int nleft = len;
int sum = ;
unsigned short *w = addr;
short answer = ;
while (nleft > )
{
sum += *w++;
nleft -=;
}
if (nleft == )
{
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> ) + (sum & 0xffff);
sum += (sum >> );
answer = ~sum;
return answer;
} /*---------------------------------------------------------
Function Name : init_socket()
Descrypthon : 初始化socket,使用原始套接字
parameter : P1 一个待初始化的原始套接字,P2 待初始化的目标地址结构,P3 目标地址,P4 目标端口
return : 返回一个原始套接字,在函数体内将目标地址结构进行初始
------------------------------------------------------------*/
int init_socket(int sockfd, struct sockaddr_in *target,const char *dst_addr, const char *dst_port)
{
const int flag = ;
//目标协议簇
target->sin_family = AF_INET;
//目标端口
target->sin_port = htons(atoi(dst_port)); //将dst_addr中的ASCII-IP地址更新到target->sin_addr结构中
if (inet_aton(dst_addr, &target->sin_addr) == )
{
perror("inet_aton fail\n");
exit(-);
}
//初始化原始套接字
if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < )
{
perror("error");
exit(-);
}
//设置套接字×××
if (setsockopt(sockfd,IPPROTO_IP, IP_HDRINCL, &flag, sizeof(flag)) < )
{
perror("setsockopt fail \n");
exit(-);
}
return sockfd;
} /*---------------------------------------------------------------
Function Name : buile_iphdr()
Descrypthon : 构建IP头部数据, 源地址使用伪随机地址
-----------------------------------------------------------------*/
void buile_iphdr(struct sockaddr_in *target, char *buffer)
{
struct iphdr *ip = (struct iphdr *)(buffer);
ip->version = ;//版本
ip->ihl = ;//首部长度 5*4 = 20
ip->tos = ;//8位服务类型
ip->tot_len = htons(PACKET_SIZE);//16位总长度
ip->id = ;//16位标识符
ip->frag_off = ;//3位标志
ip->ttl = ;//生存时间
ip->protocol = IPPROTO_TCP;//协议
ip->check = check_sum((unsigned short *)ip, sizeof(struct iphdr) + sizeof(DATA));//16位首部校验和
ip->saddr = random();//源ip地址
ip->daddr = target->sin_addr.s_addr;//目标ip地址
} /*---------------------------------------------------------------
Function Name : buile_tcphdr()
Descrypthon : 构建TCP头部信息,并加入一些自己的数据,然后进行
校验计算。
-----------------------------------------------------------------*/
void buile_tcphdr(struct sockaddr_in *target, const char *src_port, char *buffer)
{
struct tcphdr *tcp = (struct tcphdr *)(buffer);
tcp->source = htons(atoi(src_port));//16位源端口号
tcp->dest = target->sin_port;//16位目的端口号
tcp->seq = random();//32位序号
tcp->doff = ;//
tcp->syn = ;//同步序号
buffer += sizeof(struct tcphdr);
tcp->check = check_sum((unsigned short *)tcp, sizeof(struct tcphdr) + sizeof(DATA));//16位检验和
memcpy(buffer, DATA, sizeof(DATA));//将DATA中的数据拷贝sizeof(DATA)字节到buffer所指的地址中
}
int main(int argc, const char *argv[])
{
char *buffer;
char *buffer_head = NULL;
int sockfd = ;
struct sockaddr_in *target;
if (argc != )
{
printf("usage: destination addresss, destination port, source port \n");
exit(-);
}
const char *dst_addr = argv[];
const char *dst_port = argv[];
const char *src_port = argv[]; target = calloc(sizeof(struct sockaddr_in),);
//calloc在动态分配完内存后,自动初始化该内存空间为零,而malloc不初始化,里边数据是随机的垃圾数据。
buffer = calloc(PACKET_SIZE, );
buffer_head = buffer; //初始化套接字
sockfd = init_socket(sockfd, target, dst_addr, dst_port);
//创建IP首部
buile_iphdr(target, buffer);
buffer += sizeof(struct iphdr);//指针下移
//创建TCP首部
buile_tcphdr(target, src_port, buffer);
//发送
sendto(sockfd, buffer_head, PACKET_SIZE, ,(struct sockaddr *)target, sizeof(struct sockaddr_in)); //下两行是对calloc申请的释放
free(buffer_head);
free(target);
return ;
}