A lot of information is missing from the question. So, you are reading the server reply from the SOCKS protocol. First, the buffer should not and will not have a fixed size of 10 bytes. It has 10 bytes if your address is IPv4 (which coincidently was exhausted a few days ago, time to think about IPv6). If the origin has a IPv6 address, the size of the server response is different.
From RFC 1928, section 6, the server reply has this format:
+----+-----+-------+------+----------+----------+
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+
Note that the address field has variable size. For IPv4, specifically, ATYP == 0x01 and BND.ADDR has size 4, which makes 1+1+1+1+4+2 = 10 bytes. But you should consider that other sizes are possible, specially if ATYP == 0x03, which makes BND.ADDR truly variable in length.
So, answering your question, considering that you have these bytes in a char buffer[]
array (or pointer), you must first check the type of the address, then extract it like this:
#include <arpa/inet.h>
switch (buffer[3]) {
case 0x01: { /* IPv4 address */
char result[INET_ADDRSTRLEN];
inet_ntop(AF_INET, (void*)(&buffer[4]), result, sizeof result);
std::cout << "IPv4: " << result << "\n";
break;
}
case 0x04: { /* IPv6 address */
char result[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, (void*)(&buffer[4]), result, sizeof result);
std::cout << "IPv6: " << result << "\n";
break;
}
default:
std::cout << "Unsupported format.\n";
break;
}