-1

I'm trying to make a "socket" with masm32, but something is wrong with accept(), ollydbg show me an Access Violation when the code try to execute accept() and I don't know what is wrong, Can somebody tell me how can I fix it, please?

.686
.model flat, stdcall
option casemap:none

extrn ExitProcess@4:PROC
extrn WSAStartup@8:PROC
extrn socket@12:PROC
extrn bind@12:PROC
extrn listen@8:PROC
extrn accept@12:PROC

WSADATA STRUCT 8
wVersion          WORD  ?
wHighVersion      WORD  ?
iMaxSocket        WORD  ?
iMaxUdpDg         WORD  ?
lpVendorInfo      DWORD ?
szDescription     SBYTE 257 dup (?)
szSystemStatus    SBYTE 129 dup (?)
WSADATA ENDS

sockaddr STRUCT
sa_family   WORD ?
sa_port     WORD  ?
sa_addr     DWORD ?
            BYTE 8 dup (?)
sockaddr ENDS

.const
address sockaddr<2, 0B922h, 00000000h>
sbuff BYTE 50 dup (0)

.data?
wsadata WSADATA <>
Socket DWORD ?
.code

Start proc
push ebp
mov ebp, esp

lea edx, wsadata
push edx
push 2h
call WSAStartup@8

push 0h
push 1h
push 2h
call socket@12
mov Socket, eax

push 16h
lea ecx, address
push ecx
push Socket
call bind@12

push 1h
push Socket
call listen@8

push 16h
lea ecx, address
push ecx
push Socket
call accept@12



mov eax, 0
call ExitProcess@4

mov esp, ebp
pop ebp

Start endp
END

regards

Makuvex Linux
  • 91
  • 1
  • 2
  • 6

1 Answers1

0

The third argument of access is a pointer to the length, not the length itself.

By the way: The structure is 16 (decimal), not 16h bytes long.

Therefore your code should look like this:

.data?
wsadata WSADATA <>
Socket DWORD ?
addrlen DWORD 10h        <- This one is new!
.code
    ...
push 10h                 <- Instead of 16h
    ...
call bind@12
    ...
lea ecx, addrlen
push ecx
lea ecx, address
push ecx
push Socket
call accept@12
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • Regarding the struct size; MASM32 also has `LENGTHOF variable`, `SIZEOF variable` and `SIZEOF type` operators. – Michael Sep 20 '13 at 06:56