使用原始套接字 (c++)

Posted

技术标签:

【中文标题】使用原始套接字 (c++)【英文标题】:Using raw sockets (c++) 【发布时间】:2013-07-03 23:17:17 【问题描述】:

我是原始套接字的新手。我以前在套接字上工作过,但我从未对原始套接字做过任何事情。我收到错误 #10049 -> WSAEADDRNOTAVAIL“无法分配请求的地址”@“sendto”

希望你能帮助我!

我的代码:

#include <WinSock2.h>
#include <stdio.h>

typedef unsigned char U8;
typedef unsigned short U16;

struct ipheader

    U8 length : 4;// : 4;
    U8 version : 4;

    U8 differentiated_services_field;
    U16 total_length;
    U16 ident;
    U16 fragment; // fragment offset // first 8 bit = flags not 8 bit
    U8 ttl; // time to live
    U8 protocol;
    U16 checksum;
    in_addr src;
    in_addr dst;
;

struct udpheader

    U16 srcport;
    U16 dstport;
    U16 length;
    U16 checksum;
;


#define CON_SERVER_IP           "127.0.0.1"
#define CON_SERVER_PORT         1234

#define MY_PORT                 9191
#define MY_IP                   "127.0.0.1"

#define DATA_LENGTH 16
#define WHOLE_LENGTH (20+8+DATA_LENGTH)

// http://www.c-worker.ch/tuts/raw_icmp.php
unsigned short Checksum(unsigned short *p_usBuffer,int iSize)

    unsigned long lCheckSum = 0;
    while(iSize > 1)
    
        lCheckSum += *p_usBuffer++;
        iSize -= sizeof(unsigned short);
    
    if(iSize)
        lCheckSum += *(unsigned char*)p_usBuffer; // + 0 entfällt, da überflüßig
    lCheckSum = ((lCheckSum >> 16) + (lCheckSum & 0xffff)) + (lCheckSum >> 16);
    return (unsigned short)(~lCheckSum);

int main(int argc,char* argv[])

    WSADATA wsa;
    WSAStartup(MAKEWORD(2,0),&wsa);

    char packet[WHOLE_LENGTH];

    ZeroMemory(packet,WHOLE_LENGTH);

    ipheader* pIpHeader = (ipheader*)&packet;
    udpheader* pUDPHeader = (udpheader*) &packet + sizeof(ipheader);

    SOCKADDR_IN sAddr;
    ZeroMemory(&sAddr,sizeof(SOCKADDR_IN));
    sAddr.sin_family = AF_INET;
    sAddr.sin_port = htons(CON_SERVER_PORT);
sAddr.sin_addr.s_addr = inet_addr(CON_SERVER_IP); // thank you BevynQ

    pIpHeader->length = 5;
    pIpHeader->version = 4;
    pIpHeader->differentiated_services_field = 0;
    pIpHeader->total_length = sizeof(ipheader) + sizeof(udpheader) + DATA_LENGTH;
    pIpHeader->ident = htonl(54321);
    pIpHeader->fragment = 0;
    pIpHeader->ttl = 65;
    pIpHeader->protocol = IPPROTO_UDP;
    pIpHeader->checksum = 0;
    pIpHeader->src.s_addr = inet_addr(MY_IP);
    pIpHeader->dst.s_addr = inet_addr(CON_SERVER_IP);

    pUDPHeader->srcport = htons(MY_PORT);
    pUDPHeader->dstport = htons(CON_SERVER_PORT);
    pUDPHeader->length = sizeof(udpheader) + DATA_LENGTH;
    pUDPHeader->checksum = 0;

    strcpy(packet+sizeof(ipheader)+sizeof(udpheader),"Lets go\n");

    pUDPHeader->checksum = Checksum((unsigned short*)packet+sizeof(ipheader),sizeof(udpheader)+DATA_LENGTH);

    pIpHeader->checksum = Checksum((unsigned short*)packet,WHOLE_LENGTH);

    printf("UDPHeader checksum:\t0x%04X\n",pUDPHeader->checksum);
    printf("IPHeader checksum:\t0x%04X\n",pIpHeader->checksum);


    SOCKET sock = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
    if(sock == INVALID_SOCKET)
    
        printf("socket generation failed (%d)\n",WSAGetLastError());
        goto q1;
    

    int i = sendto(sock,packet,WHOLE_LENGTH,0,(SOCKADDR*)&sAddr,sizeof(SOCKADDR_IN));
    printf("sendto: %i (%d)\n",i,WSAGetLastError());
q1:
    printf("\n\n\n\nDone\n");
    getchar();
    return 0;

编辑:错误不再发生,但没有建立连接。我明天再看看。 感谢您的帮助!

Edit2:无法发送数据包,因为尚未设置 IP_HDRINCL 选项。通过这样做,它可以工作;)

【问题讨论】:

看起来你只是在设置端口。您还需要设置IP地址。 作为建议,您应该尝试libtins。它为您处理原始套接字和协议内部。 谢谢 BevynQ。我将它添加到我的帖子中并修复了指针(&数据包)。 【参考方案1】:

试试

SOCKADDR_IN sAddr;
ZeroMemory(&sAddr,sizeof(SOCKADDR_IN));
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(CON_SERVER_PORT);
inet_pton(AF_INET, CON_SERVER_IP, &sAddr.sin_addr);

【讨论】:

BevynQ,你能不能也给我的问题解决办法,拜托 ***.com/questions/51000729/… 是链接

以上是关于使用原始套接字 (c++)的主要内容,如果未能解决你的问题,请参考以下文章

原始套接字编程

python使用原始套接字 解析原始ip头数据

python使用原始套接字 解析原始ip头数据

求给个windows 下原始套接字的示例程序,要求使用C语言,不使用MFC。

原始套接字

如何在 Python 中使用原始套接字?