2012年7月12日 星期四

ipv6 pton空字串

當inet_pton進一個空字串再反向inet_ntop回來時,值會變怎樣呢?
範例程式碼如下
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
int main()
{
    unsigned char a[16];
    char b[64] = "";
    char c[64] = "";
    
    
    inet_pton(PF_INET6, b, &a);
    inet_ntop(PF_INET6, b, c, sizeof(c));
    
    fprintf(stderr, "c is %s\n", c);
    
    return 0;
}

結果:
./ntop_test 
c is ::

2012.7.12更新
在這個範例中inet_pton轉換空字串其實是失敗的,並沒有寫入a中。
其實::的結果是c為空字串轉換回去的結果。

沒有留言: