My first socket program is great except for port is incorrect

Started by gavin, July 26, 2005, 11:03:20 AM

Previous topic - Next topic

gavin

Hi guys i made a udp socket program.
I'm having a little trouble with the htons.


port db 10 dup(?)

invoke htons,ADDR port                            ; Convert port number to network byte order
   
mov [sockAddr1.sin_port],ax                       ; and store it in sin_port


When i send the packet the port is not correct.
Is my buffer incorrect?
I'm kinda lost now and i searched everywhere before posting here.

Thanks for any help. :U

rwalt

Maybe you should do it like this

.data
    nPort   dd ?

.code
    mov     eax,[nPort]
    xchg    al,ah
    mov     [sockAddr1.sin_port],ax

Tedd

If your port number is a string, then you first need to convert it into a word value.
The htons function then just re-arranges the bytes into "network order" -- which is actually just the same as "xcgh al,ah"
If you already know which port you're going to use then you can simply do:

PORT equ 80    ;for example

mov eax,PORT
invoke htons, eax
mov [sockAddr1.sin_port],ax


Obviously, if you only have the port number as a string, then you need to convert it first, and then do that :wink
No snowflake in an avalanche feels responsible.

gavin

Hi guys .
Thanks Tedd and Rwalt.
I am recieving the port from stdin,i used the
nport dd ?
But anything larger than 2 numbers doesn't work.
So maybe this seems simple to you guys but i'm stumped.
:dazzled: :red
Thanks for you patience

My code just incase you need it.


;-----------------------------------------------------------------------------
;Assembler directives
;-----------------------------------------------------------------------------
   .386
   .model flat, stdcall

   option casemap:none

; -------------------------------------------------------------------------
; Include file section
; -------------------------------------------------------------------------
   include \masm32\include\windows.inc
   include \masm32\include\kernel32.inc
   include \masm32\include\user32.inc
   include \masm32\include\masm32.inc
   include \masm32\include\crtlib.inc
   include \masm32\include\ws2_32.inc    ; the winsock 2 include

; -------------------------------------------------------------------------
; Include library section
; -------------------------------------------------------------------------
   includelib \masm32\lib\masm32.lib
   includelib \masm32\lib\kernel32.lib
   includelib \masm32\lib\user32.lib
   includelib \masm32\lib\crtlib.lib
   includelib \masm32\lib\ws2_32.lib     ; the winsock 2 library

; -------------------------------------------------------------------------
; Data section
; -------------------------------------------------------------------------
.data

   prompt  db "Enter target:",0
   prompt1 db "Enter port:",0
   prompt2 db "Enter data to send:",0,

   sent db "Payload sent...",0

   send_failed db "Could not send payload...",13,10,0
   error_msg db "Winsock failed to initialize...",13,10,0
   winsock_not2 db "Failed to get atleast winsock 2.0 ...",13,10,0
   error_create_sock db "could not create socket...",13,10,0
   
   winsock_is2 db "Winsock 2 initialized...",13,10,0
   socket_created db "Udp Socket created...",13,10,0
     
   info db "updsend By Gavin Cotter",13,10,0
   Usage db "<Usage>Ip Address Port",13,10,0
   Example0 db "Example Ip: 211.123.345.678",13,10,0
   Example1 db "port: 27015",13,10,0

; -------------------------------------------------------------------------
; Other data
; -------------------------------------------------------------------------
.data?

    wsaData     WSADATA     <?>            ; Pointer to the WSADATA data structure
    hSocket     dd  ?                      ; Socket handle
    ipaddress db 30 dup(?)                 ; Ipaddress goes here
    sockAddr1   sockaddr_in     <?>        ; Coverted ipaddress goes in here for network byte ordering
    port dd ?                              ; Port number goes in here
    data_send db 100 dup (?)               ; Data to send goes in here
   
; -------------------------------------------------------------------------
; Code section
; -------------------------------------------------------------------------
.code
; -------------------------------------------------------------------------
;User input for ipaddress
; -------------------------------------------------------------------------
start:
    invoke StdOut,ADDR prompt                 ; Prompt user to enter ipaddress
    invoke StdIn,ADDR ipaddress,17            ; Receive ip address from user, put into buffer
   

; -------------------------------------------------------------------------
; Output instructions for user if user string not valid lenght
; -------------------------------------------------------------------------
;not ready for this part yet

;instructions:   
    ;invoke StdOut,ADDR info                   ; output author msg(Gavin Cotter)
    ;invoke StdOut,ADDR Usage                  ; Output usage msg for user(usage)
    ;invoke StdOut,ADDR Example0               ; Output example msg for user(ipaddress)
    ;invoke StdOut,ADDR Example1               ; Output example msg for user(port)
    ;invoke ExitProcess,0
     
; -------------------------------------------------------------------------
; User input for port
; -------------------------------------------------------------------------
   
ip_ok:
    invoke StdOut,ADDR prompt1                 ; Prompt user to enter port
    invoke StdIn,ADDR port,6                   ; Receive port from user, put into buffer
   
   
; -------------------------------------------------------------------------
; User input for data to send
; -------------------------------------------------------------------------

  invoke StdOut,ADDR prompt2                   ; Prompt user to enter data to send
  invoke StdIn,ADDR data_send,100              ; Receive data from user, put in buffer
   

; -------------------------------------------------------------------------
; Initialize the winsock library
; -------------------------------------------------------------------------   

    invoke  WSAStartup,0002h,ADDR wsaData       ; request winsock 2.x
    test    eax,eax                             ; was it succesfull ?               
    jnz     _error                              ; if not, goto error handling

; -------------------------------------------------------------------------
; Did we get winsock 2?
; -------------------------------------------------------------------------

    cmp byte ptr [wsaData.wVersion], 2           ; is our major (2.)requested version ==whats available
    jae version_ok                               ; jump to calling winsock functions if it's the same or above
    jmp _version_less2                           ; if it isn't then goto error handling and cleanup

; -------------------------------------------------------------------------
; Call winsock functions
; -------------------------------------------------------------------------

version_ok:
    invoke StdOut,ADDR winsock_is2                    ; output winsock2 success string
    invoke socket,AF_INET,SOCK_DGRAM,IPPROTO_UDP      ; create udp socket and use udp protocol
    cmp eax,INVALID_SOCKET                            ; check if an error occurred
    je _error_create                                  ; if error goto error handling and cleanup

; -------------------------------------------------------------------------
; Socket created
; -------------------------------------------------------------------------

    mov [hSocket],eax                                 ; put value in handle to socket returned from invoke socket above
    invoke StdOut,ADDR socket_created                 ; output socket created success string

; -------------------------------------------------------------------------
; Initialize sockaddr_in structure and seperate ip and port
; -------------------------------------------------------------------------

    mov [sockAddr1.sin_family],AF_INET                ; Set address family
    invoke inet_addr,ADDR ipaddress                   ; Conver ip address into network byte order
    mov [sockAddr1.sin_addr.S_un.S_addr], eax         ; specifies the long value in the address union
     
    mov eax,[port]                                    ; Copy contents at offset port to eax
    xchg al,ah                                        ; Convert port number to network byte order
       
    mov [sockAddr1.sin_port],ax                       ; and store it in sin_port
     
; -------------------------------------------------------------------------
;Send data to server
; -------------------------------------------------------------------------
   
    invoke sendto, [hSocket],ADDR data_send,          ; prepare for sending
    sizeof data_send,                                 ; pointer to data and check size
    0,ADDR sockAddr1,sizeof sockAddr1                 ; pointer to ipaddress structure
   
    cmp     eax, SOCKET_ERROR                         ; check is their an error
    jne     _sendSucceeded                            ; if no error returned from sendto goto label
   
    invoke StdOut,ADDR send_failed                    ; Output msg couldn't send data
    invoke ExitProcess,0                              ; Kill program

_sendSucceeded:                                       
    invoke StdOut,ADDR sent                           ; Output msg that data was succesfully sent
    invoke ExitProcess,0                              ; kill program

; -------------------------------------------------------------------------
; Error handling and cleanup
; -------------------------------------------------------------------------

_error:
    invoke StdOut,ADDR error_msg                      ; output failed string
    invoke ExitProcess,0                              ; and exit app

_version_less2:
    invoke StdOut,ADDR winsock_not2                   ; output winsock < 2 string
   
    invoke  WSACleanup                                ; cleans up the winsock library
    test    eax, eax                                  ; Doesn't really matter
    invoke ExitProcess,0                              ; and exit app

_error_create:
    invoke StdOut,ADDR error_create_sock              ; output error string
    invoke WSACleanup                                 ; cleans up winsock library
    invoke ExitProcess,0                              ; and exit app

END start                                             ;End of code



thomasantony

Hi,
  What the user gives is in text format. This is not scanf() or cin .!! You first store the input in a small buffer and then convert it to number using atodw function in the masm32.lib

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

Tedd

Baby steps:

1. get port number from stdin (as a string - a list of characters)
2. convert port number (which is still a string) into its binary value (use the "atodw" function for this)
3. the return value from this will be your port number, so give this to the "htons" function
4. store this value in the sin_port part
No snowflake in an avalanche feels responsible.

gavin

Ok guys that part is done thanks alot.
I understand now  :U