This is something different. It connects to an echo server, not a http server. It has some comments and names of structures that might be helpful.
;-----------------------------
;
; nasm -f elf32 echocli.asm
; ld -o echocli echocli.o
global _start
struc sockaddr_in
.sin_family resw 1
.sin_port resw 1
.sin_addr resd 1
.sin_zero resb 8
endstruc
_ip equ 0x7F000001 ; loopback - 127.0.0.1
;_ip equ 0x48400C6B ; loopback - 127.0.0.1
_port equ 2002
; Convert numbers to network byte order
IP equ ((_ip & 0xFF000000) >> 24) | ((_ip & 0x00FF0000) >> 8) | ((_ip & 0x0000FF00) << 8) | ((_ip & 0x000000FF) << 24)
PORT equ ((_port >> 8) & 0xFF) | ((_port & 0xFF) << 8)
AF_INET equ 2
SOCK_STREAM equ 1
BUFLEN equ 0x80
STDIN equ 0
STDOUT equ 1
LF equ 10
EINTR equ 4
__NR_exit equ 1
__NR_read equ 3
__NR_write equ 4
__NR_socketcall equ 102
SYS_SOCKET equ 1
SYS_CONNECT equ 3
section .data
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 IP
at sockaddr_in.sin_zero, dd 0, 0
iend
socket_args dd AF_INET, SOCK_STREAM, 0
; first of these wants to be socket descriptor
; we fill it in later...
connect_args dd 0, my_sa, sockaddr_in_size
section .bss
my_buf resb BUFLEN
sock_desc resd 1
section .text
_start:
; socket(AF_INET, SOCK_STREAM, 0)
mov ecx, socket_args ; address of args structure
mov ebx, SYS_SOCKET ; subfunction or "command"
mov eax, __NR_socketcall ;c.f. /usr/src/linux/net/socket.c
int 80h
cmp eax, -4096
ja exit
mov [sock_desc], eax
; and fill in connect_args
mov [connect_args], eax
; connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))
mov ecx, connect_args
mov ebx, SYS_CONNECT ; subfunction or "command"
mov eax, __NR_socketcall
int 80h
cmp eax, -4096
ja exit
again:
push BUFLEN
push my_buf
push STDIN
call readline
add esp, 12
push eax
push my_buf
push dword [sock_desc]
call writeNbytes
add esp, 12
cmp dword [my_buf], 'quit'
jz goodexit
push BUFLEN
push my_buf
push dword [sock_desc]
call readline
add esp, 12
push eax
push my_buf
push STDOUT
call writeNbytes
add esp, 12
jmp again
goodexit:
xor eax, eax ; success
exit:
mov ebx, eax ; exitcode
neg ebx
mov eax, __NR_exit
int 80h
readline:
push ebp
mov ebp, esp
sub esp, 4
pusha
%define fd ebp + 8
%define buf ebp + 12
%define max ebp + 16
%define result ebp - 4
mov dword [result], 0
mov ebx, [fd]
mov ecx, [buf]
mov edx, [max]
.reread:
mov eax, __NR_read
int 80h
cmp eax, -EINTR
jz .reread
cmp eax, -4096
ja exit
add [result], eax
cmp byte [eax + ecx - 1], LF
jz .done
add ecx, eax
sub edx, eax
jna .done
jmp .reread
.done:
popa
mov eax, [result]
%undef fd
%undef buf
%undef max
%undef result
mov esp, ebp
pop ebp
ret
writeNbytes:
push ebp
mov ebp, esp
sub esp, 4
pusha
%define fd ebp + 8
%define buf ebp + 12
%define Nbytes ebp + 16
%define bytesleft ebp - 4
mov ebx, [fd]
mov ecx, [buf]
mov edx, [Nbytes]
mov [bytesleft], edx
.rewrite:
mov eax, __NR_write
int 80h
cmp eax, -EINTR
jz .rewrite
cmp eax, -4096
ja exit
sub [bytesleft], eax
jz .done
add ecx, eax
sub edx, eax
jna .done
jmp .rewrite
.done:
popa
mov eax, [Nbytes]
%undef fd
%undef buf
%undef Nbytes
%undef bytesleft
mov esp, ebp
pop ebp
ret
I've got an echo server to go with it...
Best,
Frank