Here is what I have so far.. there are lots of gaps so at this point want to get these filled in. Goal is to get this entire thing complete and running by midnight tonight
bits 32
global _start
global resolv
; creating the initial socket syscall to initiate a socket:
AF_INET equ 2 ; sin_family
SOCK_STREAM equ 1 ; socket type (TCP)
PROTOCOL equ 0 ; just because
SYS_SOCKET equ 1
SYS_CONNECT equ 3
buflen equ 1000
section .data
socket_args dd AF_INET, SOCK_STREAM, 0
section .text
_start:
push PROTOCOL
push SOCK_STREAM
push AF_INET
mov ecx, esp ; places socketcall args into ecx for syscall
mov ebx, SYS_SOCKET ; socket function to invoke (1= socket command)
mov eax, 102 ; socketcall syscall
int 0x80
add esp, 12 ; clean up stack
cmp eax, 0 ; test for error (eax= socket file descriptor)
jl exit ; jump if negative (error)
mov [sock_fd], eax ; place the newly created socket file descriptor into sock_fd
mov [connect_args], eax ; ???
; now make a connection with the created socket
push addr_len
push ip_addr
push sock_fd
mov ecx, esp ; places connect args into ecx for syscall
mov ebx, SYS_CONNECT ; socket function to invoke (3= socket connect)
mov eax, 102 ; socketcall syscall
int 0x80
add esp, 12 ; clean up stack
cmp eax, 0 ; check for errors
jl exit
; now start sending stuff
; now read into the buffer
readwrite:
mov edx, buflen ; edx= sys read size_t buflen= max. 1000 bytes
mov ecx, buffer ; each character (char *) to be read
mov ebx, [connect_fd] ; int: file descriptor to be read from the socket connection
mov eax, 3 ; read sys call
int 0x80
cmp eax, 0 ; returns the number of bytes read
jl exit ; exit on error (if return value -1 or less)
; now write the length of what was read
mov edx, eax ; the size_t count of bytes read moved into edx
mov ecx, buffer ; the characters to write are stored in "buffer"
mov ebx, [connect_fd] ; file descriptor to write to
mov eax, 4 ; write sys call
int 0x80
cmp eax, 0 ; return value= bytes written, if less than 0, error occurred
jl exit
; need something here to determine EOF??
jmp readwrite ; continue reading/ writing until EOF
xor eax, eax ; clear eax if no errors occurred
exit:
mov ebx, eax ; put exit code into ebx
mov eax, 1 ; exit sys call
int 0x80
section .bss
sock_fd resd 1 ; socket fd = 32- bits
connect_fd resd 1
buffer resb buflen ; reserve 1000 bytes for 'buffer'
A few questions on your previous server/bind code:
You use:
my_sa istruc sockaddr_in
at sockaddr_in.sin_family, dw AF_INET
at sockaddr_in.sin_port, dw PORT
at sockaddr_in.sin_addr, dd INADDR_ANY
at sockaddr_in.sin_zero, dd 0, 0
iend
My professor provided us with sample code:
struc sockaddr_in
.sin_family: resw 1
.sin_port: resw 1
.sin_addr: resd 1
.sin_pad: resb 8
endstruc
are these the same/ interchangeable? If not what is going on and what is the difference?
What is this for and how does it work:
BACKLOG equ 128 ;for listen
For code below:
mov [fd_socket], eax
; and fill in bind_args, etc.
mov [bind_args], eax
mov [listen_args], eax
mov [accept_args], eax
are fd_socket, bind_args, listen_args, and accept_args all going to have the same value (whatever is in eax) or does the value in eax change each time something is moved?
You declared in section .data:
bind_args dd 0, my_sa, sockaddr_in_size
and then there is a socket call:
mov ecx, bind_args
mov ebx, SYS_BIND ; subfunction or "command"
mov eax, __NR_socketcall
int 80h
What is the value of sockaddr_in_size and what are values of my_sa:
my_sa istruc sockaddr_in
at sockaddr_in.sin_family, dw AF_INET
at sockaddr_in.sin_port, dw PORT
at sockaddr_in.sin_addr, dd INADDR_ANY
at sockaddr_in.sin_zero, dd 0, 0
iend
I'm not making a "connection" as to how those values get filled up or how those are being stored from the previous socket call.
I notice you do this a lot:
cmp eax, -4096
ja exit
Is there any significance to -4096 or does this only pertain to the server side of things?
Are "listen" and "accept" required on the client (my) side?
Can you help explain what's going on here:
section .bss
my_buf resb BUFLEN
and
BUFLEN equ 1000
Why is BUFLEN used sometimes instead of my_buf?
For example:
; read(sock, buf, len)
mov edx, BUFLEN ; arg 3: max count
mov ecx, my_buf ; arg 2: buffer
mov ebx, [fd_conn] ; arg 1: fd
mov eax, __NR_read ; sys_read
int 80h
All the pieces coming together...