简体   繁体   English

C:inet_ntop返回值的位置?

[英]C: inet_ntop return value position?

I am reviewing the inet_ntop documentation. 我正在查看inet_ntop文档。 Typically, string builder operations in C return either a pointer to the end of the string, or an integer indicating the length of the string written. 通常, C字符串生成器操作将返回指向字符串末尾的指针,或者返回指示所写字符串长度的整数。

The documentation of inet_ntop states quite ambiguously: inet_ntop的文档含糊不清:

On success, inet_ntop() returns a non-null pointer to dst.  NULL is 
returned if there was an error, with the errno set to indicate the 
error.

There are two problems here: 这里有两个问题:

  1. Null string termination: it is not clear whether I need to add a null string delimiter throughout the memory allocation where inet_ntop is printing when compiling at order 3 or more 空字符串终止:尚不清楚在以3或更高的顺序编译时inet_ntop正在打印的内存分配中是否需要在整个内存分配中添加空字符串定界符

  2. I do not know whether the output of the function returns a pointer to the last written character, or the first written character. 我不知道函数的输出是否返回指向最后写入的字符或第一个写入的字符的指针。


What does inet_ntop return? inet_ntop返回什么?

It's quite clear to me, but perhaps you need to apply some "good will" to get it: 对我来说很清楚,但是也许您需要运用一些“善意”才能做到:

  1. It's not a string in C if it isn't terminated, so of course the result is terminated. 如果未终止,它不是C中的字符串,因此,结果当然会终止。
  2. dst is an input argument, even if it's a bit weirdly written "a pointer to dst " can't be "a pointer to dst plus something". dst是一个输入参数,即使它有点怪异地写成“指向dst的指针”也不能是“指向dst的指针加某物”。

You can of course also read an implementation to see what's going on. 当然,您也可以阅读实现以查看发生了什么。 The last statement for the "happy path" (no buffer overflow) for both IPv4 and v6 variants is: 对于IPv4和v6变体的“快乐路径”(没有缓冲区溢出)的最后一条语句是:

return strcpy(dst, tmp);

Which instantly tells you that dst is receiving a terminated string, and that dst is returned. 它立即告诉您dst正在接收终止的字符串,并且已返回dst

它返回const char *并且由于要求dst参数是有效对象(不能将NULL作为dst传递),因此无需创建中间指针来返回函数的状态。

The inet_pton() documentation : inet_pton()文档

... ...

SYNOPSIS 概要

 #include <arpa/inet.h> const char *inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size); 

... ...

DESCRIPTION 描述

The inet_ntop() function shall convert a numeric address into a text string suitable for presentation. inet_ntop()函数应将数字地址转换为适合表示的文本字符串。 The af argument shall specify the family of the address. af参数应指定地址的族。 This can be AF_INET or AF_INET6 . 可以是AF_INETAF_INET6 The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET , or an IPv6 address if the af argument is AF_INET6 ; 如果af参数为AF_INET ,则src参数指向保存IPv4地址的缓冲区;如果af参数为AF_INET6 ,则指向IPv6地址的缓冲区; the address must be in network byte order. 地址必须按网络字节顺序。 The dst argument points to a buffer where the function stores the resulting text string; dst参数指向函数存储结果文本字符串的缓冲区; it shall not be NULL . 它不能为NULL The size argument specifies the size of this buffer, which shall be large enough to hold the text string ( INET_ADDRSTRLEN characters for IPv4, INET6_ADDRSTRLEN characters for IPv6). 尺寸参数指定该缓冲区的大小,这应是大足以容纳文本字符串( INET_ADDRSTRLEN字符的IPv4, INET6_ADDRSTRLEN字符的IPv6)。

... ...

RETURN VALUE 返回值

The inet_ntop() function shall return a pointer to the buffer containing the text string if the conversion succeeds, and NULL otherwise, and set errno to indicate the error. 如果转换成功,则inet_ntop()函数应返回一个指向包含文本字符串的缓冲区的指针,否则将返回NULL ,并将errno为指示错误。

... ...

ERRORS 错误

The inet_ntop() and inet_pton() functions shall fail if: 如果inet_ntop()inet_pton()函数将失败:

[ EAFNOSUPPORT ] The af argument is invalid. [ EAFNOSUPPORT ] af参数无效。

[ ENOSPC ] The size of the inet_ntop() result buffer is inadequate. [ ENOSPC ] inet_ntop()结果缓冲区的大小不足。

And the definition of a "string" : “字符串”的定义

3.92 Character String 3.92字符串

A contiguous sequence of characters terminated by and including the first null byte. 由第一个空字节(包括第一个空字节)终止的连续字符序列。

Given that, the answer to 鉴于此,答案

  1. Null string termination: it is not clear whether I need to add a null string delimiter throughout the memory allocation where inet_ntop is printing when compiling at order 3 or more 空字符串终止:尚不清楚在以3或更高的顺序编译时inet_ntop正在打印的内存分配中是否需要在整个内存分配中添加空字符串定界符

should be clear. 应该清楚。 A string by definition includes the null byte. 根据定义,字符串包括空字节。 inet_pton() returns a pointer to a buffer that contains such a string. inet_pton()返回一个指向包含此类字符串的缓冲区的指针。 No addition of a null byte is needed. 无需添加空字节。

And the answer to 而答案

  1. I do not know whether the output of the function returns a pointer to the last written character, or the first written character. 我不知道函数的输出是否返回指向最后写入的字符或第一个写入的字符的指针。

is given by the C standard in 6.3.2.3 Pointers , paragraph 7 : 由C标准6.3.2.3指针第7段给出

... When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. ...当指向对象的指针转换为指向字符类型的指针时,结果指向该对象的最低寻址字节。

So, the "pointer to the buffer containing the text string if the conversion succeeds" returned by inet_ntop() points to the first byte in the string. 因此, inet_ntop()返回的“如果转换成功则指向包含文本字符串的缓冲区的指针”指向字符串中的第一个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM