Is their a simple way to get the bound ip address after using INADDR_ANY in the bind() call? I have been trying to use getsockname but I keep getting namelen size errors on it. Here's what I've been trying: invoke getsockname, hSocket, ADDR sin_local, ADDR namelen Thanks for any help!!
Posted on 2001-06-12 16:51:00 by r.daneel
Getsockname should work, what error code are you getting? Also, remember that the IP bound to might not be known before the socket is connected (in case of a multi-homed system). Thomas
Posted on 2001-06-12 17:07:00 by Thomas
The error code is: WSAEFAULT The namelen argument is not large enough. I'm declaring namelen as: namelen db 25 dup(0) in the .data segment. Thanks for your help Tom.
Posted on 2001-06-12 17:27:00 by r.daneel
namelen should not be the buffer, it should be a dword value containing the size of the name buffer (name is the buffer, not namelen). The code below works, it gave me 0.0.0.0 as IP because the socket wasn't connected yet, but the function succeeds so it should work for you (remove the buffer stuff and error checking, it's just something I used to test it):

.data?
hSocket     dd ?
TestAddr    sockaddr_in 
AddrLength  dd ?
Buffer      db  128 dup (?)     ;just a temporary buffer for the error message
.code
....
    invoke  socket, PF_INET, SOCK_STREAM, 0
    mov     hSocket, eax
    mov     TestAddr.sin_family, PF_INET
    mov     TestAddr.sin_port, 123
    mov     TestAddr.sin_addr.S_un.S_addr, INADDR_ANY
    invoke  bind, hSocket, ADDR TestAddr, SIZEOF TestAddr
    mov     AddrLength, SIZEOF TestAddr
    invoke  getsockname, hSocket, ADDR TestAddr, ADDR AddrLength
    .IF     eax==0
        invoke  inet_ntoa, TestAddr.sin_addr.S_un.S_addr
        invoke  MessageBox,0, eax, eax, MB_ICONINFORMATION
    .ELSE
        invoke  WSAGetLastError
        invoke  dwtoa, eax, ADDR Buffer
        invoke  MessageBox, 0, ADDR Buffer, 0 ,MB_ICONERROR
    .ENDIF
    invoke  closesocket, hSocket
.....
Thomas
Posted on 2001-06-12 17:39:00 by Thomas
Thanks for all the help and the code Tom. I can see how that works now. It's amazing how some functional code will clear things up every time. Thanks again.
Posted on 2001-06-12 18:21:00 by r.daneel