LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2003, 05:17 PM   #1
grizzly
Member
 
Registered: Jun 2003
Distribution: Slackware 9.1, Solaris 9, and IPcop
Posts: 101

Rep: Reputation: 15
Assembly Language, and networks


I am trying to learn how to send data through the network, and I am having some concept issues. I post this thread asking about where packets are formed, but that is only half the story.
http://www.linuxquestions.org/questi...hreadid=124500

From this post, I am under the thought that it would be best to make a system call to the O/S to handle the lower layers of the OSI. I would like to learn to do this without the a call to the O/S, but first things first. I know with video you can do an INT to the O/S, or do an INT to BIOS. I was wondering if there is the same type of thing with networking.

I am posting this question here because I can not find too much information on this subject when I do a search. I think mainly because I really don't have the concept down well enough to understand what exactly I am looking for. So if someone can either give me a brief summary on how to do it so I can explore more details on-line, or if you have any links that would be great. I know this subject I am asking about could probally take up a few chapters in a book.

To summerize my two questions.
1. How would you do a system call to send data through the network,
2. How would you set up the info. to be sent through the network. At some point the packet has to be completed. So if the system call to the O/S would handle from layer 3 down, how do I get the information into the form so it is ready for the third layer?

Again, I know this subject could cover chapters, but if you could give a brief summary, that would be very helpful.
 
Old 12-10-2003, 09:20 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
my friend have you heard of the sockets interface? if you want to just send data over a network, there is the socket() family of system calls. googling for "beej's sockets tutorial" will find you a great guide to learning sockets on *nix. if you want to create RAW packets, where you form your own tcp/ip/icmp headers, you need to use a Raw Socket. you seem kind of confused.. writing socket code in assembly is not that hard, it can be annoying but once you learn a few tricks it isnt bad. however, writing assembly code to create packets will definitly be a bit harder. heh you reminded me of this bit of code i wrote a while ago.. a really crappy ping program that builds and ICMP header and just sends out a few echo requests...
Code:
.include "asm_defines.h"
        
;// constants   
ADDR    = 0xB34D4D4D    ;// 77.77.77.178 in network byte order
NAP     = 360000000             ;// 1 hour in seconds = time to sleep
        
;// variables are all stored on the stack at the following offsets
SA      = -0x10         ;// struct sockaddr_in
SAFAM   = -0x10 
SAP     = -0xE  
SAIP    = -0xC  

SOCK    = -0x14         ;//socket handle
        
BUF     = -0x20         ;//send buffer
BUFSZ   = 0xC           ;//12 bytes     
        
ARGS    = -0x38         ;//function arg strings
ARG0    = -0x38 
ARG1    = -0x34 
ARG2    = -0x30
ARG3    = -0x2c 
ARG4    = -0x28
ARG5    = -0x24 
        
.text   
.globl _start   
        
_start: 
        pushl   %ebp            ;//save the old frame pointer
        movl    %esp, %ebp      ;//use ebp to reference variables
        
        ;// first we clear out our memory
        leal    SA(%ebp), %ebx  ;/load ebx with the address of starting memory to clear
        movl    $28, %ecx       ;//load ecx with number bytes to clear (buffer + sockaddr_in) = (12 + 16)
zmem:   
        movb    $0, (%ebx)      ;//zero the byte in %bl
        inc     %edx            ;//counter      
        inc     %ebx            ;//expose next byte
        cmp     %edx, %ecx      ;//loop control
        jne     zmem

        ;//set up address struct
        movw    $AF_INET, SAFAM(%ebp)
        movl    $ADDR, SAIP(%ebp)

        ;//create the socket
        ;//socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
socket: 
        movl    $SYS_socketcall, %eax
        movl    $SYS_socket, %ebx
        movl    $AF_INET, ARG0(%ebp)
        movl    $SOCK_RAW, ARG1(%ebp)
        movl    $IPPROTO_ICMP, ARG2(%ebp)
        leal    ARGS(%ebp), %ecx
        int     $0x80 
        movl    %eax, SOCK(%ebp)
        cmpl    $0x1, %eax
        jle     exit

icmp:   
        ;//setup the icmp header
        leal    BUF(%ebp), %eax         
        movb    $0x08, (%eax)           ;//type
        movb    $0x00, 0x1(%eax)        ;//code
        movw    $0xfff5, 0x2(%eax)      ;//checksum 2 bytes
        movw    $0x0001, 0x4(%eax)      ;//ID 2 bytes
        movw    $0x0001, 0x6(%eax)      ;//SEQ 2 bytes
        
        xorl    %esi, %esi      ;//esi is the counter for sending packets

send:           ;//we cant use 2 memory operands in one expression, hence the register swappdeedooing
        ;//sendto(sock, buf, 8, 0, (struct sockaddr *) &sa, SALEN)      %ecx already contains arg address
        movl    $SYS_socketcall, %eax   ;//always this for sock calls
        movl    $SYS_sendto, %ebx               ;// 11 is # for sendto
        movl    SOCK(%ebp), %edx        ;// the socket
        movl    %edx, ARG0(%ebp)        
        leal    BUF(%ebp), %edx         ;//the buffer
        movl    %edx, ARG1(%ebp)        
        movl    $0x8, ARG2(%ebp)        ;//length 
        movl    $0x0, ARG3(%ebp)        ;//no options
        leal    SA(%ebp), %edx          ;// sockaddr_in address
        movl    %edx, ARG4(%ebp)
        movl    $SALEN, ARG5(%ebp)      ;// sizeof struct
        int     $0x80
        
        ;//test return value
        cmpl    $0x1, %eax
        jle     exit
        
        incl    %esi            ;//our loop control sends 5 packets
        cmpl    $0x5, %esi
        jl      send

exit:   
        movl    $SYS_exit, %eax         ;//system exit
        movb    $0x1, %bl
        int     $0x80
eww i see all sorts of ugly things that could be different, but ehh that's just an idea of how something might look on linux.

Last edited by infamous41md; 12-10-2003 at 09:24 PM.
 
Old 12-10-2003, 10:56 PM   #3
grizzly
Member
 
Registered: Jun 2003
Distribution: Slackware 9.1, Solaris 9, and IPcop
Posts: 101

Original Poster
Rep: Reputation: 15
Thank You very much for that, infamous41md. That is definitely a great start for me. Like you said, I am somewhat confused on this. That is why I asked here, because I was not sure what I was looking for, so I was having trouble finding anything that would help. But the information you gave me helped out a lot. I hope you don't mind, I was going to copy the program you posted and dissect it a little. But between your code and the Tutorial, I should have things up and running in no time.
 
Old 12-11-2003, 12:43 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
if you're interested in raw sockets, have a look at my site www.1nfamus.netfirms.com, there is a lot of well commented source code buried in the bottom of the page.
 
Old 12-11-2003, 09:33 PM   #5
grizzly
Member
 
Registered: Jun 2003
Distribution: Slackware 9.1, Solaris 9, and IPcop
Posts: 101

Original Poster
Rep: Reputation: 15
infamous41md, I went to the site. It sounds like you have the same interest as I do, however you are much farther down the path that I am. I have a project I was going to work on in the near future. Do you mind if I hang on to your e-mail and ask you a few questions from time to time.
 
Old 12-11-2003, 09:50 PM   #6
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
sure more than happy 2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Assembly langugae to Executable in c language ssg14j Programming 15 11-04-2008 07:49 AM
Assembly language: Purpose of shift register? ksgill Programming 4 03-21-2005 08:24 PM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 04:40 PM
Assembly Language on 64 Bit systems Sleevy Programming 1 09-06-2004 01:58 AM
SPARC assembly language jclark00001 Programming 3 02-26-2003 08:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration