Sockaddr - print all the information that sa_data stores - C ++

in my c ++ application i am using sockaddr. I want to see everything that sockaddr.sa_data has [14]. while I just type ip from sa_data [2] .sa_data [3] .sa_data [4] .sa_data [5].

I want to print in such a way that I can understand (and please explain) all the information in the sa_data file of 14 bytes.

any help?

thank!

+3
source share
6 answers

One possibility is to use inet_ntop, which should be able to handle IPv4 and IPv6 addresses. It will create a readable address string.

+1
source

sa_data IPv4 Windows , , - IP-.

, 228.0.0.1:9995, sa_data...

27 0b e4 00 00 01 00 00 00 00 00 00 00 00

270b - 9995 . - IP-, 0xe4 - 228, , 0x01 228 0 0 1.
, .

, sa_data .

+5

sa_data, 14 , () : sa_family.

  • AF_INET, , 4 IP- .

  • PF_PACKET, Ethernet (wither 0800 β†’ IP, 0806 β†’ ARP ..) 4 ( ) . :

    • 02 00 00 00 = eth0,
    • 03 00 00 00 = eth1,
    • 04 00 00 00 = eth2 .. 8 AF_INET, PF_PACKET .
+1
std::copy( &sa_data[0], &sa_data[0]+sizeof(sa_data)/sizeof(sa_data[0]), 
             std::ostream_iterator<int>(std::cout, " "));

int . unsigned int, , iomanip , .

0

The information sockaddrdepends on which socket family and protocol you are using.

If you use IPv4, you need to make a pointer sockaddr sockaddr_in *. Only the first 6 bytes of the address make sense when you use IPv4. The rest should simply be ignored.

0
source

To receive and print sa_data member struct sockaddr, please refer to John's answer on Obtaining an IPV4 Address from the sockaddr Structure

0
source

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


All Articles