DGivney / assemblytutorials

This project was put together to teach myself NASM x86 assembly language on linux.
https://asmtutor.com/
654 stars 117 forks source link

Lesson 30 - Argument size explanation #44

Closed 8dcc closed 1 year ago

8dcc commented 1 year ago

In lesson 30, after pushing all the arguments for binding the socket, you push 16 to the stack as the "size of the arguments", but afaik the arguments we pushed were:

So why 16 bytes and not 8? I found this page which talks about sockets, and in the "Bind the socket" section, you can see he also pushes 2 empty dwords (2 * 4 bytes), adding up to the 16 we are using.

Can you explain if the remaining 8 bytes are those paddings he is adding? If so, what are they for, and why not add them to the asmtutor code?

Thank you anyway.

DGivney commented 1 year ago

Hi @r4v10l1

These are good questions and I'll have to think how to better document this.

The 16 bytes size is determined by the Socket Protocol (TCP in this example) and this resource has some good info about the struct sockaddr_in which is used for TCP sockets: https://www.gta.ufrj.br/ensino/eel878/sockets/sockaddr_inman.html

As you can see there's 8 bytes of buffer with the comment "// zero this if you want to". The reason you don't need to zero it, is because under the hood, bind calls a function called copy_from_user which takes this length (16 bytes) and zero-pads it before copying over our struct. So we are left with the same signature as sockaddr_in after this function is called.

I think the above tutorial is padding their call in order to future proof the code for use as an IP6 address. TCP requires additional arguments to be passed in these bytes to support IP6 and some people like to be explicit and not rely on internal workings that may change in the future.

8dcc commented 1 year ago

I see, thanks for taking the time to answer.