getting the network interface information and default gateway information ??

getting the network interface information and default gateway information ??

Post by rohi » Mon, 29 Jul 2002 00:19:36



hi,
  iam trying to get the network interface information and the default
information.After doing some googling i found out the relevant
structures and the ioctl calls for getting the interfaces information(
i suppose i have all that right).
struct ifconf(and using the ioctl SIOCGIFCONF).
there is another structure
struct ifreq
but i dont think i need that one.

Now how do i do to get the interface address(aliases also).i have
written a small program, but dont know where iam going wrong.the code
is

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <netinet/in.h>

int
main(int argc, char* argv[])
{
    struct ifconf ifc;
    struct ifreq *ifr;
    struct sockaddr_in *sin;
    char buf[128];

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    ifc.ifc_buf = buf;
    ifc.ifc_len = sizeof(buf);
    if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
        perror("ioctl SIOCGIFCONF");
        return 1;
    }

    ifc.ifc_len /= sizeof(struct ifreq);
    for (i=0; i<ifc.ifc_len; i++) {
        ifr = (struct ifreq *) &ifc.ifc_req[i];
        sin = (struct sockaddr_in *) &ifr->ifr_addr;
        printf("%s \n",sin->sin_addr);
        }
    }
    return 0;

Quote:}

And how can i get the default gateway address within my program.

any help will be highly appreciated.

thanks
rohit

 
 
 

getting the network interface information and default gateway information ??

Post by jonat.. » Mon, 29 Jul 2002 09:18:33


Which O/S are you on?
If you are on Linux, check out the codes in /usr/src/linux/net/ipv4
look under af_inet.c and scan through the function calls under ioctl

 
 
 

getting the network interface information and default gateway information ??

Post by sammysamm » Mon, 29 Jul 2002 15:33:40


check out the code ifconfig.c in BSD. It has exactly what you need in
that platform.

SW

--
Posted via dBforums
http://dbforums.com

 
 
 

getting the network interface information and default gateway information ??

Post by Bjorn Rees » Mon, 29 Jul 2002 18:59:59



>   iam trying to get the network interface information and the default
> information.After doing some googling i found out the relevant

The canonical answer is "Unix Network Programming, 2nd Edition, Volume 1"
by Richard Stevens. If you are serious about network programming, you
really need this book.

Quote:> there is another structure
> struct ifreq
> but i dont think i need that one.
[...]
>         ifr = (struct ifreq *) &ifc.ifc_req[i];

You already do ;)
 
 
 

getting the network interface information and default gateway information ??

Post by rohi » Mon, 29 Jul 2002 20:53:28


hi,
  i have managed to get the way how to find out the way of finding the
interface name and interface address.Iam working on Linux-2.4.the code
is

int main()
{
        int fd,i, j = 0;

        if (0 > (fd = socket(AF_INET, SOCK_DGRAM,0))) {
               perror("socket");
               exit(errno);
        }

        ifc.ifc_len = sizeof(buf);
        ifc.ifc_req = buf;

        if(ioctl(fd,SIOCGIFCONF,&ifc) == -1)
        {
                perror("ioctl");
                exit(errno);
        }

        ifc.ifc_len /= sizeof(struct ifreq);
        for (i=0; i<ifc.ifc_len; i++)
        {
                interface = (struct sockaddr_in *)&buf[i].ifr_addr;
                printf("\nInteface name == [%s]\n",
buf[i].ifr_ifrn.ifrn_name);
                printf("IP Addr ==
[%s]\n",inet_ntoa(interface->sin_addr));
        }

        return 0;

Quote:}

Now i also want to retrieve the default gateway.how do i get that.can
u please specify if there are some ioctls(if any) and the relevent
structures and how to use them.

thanks for ur help and support
rohit

 
 
 

getting the network interface information and default gateway information ??

Post by Ian Zimmerma » Tue, 30 Jul 2002 11:02:42


rohit> Now i also want to retrieve the default gateway.how do i get
rohit> that.can u please specify if there are some ioctls(if any) and
rohit> the relevent structures and how to use them.

IMO the best way to look for this (and the interface information, too)
is to read the files under /proc

especially /proc/net/dev, /proc/net/route

Of course this is completely nonportable - but so are the ioctls.

--
Ian Zimmerman, Oakland, California, U.S.A.
GPG: 433BA087  9C0F 194F 203A 63F7 B1B8  6E5A 8CA3 27DB 433B A087
EngSoc adopts market economy: cheap is wasteful, efficient is expensive.

 
 
 

1. Getting status information from network interface

Hi!

I'm going to create a program that will show network traffic to/from
various ip addresses, to/from different tcp/udp ports and of course also
the total number of byte in/out of the network interface.

I immediately thought there would be something interesting in /proc/net.

/proc/net/tcp contains info on open connections, but it does not say
anything about the number of bytes transmitted in each socket.

The same goes for /proc/net/udp.

/proc/net/snmp gives some info on the different protocols ip, tcp, udp,
icmp, but not much more than that.

Does anyone know how I could find out how much data is transmitted
in/out of every open socket...?

-Einar

2. Linux Frequently Asked Questions with Answers (Part 6 of 6)

3. Getting information from Interfaces...

4. Linux Lizards v 1.4

5. How can I get the information of Network Interface?

6. How are dynamic libs found

7. How can I get the network device interface information using ioctl fuction in solaris ?

8. Lotus Domino automatically changes timezone

9. Where is the network interface information stored ?

10. Network Interface information

11. Custom Logs With Web App Specific Information That Is NON Cookie NON Header Information?

12. looking for information on how to query a pc for system hardware information