使用rawsocket构造UDP数据包方法

/rudp-sender.c 用rawsocket实现的发送UDP数据包工具。

//引用时,需要多源目的IP以及端口做些修改。linux下可以直接编译。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>

#define BUFLEN 38
#define PORT 8848
typedef struct ip_hdr{//ipv4头部 
    unsigned int ip_length:4; /*little-endian*/
    unsigned int ip_version:4;
    unsigned char ip_tos;
    unsigned short ip_total_length;
    unsigned short ip_id;
    unsigned short ip_flags;
    unsigned char ip_ttl;
    unsigned char ip_protocol;
    unsigned short ip_cksum;
    unsigned int ip_source;
    unsigned int ip_dest;
}ip_hdr;
typedef struct udp_hdr{//udp头部
    unsigned short s_port;
    unsigned short d_port;
    unsigned short length;
    unsigned short cksum;
}udp_hdr;

typedef struct psd_header{//伪头部,用于计算校验和

    unsigned int s_ip;//source ip

    unsigned int d_ip;//dest ip

    unsigned char mbz;//0

    unsigned char proto;//proto type

    unsigned short plen;//length

}psd_header;

void swap(unsigned int *a, unsigned int *b)//交换
{
    *a = (*a)^(*b);
    *b = (*a)^(*b);
    *a = (*a)^(*b);
}

unsigned short checksum(unsigned short* buffer, int size)//校验和
{
    unsigned long cksum = 0;
    while(size>1)
    {
        cksum += *buffer++;
        size -= sizeof(unsigned short);
    }
    if(size)
    {
        cksum += *(unsigned char*)buffer;
    }
        cksum = (cksum>>16) + (cksum&0xffff); //将高16bit与低16bit相加

        cksum += (cksum>>16); //将进位到高位的16bit与低16bit 再相加

    return (unsigned short)(~cksum);
}
int main(int argc, char *argv[])
{
    char buf[BUFLEN];
    int sockfd = -1;
    
    struct sockaddr_in host_addr;
    if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP))<0)
    {
        printf("socket() error!/n");
        exit(1);
    }
    memset(&host_addr, 0, sizeof(host_addr));
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = htons(PORT);
    host_addr.sin_addr.s_addr = inet_addr("200.200.30.42");

    const int on = 1;
    if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))<0)
    {
        printf("setsockopt() error!/n");
        exit(0);    
    }
    
    int addr_len = sizeof(host_addr);

    while(1)
    {    
        ip_hdr *iphdr;
        iphdr = (ip_hdr*)buf;
        udp_hdr *udphdr;
        udphdr = (udp_hdr*)(buf+20);
        iphdr->ip_length = 5;
        iphdr->ip_version= 4;
        iphdr->ip_tos = 0;
        iphdr->ip_total_length = htons(sizeof(buf));
        iphdr->ip_id = 0;
        iphdr->ip_flags = 0x40;
        iphdr->ip_ttl = 0x40;
        iphdr->ip_protocol = 0x11;
        iphdr->ip_cksum = 0;
        iphdr->ip_source = inet_addr("200.200.30.14");//源地址

        iphdr->ip_dest = inet_addr("200.200.30.42");//目的地址
        iphdr->ip_cksum = checksum((unsigned short*)buf, 20);
        udphdr->s_port = htons(80);//源端口
        udphdr->d_port = htons(1024);//目的端口
        udphdr->length = htons(sizeof(buf)-20);
        udphdr->cksum = 0;
        psd_header psd;
        psd.s_ip = iphdr->ip_source;
        psd.d_ip = iphdr->ip_dest;
        psd.mbz = 0;
        psd.proto = 0x11;
        psd.plen = udphdr->length;
        char tmp[sizeof(psd)+ntohs(udphdr->length)];
        memcpy(tmp, &psd, sizeof(psd));
        memcpy(tmp+sizeof(psd), buf+20, sizeof(buf)-20);
        udphdr->cksum = checksum((unsigned short*)tmp, sizeof(tmp));
        int res =sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&host_addr, sizeof(host_addr));
        sleep(1);
    }
    return 0;

}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 VC++ 原始套接字创建 UDP 数据包的示例代码: ```c++ #include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #define DEST_IP "127.0.0.1" // 目标 IP 地址 #define DEST_PORT 8888 // 目标端口号 int main() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("WSAStartup failed!\n"); return 1; } // 创建原始套接字 SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); if (sock == INVALID_SOCKET) { printf("socket failed with error code: %d\n", WSAGetLastError()); return 1; } // 构造目标地址信息 SOCKADDR_IN destAddr; destAddr.sin_family = AF_INET; destAddr.sin_port = htons(DEST_PORT); destAddr.sin_addr.s_addr = inet_addr(DEST_IP); // 构造 UDP 数据包 char buffer[1024] = "Hello, UDP!"; int bufferLen = strlen(buffer); int totalLen = sizeof(IP_HEADER) + sizeof(UDP_HEADER) + bufferLen; char* packet = (char*)malloc(totalLen); memset(packet, 0, totalLen); // 填充 IP 头 IP_HEADER* ipHeader = (IP_HEADER*)packet; ipHeader->Version = 4; ipHeader->HeaderLength = sizeof(IP_HEADER) / sizeof(DWORD); ipHeader->TypeOfService = 0; ipHeader->TotalLength = htons(totalLen); ipHeader->Identification = rand() % 65536; ipHeader->Flags = 0; ipHeader->FragmentOffset = 0; ipHeader->TimeToLive = 128; ipHeader->Protocol = IPPROTO_UDP; ipHeader->SourceIpAddress = inet_addr("127.0.0.1"); // 源 IP 地址 ipHeader->DestinationIpAddress = destAddr.sin_addr.s_addr; // 计算 IP 头校验和 ipHeader->HeaderChecksum = 0; ipHeader->HeaderChecksum = checksum((USHORT*)ipHeader, sizeof(IP_HEADER)); // 填充 UDPUDP_HEADER* udpHeader = (UDP_HEADER*)(packet + sizeof(IP_HEADER)); udpHeader->SourcePort = htons(8888); // 源端口号 udpHeader->DestinationPort = destAddr.sin_port; udpHeader->Length = htons(sizeof(UDP_HEADER) + bufferLen); udpHeader->Checksum = 0; // 复制数据部分 memcpy(packet + sizeof(IP_HEADER) + sizeof(UDP_HEADER), buffer, bufferLen); // 计算 UDP 校验和 udpHeader->Checksum = udp_checksum(ipHeader, udpHeader, bufferLen); // 发送 UDP 数据包 if (sendto(sock, packet, totalLen, 0, (SOCKADDR*)&destAddr, sizeof(SOCKADDR)) == SOCKET_ERROR) { printf("sendto failed with error code: %d\n", WSAGetLastError()); closesocket(sock); return 1; } printf("UDP packet sent!\n"); free(packet); closesocket(sock); WSACleanup(); return 0; } // IP 头结构体 typedef struct _IP_HEADER { UCHAR HeaderLength : 4; // 头长度 UCHAR Version : 4; // 版本号 UCHAR TypeOfService; // 服务类型 USHORT TotalLength; // 总长度 USHORT Identification; // 标识符 USHORT FragmentOffset; // 片偏移 UCHAR Flags; // 标志位 UCHAR TimeToLive; // 存活时间 UCHAR Protocol; // 协议类型 USHORT HeaderChecksum; // 头校验和 ULONG SourceIpAddress; // 源 IP 地址 ULONG DestinationIpAddress; // 目标 IP 地址 } IP_HEADER, *PIP_HEADER; // UDP 头结构体 typedef struct _UDP_HEADER { USHORT SourcePort; // 源端口号 USHORT DestinationPort; // 目标端口号 USHORT Length; // 长度 USHORT Checksum; // 校验和 } UDP_HEADER, *PUDP_HEADER; // 计算校验和 USHORT checksum(USHORT* buffer, int size) { unsigned long cksum = 0; while (size > 1) { cksum += *buffer++; size -= sizeof(USHORT); } if (size) { cksum += *(UCHAR*)buffer; } cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); } // 计算 UDP 校验和 USHORT udp_checksum(IP_HEADER* ipHeader, UDP_HEADER* udpHeader, int dataLen) { char* buffer = (char*)malloc(sizeof(IP_HEADER) + sizeof(UDP_HEADER) + dataLen); memcpy(buffer, ipHeader, sizeof(IP_HEADER)); memcpy(buffer + sizeof(IP_HEADER), udpHeader, sizeof(UDP_HEADER)); memcpy(buffer + sizeof(IP_HEADER) + sizeof(UDP_HEADER), udpHeader + 1, dataLen); udpHeader->Checksum = 0; USHORT checksumValue = checksum((USHORT*)buffer, sizeof(IP_HEADER) + sizeof(UDP_HEADER) + dataLen); free(buffer); return checksumValue; } ``` 需要注意的是,使用原始套接字需要管理员权限。另外,代码中使用了两个结构体 `IP_HEADER` 和 `UDP_HEADER` 来表示 IP 头和 UDP 头,需要按照实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值