注册 登录
编程论坛 汇编论坛

sockaddr_in转换IP头大啦

卡卡西西 发布于 2009-10-31 13:50, 1681 次点击
各位大虾!小弟不才来求救了!下面的两段代码搞得头爆了!望各位前辈帮我降降温,谢谢!!
        .data
IP        db    '192.168.0.1',0

_Test        proc  
    local    addrs:sockaddr_in

    invoke    inet_addr,offset IP
    mov       addrs.sin_addr,eax

    mov       eax,addrs.sin_addr
    invoke    inet_ntoa,eax
    invoke    MessageBox,NULL,eax,NULL,NULL        ;结果相当正确   192.168.0.1
    ret
_Test        endp

......;问题来了,,,,,
    local    addrs:sockaddr_in
invoke accept,hSocket,addr addrs,edx

       .if    eax!=INVALID_SOCKET
           mov   ecx,addrs.sin_addr                                ; 如果是 lea ecx,addrs.sin_addr 转换出来的就是不正确的
           invoke    inet_ntoa,ecx                                ;转换成功但结果是0.0.0.0 相当气人
           invoke    MessageBox,NULL,eax,NULL,NULL
       .endif
数据发送都正常就是想转换对方的IP老是转换不正确
求救是哪里出了错!第一段代码正确,搬到下面就变卦了
如何才能正确的将 invoke accept,hSocket,addr addrs,edx 对方的IP转换出来
5 回复
#2
东海一鱼2009-10-31 21:34
把你的代码多贴上来些。
#3
卡卡西西2009-11-01 07:44
来了来了
_bind    proc                                          ;绑定   
        local    @stSin : sockaddr_in
        local    @InAddr : sockaddr_in
        local    @szBuffer:dword
        pushad
        invoke    socket,AF_INET,SOCK_STREAM,0
        mov    hSocket,eax
        mov    @stSin.sin_family,AF_INET
        invoke    htons,8888
        mov    @stSin.sin_port,ax
        mov    @stSin.sin_addr,INADDR_ANY
        invoke    bind,hSocket,addr @stSin,sizeof @stSin
        .if    eax == SOCKET_ERROR

        .else
            invoke listen,hSocket,50
            .While TRUE
                invoke accept,hSocket,offset addrs,0
                .break    .if    eax==INVALID_SOCKET
                invoke    CreateThread,NULL,0,offset _ServiceThread,eax,NULL,esp        ;收数据都成功 就是下面
                invoke    CloseHandle,eax        
               
                mov    eax,addrs.sin_addr                    
                invoke    inet_ntoa,eax
                invoke    MessageBox,NULL,eax,NULL,NULL    ; 这就出问题了 都是 0.0.0.0 有什么办法能取到 addrs中的正确IP?

            .endw
            invoke    closesocket,hSocket
        .endif
        popad

        ret   
_bind    endp
#4
东海一鱼2009-11-01 10:07
invoke accept,hSocket,offset addrs,0      //这里的结构大小
.break    .if    eax==INVALID_SOCKET

出现这种问题,一般有两个可能:

1、accept的结构参数没有给大小。就是你现在的。
2、网络防火墙作了端口映射处理。


#5
sll08072009-11-02 01:26
注意看 MSDN说明

accept Function

The accept function permits an incoming connection attempt on a socket.

Syntax
C++
SOCKET accept(
  __in     SOCKET s,
  __out    struct sockaddr *addr,
  __inout  int *addrlen
);

Parameters
s [in]
A descriptor that identifies a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.

addr [out]
An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.

-------------------------------------------------------------------------------------------------------
addrlen [in, out]   ; in , out !!!!!!!!
An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.

-----------------------------------------------------------------------------------------------------------

_bind    proc                                          ;绑定   
        local    @stSin : sockaddr_in
        local    @stRetSin:sockaddr_in
        local    @dwRetSize:DWORD


        pushad
        invoke    socket,AF_INET,SOCK_STREAM,0
        mov    hSocket,eax
        mov    @stSin.sin_family,AF_INET
        invoke    htons,8888
        mov    @stSin.sin_port,ax
        mov    @stSin.sin_addr,INADDR_ANY
        invoke    bind,hSocket,addr @stSin,sizeof @stSin
        .if    eax == SOCKET_ERROR

        .else
            invoke listen,hSocket,50
            .While TRUE
                mov    @dwRetSize,sizeof sockaddr_in
                invoke accept,hSocket,addr @stRetSin,addr @dwRetSize
                .break    .if    eax==INVALID_SOCKET
                push      ecx
                invoke    CreateThread,NULL,0,offset _ServiceThread,eax,NULL,esp    
                pop       ecx            
                invoke    CloseHandle,eax        
               
                mov       eax,@stRetSin.sin_addr                 
                invoke    inet_ntoa,eax
                invoke    OutputDebugString,eax


            .endw
            invoke    closesocket,hSocket
        .endif
        popad

        ret   
_bind    endp


注:

accept   
如果不需要返回信息 直接
invoke accept,hSocket,NULL,NULL
如果需要返回信息 必须指定返回缓冲区 缓冲区是一个 sockaddr_in 结构 还必须指定 缓冲区大小
但是不可以
invoke accept,hSocket,addr @szBuff,sizeof @szBuff
因为 addrlen 必须是一个整型指针 int *addrlen
也就是说 必须赐予此指针  返回缓冲区的大小
然后函数在此指针返回 返回信息数据结构的大小
正确使用的情况如下:
Local  @szBuff:sockaddr_in
Local  @dwRetSize:DWORD

mov    @dwRetSize,sizeof @szBuff
invoke accept,hSocket,addr @szBuff,addr @dwRetSize



[ 本帖最后由 sll0807 于 2009-11-2 01:44 编辑 ]
#6
卡卡西西2009-11-02 09:34
真心感谢二位!
问题已成功解决!
1