原始套接字SOCK_RAW发送UDP数据包

      使用原始套接字发送udp数据包,从传输层封包到链路层(mac头+ip头+udp头)。udp数据包,从传输层封包到链路层(mac头+ip头+udp头)。
 
 

head.h文件如下:

 

#ifndef _HEAD_H
#define _HEAD_H    

#define BUFFER_MAX 2048
#define PCKT_LEN 8492

#pragma pack (1)
//ethernet head 14 bytes
struct ether
{
	char dmac[6];
	char smac[6];
	char eth_typ_len[2];
};


//ip head 20 bytes
struct ipheader {

 unsigned char      iplv;//iph_ihl:5, iph_ver:4;

 unsigned char      iph_tos;

 unsigned short int iph_len;

 unsigned short int iph_ident;

// unsigned uint8_t      iph_flag;

 unsigned short int iph_offset;//16bit include flag and offset

 unsigned char      iph_ttl;

 unsigned char      iph_protocol;

 unsigned short int iph_chksum;

 unsigned int       iph_sourceip;

 unsigned int       iph_destip;

};

//udp head 8 bytes
struct udpheader {

 unsigned short int udph_srcport;

 unsigned short int udph_destport;

 unsigned short int udph_len;

 unsigned short int udph_chksum;

};

//tcp head 20bytes
struct tcphdr
{
    unsigned short int source;  
    unsigned short int dest;   
    unsigned int seq;  
    unsigned int ack_seq; 
    unsigned short int tcp_len;
#if 0
    unsigned short int doff:4;
    unsigned short int res1:4;
    unsigned short int res2:2;
    unsigned short int urg:1;
    unsigned short int ack:1;
    unsigned short int psh:1;
    unsigned short int rst:1;
    unsigned short int syn:1;
    unsigned short int fin:1;
#endif
    unsigned short int window;  
    unsigned short int check;  
    unsigned short int urg_prt;  
};

#pragma pack ()

#endif

 

 

 

 

 

 
 

raw_udp.c文件如下:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/in.h>
#include "head.h"

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用 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 头,需要按照实际情况进行修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值