从字节缓冲区中提取 IP
如果我有一个包含这些字节的缓冲区:510175126-94-51080
如何从中提取75126-94-51
部分(这是IP)并打印正确的IP?
for(int i = 0; i < bytes_recv; i++) { cout << static_cast
编辑:这是我的输出:5 1 0 1 75 126 -94 -51 0 80
If I have a buffer containing these bytes:510175126-94-51080
How can I extract the 75126-94-51
part from it(which is the IP) and print the correct IP?
for(int i = 0; i < bytes_recv; i++) { cout << static_cast<int>(temp[i]); } cout << endl;
edit: this is my output: 5 1 0 1 75 126 -94 -51 0 80
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题中缺少很多信息。因此,您正在读取来自 SOCKS 协议的服务器回复。首先,缓冲区不应该也不会具有 10 字节的固定大小。如果你的地址是 IPv4,它有 10 个字节(巧合的是,几天前它已经用完了,是时候考虑一下 IPv6 了)。如果源具有 IPv6 地址,则服务器响应的大小会有所不同。
从 RFC 1928 第 6 节开始,服务器回复具有以下格式:
请注意,地址字段的大小是可变的。具体来说,对于 IPv4,ATYP == 0x01 且 BND.ADDR 的大小为 4,这使得 1+1+1+1+4+2 = 10 字节。但您应该考虑其他大小也是可能的,特别是如果 ATYP == 0x03,这使得 BND.ADDR 的长度真正可变。
因此,在回答您的问题时,考虑到您在 char buffer[] 数组(或指针)中拥有这些字节,您必须首先检查地址的类型,然后像这样提取它:
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:
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:在Python中:
In Python:
你如何获得这个缓冲区?它是 struct sockaddr_in 的一部分吗?
您是否显示从缓冲区最低地址开始的十进制字节转储?
您想要做的是获取指向 IP addr 部分的基(内存)地址的指针,并使用 ntohl() 将其转换为主机字节顺序,同样使用 ntohs code> 转换端口地址。
编辑
假设您
可以使用以下命令提取点分四组形式的 IP 地址
How are you getting this buffer? Is it part of a
struct sockaddr_in
?Are you showing a decimal dump of bytes starting at the lowest address of the buffer?
What you want to do is get a pointer to the base (memory) address of the IP addr part and use
ntohl()
to translate it to host byte order, and likewise usentohs
to translate the port addr.edit
Suppose you have
You can extract the IP address in dotted-quad form with
如果您有以下缓冲区:
并且您希望在 C/C++ 中使用它。我将定义一个结构体,如下所示:
然后将其放入联合中:
现在您可以声明 Union buffer_u buf 并将字节读取到 buf.buffer 中。然后对于 ip 和 port 字段,使用
ntohl
和ntohs
将网络字节顺序转换为主机字节顺序。If you have the following buffer:
And you want it in C/C++. I would define a struct, like this:
Then put it in a union:
Now you can declare
union buffer_u buf
and read bytes into buf.buffer. Then for the ip and port fields usentohl
andntohs
to convert network byte order to host byte order.