[Ns-bugs] [Bug 283] getsockname implementation needs support in socket base class

bugzilla-daemon@nsnam-www.ece.gatech.edu bugzilla-daemon at nsnam-www.ece.gatech.edu
Sun Sep 7 00:31:59 PDT 2008


http://www.nsnam.org/bugzilla/show_bug.cgi?id=283





------- Comment #10 from liujatp at gmail.com  2008-09-07 03:31 -------
my test code:

int main()
{
    int sockfd, ret;
    struct sockaddr_in addr, addr2;
    socklen_t len = 0;

    if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
    {
        printf("socket creat error\n");
    }

    memset(&addr, 0, sizeof(addr));

    len = 6;//different value, different sockaddr output
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8001);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    addr2.sin_port = 0xffff;
    addr2.sin_addr.s_addr = 0xffffffff;

#if 0    
    if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
        printf("bind error %d\n", errno);
    }
#endif

  ret = getsockname(sockfd, (struct sockaddr*)&addr2, &len);
    if (ret == -1)
    {
        printf("getsockname error %d\n", errno);
    }

    printf("addr.sin_family = %d, len = %d, addr->sin_port = %d, addr= %s\n",
     addr2.sin_family, len, htons(addr2.sin_port),
inet_ntoa(addr2.sin_addr.s_addr));    
}

(In reply to comment #9)
> i know what has happend after reading kernel code.
> when getsockname() was called without bind(), linux kernel fill the struct
> sockaddr with the initial value(memset with zero), then it call
> move_addr_to_user() to fill the userspace sockaddr by the input namelen. 
> so,
>  if the input namelen  == 0, the output sockaddr not changed,
>  if the input namelen == sizeof(sockaddr), the output sockaddr were all zero,
>  if the input namelen (0, sizeof(sockaddr)), the output sockaddr partly zero,
> partly not changed. 
> 
> At the NS3 side, GetSockName return address value by 0, when called before
> bind(). 
> 


-- 
Configure bugmail: http://www.nsnam.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.


More information about the Ns-bugs mailing list