Function to retrieve the header destination address from a packet in windows XP

前端 未结 1 667
灰色年华
灰色年华 2021-01-22 21:11

I am interested in retrieving the destination address that an inbound packet is sent to. For example on Linux you can utilize recvmsg:

res = recvmsg         


        
相关标签:
1条回答
  • 2021-01-22 21:52

    windows does not seem to implement the recvmsg function.

    Actually, it does (well, an equivalent, anyway):

    WSARecvMsg function

    Similar functions have been revamped like the WSArevcMsg but these are only included in windows vista and above.

    You can't always trust MSDN documentation. When Microsoft officially drops support for a Windows version (like XP), it tends to remove references to that version from MSDN, including "Minimum supported ..." requirements of API functions (which annoys many programmers). The key point is supported. Microsoft does not support older Windows versions.

    WSARecvMsg() was first introduced in XP, which was EOL'ed in 2014 and many references to it were stricken from MSDN documentation (here is proof from 2013 when the WSARecvMsg() documentation stated XP and not Vista was the "Minimum supported client").


    As stated in the WSARecvFrom() documentation:

    Note The function pointer for the WSARecvMsg function must be obtained at run time by making a call to the WSAIoctl() function with the SIO_GET_EXTENSION_FUNCTION_POINTER opcode specified. The input buffer passed to the WSAIoctl function must contain WSAID_WSARECVMSG, a globally unique identifier (GUID) whose value identifies the WSARecvMsg extension function. On success, the output returned by the WSAIoctl function contains a pointer to the WSARecvMsg function. The WSAID_WSARECVMSG GUID is defined in the Mswsock.h header file.

    WSAID_WSARECVMSG is defined as:

    #define WSAID_WSARECVMSG \
        {0xf689d7c8,0x6f1f,0x436b,{0x8a,0x53,0xe5,0x4f,0xe3,0x51,0xc3,0x22}}
    

    aka {F689D7C8-6F1F-436B-8A53-E54FE351C322} in text format.

    For example:

    SOCKET sckt = ...;
    LPFN_WSARECVMSG lpWSARecvMsg = NULL;
    
    GUID g = WSAID_WSARECVMSG;
    DWORD dwBytesReturned = 0;
    if (WSAIoctl(sckt, SIO_GET_EXTENSION_FUNCTION_POINTER, &g, sizeof(g), &lpWSARecvMsg, sizeof(lpWSARecvMsg), &dwBytesReturned, NULL, NULL) != 0)
    {
        // WSARecvMsg is not available...
    }
    

    Then, to use it:

    BYTE buffer[...];
    DWORD dwBytesRecv;
    
    WSABUF msgbuf;
    memset(&msgbuf, 0, sizeof(msgbuf));
    msgbuf.len = sizeof(buffer);
    msgbuf.buf = (char *) buffer;
    
    // call WSA_CMSG_LEN() once for each option you enable
    // on the socket that can return data in WSARecvMsg()...
    int size = WSA_CMSG_LEN(WSA_CMSG_LEN(sizeof(buffer)));
    BYTE *controlbuf = (BYTE *) malloc(size);
    
    SOCKADDR_STORAGE *addrbuf = (SOCKADDR_STORAGE *) malloc(sizeof(SOCKADDR_STORAGE));
    
    WSAMSG msg;
    memset(&msg, 0, sizeof(msg));
    msg.name = (struct sockaddr *) addrbuf;
    msg.namelen = sizeof(SOCKADDR_STORAGE);
    msg.lpBuffers = &msgbuf;
    msg.dwBufferCount = 1;
    msg.Control.len = size;
    msg.Control.buf = (char *) controlbuf;
    
    if (lpWSARecvMsg(sckt, &msg, &dwBytesRecv, NULL, NULL) == 0)
    {
        // addrbuf contains the sender's IP and port...
        switch (addrbuf->ss_family)
        {
            case AF_INET:
            {
                struct sockaddr_in *addr = (struct sockaddr_in*) addrbuf;
                // use addr as needed...
                break;
            }
    
            case AF_INET6:
            {
                struct sockaddr_in6 *addr = (struct sockaddr_in6*) addrbuf;
                // use addr as needed...
                break;
            }
        }
    
        WSACMSGHDR *msghdr = WSA_CMSG_FIRSTHDR(&msg);
        while (msghdr)
        {
            switch (msghdr->cmsg_type)
            {
                case IP_PKTINFO: // also IPV6_PKTINF
                {
                    // must call setsockopt(sckt, IPPROTO_IP, IP_PKTINFO, TRUE) beforehand to receive this for IPv4
                    // must call setsockopt(sckt, IPPROTO_IPV6, IPV6_PKTINFO, TRUE) beforehand to receive this for IPv6
    
                    switch (addrbuf->ss_family)
                    {
                        case AF_INET:
                        {
                            struct in_pktinfo *pktinfo = (struct in_pktinfo *) WSA_CMSG_DATA(msghdr);
                            // use pktinfo as needed...
                            break;
                        }
    
                        case AF_INET6:
                        {
                            struct in6_pktinfo *pktinfo = (struct in6_pktinfo *) WSA_CMSG_DATA(msghdr);
                            // use pktinfo as needed...
                            break;
                        }
                    }
    
                    break;
                }
    
                // other packet options as needed...
            }
    
            msghdr = WSA_CMSG_NXTHDR(&msg, msghdr);
        }
    }
    
    free(addrbuf);
    free(controlbuf);
    
    0 讨论(0)
提交回复
热议问题