Windows 7在RAW套接字上没有收到Encapsulated Security Payload(ESP)数据包

时间:2014-05-08 14:14:32

标签: windows sockets networking raw-sockets

我正在尝试使用recvfrom()函数在原始套接字上接收Encapsulated Security Payload或ESP(IP协议号= 50)数据包。不幸的是它在Windows 7中不起作用。我想在这里提到我可以通过将IP协议号设置为47来在原始套接字上接收通用路由封装(GRE)数据包。并且套接字可以顺利地工作并接收GRE数据包。但它收到ESP数据包失败了。

我还应该提一下,我已经禁用了我的防火墙服务和windows'两个默认的IPSec服务即。 1. IKE和AuthIP IPsec密钥模块和2. IPsec策略代理。我认为由于某种原因,Windows不会将ESP数据包发送到上层。

任何人都可以帮助我吗?这是我的计划。

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

int main()
{

    int iResult = 0;

    WSADATA wsaData;

    SOCKET RecvSocket;
    sockaddr_in RecvAddr;

    unsigned short Port = 27015;

    char RecvBuf[1024];
    int BufLen = 1024;

    sockaddr_in SenderAddr;
    int SenderAddrSize = sizeof (SenderAddr);

    //-----------------------------------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error %d\n", iResult);
        return 1;
    }
    //-----------------------------------------------
    // Create a receiver socket to receive datagrams
    RecvSocket = socket(AF_INET, SOCK_RAW, 50);
    if (RecvSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error %d\n", WSAGetLastError());
        return 1;
    }
    //-----------------------------------------------
    // Bind the socket to any address and the specified port.
    RecvAddr.sin_family = AF_INET;
    RecvAddr.sin_port = htons(Port);
    RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
    if (iResult != 0) {
        wprintf(L"bind failed with error %d\n", WSAGetLastError());
        return 1;
    }
    //-----------------------------------------------
    // Call the recvfrom function to receive datagrams
    // on the bound socket.
    wprintf(L"Receiving datagrams...\n");
    iResult = recvfrom(RecvSocket,
                       RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
    }

    //-----------------------------------------------
    // Close the socket when finished receiving datagrams
    printf("\n\nBufLen = %d\n\n", BufLen);
    wprintf(L"Finished receiving. Closing socket.\n");
    iResult = closesocket(RecvSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
        return 1;
    }

    //-----------------------------------------------
    // Clean up and exit.
    wprintf(L"Exiting.\n");
    Sleep(60000);
    WSACleanup();
    return 0;
}

0 个答案:

没有答案