Fields in bytes of structure passes

I have a structure I wrote that should represent the entire UDP packet, with the ethernet header and everything. There he is:

#pragma pack(1)
struct UDPPacket {
    // an array to hold the destination mac address of the packet
    unsigned char dstmac[6];

    // an array to hold the source mac address of the packet
    unsigned char srcmac[6];

    // two bytes to hold the packet type, this is almost always IP (08 00)
    WORD ethtype;

    // each of the subfields of this take up 4 bits. ver, the first half,
    // is the ip version, which should usually be 4 for ipv4, and the second
    // is the length of the header divided by 4, which is almost always 5
    struct {
        unsigned ver : 4;
        unsigned len : 4;
    } verlen;

    // this isn't used in ipv4 and is 0
    BYTE tos;

    // the total length of the header + data
    WORD iplen;

    // the id of this datagram for reassembling fragmented packets
    WORD id;

    // the first subfield occupies 3 bits and is the flags of this packet, which is usually 0
    // the second subfield is the fragmentation offset for large datagrams that have been split up for sending, usually 0
    struct {
        unsigned flags : 3;
        unsigned fragmentation : 13;
    } flagfrag;

    // time to live; usually 35 or 128
    BYTE ttl;

    // the protocol with which this packet is being transported
    // 1 = ICMP, 2 = IGMP, 6 = TCP, 17 = UDP
    BYTE protocol;

    // the ip checksum of this packet
    WORD ipchecksum;

    // the source ip of this packet
    DWORD src;

    // the destination ip of this packet
    DWORD dest;
    // the port from which this packet is coming
    WORD srcport;

    // the port this packet is headed to
    WORD destport;

    // the length of the udp header + data, not including the ip header
    // so it usually basically iplen - 20
    WORD udplen;

    // the udp checksum of this packet
    WORD udpchecksum;

    // a char pointer to the data of the packet
    unsigned char data[10000];
};
#pragma pack()

Of course, this is a representation of a real UDP packet, bytes must be of the same offset as in the packet, and pointers to this type of structure will be discarded unsigned char*for sending.
My problem is that when I try to assign something after UDPPacket.verlen, it skips forward about 5 bytes and starts there. For example, when I assign iplenfeild and not set bytes at offsets 16 and 17, it assigns them to about 23 and 24 (I can’t say for sure because I do not have my program available on my phone).
Is there an obvious reason for this that I am absent, or am I just doing something wrong?

+3
6

#pragmas . " " , , . , verlen "unsigned" , - unsigned int, 32 . verlen "unsigned char" .

. "unsigned char" - MSFT ( ANSI C), : http://msdn.microsoft.com/en-us/library/yszfawxh(VS.80).aspx

N.B. flagfrag, "unsigned short".

+2

, (int, char, ). , 2 2 ints. - , ?

+1

#pragma pack , , (verlen flagfrag) char short, .

+1

.

0
0

unsigned unsigned int, 4 . , iplen 23, , .

0

Source: https://habr.com/ru/post/1760323/


All Articles