OpenSSL 请求长度 - 如何计算大小?
我正在尝试用 C 实现 SSL 客户端。根据 此处 给出的示例(您可以直接查看源码这里 ),要发送到 SSL 服务器的输入的“request_len”计算如下 -
request_len=strlen(REQUEST_TEMPLATE)+ strlen(host)+6;
有人可以告诉我为什么长度要添加“6”吗?
I am trying to implement a SSL client in C. As per the examples given here (you can see the source code directly here), "request_len" of input to be sent to SSL server is calculated as -
request_len=strlen(REQUEST_TEMPLATE)+ strlen(host)+6;
Can someone please tell me why "6" is being added to length?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是端口号和空终止符的空间。端口范围为 0-65535,因此最多 5 个字符。并且字符串必须以 1 个字符的空终止符结尾。所以
5 + 1 = 6
。要弄清楚这一点,您必须查看他们填写请求的位置。我把它分开,这样你就可以看到论点是如何排列的。 REQUEST_TEMPLATE 内部有一个被主机替换的
%s
和一个被端口替换的%d
。额外信息:
然后他们会执行另一个 strlen 来查明请求有多大,因为端口可能少于 5 个字符。
strlen 不是必需的,因为 snprintf() 返回它构建的字符串的长度。
It's room for the port number and null terminator. The port is in the range 0-65535, so that's up to 5 characters. And the string has to end with a 1-character null terminator. So
5 + 1 = 6
.To figure this out, you have to look at where they fill in the request. I spaced it out so you can see how the arguments line up. Inside the REQUEST_TEMPLATE is a
%s
that gets replaced by the host and a%d
that gets replaced by the port.Bonus info:
They then do another strlen to find out how big the request is because the port may have been less than 5 characters.
The strlen is not necessary because snprintf() returns the length of the string it built.