Thread: Questions about ioctl() or exec() family

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    Questions about ioctl() or exec() family

    Hi all,

    I'm writing a small program to allow my Linux illiterate wife to obtain system/network diagnostics for our 'net connection on Linux (Redhat 7). I am wanting to provide such things as IP address (ifconfig eth1 basically).

    I am writing this in C as an exercise for myself to learn C a little more in depth than the books I have (they only seem to go into Console and File IO and not networking detail).

    I understand that running a system("blah") command is not the right way to do it, and execve doesn't seem to produce the desired results (as it exits after execution instead of returning to my program) so I think I need to grab the IP from the system, by using ioctl() but I've not got any idea how to do this, and don't understand the man page as it seems rather vague.

    If you can provide any help towards this I would be very grateful,

    Thank you,

    Phil.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This ain't fully tried and tested, but it's a start.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    
    int main(void)
    {
    	/* Get the local hostname */
    	char sHostName[80];
    	int nRet;
    	
    	nRet = gethostname(sHostName, sizeof(sHostName)); 
    
    	if (nRet == 0) 
    	{
    		struct hostent* pHostEnt;
    
    		/* Resolve hostname for local address */
    		if ((pHostEnt = gethostbyname(sHostName)) == NULL)
    		{
    			perror("gethostbyname");
    			return(1);
    		}
    
    		printf("%s:%s",  pHostEnt->h_name, inet_ntoa(*((struct in_addr *)pHostEnt->h_addr)));
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    are you using~

    Are you using GCC?

    I seem to be getting a couple of errors that I can't figure out.
    Ünicode¬>world = 10.0£

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    gcc under cygwin.....(under win98). Like I said, chucked it together a bit too quick! But it does compile and work on mine. Let me know what you have to change to get it going and I'll make sure I don't do it again
    Last edited by Hammer; 04-28-2002 at 02:25 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    Thanks very much, from that I'll be able to decipher networking C a little more.

    The code worked, and compiled for me using cc version 2.96 on Redhat Linux 7.0 kernel 2.4.18

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by PhilB
    Thanks very much, from that I'll be able to decipher networking C a little more.
    Try this if you want a guide: http://www.ecst.csuchico.edu/~beej/guide/net/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    Awesome, I will do - I'm awaiting a work permit (I come from the UK), so I'm a little strapped for cash, otherwise I'd buy some books :P

    Thanks for your help,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to force a function to never return...like exec() does it
    By creeping death in forum C Programming
    Replies: 7
    Last Post: 03-28-2009, 01:08 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM