Extract IP from byte buffer

If I have a buffer containing these bytes:
510175126-94-51080

How can I extract part 75126-94-51 from it (this is ip) and print the correct IP? Thank you in advance.

for(int i = 0; i < bytes_recv; i++)
{
cout << static_cast<int>(temp[i]);
}
cout << endl;

edit: this is my conclusion: 5 1 0 1 75 126 -94 -51 0 80

0
source share
4 answers

The question is missing a lot of information. So, you are reading the server response from the SOCKS protocol. First, the buffer should not and should not have a fixed size of 10 bytes. It has 10 bytes if your address is IPv4 (which, as a general rule, was exhausted a few days ago, time to think about IPv6). If the source has an IPv6 address, the size of the server response is different.

From RFC 1928 , section 6, the server response has the following format:

 +----+-----+-------+------+----------+----------+ |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | +----+-----+-------+------+----------+----------+ | 1 | 1 | X'00' | 1 | Variable | 2 | +----+-----+-------+------+----------+----------+ 

Note that the address field is variable in size. For IPv4, in particular, ATYP == 0x01 and BND.ADDR are 4 in size, which makes 1 + 1 + 1 + 1 + 4 + 2 = 10 bytes. But you have to consider that other sizes are possible, especially if ATYP == 0x03, which makes BND.ADDR really variable in length.

So, answering your question, given that you have these bytes in the char buffer[] array (or pointer), you should first check the address type and then extract it as follows:

 #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; } 
+2
source

In Python:

 input = '510175126-94-51080' output = input[4:-3] print output 
0
source

How do you get this buffer? Is this part of struct sockaddr_in ?

Do you show a decimal byte dump starting with the lowest buffer address?

What you want to do is get a pointer to the base (memory) address of the IP address part and use ntohl() to translate it into host byte order, and use ntohs to translate the addr port.

change

Suppose you have

 stuct sockaddr_in foo; char str[16]; // do some stuff that initializes foo, eg a call to accept() returns 

You can extract the IP address of the square-labeled form with

 inet_ntop(AF_INET, &(foo.sin_addr), str, sizeof(str)); 
0
source

If you have the following buffer:

 buffer[10] = {5, 1, 0, 1, 75, 126, 162, 205, 80, 00}; 

And you want it in C / C ++. I would define a structure, for example:

 #pragma pack(1) struct buffer_s { uint8_t version; uint8_t reserve_1; uint8_t atype; uint32_t ip; uint16_t port; }; #pragma pack() 

Then put it in a union:

 union buffer_u { char buffer[10]; struct buffer_s data; }; 

Now you can declare union buffer_u buf and read the bytes in buf.buffer. Then, for the ip and port fields, use ntohl and ntohs to convert the byte order of the network to the host byte order.

0
source

All Articles