flat assembler
Message board for the users of flat assembler.

Index > Windows > [Solved] Socket bind Port

Author
Thread Post new topic Reply to topic
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 24 Jan 2011, 20:50
Hello, i would like to know which port my socket bind.
I don't wan't to set a constant value.

Code:
...
start:
mov ecx, 0
invoke WSAStartup, 0202h, wsadata
invoke socket, AF_INET, SOCK_STREAM, 0
cmp eax, -1
je _exit 
mov [msock], eax
mov [saddr.sin_family], AF_INET
invoke bind, [msock], saddr, sizeof.sockaddr_in
;How to start a proc with Port in argument or store port in memory which could be read by the proc.
...
    

By the way, how to send this port to a proc.
Code:
proc Msgport
invoke MessageBox,0,sPort,sTitle,MB_OK
ret
endp    


Thanks. Wink


Last edited by Ianis on 26 Jan 2011, 19:19; edited 1 time in total
Post 24 Jan 2011, 20:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 24 Jan 2011, 20:55
You need to set the structure
Code:
    invoke  htons,IPPORT_HTTP ;your port goes here
      mov     [STATE.sock_addr.sin_port],ax    
BTW: Are you writing server code? For server you need to "listen" before you can bind.
Post 24 Jan 2011, 20:55
View user's profile Send private message Visit poster's website Reply with quote
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 24 Jan 2011, 21:01
I'm writning server code and i listen after bind. ( that work )
Your code is to specify a port, i don't need this.
The computer automaticly set a free port to the socket bind.
I want to be able to use it in my app.
Post 24 Jan 2011, 21:01
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 24 Jan 2011, 21:04
Ianis wrote:
Your code is to specify a port, i don't need this.
The computer automaticly set a free port to the socket bind.

That's not how a server usually works.
Post 24 Jan 2011, 21:04
View user's profile Send private message Reply with quote
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 24 Jan 2011, 21:14
ManOfSteel wrote:
Ianis wrote:
Your code is to specify a port, i don't need this.
The computer automaticly set a free port to the socket bind.

That's not how a server usually works.


Yes but for my project i don't want a constant port for my server.
That's why i must let it bind on random ports.
otherwise i could be interested in a random number generator.
Generate a random number between min and max.
To bind my port on it.
Post 24 Jan 2011, 21:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 24 Jan 2011, 21:18
I have never seen a way to set a wildcard port. I think that with the standard Windows socks API you will need to set a listener for every port you want to capture.
Post 24 Jan 2011, 21:18
View user's profile Send private message Visit poster's website Reply with quote
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 24 Jan 2011, 22:12
Then, do you know a way to get binded port from msock / saddr ?
Or a way to generate random numbers and set them as port to bind ?
Thanks.
Post 24 Jan 2011, 22:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 24 Jan 2011, 22:17
You have to choose a port first and then listen on that port. The Windows socks API enforces that behaviour. You can listen on multiple ports at the same time, but each port will need a separate listener running.


Last edited by revolution on 25 Jan 2011, 11:04; edited 1 time in total
Post 24 Jan 2011, 22:17
View user's profile Send private message Visit poster's website Reply with quote
SFeLi



Joined: 03 Nov 2004
Posts: 138
SFeLi 25 Jan 2011, 07:43
Ianis, just bind to port 0 and Winsock would choose a port number automatically. You could then retrieve it by calling getsockname.
Post 25 Jan 2011, 07:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 25 Jan 2011, 07:58
Oh, I misunderstood what you wanted to do. I thought you wanted to connect to all incoming port requests. But actually you need a single port and don't want to dedicate the assignment.

In that case SFeLi is correct. But you will only get back a limited port range of something like 1024-5000 (IIRC). This may or may not be a problem but is something to be aware of.
Post 25 Jan 2011, 07:58
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1404
Location: Piraeus, Greece
Picnic 25 Jan 2011, 07:59
Hi Ianis, SFeLi, revolution,

That might not necessarily work according to msdn

If the Internet address is equal to INADDR_ANY or in6addr_any, getsockname cannot necessarily supply the address until the socket is connected
Post 25 Jan 2011, 07:59
View user's profile Send private message Visit poster's website Reply with quote
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 25 Jan 2011, 22:14
SFeLi wrote:
Ianis, just bind to port 0 and Winsock would choose a port number automatically. You could then retrieve it by calling getsockname.


Code:
...
start:
mov ecx, 0
invoke WSAStartup, 0202h, wsadata
invoke socket, AF_INET, SOCK_STREAM, 0
cmp eax, -1
je _exit 
mov [msock], eax
mov [saddr.sin_family], AF_INET
invoke bind, [msock], saddr, sizeof.sockaddr_in
test eax, eax
jnz _exit
invoke getsockname, [msock], saddr, sizeof.sockaddr_in
mov [saddrs.sin_port], ax
push ax
invoke MessageBox,0,ax,sTitle,MB_OK 
...
    

Thank you SFeLi
I would like to do something like that but it don't work.. Sad
My code seem wrong.
Post 25 Jan 2011, 22:14
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1404
Location: Piraeus, Greece
Picnic 26 Jan 2011, 07:19
MessageBox requires a long pointer to a null-terminated string. You can't pass a 16-bit register to a Windows API function as parameter. Use wsprintf to convert register value to string.
Post 26 Jan 2011, 07:19
View user's profile Send private message Visit poster's website Reply with quote
SFeLi



Joined: 03 Nov 2004
Posts: 138
SFeLi 26 Jan 2011, 12:53
Additionally remember that sin_port is big-endian and x86 works with little-endian so you need either to do xchg ax,al or call ntohs before passing ax to wsprintf.
Edit: your code contains a lot of errors. Here's is the working version:
Code:
        format  pe gui 4.1
        entry   start
        include 'win32a.inc'

start:
        invoke  WSAStartup,0202h,wsadata
        invoke  socket, AF_INET, SOCK_STREAM, 0
        cmp     eax, -1
        je      _exit
        mov     [msock], eax
        mov     [saddrs.sin_family], AF_INET
        invoke  bind, [msock], saddrs, sizeof.sockaddr_in
        test    eax, eax
        jnz     _exit
        mov     [salen],sizeof.sockaddr_in
        invoke  getsockname, [msock], saddrs, salen
        movzx   eax,[saddrs.sin_port]
        xchg    al,ah
        cinvoke wsprintf,portbuff,portfmt,eax
        invoke  MessageBox,0,portbuff,sTitle,MB_OK
    _exit:
        invoke  ExitProcess,0


        data    import
        library kernel32,'KERNEL32.DLL',\
                user32,'USER32.DLL',\
                wsock32,'WS2_32.DLL'
        include 'api/kernel32.inc'
        include 'api/user32.inc'
        include 'api/wsock32.inc'
        end     data

sTitle          db      'bind to 0',0
portfmt         db      'Port number is %d',0
wsadata         WSADATA
msock           dd      ?
salen           dd      ?
saddrs          sockaddr_in
portbuff        rb 32
    
Post 26 Jan 2011, 12:53
View user's profile Send private message Reply with quote
Ianis



Joined: 06 Jan 2011
Posts: 10
Ianis 26 Jan 2011, 19:18
SFeLi wrote:
Additionally remember that sin_port is big-endian and x86 works with little-endian so you need either to do xchg ax,al or call ntohs before passing ax to wsprintf.
Edit: your code contains a lot of errors. Here's is the working version:


Thanks a lot SFeLi with fews modifications your code solved my problem.
Very Happy
Post 26 Jan 2011, 19:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.