Frage

Ich entwickle eine Netzwerkanwendung und mit Sockel-APIs.

Ich möchte sin6_addr Byte-Reihenfolge der Sockaddr_in6-Struktur einstellen.

für 16 Bits oder 32 Bit-Variablen ist einfach: mithilfe von HTONS oder HTONL: generasacodicetagpre.

Aber für 128 Bit-Variablen weiß ich nicht, wie man Byte-Reihenfolge auf Netzwerkbyte-Reihenfolge einstellt: generasacodicetagpre.

Einige Antworten verwenden möglicherweise 8-mal (2 * 8= 16 Bytes) oder verwenden Sie die Verwendung von HTOLL für 4-mal (4 * 4= 16 Bytes), aber ich weiß nicht, welchen Weg richtig ist. .

danke.

War es hilfreich?

Lösung

The s6_addr member of struct in6_addr is defined as:

uint8_t s6_addr[16];

Since it is an array of uint8_t, rather than being a single 128-bit integer type, the issue of endianness does not arise: you simply copy from your source uint8_t [16] array to the destination. For example, to copy in the address 2001:888:0:2:0:0:0:2 you would use:

static const uint8_t myaddr[16] = { 0x20, 0x01, 0x08, 0x88, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 };

memcpy(addr.sin6_addr.s6_addr, myaddr, sizeof myaddr);

Andere Tipps

The usual thing would be to use one of the hostname lookup routines and use the result of that, which is already in network byte order. How come you're dealing with hardcoded numeric IP addresses at all?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top