flat assembler
Message board for the users of flat assembler.

Index > Linux > Problems with sockets (client/server type app)

Author
Thread Post new topic Reply to topic
aseptik



Joined: 25 Sep 2005
Posts: 4
Location: Croatia
aseptik 29 Jan 2006, 01:39
Well I thought I wrote everything right, but they seem not to be communicating... and I cannot find the error.

SERVER
Code:
format ELF

include 'sockets.inc'
include 'cdecl.inc'

section '.text' executable writeable

        public main
        extrn printf
        extrn socket
        ;extrn bzero
        ;extrn htons
        extrn bind
        extrn accept
        extrn send
        extrn close
        extrn listen
        
 main:
        ccall   printf, header
        ;create socket
        ccall   socket, PF_INET, SOCK_STREAM, 0
        cmp     eax, 0
        jnb     s1
        ccall   printf, err1
        jmp     fin
        s1:
        mov     [sockfd], eax
        ;init value in dest
        mov     [dest.sin_family], PF_INET
        ;ccall  htons, 8889
        mov     [dest.sin_port], 0xB922
        mov     [dest.sin_addr], 0
        
        ccall   bind, [sockfd], dest, dest_size
        cmp     eax, 0
        jnb     s2
        ccall   printf, err2
        jmp     fin
        s2:
        ccall   listen, [sockfd], 20
        cmp     eax, 0
        jnb     infinity
        ccall   printf, err3
        jmp     fin
        
        infinity:
                mov     [addrlen], dest_size
                ccall   accept, [sockfd], client_addr, addrlen
                cmp     eax, 0
                jnb     s3
                ccall   printf, err4
                jmp     fin
                s3:
                mov     [clientfd], eax
                ccall   send, [clientfd], buffer, buff_size, 0
                cmp     eax, 0
                jnb     s4
                ccall   printf, err5
                jmp     fin
                s4:
                ccall   close, [clientfd]
        jmp     infinity
        
        fin:
        ;close connection
        ccall   close, [sockfd]
        ccall   printf, footer
        xor     eax, eax
        ret

section '.data' writeable

        header  db 'Server started...',10,0
        footer  db 'Server closed',10,0

        err1    db 'socket() failed',10,0
        err2    db 'bind() failed',10,0
        err3    db 'listen() failed',10,0
        err4    db 'accept() failed',10,0
        err5    db 'send() failed',10, 0
        
        sockfd  dd ?
        dest    sockaddr_in
        dest_size = $-dest
        clientfd dd ?
        client_addr sockaddr_in
        addrlen dd ?
        buffer  db 'HELLO!',10,0
        buff_size = $-buffer    

CLIENT
Code:
format ELF

include 'sockets.inc'
include 'cdecl.inc'

section '.text' executable writeable

        public main
        extrn printf
        extrn socket
        ;extrn bzero
        ;extrn htons
        ;extrn inet_aton
        extrn connect
        extrn recv
        extrn close
        
 main:
        ccall   printf, header
        ;create socket
        ccall   socket, PF_INET, SOCK_STREAM, 0
        cmp     eax, 0
        jnb     s1
        ccall   printf, err1
        jmp     fin
        s1:
        mov      [sockfd], eax
        ;init value in dest
        mov     [dest.sin_family], PF_INET
        ;ccall  htons, 8889
        mov     [dest.sin_port], 0xB922
        ;lea    eax, [dest.sin_addr]
        ;ccall  inet_aton, ip, eax
        mov     [dest.sin_addr], 0x7F000001
        ;connect to server
        ccall   connect, [sockfd], dest, dest_size
        cmp     eax, 0
        jnb     s2
        ccall   printf, err2
        jmp     fin
        s2:
        ;recieve msg from the server and print to screen
        ccall   recv, [sockfd], 128, 0
        cmp     eax, 0
        ja      s3
        ccall   printf, err3
        jmp     fin
        s3:
        ccall   printf, msg, buffer
        ;close connection
        ccall   close, [sockfd]
fin:
        ccall   printf, footer
        xor     eax, eax
        ret

section '.data' writeable
        ;ip     db '127.0.0.1',0
        
        header  db 'Client started...',10,0
        footer  db 'Client closed',10,0
        
        err1    db 'socket() failed',10,0
        err2    db 'connect() failed',10,0
        err3    db 'recv() failed',10,0
        msg     db '%s',10,0

        sockfd  dd ?
        dest    sockaddr_in
        dest_size = $-dest
        buffer  db 128 dup(?)    

sockets.inc
Code:
struc sockaddr_in
{
  .sin_family dw ?
  .sin_port   dw ?
  .sin_addr   dd ?
  .sin_zero   db 8 dup (?)
}

; Socket types

SOCK_STREAM    = 1
SOCK_DGRAM     = 2
SOCK_RAW       = 3
SOCK_RDM       = 4
SOCK_SEQPACKET = 5

; Address formats

AF_UNSPEC    = 0
AF_UNIX      = 1
AF_INET      = 2
AF_IMPLINK   = 3
AF_PUP       = 4
AF_CHAOS     = 5
AF_NS        = 6
AF_IPX       = 6
AF_ISO       = 7
AF_OSI       = AF_ISO
AF_ECMA      = 8
AF_DATAKIT   = 9
AF_CCITT     = 10
AF_SNA       = 11
AF_DECnet    = 12
AF_DLI       = 13
AF_LAT       = 14
AF_HYLINK    = 15
AF_APPLETALK = 16
AF_NETBIOS   = 17

; Protocol formats

PF_UNSPEC    = 0
PF_UNIX      = 1
PF_INET      = 2
PF_IMPLINK   = 3
PF_PUP       = 4
PF_CHAOS     = 5
PF_NS        = 6
PF_IPX       = 6
PF_ISO       = 7
PF_OSI       = PF_ISO
PF_ECMA      = 8
PF_DATAKIT   = 9
PF_CCITT     = 10
PF_SNA       = 11
PF_DECnet    = 12
PF_DLI       = 13
PF_LAT       = 14
PF_HYLINK    = 15
PF_APPLETALK = 16
PF_NETBIOS   = 17

; IP Ports

IPPORT_ECHO        = 7
IPPORT_DISCARD     = 9
IPPORT_SYSTAT      = 11
IPPORT_DAYTIME     = 13
IPPORT_NETSTAT     = 15
IPPORT_FTP         = 21
IPPORT_TELNET      = 23
IPPORT_SMTP        = 25
IPPORT_TIMESERVER  = 37
IPPORT_NAMESERVER  = 42
IPPORT_WHOIS       = 43
IPPORT_MTP         = 57
IPPORT_TFTP        = 69
IPPORT_RJE         = 77
IPPORT_FINGER      = 79
IPPORT_TTYLINK     = 87
IPPORT_SUPDUP      = 95
IPPORT_EXECSERVER  = 512
IPPORT_LOGINSERVER = 513
IPPORT_CMDSERVER   = 514
IPPORT_EFSSERVER   = 520
IPPORT_BIFFUDP     = 512
IPPORT_WHOSERVER   = 513
IPPORT_ROUTESERVER = 520
IPPORT_RESERVED    = 1024

; Notifications

FD_READ    = 01h
FD_WRITE   = 02h
FD_OOB     = 04h
FD_ACCEPT  = 08h
FD_CONNECT = 10h
FD_CLOSE   = 20h    
If someone can figure out why this won't work I would really appreciate it!
Regards.
Post 29 Jan 2006, 01:39
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 29 Jan 2006, 22:15
Took me longer to find out where to get cdecl.inc from than to find the error. Smile
You forgot to take into account that intel is small-endian which means data is stored backwards. Basically.. replace 0x7F000001 with 0x0100007F.

I found this out using strace. Great little program which shows all kernel calls a program makes, such as read, open, accept, listen, etc.. and its parameters. So using that i could easily see that the address the client was trying to connect to was 1.0.0.127 rather than 127.0.0.1.
You should use it for debugging programs in linux. If i ever have any problems with a program i made, i usually strace it, if that doesnt show the problem (such as a SIGSERV), then i use ald (assembly language debugger). If that doesnt work, i run around the room screaming Very Happy.


gunblade
Post 29 Jan 2006, 22:15
View user's profile Send private message Reply with quote
aseptik



Joined: 25 Sep 2005
Posts: 4
Location: Croatia
aseptik 30 Jan 2006, 14:20
Quote:
0x0100007F

Yes that's correct but I've just wrote this as optimization to not call inet_aton - i've corrected this just after posting here but still doesn't work... Tried tracing it with ald, also made a c version which works but this asm version just won't give any results!
Did it work for you after correcting the ip addr?
Post 30 Jan 2006, 14:20
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 31 Jan 2006, 11:32
Can it not be that your assembly implementation of sockaddr_in is wrong?
I mean here that every member of a C-structure is usually aligned to dword boundary.
Post 31 Jan 2006, 11:32
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 01 Feb 2006, 12:30
Worked fine for me after i fixed the address.
Server listens on 0.0.0.0, client connects to it through 127.0.0.1, displays the message, then quits.
I'm almost certain i didnt make any other modifications, i'll test it again and edit the post to let you know.

edit: That's funny, theres another error there, dunno how it worked before, not on my home computer so i cant test it in the same conditions. But i've tested it on a 2.4 kernel and a 2.6, and the error was in your recv call. You're missing the buffer parameter.
You have:
Code:
ccall   recv, [sockfd], 128, 0    

Should be:
Code:
ccall   recv, [sockfd], buffer, 128, 0    


So just that problem, and the IP address being the wrong way round, and it should work.

Good luck,
gunblade
Post 01 Feb 2006, 12:30
View user's profile Send private message Reply with quote
aseptik



Joined: 25 Sep 2005
Posts: 4
Location: Croatia
aseptik 01 Feb 2006, 15:53
Hehe, funny error. (there's no i'm with stupid emoticon so i'll just leave it like this)
I've just started to examine that code from start and saw that thingiee lying there missing one parameter, fixed it and it worked. Then I saw you came to same conclusion.
It was quite late when I coded that so I'll blame the no-sleep ghost for this. Wink Thanks for your time and help, I really apreciate it!
Regards.
Post 01 Feb 2006, 15:53
View user's profile Send private message Reply with quote
noob



Joined: 04 Sep 2006
Posts: 8
noob 06 Sep 2006, 11:13
excuse my ignorance, where do I find cdecl.inc ?

thanks
Post 06 Sep 2006, 11:13
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 06 Sep 2006, 11:19
i believe you can include PROC32.inc from standard win32 FASM package...
Post 06 Sep 2006, 11:19
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
noob



Joined: 04 Sep 2006
Posts: 8
noob 06 Sep 2006, 11:42
win32 even though this is a linux machine ?
Post 06 Sep 2006, 11:42
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 06 Sep 2006, 14:37
it is only macros package. in fact, you only need "ccall" macro from it
Post 06 Sep 2006, 14:37
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost 06 Sep 2006, 19:31
vid wrote:
it is only macros package. in fact, you only need "ccall" macro from it


Code:
add esp, #    

_________________
redghost.ca
Post 06 Sep 2006, 19:31
View user's profile Send private message AIM Address MSN Messenger Reply with quote
noob



Joined: 04 Sep 2006
Posts: 8
noob 06 Sep 2006, 20:15
I'm still confused with this, is this socket example supposed to be for windows or linux ? I'm looking for a client socket example for linux, I dont have cdecl.inc, and not sure how this example has worked on linux. Vid, I dont follow your post on macros, I think I need to find myself some more reading material!
Post 06 Sep 2006, 20:15
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 06 Sep 2006, 22:06
noob wrote:
I'm still confused with this, is this socket example supposed to be for windows or linux ? I'm looking for a client socket example for linux, I dont have cdecl.inc, and not sure how this example has worked on linux. Vid, I dont follow your post on macros, I think I need to find myself some more reading material!


cdecl.inc is for ccall macro.

Code:
ccall   send, [clientfd], buffer, buff_size, 0    

without macro:
Code:
push 0
push buff_size
push buffer
push [clientfd]
call send
add esp, 4*4 ;to balance stack    

the 4*4 is how many bytes you pushed to stack for send, 4 params, 4bytes each.

and these examples are for Linux.

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 06 Sep 2006, 22:06
View user's profile Send private message MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 07 Sep 2006, 07:09
CDECL.INC:
Code:
macro ccall proc,[arg]                  ; directly call CDECL procedure
 { common
    size@ccall = 0
    if ~ arg eq
   reverse
    pushd arg
    size@ccall = size@ccall+4
   common
    end if
    call proc
    if size@ccall
    add esp,size@ccall
    end if }    
Post 07 Sep 2006, 07:09
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sylwek32



Joined: 27 Apr 2006
Posts: 339
sylwek32 07 Sep 2006, 20:18
how to use .O on linux ?

i type chmod 777 srva.O
and it says ok
then ./srva.O
proxy:/var/www# ./srva.O
-bash: ./srva.O: cannot execute binary file
proxy:/var/www#
Post 07 Sep 2006, 20:18
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 07 Sep 2006, 21:20
.o are the linux equivalent of MSCOFF .obj files.
You got 2 choices, either use gcc/ld to link it into an executable.

Code:
ld filename.o -o filename    

should link it fine, if it gives you errors about undefined symbols and such, use:
Code:
gcc filename.o -o filename    

gcc uses ld to link the object into an executable too, but it includes the default libraries by default, meaning you dont have to.
Post 07 Sep 2006, 21:20
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.