Thread: INT n instruction in C Program

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    INT n instruction in C Program

    Hello,

    I am trying to read the IP address of a machine through a software interrupt.
    Is it possible to call the Software Interrupt (INT instruction) from a C program to do a specific task (in this case find the IP) that I want? If so can someone throw me a pointer or a sample code?

    Thanks in Advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're not supposed to use interrupts on windows this way. What OS you actually working on? If you're using Windows, do it the Windows way.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, technically, I believe Windows and I know that Linux does use an interrupt for system calls [at least on older processors that do not have specific system call functions].

    However, there is no need for the user to know any of that, since interrupts are just a way to transfer from user-mode to kernel-mode when performing the system call itself, and not how you should be using the Windows or Linux API - in fact, I'd be surprised if it's even documented how the system calls are done.

    Further, finding the IP address is not a simple process of just calling a single functionality - you most likely would have to open the device, and then perform some IOCTL or similar function to get the IP address.

    Of course, in a DOS environment, there may be an interrupt function to perform this task (which may require more than one step).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    19
    Hey thanks for replying. I am using Ubuntu 8.04. And gcc 4.2.3
    I managed to write the socket() call in _asm, I am next trying to write the ioctl call in _asm. I wrote the following code for socket
    Code:
     
    *__asm__("sub  $12,%%esp\n" // increasing stack for Interrupt
            "movl   $2,(%%esp)\n" // AF_INET
            "movl   $1,4(%%esp)\n" // SOCK_STREAM
            "movl   $0,8(%%esp)\n"  // third parameter to socket()
            "movl   $102,%%eax\n" // INT number for socket_CALL
            "movl   $1,%%ebx\n"     // SYS_socketcall_socket
            "movl   %%esp,%%ecx\n"
            "int    $0x80\n"
            "add    $12,%%esp\n" // restoring stack 
            : "=a" (s)
           );
    This works perfectly. On trying to write similar code for ioctl, I am getting a return value of -9. Can someone help me out with this.
    Code:
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    int main()
    {
    int i, res;
    int s = 0;
    s = socket(AF_INET,SOCK_STREAM,0);
       struct ifreq ifr;
       struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
       char * ip;
       ifr.ifr_ifindex = 1;
       res = ioctl (s, SIOCGIFNAME, &ifr);
       printf("\n first res %d", res);
     res = ioctl(s, 35088, &ifr);
       printf("\n second  result %d", res);
    __asm__("sub    $12,%%esp\n"
            "movl   %%edx,(%%esp)\n"
            "movl   $35088,4(%%esp)\n"
            "movl   %%ecx,8(%%esp)\n"
            "movl   $54,%%eax\n" // IOCTL interrupt number
            "movl   %%esp,%%ebx\n" // Placing ESP for interrupt handler, is this a wrong register? 
            "int    $0x80\n"
            "add    $12,%%esp\n"
            :"=a" (res)
            :"d" (s), "c" (&ifr)
    );
     printf(" \n new result was %d ", res);
    return 0;
    }
    I am getting -9 as output. Am I passing the parameters wrong?
    Thanks in advance.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    19
    I resolved it, I was clobbering the parameters, the following code worked
    Code:
     
       i = 35088;
    __asm__("movl   $54,%%eax\n"
            "int    $0x80\n"
           :"=a" (res)
            :"b" (s), "c" (i), "d"(&ifr)
    );

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You can debug that stuff pretty easily with strace.
    I was just looking at it, here's the output of previos code:
    Code:
    ioctl(4, SIOCGIFNAME, {ifr_index=1, ifr_name="lo"}) = 0
    ioctl(4, SIOCGIFNAME, {ifr_index=1, ifr_name="lo"}) = 0
    ioctl(-1079534860, 0xbfa79b30, 0x4)     = -1 EBADF (Bad file descriptor)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM

Tags for this Thread