0

I think I've set up the socket correctly, but I'm having a lot of trouble trying to bind it. I don't really understand what I'm supposed to do. Like do I have to define the socket function somehow and use it as an argument in the bind function? I think I may have made several errors, like passing arguments into the socket function, so I could use some help.

Heres my code:

;this is an attempt at socket programming with x64 ;i will attempt to open a socket, and then immediately close it.

global _start


section .data
    domain: db "AF_INET"
    sockType: db "SOCK_STREAM"
    clientMessage: db "Connection succesfull", 10
    serverMessage: db "Client has connected", 10




section .bss
    message: RESB 12


section .text
_start:

    mov rax, 41
    mov rdi, domain
    mov rsi, sockType
    mov rdx, 0
    syscall

    mov rax, 60
    mov rdi, 1
    syscall

note: I'm not yet using the last two constants in .data, they are simply for letting the user know the connection status.

note #2: It assembles without error

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    You are passing pointers to strings to the syscall? Those `AF_INET` and `SOCK_STREAM` are *numeric constants* defined in system header files as `int`, not strings. You certainly don't write `socket("AF_INET", "SOCK_STREAM", "IPPROTO_TCP")` when creating a socket in C! Take a look at `man 2 socket` and `man 2 bind`. `AF_INET` should be `2` and `SOCK_STREAM` should be `1` IIRC. – Marco Bonelli Feb 01 '22 at 20:39
  • Also, `socket` returns the fd in `rax` (or a negative number in case of error), you can't just assume that the opened fd will be `1` (as you are doing when you call the `close` syscall, which by the way is `3` not `60`). – Marco Bonelli Feb 01 '22 at 20:41
  • alright thank you. i use 60 because its the exit syscall, it works just fine, when i replace it with 3 it errors out. (https://filippo.io/linux-syscall-table/) – Mr_Arsonist Feb 01 '22 at 22:52
  • also i dont understand, you do do that when making a socket in C, for example: `int sockfd = socket(domain, type, protocol)` also also as far as i understand you can define much more than integers using db. im not sure i understand your explanation – Mr_Arsonist Feb 01 '22 at 22:54
  • In C, do you know the difference between `int domain = "AF_INET"` and `int domain = AF_INET`? In the second case, the CPP macro is replaced with a number before compilation, so it wouldn't be a type-mismatch error. Notice in the man page (https://man7.org/linux/man-pages/man2/socket.2.html) that it takes `int` args, not `const char*`. – Peter Cordes Feb 02 '22 at 02:30
  • oh thank you. but my original question wasnt really answered and for some reason the question was closed (i had already seen the one it replaced my question with but that didnt answer what i wanted), im still having trouble binding the socket – Mr_Arsonist Feb 02 '22 at 04:24
  • also i tried defining it as an int but it errored out so idk how to do it. could you help with that as well – Mr_Arsonist Feb 02 '22 at 04:28

0 Answers0