天天看點

inet_ntoa()

功能:

将一個IP轉換成一個網際網路标準點分格式的字元串。

原型:

char FAR * inet_ntoa( struct in_addr in);

頭檔案:

arpa/inet.h

inet_ntoa()傳回的字元串是臨時裝在一個靜态配置設定的緩沖區裡面,下一次調用此函數的時候緩沖區會被重寫 

源碼如下:

char *
inet_ntoa (struct in_addr in)
{
  __libc_once_define (static, once);
  char *buffer;
  unsigned char *bytes;

  /* If we have not yet initialized the buffer do it now.  */
  __libc_once (once, init);

  if (static_buf != NULL)
    buffer = static_buf;
  else
    {
      /* We don't use the static buffer and so we have a key.  Use it
	 to get the thread-specific buffer.  */
      buffer = __libc_getspecific (key);
      if (buffer == NULL)
	{
	  /* No buffer allocated so far.  */
	  buffer = malloc (18);
	  if (buffer == NULL)
	    /* No more memory available.  We use the static buffer.  */
	    buffer = local_buf;
	  else
	    __libc_setspecific (key, buffer);
	}
    }

  bytes = (unsigned char *) ∈
  __snprintf (buffer, 18, "%d.%d.%d.%d",
	      bytes[0], bytes[1], bytes[2], bytes[3]);

  return buffer;
}