S
S
sddvxd2019-05-12 02:32:22
assembler
sddvxd, 2019-05-12 02:32:22

Why not connect to the server?

section .bss
  wsadata resb 408 ; sizeof(WSADATA)

section .data
  IP0 EQU 188
  IP1 EQU 187
  IP2 EQU 188
  IP3 EQU 179
  
  AF_INET EQU 2
  IPPROTO_TCP EQU 6
  SOCK_STREAM EQU 1
  
  sockaddr_in dd 0, 0, 0, 0 ; 16 bytes : sizeof(SOCKADDR_IN)
  socket dd 0
  
  errorCreatingSocket db "Error creating socket", 0Dh, 0Ah
  errorCreatingSocketLen EQU $-errorCreatingSocket
  
  errorCreatingConnect db "Error creating connect", 0Dh, 0Ah
  errorCreatingConnectLen EQU $-errorCreatingConnect
  
  errorSendingData db "Error sending data", 0Dh, 0Ah
  errorSendingDataLen EQU $-errorSendingData
    
section .text
_start:
  mov al, 2
  mov ah, 2
  push wsadata
  push eax
  call [email protected]
  test eax, eax
  jnz error
  
  mov byte [sockaddr_in + 1], AF_INET ; sin_family
  mov byte [sockaddr_in + 2], 80 ; sin_port[0]
  mov byte [sockaddr_in + 3], 0 ; sin_port[1]
  
  mov byte [sockaddr_in + 4], IP0
  mov byte [sockaddr_in + 5], IP1
  mov byte [sockaddr_in + 6], IP2   ;IN_ADDR structure
  mov byte [sockaddr_in + 7], IP3
  
  push IPPROTO_TCP
  push SOCK_STREAM
  push AF_INET
  call [email protected]
  mov ebx, errorCreatingSocket
  mov edx, errorCreatingSocketLen
  cmp eax, -1
  je error
  
  mov dword [socket], eax
  
  push 16
  push sockaddr_in
  push dword [socket]
  call [email protected]
  mov ebx, errorCreatingConnect
  mov edx, errorCreatingConnectLen
  cmp eax, -1
  je error
  
  push 0
  push msgRequestLen
  push msgRequest
  push dword [socket]
  call [email protected]
  mov ebx, errorSendingData
  mov edx, errorSendingDataLen
  cmp eax, -1
  je error
  
  push 0
  push 8128
  push buffer
  push dword [socket]
  call [email protected]
  
  mov dword [requestLength], eax	

exit:
  push 0
  call [email protected]


The program runs successfully until [email protected] is called - EAX gets -1, indicating an error. LastError shows WSAEAFNOSUPPORT (The address does not match the selected address family). The contents of the memory at sockaddr_in :

00 02 50 00 bc bb bc b3 00 00 00 00 00 00 00 00
The first 2 bytes indicate that the socket belongs to AF_INET, the second 2 indicate port 80, and both address and port are in big-endian, because my processor works with little-endian. In the next 4 bytes, the server address and the next 8 are empty
. It seems that I did everything right, tell me please - what could be wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-05-12
@sddvxd

I'm looking here , and it seems to me that the beginning of the sockaddr_in initialization in your code should look like this:

mov byte [sockaddr_in + 0], AF_INET ; sin_family
mov byte [sockaddr_in + 2], 0 ; sin_port[0] это старший байт sin_port
mov byte [sockaddr_in + 3], 80 ; sin_port[1] а это младший

Those. mixed up endianness sin_family and sin_port. sin_addr looks ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question