inet_ntoa的實現

來源:互聯網
上載者:User

從一段代碼開始:

[cpp]
view plaincopyprint?

  1. int test_ntoa() 
  2.     struct sockaddr_in recv = {0}; 
  3.     struct sockaddr_in sa   = {0}; 
  4.     recv.sin_addr.s_addr = -217732928;
    //192.168.5.243   
  5.     sa.sin_addr.s_addr   = -939415360; //192.168.5.200 
  6.     printf("ip of recv:%s ip of sa:%s\n", inet_ntoa(recv.sin_addr), inet_ntoa(sa.sin_addr)); 
  7.     printf("ip of recv:%s\n", inet_ntoa(recv.sin_addr)); 
  8.     printf("ip of sa  :%s\n", inet_ntoa(sa.sin_addr));  
  9.     return 0;    

int test_ntoa()<br />{<br />struct sockaddr_in recv = {0};<br />struct sockaddr_in sa = {0};<br />recv.sin_addr.s_addr = -217732928; //192.168.5.243<br />sa.sin_addr.s_addr = -939415360; //192.168.5.200<br />printf("ip of recv:%s ip of sa:%s\n", inet_ntoa(recv.sin_addr), inet_ntoa(sa.sin_addr));<br />printf("ip of recv:%s\n", inet_ntoa(recv.sin_addr));<br />printf("ip of sa :%s\n", inet_ntoa(sa.sin_addr));<br />return 0;<br />}

結果有點詫異:

[cpp]
view plaincopyprint?
  1. ip of recv:192.168.5.243 ip of sa:192.168.5.243 
  2. ip of recv:192.168.5.243 
  3. ip of sa   :192.168.1.200 

ip of recv:192.168.5.243 ip of sa:192.168.5.243<br />ip of recv:192.168.5.243<br />ip of sa :192.168.1.200

C函數出現這類問題,多半是由於其實現使用了靜態記憶體所知.

google其源碼:

[cpp]
view plaincopyprint?
  1. char * 
  2. inet_ntoa (struct in_addr in) 
  3.   __libc_once_define (static, once); 
  4.   char *buffer; 
  5.   unsigned char *bytes; 
  6.  
  7.   /* If we have not yet initialized the buffer do it now.  */ 
  8.   __libc_once (once, init); 
  9.  
  10.   if (static_buf != NULL) 
  11.     buffer = static_buf; 
  12.   else 
  13.     { 
  14.       /* We don't use the static buffer and so we have a key.  Use it
  15.      to get the thread-specific buffer.  */ 
  16.       buffer = __libc_getspecific (key); 
  17.       if (buffer == NULL) 
  18.     { 
  19.       /* No buffer allocated so far.  */ 
  20.       buffer = malloc (18); 
  21.       if (buffer == NULL) 
  22.         /* No more memory available.  We use the static buffer.  */ 
  23.         buffer = local_buf; 
  24.       else 
  25.         __libc_setspecific (key, buffer); 
  26.     } 
  27.     } 
  28.  
  29.   bytes = (unsigned char *) ∈ 
  30.   __snprintf (buffer, 18, "%d.%d.%d.%d", 
  31.           bytes[0], bytes[1], bytes[2], bytes[3]); 
  32.  
  33.   return buffer; 

char *<br />inet_ntoa (struct in_addr in)<br />{<br /> __libc_once_define (static, once);<br /> char *buffer;<br /> unsigned char *bytes;</p><p> /* If we have not yet initialized the buffer do it now. */<br /> __libc_once (once, init);</p><p> if (static_buf != NULL)<br /> buffer = static_buf;<br /> else<br /> {<br /> /* We don't use the static buffer and so we have a key. Use it<br /> to get the thread-specific buffer. */<br /> buffer = __libc_getspecific (key);<br /> if (buffer == NULL)<br />{<br /> /* No buffer allocated so far. */<br /> buffer = malloc (18);<br /> if (buffer == NULL)<br /> /* No more memory available. We use the static buffer. */<br /> buffer = local_buf;<br /> else<br /> __libc_setspecific (key, buffer);<br />}<br /> }</p><p> bytes = (unsigned char *) ∈<br /> __snprintf (buffer, 18, "%d.%d.%d.%d",<br /> bytes[0], bytes[1], bytes[2], bytes[3]);</p><p> return buffer;<br />}
這裡使用了靜態緩衝buffer,函數把轉換後的ip寫入緩衝buffer,然後返回buffer的首地址值.

看似無奈的選擇.glibc裡的某些底層函數都使用這類處理方式.

其實也可以選擇外部傳參的做法解決:

[cpp]
view plaincopyprint?
  1. <pre name="code"class="cpp">char * 
  2. inet_ntoa (struct in_addr in,
    char* buf, int buflen) 
  3.       ... ... 

<pre name="code" class="cpp">char *<br />inet_ntoa (struct in_addr in, char* buf, int buflen)<br />{<br /> ... ...<br />}


這樣的處理方式,效率略低於第一種.或許這也是glibc選擇第一種方式的原因.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.